Skip to content

Commit dafeb89

Browse files
Merge pull request swiftlang#34764 from aschwaighofer/fix_irge_builtins_create_async_tasks
Fix lowering of Builtin.createAsyncTask and Builtin.createAsyncTaskFuture
2 parents f52029e + fa54ff8 commit dafeb89

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,23 +1498,24 @@ FUNCTION(TaskCancel,
14981498
ATTRS(NoUnwind, ArgMemOnly))
14991499

15001500
// AsyncTaskAndContext swift_task_create(
1501-
// size_t flags, AsyncTask *task, AsyncFunctionPointer *function);
1501+
// size_t flags, AsyncTask *task, TaskContinuationFunction* function,
1502+
// size_t contextSize);
15021503
FUNCTION(TaskCreateFunc,
1503-
swift_task_create, SwiftCC,
1504+
swift_task_create_f, SwiftCC,
15041505
ConcurrencyAvailability,
15051506
RETURNS(AsyncTaskAndContextTy),
1506-
ARGS(SizeTy, SwiftTaskPtrTy, AsyncFunctionPointerPtrTy),
1507+
ARGS(SizeTy, SwiftTaskPtrTy, TaskContinuationFunctionPtrTy, SizeTy),
15071508
ATTRS(NoUnwind, ArgMemOnly))
15081509

1509-
// AsyncTaskAndContext swift_task_create_future(
1510+
// AsyncTaskAndContext swift_task_create_future_f(
15101511
// size_t flags, AsyncTask *task, const Metadata *futureResultType,
1511-
// AsyncFunctionPointer *function);
1512+
// TaskContinuationFunction *function, size_t contextSize);
15121513
FUNCTION(TaskCreateFutureFunc,
1513-
swift_task_create_future, SwiftCC,
1514+
swift_task_create_future_f, SwiftCC,
15141515
ConcurrencyAvailability,
15151516
RETURNS(AsyncTaskAndContextTy),
15161517
ARGS(SizeTy, SwiftTaskPtrTy, TypeMetadataPtrTy,
1517-
AsyncFunctionPointerPtrTy),
1518+
TaskContinuationFunctionPtrTy, SizeTy),
15181519
ATTRS(NoUnwind, ArgMemOnly))
15191520

15201521
#undef RETURNS

lib/IRGen/GenCall.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,8 +3596,6 @@ llvm::Value *irgen::emitTaskCreate(
35963596
SubstitutionMap subs) {
35973597
parentTask = IGF.Builder.CreateBitOrPointerCast(
35983598
parentTask, IGF.IGM.SwiftTaskPtrTy);
3599-
taskFunction = IGF.Builder.CreateBitOrPointerCast(
3600-
taskFunction, IGF.IGM.AsyncFunctionPointerPtrTy);
36013599

36023600
// Determine the size of the async context for the closure.
36033601
ASTContext &ctx = IGF.IGM.IRGen.SIL.getASTContext();
@@ -3621,29 +3619,36 @@ llvm::Value *irgen::emitTaskCreate(
36213619

36223620
// Call the function.
36233621
llvm::CallInst *result;
3622+
llvm::Value *theSize, *theFunction;
3623+
std::tie(theFunction, theSize) =
3624+
getAsyncFunctionAndSize(IGF, SILFunctionTypeRepresentation::Thick,
3625+
FunctionPointer::forExplosionValue(
3626+
IGF, taskFunction, taskFunctionCanSILType),
3627+
localContextInfo);
3628+
theFunction = IGF.Builder.CreateBitOrPointerCast(
3629+
theFunction, IGF.IGM.TaskContinuationFunctionPtrTy);
3630+
theSize = IGF.Builder.CreateZExtOrBitCast(theSize, IGF.IGM.SizeTy);
36243631
if (futureResultType) {
36253632
result = IGF.Builder.CreateCall(
36263633
IGF.IGM.getTaskCreateFutureFuncFn(),
3627-
{ flags, parentTask, futureResultType, taskFunction });
3634+
{ flags, parentTask, futureResultType, theFunction, theSize });
36283635
} else {
3629-
result = IGF.Builder.CreateCall(
3630-
IGF.IGM.getTaskCreateFuncFn(),
3631-
{ flags, parentTask, taskFunction });
3636+
result = IGF.Builder.CreateCall(IGF.IGM.getTaskCreateFuncFn(),
3637+
{flags, parentTask, theFunction, theSize});
36323638
}
36333639
result->setDoesNotThrow();
36343640
result->setCallingConv(IGF.IGM.SwiftCC);
36353641

36363642
// Write the local context information into the initial context for the task.
3637-
if (layout.hasLocalContext()) {
3638-
// Dig out the initial context returned from task creation.
3639-
auto initialContext = IGF.Builder.CreateExtractValue(result, { 1 });
3640-
Address initialContextAddr = layout.emitCastTo(IGF, initialContext);
3641-
3642-
auto localContextLayout = layout.getLocalContextLayout();
3643-
auto localContextAddr = localContextLayout.project(
3644-
IGF, initialContextAddr, llvm::None);
3645-
IGF.Builder.CreateStore(localContextInfo, localContextAddr);
3646-
}
3643+
assert(layout.hasLocalContext());
3644+
// Dig out the initial context returned from task creation.
3645+
auto initialContext = IGF.Builder.CreateExtractValue(result, {1});
3646+
Address initialContextAddr = layout.emitCastTo(IGF, initialContext);
3647+
3648+
auto localContextLayout = layout.getLocalContextLayout();
3649+
auto localContextAddr =
3650+
localContextLayout.project(IGF, initialContextAddr, llvm::None);
3651+
IGF.Builder.CreateStore(localContextInfo, localContextAddr);
36473652

36483653
return result;
36493654
}

test/IRGen/async/builtins.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ sil hidden [ossa] @launch_task : $@convention(method) @async (Int, Optional<Buil
3434
bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@async @callee_guaranteed () -> (@error Error)):
3535
%3 = begin_borrow %1 : $Optional<Builtin.NativeObject>
3636
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[FN_CONTEXT:%.*]])
37-
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create(
37+
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_f(
3838
// CHECK-NEXT: [[NEW_CONTEXT_RAW:%.*]] = extractvalue %swift.async_task_and_context [[NEW_TASK_AND_CONTEXT]], 1
3939
// CHECK-NEXT: [[NEW_CONTEXT:%.*]] = bitcast %swift.context* [[NEW_CONTEXT_RAW]] to
4040
// CHECK-NEXT: [[CONTEXT_INFO_LOC:%.*]] = getelementptr inbounds <{{.*}}>* [[NEW_CONTEXT]]
@@ -53,7 +53,7 @@ bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@a
5353
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[FN_CONTEXT:%.*]])
5454
%9 = metatype $@thick T.Type
5555
%10 = init_existential_metatype %9 : $@thick T.Type, $@thick Any.Type
56-
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_future(
56+
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_future_f(
5757
// CHECK-NEXT: [[NEW_CONTEXT_RAW:%.*]] = extractvalue %swift.async_task_and_context [[NEW_TASK_AND_CONTEXT]], 1
5858
// CHECK-NEXT: [[NEW_CONTEXT:%.*]] = bitcast %swift.context* [[NEW_CONTEXT_RAW]] to
5959
// CHECK-NEXT: [[CONTEXT_INFO_LOC:%.*]] = getelementptr inbounds <{{.*}}>* [[NEW_CONTEXT]]

0 commit comments

Comments
 (0)