Skip to content

Conditionally start using llvm::CallingConv::SwiftTail for async functions #36037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ FUNCTION(TaskCreateFutureFunc,
// ExecutorRef currentExecutor,
// ExecutorRef newExecutor);
FUNCTION(TaskSwitchFunc,
swift_task_switch, SwiftCC,
swift_task_switch, SwiftAsyncCC,
ConcurrencyAvailability,
RETURNS(VoidTy),
ARGS(SwiftTaskPtrTy, SwiftExecutorPtrTy, SwiftExecutorPtrTy),
Expand Down
10 changes: 7 additions & 3 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ static llvm::CallingConv::ID getFreestandingConvention(IRGenModule &IGM) {
/// Expand the requirements of the given abstract calling convention
/// into a "physical" calling convention.
llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
SILFunctionTypeRepresentation convention) {
SILFunctionTypeRepresentation convention,
bool isAsync) {
switch (convention) {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::ObjCMethod:
Expand All @@ -474,6 +475,8 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
case SILFunctionTypeRepresentation::Closure:
case SILFunctionTypeRepresentation::Thin:
case SILFunctionTypeRepresentation::Thick:
if (isAsync)
return IGM.SwiftAsyncCC;
return getFreestandingConvention(IGM);
}
llvm_unreachable("bad calling convention!");
Expand Down Expand Up @@ -1849,7 +1852,8 @@ Signature SignatureExpansion::getSignature() {
(FnType->getLanguage() == SILFunctionLanguage::C) &&
"C function type without C function info");

auto callingConv = expandCallingConv(IGM, FnType->getRepresentation());
auto callingConv =
expandCallingConv(IGM, FnType->getRepresentation(), FnType->isAsync());

Signature result;
result.Type = llvmType;
Expand Down Expand Up @@ -4808,7 +4812,7 @@ IRGenFunction::getFunctionPointerForResumeIntrinsic(llvm::Value *resume) {
IGM.VoidTy, {IGM.Int8PtrTy, IGM.Int8PtrTy, IGM.Int8PtrTy},
false /*vaargs*/);
auto signature =
Signature(fnTy, IGM.constructInitialAttributes(), IGM.SwiftCC);
Signature(fnTy, IGM.constructInitialAttributes(), IGM.SwiftAsyncCC);
auto fnPtr = FunctionPointer(
FunctionPointer::KindTy::Function,
Builder.CreateBitOrPointerCast(resume, fnTy->getPointerTo()),
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ namespace irgen {
std::pair<bool, bool> values = {true, true},
Size initialContextSize = Size(0));
llvm::CallingConv::ID expandCallingConv(IRGenModule &IGM,
SILFunctionTypeRepresentation convention);
SILFunctionTypeRepresentation convention,
bool isAsync);

Signature emitCastOfFunctionPointer(IRGenFunction &IGF, llvm::Value *&fnPtr,
CanSILFunctionType fnType);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,7 @@ llvm::Function *IRGenFunction::createAsyncSuspendFn() {
IGM.getTaskSwitchFuncFn(),
{ task, executor, targetExecutor });
suspendCall->setDoesNotThrow();
suspendCall->setCallingConv(IGM.SwiftCC);
suspendCall->setCallingConv(IGM.SwiftAsyncCC);
suspendCall->setTailCall();
Builder.CreateRetVoid();
return suspendFn;
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ static llvm::Function *emitObjCPartialApplicationForwarder(IRGenModule &IGM,
llvm::Function::Create(fwdTy, llvm::Function::InternalLinkage,
MANGLE_AS_STRING(OBJC_PARTIAL_APPLY_THUNK_SYM),
&IGM.Module);
fwd->setCallingConv(
expandCallingConv(IGM, SILFunctionTypeRepresentation::Thick));
fwd->setCallingConv(expandCallingConv(
IGM, SILFunctionTypeRepresentation::Thick, false/*isAsync*/));

fwd->setAttributes(attrs);
// Merge initial attributes with attrs.
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
// TODO: use "tinycc" on platforms that support it
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
SwiftCC = llvm::CallingConv::Swift;
// TODO: Once clang support is merged this should also use
// clangASTContext.getTargetInfo().isSwiftAsyncCCSupported()
SwiftAsyncCC = opts.UseAsyncLowering ? llvm::CallingConv::SwiftTail : SwiftCC;

if (opts.DebugInfoLevel > IRGenDebugInfoLevel::None)
DebugInfo = IRGenDebugInfo::createIRGenDebugInfo(IRGen.Opts, *CI, *this,
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ class IRGenModule {
llvm::CallingConv::ID C_CC; /// standard C calling convention
llvm::CallingConv::ID DefaultCC; /// default calling convention
llvm::CallingConv::ID SwiftCC; /// swift calling convention
llvm::CallingConv::ID SwiftAsyncCC; /// swift calling convention for async

Signature getAssociatedTypeWitnessTableAccessFunctionSignature();

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef newExecutor,
actor->giveUpThread(runner);
}

SWIFT_CC(swiftasync)
void swift::swift_task_switch(AsyncTask *task, ExecutorRef currentExecutor,
ExecutorRef newExecutor) {
assert(task && "no task provided");
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Concurrency/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ AsyncTaskAndContext swift::swift_task_create_future_f(
return {task, initialContext};
}

SWIFT_CC(swiftasync)
void swift::swift_task_future_wait(
AsyncTask *waitingTask, ExecutorRef executor,
SWIFT_ASYNC_CONTEXT AsyncContext *rawContext) {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/TaskGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void AsyncTask::groupOffer(AsyncTask *completedTask, AsyncContext *context,

// =============================================================================
// ==== group.next() implementation (wait_next and groupPoll) ------------------

SWIFT_CC(swiftasync)
void swift::swift_task_group_wait_next(
AsyncTask *waitingTask,
ExecutorRef executor,
Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class SomeClass {}
@_silgen_name("swift_task_future_wait")
public func task_future_wait(_ task: __owned SomeClass) async throws -> Int

// CHECK: define{{.*}} swiftcc void @"$s5async8testThisyyAA9SomeClassCnYF"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
// CHECK: define{{.*}} swift{{(tail)?}}cc void @"$s5async8testThisyyAA9SomeClassCnYF"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
// CHECK-64: call swiftcc i8* @swift_task_alloc(%swift.task* %{{[0-9]+}}, i64 64)
// CHECK: tail call swiftcc void @swift_task_future_wait(
// CHECK: tail call swift{{(tail)?}}cc void @swift_task_future_wait(
public func testThis(_ task: __owned SomeClass) async {
do {
let _ = try await task_future_wait(task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ func printGeneric<T>(_ t: T) {
// CHECK-LL: @"$s4main6call_fyyAA1CCYFTu" = {{(dllexport )?}}{{(protected )?}}global %swift.async_func_pointer
// CHECK-LL: @"$s4main1CC1fyyYFTu" = {{(dllexport )?}}{{(protected )?}}global %swift.async_func_pointer

// CHECK-LL: define {{(dllexport )?}}{{(protected )?}}swiftcc void @"$s4main6call_fyyAA1CCYF"(%swift.task* {{%[0-9]+}}, %swift.executor* {{%[0-9]+}}, %swift.context* swiftasync {{%[0-9]+}}) {{#[0-9]*}} {
// CHECK-LL: define {{(dllexport )?}}{{(protected )?}}swiftcc void @"$s4main1CC1fyyYF"(%swift.task* {{%[0-9]+}}, %swift.executor* {{%[0-9]+}}, %swift.context* swiftasync {{%[0-9]+}}) {{#[0-9]*}} {
// CHECK-LL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s4main6call_fyyAA1CCYF"(%swift.task* {{%[0-9]+}}, %swift.executor* {{%[0-9]+}}, %swift.context* swiftasync {{%[0-9]+}}) {{#[0-9]*}} {
// CHECK-LL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s4main1CC1fyyYF"(%swift.task* {{%[0-9]+}}, %swift.executor* {{%[0-9]+}}, %swift.context* swiftasync {{%[0-9]+}}) {{#[0-9]*}} {
public func call_f(_ c: C) async {
print("entering call_f")
await c.f()
Expand Down
18 changes: 9 additions & 9 deletions test/IRGen/async/builtins.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sil_stage canonical
import Builtin
import Swift

// CHECK-LABEL: define hidden swiftcc void @get_task(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
// CHECK-LABEL: define hidden swift{{(tail)?}}cc void @get_task(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
sil hidden [ossa] @get_task : $@async @convention(thin) () -> @owned Builtin.NativeObject {
bb0:
// CHECK: [[TASKLOC:%.*]] = alloca %swift.task*
Expand All @@ -22,24 +22,24 @@ bb0:
return %1 : $Builtin.NativeObject
}

// CHECK-LABEL: define hidden swiftcc void @cancel_task(%swift.refcounted* %0)
// CHECK-LABEL: define hidden swift{{(tail)?}}cc void @cancel_task(%swift.refcounted* %0)
// CHECK: [[TASK:%.*]] = bitcast %swift.refcounted* %0 to %swift.task*
// CHECK-NEXT: call swiftcc void @swift_task_cancel(%swift.task* [[TASK]])
// CHECK-NEXT: call swift{{(tail)?}}cc void @swift_task_cancel(%swift.task* [[TASK]])
sil hidden [ossa] @cancel_task : $@convention(method) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
%4 = builtin "cancelAsyncTask"(%0 : $Builtin.NativeObject) : $()
%5 = tuple ()
return %5 : $()
}

// CHECK-LABEL: define hidden swiftcc void @launch_task
// CHECK-LABEL: define hidden swift{{(tail)?}}cc void @launch_task
sil hidden [ossa] @launch_task : $@convention(method) @async (Int, Optional<Builtin.NativeObject>, @guaranteed @async @callee_guaranteed () -> (@error Error)) -> () {
bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@async @callee_guaranteed () -> (@error Error)):
%copy = copy_value %1 : $Optional<Builtin.NativeObject>
%3 = begin_borrow %copy : $Optional<Builtin.NativeObject>
// CHECK: [[FN_CONTEXT:%.*]] = load %swift.refcounted*, %swift.refcounted** %.data
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[FN_CONTEXT]])
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_f(
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swift{{(tail)?}}cc %swift.async_task_and_context @swift_task_create_f(
// CHECK-NEXT: [[NEW_CONTEXT_RAW:%.*]] = extractvalue %swift.async_task_and_context [[NEW_TASK_AND_CONTEXT]], 1
// CHECK-NEXT: [[NEW_CONTEXT:%.*]] = bitcast %swift.context* [[NEW_CONTEXT_RAW]] to
// CHECK-NEXT: [[CONTEXT_INFO_LOC:%.*]] = getelementptr inbounds <{{.*}}>* [[NEW_CONTEXT]]
Expand All @@ -52,7 +52,7 @@ bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@a
return %21 : $()
}

// CHECK-LABEL: define hidden swiftcc void @launch_future
// CHECK-LABEL: define hidden swift{{(tail)?}}cc void @launch_future
sil hidden [ossa] @launch_future : $@convention(method) <T> (Int, Optional<Builtin.NativeObject>, @guaranteed @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <T>, @in_guaranteed T) -> () {
bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <T>, %3: $*T):
%copy = copy_value %1 : $Optional<Builtin.NativeObject>
Expand All @@ -63,7 +63,7 @@ bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@a
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[FN_CONTEXT:%.*]])
%9 = metatype $@thick T.Type
%10 = init_existential_metatype %9 : $@thick T.Type, $@thick Any.Type
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_future_f(
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swift{{(tail)?}}cc %swift.async_task_and_context @swift_task_create_future_f(
// CHECK-NEXT: [[NEW_CONTEXT_RAW:%.*]] = extractvalue %swift.async_task_and_context [[NEW_TASK_AND_CONTEXT]], 1
// CHECK-NEXT: [[NEW_CONTEXT:%.*]] = bitcast %swift.context* [[NEW_CONTEXT_RAW]] to
// CHECK-32-NEXT: [[CONTEXT_INFO_LOC:%.*]] = getelementptr inbounds <{{.*}}>* [[NEW_CONTEXT]], i32 0, i32 6
Expand All @@ -77,7 +77,7 @@ bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@a
return %21 : $()
}

// CHECK-LABEL: define hidden swiftcc void @launch_void_future
// CHECK-LABEL: define hidden swift{{(tail)?}}cc void @launch_void_future
sil hidden [ossa] @launch_void_future : $@convention(method) (Int, Optional<Builtin.NativeObject>, @guaranteed @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <()>) -> () {
bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error Error) for <()>):
%copy = copy_value %1 : $Optional<Builtin.NativeObject>
Expand All @@ -88,7 +88,7 @@ bb0(%0 : $Int, %1: @unowned $Optional<Builtin.NativeObject>, %2: @guaranteed $@a
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[FN_CONTEXT:%.*]])
%8 = metatype $@thick ().Type // user: %9
%9 = init_existential_metatype %8 : $@thick ().Type, $@thick Any.Type // user: %10
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swiftcc %swift.async_task_and_context @swift_task_create_future_f(
// CHECK: [[NEW_TASK_AND_CONTEXT:%.*]] = call swift{{(tail)?}}cc %swift.async_task_and_context @swift_task_create_future_f(
// CHECK-NEXT: [[NEW_CONTEXT_RAW:%.*]] = extractvalue %swift.async_task_and_context [[NEW_TASK_AND_CONTEXT]], 1
// CHECK-NEXT: [[NEW_CONTEXT:%.*]] = bitcast %swift.context* [[NEW_CONTEXT_RAW]] to
// CHECK-32-NEXT: [[CONTEXT_INFO_LOC:%.*]] = getelementptr inbounds <{{.*}}>* [[NEW_CONTEXT]], i32 0, i32 6
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/async/class_resilience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ open class MyBaseClass<T> {
// CHECK-LABEL: @"$s16class_resilience9MyDerivedCMn" = hidden constant
// CHECK-SAME: %swift.async_func_pointer* @"$s16class_resilience9MyDerivedC4waitSiyYF010resilient_A09BaseClassCADxyYFTVTu"

// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swiftcc void @"$s16class_resilience14callsAwaitableyx010resilient_A09BaseClassCyxGYlF"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s16class_resilience14callsAwaitableyx010resilient_A09BaseClassCyxGYlF"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2)
// CHECK: %swift.async_func_pointer* @"$s15resilient_class9BaseClassC4waitxyYFTjTu"
// CHECK: ret void
public func callsAwaitable<T>(_ c: BaseClass<T>) async -> T {
return await c.wait()
}

// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swiftcc void @"$s16class_resilience11MyBaseClassC4waitxyYFTj"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2) #0 {
// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swift{{(tail)?}}cc void @"$s16class_resilience11MyBaseClassC4waitxyYFTj"(%swift.task* %0, %swift.executor* %1, %swift.context* swiftasync %2) #0 {

class MyDerived : BaseClass<Int> {
override func wait() async -> Int {
return await super.wait()
}
}
}
2 changes: 1 addition & 1 deletion test/IRGen/async/debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// REQUIRES: concurrency

// Don't assert on dynamically sized variables.
// CHECK: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s5debug1fyxxYKlF"
// CHECK: define{{( dllexport)?}}{{( protected)?}} swift{{(tail)?}}cc void @"$s5debug1fyxxYKlF"

public func f<Success>(_ value: Success) async throws -> Success {
switch Result<Success, Error>.success(value) {
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/async/hop_to_executor.sil
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bb0(%0 : $MyActor):
// CHECK-arm64e: [[PTRAUTH_SIGN:%[^,]+]] = call i64 @llvm.ptrauth.sign.i64(i64 [[CTXT_INT]], i32 2, i64 [[PTRAUTH_BLEND]])
// CHECK-arm64e: [[CTXT:%[^,]+]] = inttoptr i64 [[PTRAUTH_SIGN]] to %swift.context*
// CHECK: store %swift.context* [[CTXT]], %swift.context** [[CTXT_ADDR]]
// CHECK: tail call swiftcc void @swift_task_switch(%swift.task* %2, %swift.executor* %3, %swift.executor* %1)
// CHECK: tail call swift{{(tail)?}}cc void @swift_task_switch(%swift.task* %2, %swift.executor* %3, %swift.executor* %1)
// CHECK: ret void

sil_vtable MyActor {
Expand Down
Loading