Skip to content

Commit 2bcc74c

Browse files
Merge pull request #34454 from nate-chandler/concurrency/irgen/threading-values-through
[Async CC] Propagate runtime values.
2 parents 811fc06 + 2ff6ea3 commit 2bcc74c

File tree

6 files changed

+125
-12
lines changed

6 files changed

+125
-12
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ AsyncContextLayout irgen::getAsyncContextLayout(
103103
auto parameters = substitutedType->getParameters();
104104
SILFunctionConventions fnConv(substitutedType, IGF.getSILModule());
105105

106+
// AsyncContext * __ptrauth_swift_async_context_parent Parent;
107+
{
108+
auto ty = SILType();
109+
auto &ti = IGF.IGM.getSwiftContextPtrTypeInfo();
110+
valTypes.push_back(ty);
111+
typeInfos.push_back(&ti);
112+
}
113+
114+
// TaskContinuationFunction * __ptrauth_swift_async_context_resume
115+
// ResumeParent;
116+
{
117+
auto ty = SILType();
118+
auto &ti = IGF.IGM.getTaskContinuationFunctionPtrTypeInfo();
119+
valTypes.push_back(ty);
120+
typeInfos.push_back(&ti);
121+
}
122+
123+
// ExecutorRef ResumeParentExecutor;
124+
{
125+
auto ty = SILType();
126+
auto &ti = IGF.IGM.getSwiftExecutorPtrTypeInfo();
127+
valTypes.push_back(ty);
128+
typeInfos.push_back(&ti);
129+
}
130+
106131
// SwiftError *errorResult;
107132
auto errorCanType = IGF.IGM.Context.getExceptionType();
108133
auto errorType = SILType::getPrimitiveObjectType(errorCanType);
@@ -268,11 +293,6 @@ static Alignment getAsyncContextAlignment(IRGenModule &IGM) {
268293
return IGM.getPointerAlignment();
269294
}
270295

271-
static llvm::Value *getAsyncTask(IRGenFunction &IGF) {
272-
// TODO: Return the appropriate task.
273-
return llvm::Constant::getNullValue(IGF.IGM.SwiftTaskPtrTy);
274-
}
275-
276296
llvm::Value *IRGenFunction::getAsyncTask() {
277297
assert(isAsync());
278298
auto *value = CurFn->getArg((unsigned)AsyncFunctionArgumentIndex::Task);
@@ -2165,9 +2185,8 @@ class AsyncCallEmission final : public CallEmission {
21652185
void setArgs(Explosion &llArgs, bool isOutlined,
21662186
WitnessMetadata *witnessMetadata) override {
21672187
Explosion asyncExplosion;
2168-
asyncExplosion.add(llvm::Constant::getNullValue(IGF.IGM.SwiftTaskPtrTy));
2169-
asyncExplosion.add(
2170-
llvm::Constant::getNullValue(IGF.IGM.SwiftExecutorPtrTy));
2188+
asyncExplosion.add(IGF.getAsyncTask());
2189+
asyncExplosion.add(IGF.getAsyncExecutor());
21712190
asyncExplosion.add(contextBuffer.getAddress());
21722191
if (getCallee().getRepresentation() ==
21732192
SILFunctionTypeRepresentation::Thick) {
@@ -2176,9 +2195,22 @@ class AsyncCallEmission final : public CallEmission {
21762195
super::setArgs(asyncExplosion, false, witnessMetadata);
21772196
SILFunctionConventions fnConv(getCallee().getSubstFunctionType(),
21782197
IGF.getSILModule());
2198+
auto layout = getAsyncContextLayout();
21792199

2200+
// Set caller info into the context.
2201+
{ // caller context
2202+
Explosion explosion;
2203+
explosion.add(IGF.getAsyncContext());
2204+
auto fieldLayout = layout.getParentLayout();
2205+
saveValue(fieldLayout, explosion, isOutlined);
2206+
}
2207+
{ // caller executor
2208+
Explosion explosion;
2209+
explosion.add(IGF.getAsyncExecutor());
2210+
auto fieldLayout = layout.getResumeParentExecutorLayout();
2211+
saveValue(fieldLayout, explosion, isOutlined);
2212+
}
21802213
// Move all the arguments into the context.
2181-
auto layout = getAsyncContextLayout();
21822214
for (unsigned index = 0, count = layout.getIndirectReturnCount();
21832215
index < count; ++index) {
21842216
auto fieldLayout = layout.getIndirectReturnLayout(index);
@@ -3354,7 +3386,7 @@ void irgen::emitDeallocYieldManyCoroutineBuffer(IRGenFunction &IGF,
33543386
Address irgen::emitTaskAlloc(IRGenFunction &IGF, llvm::Value *size,
33553387
Alignment alignment) {
33563388
auto *call = IGF.Builder.CreateCall(IGF.IGM.getTaskAllocFn(),
3357-
{getAsyncTask(IGF), size});
3389+
{IGF.getAsyncTask(), size});
33583390
call->setDoesNotThrow();
33593391
call->setCallingConv(IGF.IGM.SwiftCC);
33603392
call->addAttribute(llvm::AttributeList::FunctionIndex,
@@ -3366,7 +3398,7 @@ Address irgen::emitTaskAlloc(IRGenFunction &IGF, llvm::Value *size,
33663398
void irgen::emitTaskDealloc(IRGenFunction &IGF, Address address,
33673399
llvm::Value *size) {
33683400
auto *call = IGF.Builder.CreateCall(
3369-
IGF.IGM.getTaskDeallocFn(), {getAsyncTask(IGF), address.getAddress()});
3401+
IGF.IGM.getTaskDeallocFn(), {IGF.getAsyncTask(), address.getAddress()});
33703402
call->setDoesNotThrow();
33713403
call->setCallingConv(IGF.IGM.SwiftCC);
33723404
call->addAttribute(llvm::AttributeList::FunctionIndex,

lib/IRGen/GenCall.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,15 @@ namespace irgen {
9393

9494
private:
9595
enum class FixedIndex : unsigned {
96-
Error = 0,
96+
Parent = 0,
97+
ResumeParent = 1,
98+
ResumeParentExecutor = 2,
99+
Error = 3,
97100
};
98101
enum class FixedCount : unsigned {
102+
Parent = 1,
103+
ResumeParent = 1,
104+
ResumeParentExecutor = 1,
99105
Error = 1,
100106
};
101107
IRGenFunction &IGF;
@@ -111,6 +117,13 @@ namespace irgen {
111117
Optional<TrailingWitnessInfo> trailingWitnessInfo;
112118
SmallVector<ArgumentInfo, 4> argumentInfos;
113119

120+
unsigned getParentIndex() { return (unsigned)FixedIndex::Parent; }
121+
unsigned getResumeParentIndex() {
122+
return (unsigned)FixedIndex::ResumeParent;
123+
}
124+
unsigned getResumeParentExecutorIndex() {
125+
return (unsigned)FixedIndex::ResumeParentExecutor;
126+
}
114127
unsigned getErrorIndex() { return (unsigned)FixedIndex::Error; }
115128
unsigned getFirstIndirectReturnIndex() {
116129
return getErrorIndex() + getErrorCount();
@@ -156,6 +169,13 @@ namespace irgen {
156169
}
157170

158171
public:
172+
ElementLayout getParentLayout() { return getElement(getParentIndex()); }
173+
ElementLayout getResumeParentLayout() {
174+
return getElement(getResumeParentIndex());
175+
}
176+
ElementLayout getResumeParentExecutorLayout() {
177+
return getElement(getResumeParentExecutorIndex());
178+
}
159179
bool canHaveError() { return canHaveValidError; }
160180
ElementLayout getErrorLayout() { return getElement(getErrorIndex()); }
161181
unsigned getErrorCount() { return (unsigned)FixedCount::Error; }

lib/IRGen/GenType.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,48 @@ const TypeInfo &TypeConverter::getTypeMetadataPtrTypeInfo() {
14531453
return *TypeMetadataPtrTI;
14541454
}
14551455

1456+
const TypeInfo &IRGenModule::getSwiftContextPtrTypeInfo() {
1457+
return Types.getSwiftContextPtrTypeInfo();
1458+
}
1459+
1460+
const TypeInfo &TypeConverter::getSwiftContextPtrTypeInfo() {
1461+
if (SwiftContextPtrTI) return *SwiftContextPtrTI;
1462+
SwiftContextPtrTI = createUnmanagedStorageType(IGM.SwiftContextPtrTy,
1463+
ReferenceCounting::Unknown,
1464+
/*isOptional*/false);
1465+
SwiftContextPtrTI->NextConverted = FirstType;
1466+
FirstType = SwiftContextPtrTI;
1467+
return *SwiftContextPtrTI;
1468+
}
1469+
1470+
const TypeInfo &IRGenModule::getTaskContinuationFunctionPtrTypeInfo() {
1471+
return Types.getTaskContinuationFunctionPtrTypeInfo();
1472+
}
1473+
1474+
const TypeInfo &TypeConverter::getTaskContinuationFunctionPtrTypeInfo() {
1475+
if (TaskContinuationFunctionPtrTI) return *TaskContinuationFunctionPtrTI;
1476+
TaskContinuationFunctionPtrTI = createUnmanagedStorageType(
1477+
IGM.TaskContinuationFunctionPtrTy, ReferenceCounting::Unknown,
1478+
/*isOptional*/ false);
1479+
TaskContinuationFunctionPtrTI->NextConverted = FirstType;
1480+
FirstType = TaskContinuationFunctionPtrTI;
1481+
return *TaskContinuationFunctionPtrTI;
1482+
}
1483+
1484+
const TypeInfo &IRGenModule::getSwiftExecutorPtrTypeInfo() {
1485+
return Types.getSwiftExecutorPtrTypeInfo();
1486+
}
1487+
1488+
const TypeInfo &TypeConverter::getSwiftExecutorPtrTypeInfo() {
1489+
if (SwiftExecutorPtrTI) return *SwiftExecutorPtrTI;
1490+
SwiftExecutorPtrTI = createUnmanagedStorageType(IGM.SwiftExecutorPtrTy,
1491+
ReferenceCounting::Unknown,
1492+
/*isOptional*/ false);
1493+
SwiftExecutorPtrTI->NextConverted = FirstType;
1494+
FirstType = SwiftExecutorPtrTI;
1495+
return *SwiftExecutorPtrTI;
1496+
}
1497+
14561498
const LoadableTypeInfo &
14571499
IRGenModule::getReferenceObjectTypeInfo(ReferenceCounting refcounting) {
14581500
switch (refcounting) {

lib/IRGen/GenType.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class TypeConverter {
105105
const LoadableTypeInfo *RawPointerTI = nullptr;
106106
const LoadableTypeInfo *WitnessTablePtrTI = nullptr;
107107
const TypeInfo *TypeMetadataPtrTI = nullptr;
108+
const TypeInfo *SwiftContextPtrTI = nullptr;
109+
const TypeInfo *TaskContinuationFunctionPtrTI = nullptr;
110+
const TypeInfo *SwiftExecutorPtrTI = nullptr;
108111
const TypeInfo *ObjCClassPtrTI = nullptr;
109112
const LoadableTypeInfo *EmptyTI = nullptr;
110113
const LoadableTypeInfo *IntegerLiteralTI = nullptr;
@@ -181,6 +184,9 @@ class TypeConverter {
181184
const LoadableTypeInfo &getBridgeObjectTypeInfo();
182185
const LoadableTypeInfo &getRawPointerTypeInfo();
183186
const TypeInfo &getTypeMetadataPtrTypeInfo();
187+
const TypeInfo &getSwiftContextPtrTypeInfo();
188+
const TypeInfo &getTaskContinuationFunctionPtrTypeInfo();
189+
const TypeInfo &getSwiftExecutorPtrTypeInfo();
184190
const TypeInfo &getObjCClassPtrTypeInfo();
185191
const LoadableTypeInfo &getWitnessTablePtrTypeInfo();
186192
const LoadableTypeInfo &getEmptyTypeInfo();

lib/IRGen/IRGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,14 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
594594
SwiftTaskPtrTy = SwiftTaskTy->getPointerTo(DefaultAS);
595595
SwiftExecutorPtrTy = SwiftExecutorTy->getPointerTo(DefaultAS);
596596

597+
// using TaskContinuationFunction =
598+
// SWIFT_CC(swift)
599+
// void (AsyncTask *, ExecutorRef, AsyncContext *);
600+
TaskContinuationFunctionTy = llvm::FunctionType::get(
601+
VoidTy, {SwiftTaskPtrTy, SwiftExecutorPtrTy, SwiftContextPtrTy},
602+
/*isVarArg*/ false);
603+
TaskContinuationFunctionPtrTy = TaskContinuationFunctionTy->getPointerTo();
604+
597605
DifferentiabilityWitnessTy = createStructType(
598606
*this, "swift.differentiability_witness", {Int8PtrTy, Int8PtrTy});
599607
}

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ class IRGenModule {
729729
llvm::PointerType *SwiftContextPtrTy;
730730
llvm::PointerType *SwiftTaskPtrTy;
731731
llvm::PointerType *SwiftExecutorPtrTy;
732+
llvm::FunctionType *TaskContinuationFunctionTy;
733+
llvm::PointerType *TaskContinuationFunctionPtrTy;
732734

733735
llvm::StructType *DifferentiabilityWitnessTy; // { i8*, i8* }
734736

@@ -891,6 +893,9 @@ class IRGenModule {
891893
const TypeInfo &getTypeInfo(SILType T);
892894
const TypeInfo &getWitnessTablePtrTypeInfo();
893895
const TypeInfo &getTypeMetadataPtrTypeInfo();
896+
const TypeInfo &getSwiftContextPtrTypeInfo();
897+
const TypeInfo &getTaskContinuationFunctionPtrTypeInfo();
898+
const TypeInfo &getSwiftExecutorPtrTypeInfo();
894899
const TypeInfo &getObjCClassPtrTypeInfo();
895900
const LoadableTypeInfo &getOpaqueStorageTypeInfo(Size size, Alignment align);
896901
const LoadableTypeInfo &

0 commit comments

Comments
 (0)