Skip to content

Commit 6e250ca

Browse files
committed
[coro async] Add support for specifying which parameter is swiftself in
async resume functions Differential Revision: https://reviews.llvm.org/D104147
1 parent 8ba3b7a commit 6e250ca

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,8 @@ Value *CoroCloner::deriveNewFramePointer() {
765765
// context header.
766766
case coro::ABI::Async: {
767767
auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(ActiveSuspend);
768-
auto *CalleeContext =
769-
NewF->getArg(ActiveAsyncSuspend->getStorageArgumentIndex());
768+
auto ContextIdx = ActiveAsyncSuspend->getStorageArgumentIndex() & 0xff;
769+
auto *CalleeContext = NewF->getArg(ContextIdx);
770770
auto *FramePtrTy = Shape.FrameTy->getPointerTo();
771771
auto *ProjectionFunc =
772772
ActiveAsyncSuspend->getAsyncContextProjectionFunction();
@@ -827,6 +827,13 @@ static void addAsyncContextAttrs(AttributeList &Attrs, LLVMContext &Context,
827827
Attrs = Attrs.addParamAttributes(Context, ParamIndex, ParamAttrs);
828828
}
829829

830+
static void addSwiftSelfAttrs(AttributeList &Attrs, LLVMContext &Context,
831+
unsigned ParamIndex) {
832+
AttrBuilder ParamAttrs;
833+
ParamAttrs.addAttribute(Attribute::SwiftSelf);
834+
Attrs = Attrs.addParamAttributes(Context, ParamIndex, ParamAttrs);
835+
}
836+
830837
/// Clone the body of the original function into a resume function of
831838
/// some sort.
832839
void CoroCloner::create() {
@@ -953,8 +960,16 @@ void CoroCloner::create() {
953960
auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(ActiveSuspend);
954961
if (OrigF.hasParamAttribute(Shape.AsyncLowering.ContextArgNo,
955962
Attribute::SwiftAsync)) {
956-
auto ContextArgIndex = ActiveAsyncSuspend->getStorageArgumentIndex();
963+
uint32_t ArgAttributeIndices =
964+
ActiveAsyncSuspend->getStorageArgumentIndex();
965+
auto ContextArgIndex = ArgAttributeIndices & 0xff;
957966
addAsyncContextAttrs(NewAttrs, Context, ContextArgIndex);
967+
968+
// `swiftasync` must preceed `swiftself` so 0 is not a valid index for
969+
// `swiftself`.
970+
auto SwiftSelfIndex = ArgAttributeIndices >> 8;
971+
if (SwiftSelfIndex)
972+
addSwiftSelfAttrs(NewAttrs, Context, SwiftSelfIndex);
958973
}
959974
break;
960975
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ is_not_equal:
416416
i32 64 ; Initial async context size without space for frame
417417
}>
418418

419-
define swiftcc void @polymorphic_suspend_return(i8* %async.ctxt, %async.task* %task, %async.actor* %actor) {
419+
define swiftcc void @polymorphic_suspend_return(i8* swiftasync %async.ctxt, %async.task* %task, %async.actor* %actor) {
420420
entry:
421421
%tmp = alloca { i64, i64 }, align 8
422422
%proj.1 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %tmp, i64 0, i32 0
@@ -451,7 +451,7 @@ entry:
451451
%resume_proj_fun = bitcast i8*(i8*)* @resume_context_projection to i8*
452452
%callee = bitcast void(i8*, %async.task*, %async.actor*)* @asyncSuspend to i8*
453453
%res = call {i8*, i8*, i8*, i8*} (i32, i8*, i8*, ...)
454-
@llvm.coro.suspend.async.sl_p0i8p0i8p0i8p0i8s(i32 0,
454+
@llvm.coro.suspend.async.sl_p0i8p0i8p0i8p0i8s(i32 256, ;; swiftasync at 0 and swiftself at 1 in resume function
455455
i8* %resume.func_ptr,
456456
i8* %resume_proj_fun,
457457
void (i8*, i8*, %async.task*, %async.actor*)* @my_async_function.my_other_async_function_fp.apply,
@@ -470,8 +470,8 @@ entry:
470470
unreachable
471471
}
472472

473-
; CHECK-LABEL: define swiftcc void @polymorphic_suspend_return(i8* %async.ctxt, %async.task* %task, %async.actor* %actor)
474-
; CHECK-LABEL: define internal swiftcc void @polymorphic_suspend_return.resume.0(i8* {{.*}}%0, i8* {{.*}}%1, i8* {{.*}}%2, i8* {{.*}}%3)
473+
; CHECK-LABEL: define swiftcc void @polymorphic_suspend_return(i8* swiftasync %async.ctxt, %async.task* %task, %async.actor* %actor)
474+
; CHECK-LABEL: define internal swiftcc void @polymorphic_suspend_return.resume.0(i8* {{.*}}swiftasync{{.*}} %0, i8* {{.*}}swiftself{{.*}} %1, i8* {{.*}}%2, i8* {{.*}}%3)
475475
; CHECK: bitcast i8* %3 to %async.task*
476476
; CHECK: }
477477

0 commit comments

Comments
 (0)