Skip to content

Commit a0098c0

Browse files
Merge pull request swiftlang#36037 from aschwaighofer/irgen_use_swifttailcc
Conditionally start using llvm::CallingConv::SwiftTail for async functions
2 parents b49be98 + afac324 commit a0098c0

File tree

63 files changed

+154
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+154
-143
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ FUNCTION(TaskCreateFutureFunc,
15271527
// ExecutorRef currentExecutor,
15281528
// ExecutorRef newExecutor);
15291529
FUNCTION(TaskSwitchFunc,
1530-
swift_task_switch, SwiftCC,
1530+
swift_task_switch, SwiftAsyncCC,
15311531
ConcurrencyAvailability,
15321532
RETURNS(VoidTy),
15331533
ARGS(SwiftTaskPtrTy, SwiftExecutorPtrTy, SwiftExecutorPtrTy),

lib/IRGen/GenCall.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ static llvm::CallingConv::ID getFreestandingConvention(IRGenModule &IGM) {
462462
/// Expand the requirements of the given abstract calling convention
463463
/// into a "physical" calling convention.
464464
llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
465-
SILFunctionTypeRepresentation convention) {
465+
SILFunctionTypeRepresentation convention,
466+
bool isAsync) {
466467
switch (convention) {
467468
case SILFunctionTypeRepresentation::CFunctionPointer:
468469
case SILFunctionTypeRepresentation::ObjCMethod:
@@ -474,6 +475,8 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
474475
case SILFunctionTypeRepresentation::Closure:
475476
case SILFunctionTypeRepresentation::Thin:
476477
case SILFunctionTypeRepresentation::Thick:
478+
if (isAsync)
479+
return IGM.SwiftAsyncCC;
477480
return getFreestandingConvention(IGM);
478481
}
479482
llvm_unreachable("bad calling convention!");
@@ -1849,7 +1852,8 @@ Signature SignatureExpansion::getSignature() {
18491852
(FnType->getLanguage() == SILFunctionLanguage::C) &&
18501853
"C function type without C function info");
18511854

1852-
auto callingConv = expandCallingConv(IGM, FnType->getRepresentation());
1855+
auto callingConv =
1856+
expandCallingConv(IGM, FnType->getRepresentation(), FnType->isAsync());
18531857

18541858
Signature result;
18551859
result.Type = llvmType;
@@ -4808,7 +4812,7 @@ IRGenFunction::getFunctionPointerForResumeIntrinsic(llvm::Value *resume) {
48084812
IGM.VoidTy, {IGM.Int8PtrTy, IGM.Int8PtrTy, IGM.Int8PtrTy},
48094813
false /*vaargs*/);
48104814
auto signature =
4811-
Signature(fnTy, IGM.constructInitialAttributes(), IGM.SwiftCC);
4815+
Signature(fnTy, IGM.constructInitialAttributes(), IGM.SwiftAsyncCC);
48124816
auto fnPtr = FunctionPointer(
48134817
FunctionPointer::KindTy::Function,
48144818
Builder.CreateBitOrPointerCast(resume, fnTy->getPointerTo()),

lib/IRGen/GenCall.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ namespace irgen {
316316
std::pair<bool, bool> values = {true, true},
317317
Size initialContextSize = Size(0));
318318
llvm::CallingConv::ID expandCallingConv(IRGenModule &IGM,
319-
SILFunctionTypeRepresentation convention);
319+
SILFunctionTypeRepresentation convention,
320+
bool isAsync);
320321

321322
Signature emitCastOfFunctionPointer(IRGenFunction &IGF, llvm::Value *&fnPtr,
322323
CanSILFunctionType fnType);

lib/IRGen/GenFunc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2582,7 +2582,7 @@ llvm::Function *IRGenFunction::createAsyncSuspendFn() {
25822582
IGM.getTaskSwitchFuncFn(),
25832583
{ task, executor, targetExecutor });
25842584
suspendCall->setDoesNotThrow();
2585-
suspendCall->setCallingConv(IGM.SwiftCC);
2585+
suspendCall->setCallingConv(IGM.SwiftAsyncCC);
25862586
suspendCall->setTailCall();
25872587
Builder.CreateRetVoid();
25882588
return suspendFn;

lib/IRGen/GenObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ static llvm::Function *emitObjCPartialApplicationForwarder(IRGenModule &IGM,
710710
llvm::Function::Create(fwdTy, llvm::Function::InternalLinkage,
711711
MANGLE_AS_STRING(OBJC_PARTIAL_APPLY_THUNK_SYM),
712712
&IGM.Module);
713-
fwd->setCallingConv(
714-
expandCallingConv(IGM, SILFunctionTypeRepresentation::Thick));
713+
fwd->setCallingConv(expandCallingConv(
714+
IGM, SILFunctionTypeRepresentation::Thick, false/*isAsync*/));
715715

716716
fwd->setAttributes(attrs);
717717
// Merge initial attributes with attrs.

lib/IRGen/IRGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
547547
// TODO: use "tinycc" on platforms that support it
548548
DefaultCC = SWIFT_DEFAULT_LLVM_CC;
549549
SwiftCC = llvm::CallingConv::Swift;
550+
// TODO: Once clang support is merged this should also use
551+
// clangASTContext.getTargetInfo().isSwiftAsyncCCSupported()
552+
SwiftAsyncCC = opts.UseAsyncLowering ? llvm::CallingConv::SwiftTail : SwiftCC;
550553

551554
if (opts.DebugInfoLevel > IRGenDebugInfoLevel::None)
552555
DebugInfo = IRGenDebugInfo::createIRGenDebugInfo(IRGen.Opts, *CI, *this,

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ class IRGenModule {
753753
llvm::CallingConv::ID C_CC; /// standard C calling convention
754754
llvm::CallingConv::ID DefaultCC; /// default calling convention
755755
llvm::CallingConv::ID SwiftCC; /// swift calling convention
756+
llvm::CallingConv::ID SwiftAsyncCC; /// swift calling convention for async
756757

757758
Signature getAssociatedTypeWitnessTableAccessFunctionSignature();
758759

stdlib/public/Concurrency/Actor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,7 @@ static void runOnAssumedThread(AsyncTask *task, ExecutorRef newExecutor,
13281328
actor->giveUpThread(runner);
13291329
}
13301330

1331+
SWIFT_CC(swiftasync)
13311332
void swift::swift_task_switch(AsyncTask *task, ExecutorRef currentExecutor,
13321333
ExecutorRef newExecutor) {
13331334
assert(task && "no task provided");

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ AsyncTaskAndContext swift::swift_task_create_future_f(
317317
return {task, initialContext};
318318
}
319319

320+
SWIFT_CC(swiftasync)
320321
void swift::swift_task_future_wait(
321322
AsyncTask *waitingTask, ExecutorRef executor,
322323
SWIFT_ASYNC_CONTEXT AsyncContext *rawContext) {

stdlib/public/Concurrency/TaskGroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void AsyncTask::groupOffer(AsyncTask *completedTask, AsyncContext *context,
153153

154154
// =============================================================================
155155
// ==== group.next() implementation (wait_next and groupPoll) ------------------
156-
156+
SWIFT_CC(swiftasync)
157157
void swift::swift_task_group_wait_next(
158158
AsyncTask *waitingTask,
159159
ExecutorRef executor,

0 commit comments

Comments
 (0)