Skip to content

Commit c4bbb16

Browse files
committed
[Coro] RetconOnceDynamic: Add type id.
Add type id to coro function pointer struct, write type id into id intrinsic, pass type id to allocation functions.
1 parent e52b2dd commit c4bbb16

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ def int_coro_id_retcon_once : Intrinsic<[llvm_token_ty],
17711771
[]>;
17721772
def int_coro_id_retcon_once_dynamic : Intrinsic<[llvm_token_ty],
17731773
[llvm_i32_ty, llvm_i32_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty,
1774-
llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty],
1774+
llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty, llvm_i64_ty],
17751775
[]>;
17761776
def int_coro_alloc : Intrinsic<[llvm_i1_ty], [llvm_token_ty], []>;
17771777
def int_coro_id_async : Intrinsic<[llvm_token_ty],
@@ -1819,7 +1819,7 @@ def int_coro_prepare_retcon : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty],
18191819
def int_coro_alloca_alloc : Intrinsic<[llvm_token_ty],
18201820
[llvm_anyint_ty, llvm_i32_ty], []>;
18211821
def int_coro_alloca_alloc_frame : Intrinsic<[llvm_token_ty],
1822-
[llvm_anyint_ty, llvm_i32_ty], []>;
1822+
[llvm_anyint_ty, llvm_i32_ty, llvm_i64_ty], []>;
18231823
def int_coro_alloca_get : Intrinsic<[llvm_ptr_ty], [llvm_token_ty], []>;
18241824
def int_coro_alloca_free : Intrinsic<[], [llvm_token_ty], []>;
18251825
def int_coro_alloca_free_frame : Intrinsic<[], [llvm_token_ty], []>;

llvm/include/llvm/Transforms/Coroutines/CoroInstr.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ class LLVM_LIBRARY_VISIBILITY CoroIdRetconOnceDynamicInst
330330
DeallocArg,
331331
AllocFrameArg,
332332
DeallocFrameArg,
333+
TypeIdArg,
333334
};
334335

335336
public:
@@ -385,6 +386,13 @@ class LLVM_LIBRARY_VISIBILITY CoroIdRetconOnceDynamicInst
385386

386387
Value *getAllocator() const { return getArgOperand(AllocatorArg); }
387388

389+
/// The TypeId to be used for allocating memory.
390+
ConstantInt *getTypeId() const {
391+
if (arg_size() <= TypeIdArg)
392+
return nullptr;
393+
return cast<ConstantInt>(getArgOperand(TypeIdArg));
394+
}
395+
388396
// Methods to support type inquiry through isa, cast, and dyn_cast:
389397
static bool classof(const IntrinsicInst *I) {
390398
return I->getIntrinsicID() == Intrinsic::coro_id_retcon_once_dynamic;
@@ -869,7 +877,16 @@ class CoroAllocaAllocInst : public AnyCoroAllocaAllocInst {
869877

870878
/// This represents the llvm.coro.alloca.alloc.frame instruction.
871879
class CoroAllocaAllocFrameInst : public AnyCoroAllocaAllocInst {
880+
enum { TypeIdArg = 2 };
881+
872882
public:
883+
/// Return the TypeId to be used for allocating typed memory
884+
Value *getTypeId() const {
885+
if (arg_size() <= TypeIdArg)
886+
return nullptr;
887+
return getArgOperand(TypeIdArg);
888+
}
889+
873890
// Methods to support type inquiry through isa, cast, and dyn_cast:
874891
static bool classof(const IntrinsicInst *I) {
875892
return I->getIntrinsicID() == Intrinsic::coro_alloca_alloc_frame;

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,10 @@ static void updateCoroFuncPointerContextSize(coro::Shape &Shape) {
11861186
auto *OrigContextSize = FuncPtrStruct->getOperand(1);
11871187
auto *NewContextSize = ConstantInt::get(OrigContextSize->getType(),
11881188
Shape.RetconLowering.ContextSize);
1189-
auto *NewFuncPtrStruct = ConstantStruct::get(
1190-
FuncPtrStruct->getType(), OrigRelativeFunOffset, NewContextSize);
1189+
auto *OrigTypeID = FuncPtrStruct->getOperand(2);
1190+
auto *NewFuncPtrStruct =
1191+
ConstantStruct::get(FuncPtrStruct->getType(), OrigRelativeFunOffset,
1192+
NewContextSize, OrigTypeID);
11911193

11921194
Shape.RetconLowering.CoroFuncPointer->setInitializer(NewFuncPtrStruct);
11931195
}

llvm/lib/Transforms/Coroutines/Coroutines.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ void coro::Shape::analyze(Function &F,
336336
RetconLowering.StorageSize = ContinuationId->getStorageSize();
337337
RetconLowering.StorageAlignment = ContinuationId->getStorageAlignment();
338338
RetconLowering.CoroFuncPointer = ContinuationId->getCoroFunctionPointer();
339+
RetconLowering.TypeId = ContinuationId->getTypeId();
339340
break;
340341
}
341342
case Intrinsic::coro_id_retcon:
@@ -535,8 +536,7 @@ Value *coro::Shape::emitAlloc(IRBuilder<> &Builder, Value *Size,
535536
case coro::ABI::RetconOnceDynamic: {
536537
unsigned sizeParamIndex = 0;
537538
Function *Alloc = nullptr;
538-
if (isa_and_nonnull<CoroAllocaAllocFrameInst>(AI)) {
539-
assert(ABI == coro::ABI::RetconOnceDynamic);
539+
if ((ABI == coro::ABI::RetconOnceDynamic) && isa<CoroAllocaAllocFrameInst>(AI)) {
540540
Alloc = RetconLowering.AllocFrame;
541541
} else {
542542
Alloc = RetconLowering.Alloc;
@@ -553,11 +553,14 @@ Value *coro::Shape::emitAlloc(IRBuilder<> &Builder, Value *Size,
553553
Size, Alloc->getFunctionType()->getParamType(sizeParamIndex),
554554
/*is signed*/ false);
555555
Args.push_back(Size);
556-
if (ABI == coro::ABI::RetconOnce) {
557-
ConstantInt *TypeId = RetconLowering.TypeId;
558-
if (TypeId != nullptr)
559-
Args.push_back(TypeId);
556+
Value *TypeId = nullptr;
557+
if (auto *CAAFI = dyn_cast_or_null<CoroAllocaAllocFrameInst>(AI)) {
558+
TypeId = CAAFI->getTypeId();
559+
} else if (ABI == coro::ABI::RetconOnce || ABI == coro::ABI::RetconOnceDynamic) {
560+
TypeId = RetconLowering.TypeId;
560561
}
562+
if (TypeId != nullptr)
563+
Args.push_back(TypeId);
561564
auto *Call = Builder.CreateCall(Alloc, Args);
562565
if (ABI == coro::ABI::RetconOnceDynamic) {
563566
Call->addParamAttr(1, Attribute::SwiftCoro);
@@ -583,7 +586,6 @@ void coro::Shape::emitDealloc(IRBuilder<> &Builder, Value *Ptr,
583586
case coro::ABI::RetconOnceDynamic: {
584587
Function *Dealloc = nullptr;
585588
if (isa_and_nonnull<CoroAllocaAllocFrameInst>(AI)) {
586-
assert(ABI == coro::ABI::RetconOnceDynamic);
587589
Dealloc = RetconLowering.DeallocFrame;
588590
} else {
589591
Dealloc = RetconLowering.Dealloc;
@@ -737,6 +739,8 @@ void CoroIdRetconOnceDynamicInst::checkWellFormed() const {
737739
checkWFRetconPrototype(this, getArgOperand(PrototypeArg));
738740
checkWFAlloc(this, getArgOperand(AllocArg));
739741
checkWFDealloc(this, getArgOperand(DeallocArg));
742+
checkWFAlloc(this, getArgOperand(AllocFrameArg));
743+
checkWFDealloc(this, getArgOperand(DeallocFrameArg));
740744
}
741745

742746
static void checkAsyncFuncPointer(const Instruction *I, Value *V) {

llvm/test/Transforms/Coroutines/coro-retcon-once-dynamic-nocleanup.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
44
target triple = "arm64-apple-macos99.99"
55

66

7-
@func_cfp = constant <{ i32, i32 }>
7+
@func_cfp = constant <{ i32, i32, i64 }>
88
<{ i32 trunc (
99
i64 sub (
1010
i64 ptrtoint (ptr @func to i64),
1111
i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, ptr @func_cfp, i32 0, i32 1) to i64)
1212
)
1313
to i32),
14-
i32 64
14+
i32 64, ; frame size
15+
i64 1010101010 ; type_id
1516
}>
1617

1718

@@ -45,7 +46,8 @@ entry:
4546
ptr nonnull @allocate,
4647
ptr nonnull @deallocate,
4748
ptr nonnull @allocate_frame,
48-
ptr nonnull @deallocate_frame
49+
ptr nonnull @deallocate_frame,
50+
i64 1010101010
4951
)
5052
%handle = call ptr @llvm.coro.begin(token %3, ptr null)
5153
%yielded = getelementptr inbounds %func_self, ptr %2, i32 0, i32 0

llvm/test/Transforms/Coroutines/coro-retcon-once-dynamic.ll

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@
33
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
44
target triple = "arm64-apple-macos99.99"
55

6-
%coro_func_pointer = type <{ i32, i32 }>
6+
%coro_func_pointer = type <{ i32, i32, i64 }>
77

88
; CHECK-LABEL: %func.Frame = type { ptr }
99
; CHECK-LABEL: %big_types.Frame = type { <32 x i8>, [16 x i8], i64, ptr, %Integer8 }
1010

11-
; CHECK-LABEL: @func_cfp = constant <{ i32, i32 }>
11+
; CHECK-LABEL: @func_cfp = constant <{ i32, i32, i64 }>
1212
; CHECK-SAME: <{
1313
; CHECK-SAME: i32 trunc
1414
; CHECK-SAME: i32 16
1515
; CHECK-SAME: }>
16-
@func_cfp = constant <{ i32, i32 }>
16+
@func_cfp = constant <{ i32, i32, i64 }>
1717
<{ i32 trunc ( ; offset to @func from @func_cfp
1818
i64 sub (
1919
i64 ptrtoint (ptr @func to i64),
2020
i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, ptr @func_cfp, i32 0, i32 1) to i64)
2121
)
2222
to i32),
23-
i32 64 ; frame size
23+
i32 64, ; frame size
24+
i64 1010101010 ; type_id
2425
}>
2526

26-
@big_types_cfp = constant <{ i32, i32 }>
27+
@big_types_cfp = constant <{ i32, i32, i64 }>
2728
<{ i32 trunc ( ; offset to @func from @big_types_cfp
2829
i64 sub (
2930
i64 ptrtoint (ptr @big_types to i64),
3031
i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, ptr @big_types_cfp, i32 0, i32 1) to i64)
3132
)
3233
to i32),
33-
i32 64 ; frame size
34+
i32 64, ; frame size
35+
i64 1010101010 ; type_id
3436
}>
3537

3638

@@ -90,7 +92,8 @@ entry:
9092
ptr nonnull @allocate,
9193
ptr nonnull @deallocate,
9294
ptr nonnull @allocate_frame,
93-
ptr nonnull @deallocate_frame
95+
ptr nonnull @deallocate_frame,
96+
i64 1010101010
9497
)
9598
%handle = call ptr @llvm.coro.begin(token %id, ptr null)
9699
%load = load i32, ptr %array
@@ -114,9 +117,9 @@ cleanup:
114117

115118
declare void @continuation_prototype(ptr, ptr)
116119

117-
declare swiftcorocc noalias ptr @allocate(ptr %frame, ptr swiftcoro %cator, i32 %size)
120+
declare swiftcorocc noalias ptr @allocate(ptr %frame, ptr swiftcoro %cator, i32 %size, i64 %typeid)
118121
declare void @deallocate(ptr %frame, ptr swiftcoro %cator, ptr %ptr)
119-
declare swiftcorocc noalias ptr @allocate_frame(ptr %frame, ptr swiftcoro %cator, i32 %size)
122+
declare swiftcorocc noalias ptr @allocate_frame(ptr %frame, ptr swiftcoro %cator, i32 %size, i64 %typeid)
120123
declare void @deallocate_frame(ptr %frame, ptr swiftcoro %cator, ptr %ptr)
121124

122125
declare void @use(ptr %ptr)
@@ -146,7 +149,8 @@ define swiftcorocc { ptr, ptr } @big_types(ptr noalias %frame, ptr swiftcoro %al
146149
ptr nonnull @allocate,
147150
ptr nonnull @deallocate,
148151
ptr nonnull @allocate_frame,
149-
ptr nonnull @deallocate_frame
152+
ptr nonnull @deallocate_frame,
153+
i64 1010101010
150154
)
151155
%handle = tail call ptr @llvm.coro.begin(token %id, ptr null)
152156
call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %element_addr)
@@ -168,14 +172,15 @@ define swiftcorocc { ptr, ptr } @big_types(ptr noalias %frame, ptr swiftcoro %al
168172
}
169173

170174
declare i32 @getSize()
171-
@allocating_something_else_cfp = constant <{ i32, i32 }>
175+
@allocating_something_else_cfp = constant <{ i32, i32, i64 }>
172176
<{ i32 trunc ( ; offset to @func from @allocating_something_else_cfp
173177
i64 sub (
174178
i64 ptrtoint (ptr @allocating_something_else to i64),
175179
i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, ptr @allocating_something_else_cfp, i32 0, i32 1) to i64)
176180
)
177181
to i32),
178-
i32 64 ; frame size
182+
i32 64, ; frame size
183+
i64 1010101010 ; type_id
179184
}>
180185
declare { ptr, ptr } @allocating_something_else(ptr noalias %frame, ptr swiftcoro %allocator)
181186

@@ -210,14 +215,15 @@ declare { ptr, ptr } @allocating_something_else(ptr noalias %frame, ptr swiftcor
210215
; CHECK-SAME: ptr swiftcoro %1,
211216
; CHECK-SAME: ptr
212217
; CHECK-SAME: )
213-
@allocating_something_cfp = constant <{ i32, i32 }>
218+
@allocating_something_cfp = constant <{ i32, i32, i64 }>
214219
<{ i32 trunc ( ; offset to @func from @allocating_something_cfp
215220
i64 sub (
216221
i64 ptrtoint (ptr @allocating_something to i64),
217222
i64 ptrtoint (ptr getelementptr inbounds (<{ i32, i32 }>, ptr @allocating_something_cfp, i32 0, i32 1) to i64)
218223
)
219224
to i32),
220-
i32 64 ; frame size
225+
i32 64, ; frame size
226+
i64 1010101010 ; type_id
221227
}>
222228
define { ptr, ptr } @allocating_something(ptr noalias %frame, ptr swiftcoro %allocator) {
223229
entry:
@@ -231,7 +237,8 @@ entry:
231237
ptr nonnull @allocate,
232238
ptr nonnull @deallocate,
233239
ptr nonnull @allocate_frame,
234-
ptr nonnull @deallocate_frame
240+
ptr nonnull @deallocate_frame,
241+
i64 1010101010
235242
)
236243
%hdl = call ptr @llvm.coro.begin(token %id, ptr null)
237244
%size = call i32 @getSize()
@@ -240,7 +247,7 @@ entry:
240247

241248
%callee_frame_size = load i32, ptr getelementptr inbounds nuw (%coro_func_pointer, ptr @allocating_something_else_cfp, i32 0, i32 1), align 8
242249
%callee_frame_size_64 = zext i32 %callee_frame_size to i64
243-
%callee_frame_token = call token @llvm.coro.alloca.alloc.frame.i64(i64 %callee_frame_size_64, i32 16)
250+
%callee_frame_token = call token @llvm.coro.alloca.alloc.frame.i64(i64 %callee_frame_size_64, i32 16, i64 1010101010)
244251
%callee_frame = call ptr @llvm.coro.alloca.get(token %callee_frame_token)
245252
call void @llvm.lifetime.start.p0(i64 -1, ptr %callee_frame)
246253
%allocating_something_else = call ptr @llvm.coro.prepare.retcon(ptr @allocating_something_else)

0 commit comments

Comments
 (0)