Skip to content

Commit 0446a4a

Browse files
committed
[coro async] Use swift mangling for resume functions
The resume partial functions generated for swift suspend points will now use a Swift mangling suffix. Await resume partial functions will use the suffix 'TQ'[0-9]+'_' (e.g "...TQ0_") and suspend resume partial functions will use the suffix 'TY'[0-9]+'_' (e.g "...TY1_"). Reviewed By: nate_chandler Differential Revision: https://reviews.llvm.org/D104144
1 parent 9ad294d commit 0446a4a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,8 +1600,23 @@ static void splitAsyncCoroutine(Function &F, coro::Shape &Shape,
16001600
auto *Suspend = cast<CoroSuspendAsyncInst>(Shape.CoroSuspends[Idx]);
16011601

16021602
// Create the clone declaration.
1603+
auto ResumeNameSuffix = ".resume.";
1604+
auto ProjectionFunctionName =
1605+
Suspend->getAsyncContextProjectionFunction()->getName();
1606+
bool UseSwiftMangling = false;
1607+
if (ProjectionFunctionName.equals("__swift_async_resume_project_context")) {
1608+
ResumeNameSuffix = "TQ";
1609+
UseSwiftMangling = true;
1610+
} else if (ProjectionFunctionName.equals(
1611+
"__swift_async_resume_get_context")) {
1612+
ResumeNameSuffix = "TY";
1613+
UseSwiftMangling = true;
1614+
}
16031615
auto *Continuation = createCloneDeclaration(
1604-
F, Shape, ".resume." + Twine(Idx), NextF, Suspend);
1616+
F, Shape,
1617+
UseSwiftMangling ? ResumeNameSuffix + Twine(Idx) + "_"
1618+
: ResumeNameSuffix + Twine(Idx),
1619+
NextF, Suspend);
16051620
Clones.push_back(Continuation);
16061621

16071622
// Insert a branch to a new return block immediately before the suspend

llvm/test/Transforms/Coroutines/coro-async.ll

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ define swiftcc void @my_async_function.my_other_async_function_fp.apply(i8* %fnP
4646
declare void @some_user(i64)
4747
declare void @some_may_write(i64*)
4848

49+
define i8* @__swift_async_resume_project_context(i8* %ctxt) {
50+
entry:
51+
%resume_ctxt_addr = bitcast i8* %ctxt to i8**
52+
%resume_ctxt = load i8*, i8** %resume_ctxt_addr, align 8
53+
ret i8* %resume_ctxt
54+
}
55+
4956
define i8* @resume_context_projection(i8* %ctxt) {
5057
entry:
5158
%resume_ctxt_addr = bitcast i8* %ctxt to i8**
@@ -86,7 +93,7 @@ entry:
8693
; store caller context into callee context
8794
%callee_context.caller_context.addr = getelementptr inbounds %async.ctxt, %async.ctxt* %callee_context.0, i32 0, i32 0
8895
store i8* %async.ctxt, i8** %callee_context.caller_context.addr
89-
%resume_proj_fun = bitcast i8*(i8*)* @resume_context_projection to i8*
96+
%resume_proj_fun = bitcast i8*(i8*)* @__swift_async_resume_project_context to i8*
9097
%callee = bitcast void(i8*, %async.task*, %async.actor*)* @asyncSuspend to i8*
9198
%res = call {i8*, i8*, i8*} (i32, i8*, i8*, ...) @llvm.coro.suspend.async(i32 0,
9299
i8* %resume.func_ptr,
@@ -141,14 +148,14 @@ define void @my_async_function_pa(i8* %ctxt, %async.task* %task, %async.actor* %
141148
; CHECK: store i8* [[CALLEE_CTXT]], i8** [[CAST2]]
142149
; CHECK: [[TYPED_RETURN_TO_CALLER_ADDR:%.*]] = getelementptr inbounds i8, i8* [[CALLEE_CTXT]], i64 8
143150
; CHECK: [[RETURN_TO_CALLER_ADDR:%.*]] = bitcast i8* [[TYPED_RETURN_TO_CALLER_ADDR]] to i8**
144-
; CHECK: store i8* bitcast (void (i8*, i8*, i8*)* @my_async_function.resume.0 to i8*), i8** [[RETURN_TO_CALLER_ADDR]]
151+
; CHECK: store i8* bitcast (void (i8*, i8*, i8*)* @my_async_functionTQ0_ to i8*), i8** [[RETURN_TO_CALLER_ADDR]]
145152
; CHECK: [[CALLER_CONTEXT_ADDR:%.*]] = bitcast i8* [[CALLEE_CTXT]] to i8**
146153
; CHECK: store i8* %async.ctxt, i8** [[CALLER_CONTEXT_ADDR]]
147154
; CHECK: tail call swiftcc void @asyncSuspend(i8* [[CALLEE_CTXT]], %async.task* %task, %async.actor* %actor)
148155
; CHECK: ret void
149156
; CHECK: }
150157

151-
; CHECK-LABEL: define internal swiftcc void @my_async_function.resume.0(i8* nocapture readonly swiftasync %0, i8* %1, i8* nocapture readnone %2)
158+
; CHECK-LABEL: define internal swiftcc void @my_async_functionTQ0_(i8* nocapture readonly swiftasync %0, i8* %1, i8* nocapture readnone %2)
152159
; CHECK-SAME: !dbg ![[SP2:[0-9]+]] {
153160
; CHECK: entryresume.0:
154161
; CHECK: [[CALLER_CONTEXT_ADDR:%.*]] = bitcast i8* %0 to i8**
@@ -278,7 +285,7 @@ define swiftcc void @top_level_caller(i8* %ctxt, i8* %task, i8* %actor) {
278285
}
279286

280287
; CHECK-LABEL: define swiftcc void @top_level_caller(i8* %ctxt, i8* %task, i8* %actor)
281-
; CHECK: store i8* bitcast (void (i8*, i8*, i8*)* @my_async_function.resume.0
288+
; CHECK: store i8* bitcast (void (i8*, i8*, i8*)* @my_async_functionTQ0_
282289
; CHECK: store i8* %ctxt
283290
; CHECK: tail call swiftcc void @asyncSuspend
284291
; CHECK: ret void
@@ -548,7 +555,7 @@ declare void @llvm.coro.async.size.replace(i8*, i8*)
548555
scope: !2, file: !3, line: 1, type: !4,
549556
scopeLine: 1, spFlags: DISPFlagDefinition, unit: !2)
550557
; CHECK: ![[SP2]] = distinct !DISubprogram(name: "my_async_function",
551-
; CHECK-SAME: linkageName: "my_async_function.resume.0",
558+
; CHECK-SAME: linkageName: "my_async_functionTQ0_",
552559
; CHECK-SAME: scopeLine: 2
553560
!2 = distinct !DICompileUnit(language: DW_LANG_Swift, file: !3, emissionKind: FullDebug)
554561
!3 = !DIFile(filename: "/tmp/1.swift", directory: "/")

llvm/test/Transforms/Coroutines/coro-split-swift-async.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
; dbg.declare and not stashed into an alloca. They will get lowered to
1717
; an entry value in the backend.
1818

19-
; CHECK: define internal swiftcc void @"$s1a1fyS2iYF.resume.0"(i8* %0, i8* %1, i8* swiftasync %2)
19+
; CHECK: define internal swiftcc void @"$s1a1fyS2iYFTY0_"(i8* %0, i8* %1, i8* swiftasync %2)
2020
; CHECK: entryresume.0:
2121
; CHECK-NEXT: call void @llvm.dbg.declare(metadata i8* %2, metadata ![[X:[0-9]+]], metadata !DIExpression(DW_OP_plus_uconst, 64, DW_OP_plus_uconst, 24))
2222
; CHECK: ![[X]] = !DILocalVariable(name: "x",

0 commit comments

Comments
 (0)