Skip to content

Commit cdf9f9d

Browse files
committed
[CoroutineAccessors] Forward frame to de/alloc fns
Pass the frame from the outer yield_once_2 coroutine into the swift_coro_de/alloc functions and from there into all four de/allocation witnesses. This enables ABIs which key off of information stored in the frame.
1 parent 76881b1 commit cdf9f9d

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

lib/IRGen/GenCoro.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ struct Allocator {
737737
case Field::AllocateFrame:
738738
case Field::DeallocateFrame:
739739
auto attrs = llvm::AttributeList();
740-
IGM.addSwiftCoroAttributes(attrs, 0);
740+
IGM.addSwiftCoroAttributes(attrs, 1);
741741
return attrs;
742742
}
743743
}
@@ -877,12 +877,13 @@ struct Allocator {
877877

878878
llvm::Constant *getCoroAllocFn(AllocationKind kind, IRGenModule &IGM) {
879879
return IGM.getOrCreateHelperFunction(
880-
kind.getAllocationFunctionName(), IGM.Int8PtrTy,
881-
{IGM.CoroAllocatorPtrTy, IGM.SizeTy},
880+
kind.getAllocationFunctionName(), IGM.CoroAllocationTy,
881+
{IGM.CoroAllocatorPtrTy, IGM.CoroAllocationTy, IGM.SizeTy},
882882
[kind](IRGenFunction &IGF) {
883883
auto isSwiftCoroCCAvailable =
884884
IGF.IGM.SwiftCoroCC == llvm::CallingConv::SwiftCoro;
885885
auto parameters = IGF.collectParameters();
886+
auto *frame = parameters.claimNext();
886887
auto *allocatorValue = parameters.claimNext();
887888
auto allocator = Allocator(allocatorValue, IGF);
888889
auto *size = parameters.claimNext();
@@ -903,7 +904,7 @@ llvm::Constant *getCoroAllocFn(AllocationKind kind, IRGenModule &IGM) {
903904
});
904905
}
905906
auto fnPtr = allocator.getAllocate(kind);
906-
auto *call = IGF.Builder.CreateCall(fnPtr, {allocatorValue, size});
907+
auto *call = IGF.Builder.CreateCall(fnPtr, {frame, allocatorValue, size});
907908
call->setDoesNotThrow();
908909
call->setCallingConv(IGF.IGM.SwiftCC);
909910
IGF.Builder.CreateRet(call);
@@ -914,18 +915,19 @@ llvm::Constant *getCoroAllocFn(AllocationKind kind, IRGenModule &IGM) {
914915
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC,
915916
/*transformAttributes=*/
916917
[&IGM](llvm::AttributeList &attrs) {
917-
IGM.addSwiftCoroAttributes(attrs, 0);
918+
IGM.addSwiftCoroAttributes(attrs, 1);
918919
});
919920
}
920921

921922
llvm::Constant *getCoroDeallocFn(AllocationKind kind, IRGenModule &IGM) {
922923
return IGM.getOrCreateHelperFunction(
923924
kind.getDeallocationFunctionName(), IGM.VoidTy,
924-
{IGM.CoroAllocatorPtrTy, IGM.Int8PtrTy},
925+
{IGM.CoroAllocatorPtrTy, IGM.CoroAllocationTy, IGM.CoroAllocationTy},
925926
[kind](IRGenFunction &IGF) {
926927
auto isSwiftCoroCCAvailable =
927928
IGF.IGM.SwiftCoroCC == llvm::CallingConv::SwiftCoro;
928929
auto parameters = IGF.collectParameters();
930+
auto *frame = parameters.claimNext();
929931
auto *allocatorValue = parameters.claimNext();
930932
auto allocator = Allocator(allocatorValue, IGF);
931933
auto *ptr = parameters.claimNext();
@@ -959,7 +961,7 @@ llvm::Constant *getCoroDeallocFn(AllocationKind kind, IRGenModule &IGM) {
959961
// Start emitting the "normal" block.
960962
IGF.Builder.emitBlock(normalBlock);
961963
auto fnPtr = allocator.getDeallocate(kind);
962-
auto *call = IGF.Builder.CreateCall(fnPtr, {allocatorValue, ptr});
964+
auto *call = IGF.Builder.CreateCall(fnPtr, {frame, allocatorValue, ptr});
963965
call->setDoesNotThrow();
964966
call->setCallingConv(IGF.IGM.SwiftCC);
965967
IGF.Builder.CreateRetVoid();
@@ -970,7 +972,7 @@ llvm::Constant *getCoroDeallocFn(AllocationKind kind, IRGenModule &IGM) {
970972
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC,
971973
/*transformAttributes=*/
972974
[&IGM](llvm::AttributeList &attrs) {
973-
IGM.addSwiftCoroAttributes(attrs, 0);
975+
IGM.addSwiftCoroAttributes(attrs, 1);
974976
});
975977
}
976978

@@ -1003,6 +1005,7 @@ getAddrOfSwiftCoroAllocThunk(StringRef name,
10031005
name, ty->getReturnType(), ty->params(),
10041006
[getAlloc](IRGenFunction &IGF) {
10051007
auto parameters = IGF.collectParameters();
1008+
parameters.claimNext(); // frame
10061009
parameters.claimNext(); // allocator
10071010
auto *size = parameters.claimNext();
10081011
auto alloc = (IGF.IGM.*getAlloc)();
@@ -1015,7 +1018,7 @@ getAddrOfSwiftCoroAllocThunk(StringRef name,
10151018
/*specialCallingConv=*/std::nullopt,
10161019
/*transformAttributes=*/
10171020
[&IGM](llvm::AttributeList &attrs) {
1018-
IGM.addSwiftCoroAttributes(attrs, 0);
1021+
IGM.addSwiftCoroAttributes(attrs, 1);
10191022
});
10201023
}
10211024

@@ -1030,6 +1033,7 @@ getAddrOfSwiftCoroDeallocThunk(StringRef name,
10301033
name, ty->getReturnType(), ty->params(),
10311034
[getDealloc](IRGenFunction &IGF) {
10321035
auto parameters = IGF.collectParameters();
1036+
parameters.claimNext(); // frame
10331037
parameters.claimNext(); // allocator
10341038
auto *ptr = parameters.claimNext();
10351039
auto dealloc = (IGF.IGM.*getDealloc)();
@@ -1042,7 +1046,7 @@ getAddrOfSwiftCoroDeallocThunk(StringRef name,
10421046
/*specialCallingConv=*/std::nullopt,
10431047
/*transformAttributes=*/
10441048
[&IGM](llvm::AttributeList &attrs) {
1045-
IGM.addSwiftCoroAttributes(attrs, 0);
1049+
IGM.addSwiftCoroAttributes(attrs, 1);
10461050
});
10471051
}
10481052

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,9 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
773773
CoroFunctionPointerTy = createStructType(*this, "swift.coro_func_pointer",
774774
{RelativeAddressTy, Int32Ty}, true);
775775
CoroAllocateFnTy = llvm::FunctionType::get(
776-
CoroAllocationTy, {CoroAllocatorPtrTy, SizeTy}, /*isVarArg*/ false);
776+
CoroAllocationTy, {CoroAllocationTy, CoroAllocatorPtrTy, SizeTy}, /*isVarArg*/ false);
777777
CoroDeallocateFnTy = llvm::FunctionType::get(
778-
VoidTy, {CoroAllocatorPtrTy, CoroAllocationTy}, /*isVarArg*/ false);
778+
VoidTy, {CoroAllocationTy, CoroAllocatorPtrTy, CoroAllocationTy}, /*isVarArg*/ false);
779779
CoroAllocatorFlagsTy = Int32Ty;
780780
// swift/ABI/Coro.h : CoroAllocator
781781
CoroAllocatorTy = createStructType(*this, "swift.coro_allocator",

test/IRGen/coroutine_accessors.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
// CHECK-SAME: }
6464

6565
// CHECK-LABEL: @_swift_coro_alloc(
66+
// CHECK-SAME: ptr [[FRAME:%[^,]+]]
6667
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
6768
// CHECK-SAME: [[INT]] [[SIZE:%[^)]+]]
6869
// CHECK-SAME: )
@@ -76,11 +77,12 @@
7677
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
7778
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 24469)
7879
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
79-
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
80+
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
8081
// CHECK: ret ptr [[ALLOCATION]]
8182
// CHECK: }
8283

8384
// CHECK-LABEL: @_swift_coro_dealloc(
85+
// CHECK-SAME: ptr [[FRAME:%[^,]+]]
8486
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
8587
// CHECK-SAME: ptr [[ADDRESS:%[^)]+]]
8688
// CHECK-SAME: )
@@ -107,7 +109,7 @@
107109
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
108110
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 40879)
109111
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
110-
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
112+
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
111113
// CHECK: ret void
112114
// CHECK: }
113115

test/IRGen/coroutine_accessors_popless.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
// CHECK-SAME: }
6666

6767
// CHECK-LABEL: @_swift_coro_alloc(
68-
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
68+
// CHECK-SAME: ptr [[FRAME:%[^,]+]]
69+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]],
6970
// CHECK-SAME: i64 [[SIZE:%[^)]+]]
7071
// CHECK-SAME: )
7172
// CHECK-SAME: {
@@ -87,12 +88,13 @@
8788
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
8889
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 24469)
8990
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
90-
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
91+
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
9192
// CHECK: ret ptr [[ALLOCATION]]
9293
// CHECK: }
9394

9495
// CHECK-LABEL: @_swift_coro_dealloc(
95-
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
96+
// CHECK-SAME: ptr [[FRAME:%[^,]+]]
97+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]],
9698
// CHECK-SAME: ptr [[ADDRESS:%[^)]+]]
9799
// CHECK-SAME: )
98100
// CHECK-SAME: {
@@ -125,7 +127,7 @@
125127
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
126128
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 40879)
127129
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
128-
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
130+
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
129131
// CHECK: ret void
130132
// CHECK: }
131133

0 commit comments

Comments
 (0)