-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathGenConcurrency.cpp
801 lines (689 loc) · 31.4 KB
/
GenConcurrency.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//===--- GenConcurrency.cpp - IRGen for concurrency features --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation for concurrency features (other than
// basic async function lowering, which is more spread out).
//
//===----------------------------------------------------------------------===//
#include "GenConcurrency.h"
#include "BitPatternBuilder.h"
#include "ExtraInhabitants.h"
#include "GenCall.h"
#include "GenProto.h"
#include "GenType.h"
#include "IRGenDebugInfo.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "LoadableTypeInfo.h"
#include "ScalarPairTypeInfo.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
using namespace irgen;
namespace {
/// A TypeInfo implementation for Builtin.Executor.
class ExecutorTypeInfo :
public TrivialScalarPairTypeInfo<ExecutorTypeInfo, LoadableTypeInfo> {
public:
ExecutorTypeInfo(llvm::StructType *storageType,
Size size, Alignment align, SpareBitVector &&spareBits)
: TrivialScalarPairTypeInfo(storageType, size, std::move(spareBits),
align, IsTriviallyDestroyable,
IsCopyable, IsFixedSize) {}
static Size getFirstElementSize(IRGenModule &IGM) {
return IGM.getPointerSize();
}
static StringRef getFirstElementLabel() {
return ".identity";
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
return IGM.typeLayoutCache.getOrCreateScalarEntry(*this, T,
ScalarKind::TriviallyDestroyable);
}
static Size getSecondElementOffset(IRGenModule &IGM) {
return IGM.getPointerSize();
}
static Size getSecondElementSize(IRGenModule &IGM) {
return IGM.getPointerSize();
}
static StringRef getSecondElementLabel() {
return ".impl";
}
// The identity pointer is a heap object reference.
bool mayHaveExtraInhabitants(IRGenModule &IGM) const override {
return true;
}
PointerInfo getPointerInfo(IRGenModule &IGM) const {
return PointerInfo::forHeapObject(IGM);
}
unsigned getFixedExtraInhabitantCount(IRGenModule &IGM) const override {
return getPointerInfo(IGM).getExtraInhabitantCount(IGM);
}
APInt getFixedExtraInhabitantValue(IRGenModule &IGM,
unsigned bits,
unsigned index) const override {
return getPointerInfo(IGM)
.getFixedExtraInhabitantValue(IGM, bits, index, 0);
}
llvm::Value *getExtraInhabitantIndex(IRGenFunction &IGF, Address src,
SILType T,
bool isOutlined) const override {
src = projectFirstElement(IGF, src);
return getPointerInfo(IGF.IGM).getExtraInhabitantIndex(IGF, src);
}
void storeExtraInhabitant(IRGenFunction &IGF, llvm::Value *index,
Address dest, SILType T,
bool isOutlined) const override {
// Store the extra-inhabitant value in the first (identity) word.
auto first = projectFirstElement(IGF, dest);
getPointerInfo(IGF.IGM).storeExtraInhabitant(IGF, index, first);
// Zero the second word.
auto second = projectSecondElement(IGF, dest);
IGF.Builder.CreateStore(llvm::ConstantInt::get(IGF.IGM.ExecutorSecondTy, 0),
second);
}
};
} // end anonymous namespace
const LoadableTypeInfo &IRGenModule::getExecutorTypeInfo() {
return Types.getExecutorTypeInfo();
}
const LoadableTypeInfo &TypeConverter::getExecutorTypeInfo() {
if (ExecutorTI) return *ExecutorTI;
auto ty = IGM.SwiftExecutorTy;
SpareBitVector spareBits;
spareBits.append(IGM.getHeapObjectSpareBits());
spareBits.appendClearBits(IGM.getPointerSize().getValueInBits());
ExecutorTI =
new ExecutorTypeInfo(ty, IGM.getPointerSize() * 2,
IGM.getPointerAlignment(),
std::move(spareBits));
ExecutorTI->NextConverted = FirstType;
FirstType = ExecutorTI;
return *ExecutorTI;
}
void irgen::emitBuildMainActorExecutorRef(IRGenFunction &IGF,
Explosion &out) {
auto call = IGF.Builder.CreateCall(
IGF.IGM.getTaskGetMainExecutorFunctionPointer(), {});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
IGF.emitAllExtractValues(call, IGF.IGM.SwiftExecutorTy, out);
}
void irgen::emitBuildDefaultActorExecutorRef(IRGenFunction &IGF,
llvm::Value *actor,
Explosion &out) {
// The implementation word of a default actor is just a null pointer.
llvm::Value *identity =
IGF.Builder.CreatePtrToInt(actor, IGF.IGM.ExecutorFirstTy);
llvm::Value *impl = llvm::ConstantInt::get(IGF.IGM.ExecutorSecondTy, 0);
out.add(identity);
out.add(impl);
}
void irgen::emitBuildOrdinaryTaskExecutorRef(
IRGenFunction &IGF, llvm::Value *executor, CanType executorType,
ProtocolConformanceRef executorConf, Explosion &out) {
// The implementation word of an "ordinary" executor is
// just the witness table pointer with no flags set.
llvm::Value *identity =
IGF.Builder.CreatePtrToInt(executor, IGF.IGM.ExecutorFirstTy);
llvm::Value *impl = emitWitnessTableRef(IGF, executorType, executorConf);
impl = IGF.Builder.CreatePtrToInt(impl, IGF.IGM.ExecutorSecondTy);
out.add(identity);
out.add(impl);
}
void irgen::emitBuildOrdinarySerialExecutorRef(IRGenFunction &IGF,
llvm::Value *executor,
CanType executorType,
ProtocolConformanceRef executorConf,
Explosion &out) {
// The implementation word of an "ordinary" serial executor is
// just the witness table pointer with no flags set.
llvm::Value *identity =
IGF.Builder.CreatePtrToInt(executor, IGF.IGM.ExecutorFirstTy);
llvm::Value *impl =
emitWitnessTableRef(IGF, executorType, executorConf);
impl = IGF.Builder.CreatePtrToInt(impl, IGF.IGM.ExecutorSecondTy);
out.add(identity);
out.add(impl);
}
void irgen::emitBuildComplexEqualitySerialExecutorRef(IRGenFunction &IGF,
llvm::Value *executor,
CanType executorType,
ProtocolConformanceRef executorConf,
Explosion &out) {
llvm::Value *identity =
IGF.Builder.CreatePtrToInt(executor, IGF.IGM.ExecutorFirstTy);
// The implementation word of an "complex equality" serial executor is
// the witness table pointer with the ExecutorKind::ComplexEquality flag set.
llvm::Value *impl =
emitWitnessTableRef(IGF, executorType, executorConf);
impl = IGF.Builder.CreatePtrToInt(impl, IGF.IGM.ExecutorSecondTy);
// NOTE: Refer to SerialExecutorRef::ExecutorKind for the flag values.
llvm::IntegerType *IntPtrTy = IGF.IGM.IntPtrTy;
auto complexEqualityExecutorKindFlag =
llvm::Constant::getIntegerValue(IntPtrTy, APInt(IntPtrTy->getBitWidth(),
0b01));
impl = IGF.Builder.CreateOr(impl, complexEqualityExecutorKindFlag);
out.add(identity);
out.add(impl);
}
void irgen::emitGetCurrentExecutor(IRGenFunction &IGF, Explosion &out) {
auto *call = IGF.Builder.CreateCall(
IGF.IGM.getTaskGetCurrentExecutorFunctionPointer(), {});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
IGF.emitAllExtractValues(call, IGF.IGM.SwiftExecutorTy, out);
}
llvm::Value *irgen::emitBuiltinStartAsyncLet(IRGenFunction &IGF,
llvm::Value *taskOptions,
llvm::Value *taskFunction,
llvm::Value *localContextInfo,
llvm::Value *localResultBuffer,
SubstitutionMap subs) {
localContextInfo = IGF.Builder.CreateBitCast(localContextInfo,
IGF.IGM.OpaquePtrTy);
// stack allocate AsyncLet, and begin lifetime for it (until EndAsyncLet)
auto ty = llvm::ArrayType::get(IGF.IGM.Int8PtrTy, NumWords_AsyncLet);
auto address = IGF.createAlloca(ty, Alignment(Alignment_AsyncLet));
auto alet = IGF.Builder.CreateBitCast(address.getAddress(),
IGF.IGM.Int8PtrTy);
IGF.Builder.CreateLifetimeStart(alet);
assert(subs.getReplacementTypes().size() == 1 &&
"startAsyncLet should have a type substitution");
auto futureResultType = subs.getReplacementTypes()[0]->getCanonicalType();
llvm::Value *futureResultTypeMetadata =
llvm::ConstantPointerNull::get(IGF.IGM.Int8PtrTy);
if (!IGF.IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
futureResultTypeMetadata =
IGF.emitAbstractTypeMetadataRef(futureResultType);
}
// The concurrency runtime for older Apple OSes has a bug in task formation
// for `async let`s that may manifest when trying to use room in the
// parent task's preallocated `async let` buffer for the child task's
// initial task allocator slab. If targeting those older OSes, pad the
// context size for async let entry points to never fit in the preallocated
// space, so that we don't run into that bug. We leave a note on the
// declaration so that coroutine splitting can pad out the final context
// size after splitting.
auto deploymentAvailability
= AvailabilityContext::forDeploymentTarget(IGF.IGM.Context);
if (!deploymentAvailability.isContainedIn(
IGF.IGM.Context.getSwift57Availability()))
{
auto taskAsyncFunctionPointer
= cast<llvm::GlobalVariable>(taskFunction->stripPointerCasts());
if (auto taskAsyncID
= IGF.IGM.getAsyncCoroIDMapping(taskAsyncFunctionPointer)) {
// If the entry point function has already been emitted, retroactively
// pad out the initial context size in the async function pointer record
// and ID intrinsic so that it will never fit in the preallocated space.
uint64_t origSize = cast<llvm::ConstantInt>(taskAsyncID->getArgOperand(0))
->getValue().getLimitedValue();
uint64_t paddedSize = std::max(origSize,
(NumWords_AsyncLet * IGF.IGM.getPointerSize()).getValue());
auto paddedSizeVal = llvm::ConstantInt::get(IGF.IGM.Int32Ty, paddedSize);
taskAsyncID->setArgOperand(0, paddedSizeVal);
auto origInit = taskAsyncFunctionPointer->getInitializer();
auto newInit = llvm::ConstantStruct::get(
cast<llvm::StructType>(origInit->getType()),
origInit->getAggregateElement(0u),
paddedSizeVal);
taskAsyncFunctionPointer->setInitializer(newInit);
} else {
// If it hasn't been emitted yet, mark it to get the padding when it does
// get emitted.
IGF.IGM.markAsyncFunctionPointerForPadding(taskAsyncFunctionPointer);
}
}
// In embedded Swift, create and pass result type info.
taskOptions =
maybeAddEmbeddedSwiftResultTypeInfo(IGF, taskOptions, futureResultType);
llvm::CallInst *call;
if (localResultBuffer) {
// This is @_silgen_name("swift_asyncLet_begin")
call = IGF.Builder.CreateCall(IGF.IGM.getAsyncLetBeginFunctionPointer(),
{alet, taskOptions, futureResultTypeMetadata,
taskFunction, localContextInfo,
localResultBuffer});
} else {
// This is @_silgen_name("swift_asyncLet_start")
call = IGF.Builder.CreateCall(IGF.IGM.getAsyncLetStartFunctionPointer(),
{alet, taskOptions, futureResultTypeMetadata,
taskFunction, localContextInfo});
}
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
return alet;
}
void irgen::emitEndAsyncLet(IRGenFunction &IGF, llvm::Value *alet) {
auto *call =
IGF.Builder.CreateCall(IGF.IGM.getEndAsyncLetFunctionPointer(), {alet});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
IGF.Builder.CreateLifetimeEnd(alet);
}
llvm::Value *irgen::emitCreateTaskGroup(IRGenFunction &IGF,
SubstitutionMap subs,
llvm::Value *groupFlags) {
auto ty = llvm::ArrayType::get(IGF.IGM.Int8PtrTy, NumWords_TaskGroup);
auto address = IGF.createAlloca(ty, Alignment(Alignment_TaskGroup));
auto group = IGF.Builder.CreateBitCast(address.getAddress(),
IGF.IGM.Int8PtrTy);
IGF.Builder.CreateLifetimeStart(group);
assert(subs.getReplacementTypes().size() == 1 &&
"createTaskGroup should have a type substitution");
auto resultType = subs.getReplacementTypes()[0]->getCanonicalType();
auto resultTypeMetadata = IGF.emitAbstractTypeMetadataRef(resultType);
llvm::CallInst *call;
if (groupFlags) {
call = IGF.Builder.CreateCall(IGF.IGM.getTaskGroupInitializeWithFlagsFunctionPointer(),
{groupFlags, group, resultTypeMetadata});
} else {
call = IGF.Builder.CreateCall(IGF.IGM.getTaskGroupInitializeFunctionPointer(),
{group, resultTypeMetadata});
}
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
return group;
}
void irgen::emitDestroyTaskGroup(IRGenFunction &IGF, llvm::Value *group) {
auto *call = IGF.Builder.CreateCall(
IGF.IGM.getTaskGroupDestroyFunctionPointer(), {group});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
IGF.Builder.CreateLifetimeEnd(group);
}
llvm::Function *IRGenModule::getAwaitAsyncContinuationFn() {
StringRef name = "__swift_continuation_await_point";
if (llvm::GlobalValue *F = Module.getNamedValue(name))
return cast<llvm::Function>(F);
// The parameters here match the extra arguments passed to
// @llvm.coro.suspend.async by emitAwaitAsyncContinuation.
llvm::Type *argTys[] = { ContinuationAsyncContextPtrTy };
auto *suspendFnTy =
llvm::FunctionType::get(VoidTy, argTys, false /*vaargs*/);
llvm::Function *suspendFn =
llvm::Function::Create(suspendFnTy, llvm::Function::InternalLinkage,
name, &Module);
suspendFn->setCallingConv(SwiftAsyncCC);
suspendFn->setDoesNotThrow();
IRGenFunction suspendIGF(*this, suspendFn);
if (DebugInfo)
DebugInfo->emitArtificialFunction(suspendIGF, suspendFn);
auto &Builder = suspendIGF.Builder;
llvm::Value *context = suspendFn->getArg(0);
auto *call =
Builder.CreateCall(getContinuationAwaitFunctionPointer(), {context});
call->setCallingConv(SwiftAsyncCC);
call->setDoesNotThrow();
call->setTailCallKind(AsyncTailCallKind);
Builder.CreateRetVoid();
return suspendFn;
}
void irgen::emitTaskRunInline(IRGenFunction &IGF, SubstitutionMap subs,
llvm::Value *result, llvm::Value *closure,
llvm::Value *closureContext) {
assert(subs.getReplacementTypes().size() == 1 &&
"taskRunInline should have a type substitution");
auto resultType = subs.getReplacementTypes()[0]->getCanonicalType();
auto resultTypeMetadata = IGF.emitAbstractTypeMetadataRef(resultType);
auto *call = IGF.Builder.CreateCall(
IGF.IGM.getTaskRunInlineFunctionPointer(),
{result, closure, closureContext, resultTypeMetadata});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
}
void irgen::emitTaskCancel(IRGenFunction &IGF, llvm::Value *task) {
if (task->getType() != IGF.IGM.SwiftTaskPtrTy) {
task = IGF.Builder.CreateBitCast(task, IGF.IGM.SwiftTaskPtrTy);
}
auto *call =
IGF.Builder.CreateCall(IGF.IGM.getTaskCancelFunctionPointer(), {task});
call->setDoesNotThrow();
call->setCallingConv(IGF.IGM.SwiftCC);
}
template <class RecordTraits>
static Address allocateOptionRecord(IRGenFunction &IGF,
const RecordTraits &traits) {
return IGF.createAlloca(RecordTraits::getRecordType(IGF.IGM),
IGF.IGM.getPointerAlignment(),
traits.getLabel() + "_record");
}
static void initializeOptionRecordHeader(IRGenFunction &IGF,
Address recordAddr,
TaskOptionRecordFlags flags,
llvm::Value *curRecordPointer) {
auto baseRecordAddr =
IGF.Builder.CreateStructGEP(recordAddr, 0, Size(0));
// Flags
auto flagsValue =
llvm::ConstantInt::get(IGF.IGM.SizeTy, flags.getOpaqueValue());
IGF.Builder.CreateStore(flagsValue,
IGF.Builder.CreateStructGEP(baseRecordAddr, 0, Size(0)));
// Parent
IGF.Builder.CreateStore(curRecordPointer,
IGF.Builder.CreateStructGEP(baseRecordAddr, 1, IGF.IGM.getPointerSize()));
}
template <class RecordTraits, class... Args>
static llvm::Value *initializeOptionRecord(IRGenFunction &IGF,
Address recordAddr,
llvm::Value *curRecordPointer,
const RecordTraits &traits,
Args &&... args) {
initializeOptionRecordHeader(IGF, recordAddr, traits.getRecordFlags(),
curRecordPointer);
traits.initialize(IGF, recordAddr, std::forward<Args>(args)...);
llvm::Value *newRecordPointer = IGF.Builder.CreateBitOrPointerCast(
recordAddr.getAddress(), IGF.IGM.SwiftTaskOptionRecordPtrTy);
return newRecordPointer;
}
template <class RecordTraits, class... Args>
static llvm::Value *addOptionRecord(IRGenFunction &IGF,
llvm::Value *curRecordPointer,
const RecordTraits &traits,
Args &&... args) {
auto recordAddr = allocateOptionRecord(IGF, traits);
return initializeOptionRecord(IGF, recordAddr, curRecordPointer, traits,
std::forward<Args>(args)...);
}
/// Add a task option record to the options list if the given value
/// is present.
template <class RecordTraits>
static llvm::Value *maybeAddOptionRecord(IRGenFunction &IGF,
llvm::Value *curRecordPointer,
const RecordTraits &traits,
OptionalExplosion &value) {
// We can completely avoid doing any work if the value is statically nil.
if (value.isNone()) return curRecordPointer;
// Otherwise, allocate the option record.
auto recordAddr = allocateOptionRecord(IGF, traits);
// If the value is statically non-nil, we can unconditionally
// initialize the record and add it to the chain.
if (value.isSome()) {
return initializeOptionRecord(IGF, recordAddr, curRecordPointer,
traits, value.getSomeExplosion());
}
// Otherwise, we have to check whether the value is nil dynamically.
llvm::BasicBlock *contBB = IGF.createBasicBlock(traits.getLabel() + ".cont");
llvm::BasicBlock *someBB = IGF.createBasicBlock(traits.getLabel() + ".some");
auto &ctx = IGF.IGM.Context;
SILType optionalType =
IGF.IGM.getLoweredType(traits.getValueType(ctx).wrapInOptionalType());
auto &optionalStrategy = getEnumImplStrategy(IGF.IGM, optionalType);
// Branch based on whether the value is nil. We're going to use the
// value twice, so borrow it the first time.
value.getOptionalExplosion().borrowing([&](Explosion &borrowedValue) {
optionalStrategy.emitValueSwitch(IGF, borrowedValue,
{{ctx.getOptionalSomeDecl(), someBB},
{ctx.getOptionalNoneDecl(), contBB}},
/*default*/ nullptr);
});
auto noneOriginBB = IGF.Builder.GetInsertBlock();
// Enter the block for the case where the value is non-nil.
IGF.Builder.emitBlock(someBB);
// Project the payload from the optional value.
Explosion objectValue;
optionalStrategy.emitValueProject(IGF, value.getOptionalExplosion(),
ctx.getOptionalSomeDecl(),
objectValue);
// Initialize the record.
llvm::Value *someRecordPointer =
initializeOptionRecord(IGF, recordAddr, curRecordPointer,
traits, objectValue);
auto someOriginBB = IGF.Builder.GetInsertBlock();
IGF.Builder.CreateBr(contBB);
// Enter the continuation block and create a phi to merge the two cases.
IGF.Builder.emitBlock(contBB);
auto recordPointerPHI =
IGF.Builder.CreatePHI(IGF.IGM.SwiftTaskOptionRecordPtrTy, /*num cases*/ 2);
recordPointerPHI->addIncoming(curRecordPointer, noneOriginBB);
recordPointerPHI->addIncoming(someRecordPointer, someOriginBB);
return recordPointerPHI;
}
namespace {
struct EmbeddedSwiftResultTypeOptionRecordTraits {
CanType formalResultType;
static StringRef getLabel() {
return "result_type_info";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftResultTypeInfoTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::ResultTypeInfo);
}
void initialize(IRGenFunction &IGF, Address optionsRecord) const {
SILType lowered = IGF.IGM.getLoweredType(formalResultType);
const TypeInfo &TI = IGF.IGM.getTypeInfo(lowered);
CanType canType = lowered.getASTType();
FixedPacking packing = TI.getFixedPacking(IGF.IGM);
// Size
IGF.Builder.CreateStore(
TI.getStaticSize(IGF.IGM),
IGF.Builder.CreateStructGEP(optionsRecord, 1, Size()));
// Align mask
IGF.Builder.CreateStore(
TI.getStaticAlignmentMask(IGF.IGM),
IGF.Builder.CreateStructGEP(optionsRecord, 2, Size()));
// initializeWithCopy witness
IGF.Builder.CreateStore(
IGF.IGM.getOrCreateValueWitnessFunction(
ValueWitness::InitializeWithCopy, packing, canType, lowered, TI),
IGF.Builder.CreateStructGEP(optionsRecord, 3, Size()));
// storeEnumTagSinglePayload witness
IGF.Builder.CreateStore(
IGF.IGM.getOrCreateValueWitnessFunction(
ValueWitness::StoreEnumTagSinglePayload, packing, canType, lowered,
TI),
IGF.Builder.CreateStructGEP(optionsRecord, 4, Size()));
// destroy witness
IGF.Builder.CreateStore(
IGF.IGM.getOrCreateValueWitnessFunction(ValueWitness::Destroy, packing,
canType, lowered, TI),
IGF.Builder.CreateStructGEP(optionsRecord, 5, Size()));
}
};
} // end anonymous namespace
llvm::Value *irgen::maybeAddEmbeddedSwiftResultTypeInfo(IRGenFunction &IGF,
llvm::Value *taskOptions,
CanType formalResultType) {
if (!IGF.IGM.Context.LangOpts.hasFeature(Feature::Embedded))
return taskOptions;
EmbeddedSwiftResultTypeOptionRecordTraits traits{formalResultType};
return addOptionRecord(IGF, taskOptions, traits);
}
namespace {
struct InitialSerialExecutorRecordTraits {
static StringRef getLabel() {
return "initial_serial_executor";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftInitialSerialExecutorTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::InitialSerialExecutor);
}
static CanType getValueType(ASTContext &ctx) {
return ctx.TheExecutorType;
}
void initialize(IRGenFunction &IGF, Address recordAddr,
Explosion &serialExecutor) const {
auto executorRecord =
IGF.Builder.CreateStructGEP(recordAddr, 1, 2 * IGF.IGM.getPointerSize());
IGF.Builder.CreateStore(serialExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 0, Size()));
IGF.Builder.CreateStore(serialExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 1, Size()));
}
};
struct TaskGroupRecordTraits {
static StringRef getLabel() {
return "task_group";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftTaskGroupTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::TaskGroup);
}
static CanType getValueType(ASTContext &ctx) {
return ctx.TheRawPointerType;
}
void initialize(IRGenFunction &IGF, Address recordAddr,
Explosion &taskGroup) const {
IGF.Builder.CreateStore(
taskGroup.claimNext(),
IGF.Builder.CreateStructGEP(recordAddr, 1, 2 * IGF.IGM.getPointerSize()));
}
};
struct InitialTaskExecutorUnownedRecordTraits {
static StringRef getLabel() {
return "task_executor_unowned";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftInitialTaskExecutorUnownedPreferenceTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::InitialTaskExecutorUnowned);
}
static CanType getValueType(ASTContext &ctx) {
return ctx.TheExecutorType;
}
void initialize(IRGenFunction &IGF, Address recordAddr,
Explosion &taskExecutor) const {
auto executorRecord =
IGF.Builder.CreateStructGEP(recordAddr, 1, 2 * IGF.IGM.getPointerSize());
IGF.Builder.CreateStore(taskExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 0, Size()));
IGF.Builder.CreateStore(taskExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 1, Size()));
}
};
struct InitialTaskExecutorOwnedRecordTraits {
static StringRef getLabel() {
return "task_executor_owned";
}
static llvm::StructType *getRecordType(IRGenModule &IGM) {
return IGM.SwiftInitialTaskExecutorOwnedPreferenceTaskOptionRecordTy;
}
static TaskOptionRecordFlags getRecordFlags() {
return TaskOptionRecordFlags(TaskOptionRecordKind::InitialTaskExecutorOwned);
}
static CanType getValueType(ASTContext &ctx) {
return OptionalType::get(ctx.getProtocol(KnownProtocolKind::TaskExecutor)
->getDeclaredInterfaceType())
->getCanonicalType();
}
void initialize(IRGenFunction &IGF, Address recordAddr,
Explosion &taskExecutor) const {
auto executorRecord =
IGF.Builder.CreateStructGEP(recordAddr, 1, 2 * IGF.IGM.getPointerSize());
// This relies on the fact that the HeapObject is directly followed by a
// pointer to the witness table.
IGF.Builder.CreateStore(taskExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 0, Size()));
IGF.Builder.CreateStore(taskExecutor.claimNext(),
IGF.Builder.CreateStructGEP(executorRecord, 1, Size()));
}
};
} // end anonymous namespace
static llvm::Value *
maybeAddInitialSerialExecutorOptionRecord(IRGenFunction &IGF,
llvm::Value *prevOptions,
OptionalExplosion &serialExecutor) {
return maybeAddOptionRecord(IGF, prevOptions,
InitialSerialExecutorRecordTraits(),
serialExecutor);
}
static llvm::Value *
maybeAddTaskGroupOptionRecord(IRGenFunction &IGF, llvm::Value *prevOptions,
OptionalExplosion &taskGroup) {
return maybeAddOptionRecord(IGF, prevOptions, TaskGroupRecordTraits(),
taskGroup);
}
static llvm::Value *
maybeAddInitialTaskExecutorOptionRecord(IRGenFunction &IGF,
llvm::Value *prevOptions,
OptionalExplosion &taskExecutor) {
return maybeAddOptionRecord(IGF, prevOptions,
InitialTaskExecutorUnownedRecordTraits(),
taskExecutor);
}
static llvm::Value *
maybeAddInitialTaskExecutorOwnedOptionRecord(IRGenFunction &IGF,
llvm::Value *prevOptions,
OptionalExplosion &taskExecutorExistential) {
return maybeAddOptionRecord(IGF, prevOptions,
InitialTaskExecutorOwnedRecordTraits(),
taskExecutorExistential);
}
std::pair<llvm::Value *, llvm::Value *>
irgen::emitTaskCreate(IRGenFunction &IGF, llvm::Value *flags,
OptionalExplosion &serialExecutor,
OptionalExplosion &taskGroup,
OptionalExplosion &taskExecutorUnowned,
OptionalExplosion &taskExecutorExistential,
Explosion &taskFunction,
SubstitutionMap subs) {
llvm::Value *taskOptions =
llvm::ConstantPointerNull::get(IGF.IGM.SwiftTaskOptionRecordPtrTy);
CanType resultType;
if (subs) {
resultType = subs.getReplacementTypes()[0]->getCanonicalType();
} else {
resultType = IGF.IGM.Context.TheEmptyTupleType;
}
llvm::Value *resultTypeMetadata;
if (IGF.IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
resultTypeMetadata = llvm::ConstantPointerNull::get(IGF.IGM.Int8PtrTy);
} else {
resultTypeMetadata = IGF.emitTypeMetadataRef(resultType);
}
// Add an option record for the initial serial executor, if present.
taskOptions =
maybeAddInitialSerialExecutorOptionRecord(IGF, taskOptions, serialExecutor);
// Add an option record for the task group, if present.
taskOptions = maybeAddTaskGroupOptionRecord(IGF, taskOptions, taskGroup);
// Add an option record for the initial task executor, if present.
{
// Deprecated: This is the UnownedTaskExecutor? which is NOT consuming
taskOptions = maybeAddInitialTaskExecutorOptionRecord(
IGF, taskOptions, taskExecutorUnowned);
// Take an (any TaskExecutor)? which we retain until task has completed
taskOptions = maybeAddInitialTaskExecutorOwnedOptionRecord(
IGF, taskOptions, taskExecutorExistential);
}
// In embedded Swift, create and pass result type info.
taskOptions = maybeAddEmbeddedSwiftResultTypeInfo(IGF, taskOptions, resultType);
auto taskFunctionPtr = taskFunction.claimNext();
auto taskFunctionContext = taskFunction.claimNext();
llvm::CallInst *result = IGF.Builder.CreateCall(
IGF.IGM.getTaskCreateFunctionPointer(),
{flags, taskOptions, resultTypeMetadata,
taskFunctionPtr, taskFunctionContext});
result->setDoesNotThrow();
result->setCallingConv(IGF.IGM.SwiftCC);
// Cast back to NativeObject/RawPointer.
auto newTask = IGF.Builder.CreateExtractValue(result, { 0 });
newTask = IGF.Builder.CreateBitCast(newTask, IGF.IGM.RefCountedPtrTy);
auto newContext = IGF.Builder.CreateExtractValue(result, { 1 });
newContext = IGF.Builder.CreateBitCast(newContext, IGF.IGM.Int8PtrTy);
return { newTask, newContext };
}