Skip to content

Commit 4a361f5

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 513ad68 commit 4a361f5

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() {
@@ -949,8 +956,16 @@ void CoroCloner::create() {
949956
auto *ActiveAsyncSuspend = cast<CoroSuspendAsyncInst>(ActiveSuspend);
950957
if (OrigF.hasParamAttribute(Shape.AsyncLowering.ContextArgNo,
951958
Attribute::SwiftAsync)) {
952-
auto ContextArgIndex = ActiveAsyncSuspend->getStorageArgumentIndex();
959+
uint32_t ArgAttributeIndices =
960+
ActiveAsyncSuspend->getStorageArgumentIndex();
961+
auto ContextArgIndex = ArgAttributeIndices & 0xff;
953962
addAsyncContextAttrs(NewAttrs, Context, ContextArgIndex);
963+
964+
// `swiftasync` must preceed `swiftself` so 0 is not a valid index for
965+
// `swiftself`.
966+
auto SwiftSelfIndex = ArgAttributeIndices >> 8;
967+
if (SwiftSelfIndex)
968+
addSwiftSelfAttrs(NewAttrs, Context, SwiftSelfIndex);
954969
}
955970
break;
956971
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ is_not_equal:
410410
i32 64 ; Initial async context size without space for frame
411411
}>
412412

413-
define swiftcc void @polymorphic_suspend_return(i8* %async.ctxt, %async.task* %task, %async.actor* %actor) {
413+
define swiftcc void @polymorphic_suspend_return(i8* swiftasync %async.ctxt, %async.task* %task, %async.actor* %actor) {
414414
entry:
415415
%tmp = alloca { i64, i64 }, align 8
416416
%proj.1 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %tmp, i64 0, i32 0
@@ -445,7 +445,7 @@ entry:
445445
%resume_proj_fun = bitcast i8*(i8*)* @resume_context_projection to i8*
446446
%callee = bitcast void(i8*, %async.task*, %async.actor*)* @asyncSuspend to i8*
447447
%res = call {i8*, i8*, i8*, i8*} (i32, i8*, i8*, ...)
448-
@llvm.coro.suspend.async.sl_p0i8p0i8p0i8p0i8s(i32 0,
448+
@llvm.coro.suspend.async.sl_p0i8p0i8p0i8p0i8s(i32 256, ;; swiftasync at 0 and swiftself at 1 in resume function
449449
i8* %resume.func_ptr,
450450
i8* %resume_proj_fun,
451451
void (i8*, i8*, %async.task*, %async.actor*)* @my_async_function.my_other_async_function_fp.apply,
@@ -464,8 +464,8 @@ entry:
464464
unreachable
465465
}
466466

467-
; CHECK-LABEL: define swiftcc void @polymorphic_suspend_return(i8* %async.ctxt, %async.task* %task, %async.actor* %actor)
468-
; CHECK-LABEL: define internal swiftcc void @polymorphic_suspend_return.resume.0(i8* {{.*}}%0, i8* {{.*}}%1, i8* {{.*}}%2, i8* {{.*}}%3)
467+
; CHECK-LABEL: define swiftcc void @polymorphic_suspend_return(i8* swiftasync %async.ctxt, %async.task* %task, %async.actor* %actor)
468+
; CHECK-LABEL: define internal swiftcc void @polymorphic_suspend_return.resume.0(i8* {{.*}}swiftasync{{.*}} %0, i8* {{.*}}swiftself{{.*}} %1, i8* {{.*}}%2, i8* {{.*}}%3)
469469
; CHECK: bitcast i8* %3 to %async.task*
470470
; CHECK: }
471471

0 commit comments

Comments
 (0)