Skip to content

Commit 7bf2994

Browse files
Merge pull request #79824 from nate-chandler/general-coro/20250306/1
[CoroutineAccessors] Adopt swiftcoro param attr.
2 parents 87f5309 + 24bb9bb commit 7bf2994

File tree

5 files changed

+126
-15
lines changed

5 files changed

+126
-15
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ void IRGenModule::addSwiftSelfAttributes(llvm::AttributeList &attrs,
505505
attrs = attrs.addParamAttributes(this->getLLVMContext(), argIndex, b);
506506
}
507507

508+
void IRGenModule::addSwiftCoroAttributes(llvm::AttributeList &attrs,
509+
unsigned argIndex) {
510+
llvm::AttrBuilder b(getLLVMContext());
511+
b.addAttribute(llvm::Attribute::SwiftCoro);
512+
attrs = attrs.addParamAttributes(this->getLLVMContext(), argIndex, b);
513+
}
514+
508515
void IRGenModule::addSwiftErrorAttributes(llvm::AttributeList &attrs,
509516
unsigned argIndex) {
510517
llvm::AttrBuilder b(getLLVMContext());
@@ -892,6 +899,7 @@ void SignatureExpansion::expandCoroutineContinuationParameters() {
892899
if (FnType->isCalleeAllocatedCoroutine()) {
893900
// Whether this is an unwind resumption.
894901
ParamIRTypes.push_back(IGM.CoroAllocatorPtrTy);
902+
IGM.addSwiftCoroAttributes(Attrs, ParamIRTypes.size() - 1);
895903
} else {
896904
// Whether this is an unwind resumption.
897905
ParamIRTypes.push_back(IGM.Int1Ty);
@@ -923,6 +931,7 @@ void SignatureExpansion::addCoroutineContextParameter() {
923931

924932
void SignatureExpansion::addCoroutineAllocatorParameter() {
925933
ParamIRTypes.push_back(IGM.CoroAllocatorPtrTy);
934+
IGM.addSwiftCoroAttributes(Attrs, ParamIRTypes.size() - 1);
926935
}
927936

928937
NativeConventionSchema::NativeConventionSchema(IRGenModule &IGM,
@@ -5170,7 +5179,11 @@ static llvm::Constant *getCoroAllocFn(IRGenModule &IGM) {
51705179
/*setIsNoInline=*/true,
51715180
/*forPrologue=*/false,
51725181
/*isPerformanceConstraint=*/false,
5173-
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC);
5182+
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC,
5183+
/*transformAttributes=*/
5184+
[&IGM](llvm::AttributeList &attrs) {
5185+
IGM.addSwiftCoroAttributes(attrs, 0);
5186+
});
51745187
}
51755188

51765189
static llvm::Constant *getCoroDeallocFn(IRGenModule &IGM) {
@@ -5244,7 +5257,11 @@ static llvm::Constant *getCoroDeallocFn(IRGenModule &IGM) {
52445257
/*setIsNoInline=*/true,
52455258
/*forPrologue=*/false,
52465259
/*isPerformanceConstraint=*/false,
5247-
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC);
5260+
/*optionalLinkageOverride=*/nullptr, IGM.SwiftCoroCC,
5261+
/*transformAttributes=*/
5262+
[&IGM](llvm::AttributeList &attrs) {
5263+
IGM.addSwiftCoroAttributes(attrs, 0);
5264+
});
52485265
}
52495266

52505267
void irgen::emitYieldOnce2CoroutineEntry(IRGenFunction &IGF,

lib/IRGen/GenDecl.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6283,10 +6283,11 @@ IRGenModule::getAddrOfContinuationPrototype(CanSILFunctionType fnType) {
62836283
}
62846284

62856285
/// Should we be defining the given helper function?
6286-
static llvm::Function *
6287-
shouldDefineHelper(IRGenModule &IGM, llvm::Constant *fn, bool setIsNoInline,
6288-
IRLinkage *linkage,
6289-
std::optional<llvm::CallingConv::ID> specialCallingConv) {
6286+
static llvm::Function *shouldDefineHelper(
6287+
IRGenModule &IGM, llvm::Constant *fn, bool setIsNoInline,
6288+
IRLinkage *linkage, std::optional<llvm::CallingConv::ID> specialCallingConv,
6289+
std::optional<llvm::function_ref<void(llvm::AttributeList &)>>
6290+
transformAttrs) {
62906291
auto *def = dyn_cast<llvm::Function>(fn);
62916292
if (!def) return nullptr;
62926293
if (!def->empty()) return nullptr;
@@ -6301,6 +6302,12 @@ shouldDefineHelper(IRGenModule &IGM, llvm::Constant *fn, bool setIsNoInline,
63016302
def->setCallingConv(specialCallingConv.value_or(IGM.DefaultCC));
63026303
if (setIsNoInline)
63036304
def->addFnAttr(llvm::Attribute::NoInline);
6305+
6306+
if (transformAttrs) {
6307+
auto attrs = def->getAttributes();
6308+
(*transformAttrs)(attrs);
6309+
def->setAttributes(attrs);
6310+
}
63046311
return def;
63056312
}
63066313

@@ -6317,7 +6324,9 @@ llvm::Constant *IRGenModule::getOrCreateHelperFunction(
63176324
llvm::function_ref<void(IRGenFunction &IGF)> generate, bool setIsNoInline,
63186325
bool forPrologue, bool isPerformanceConstraint,
63196326
IRLinkage *optionalLinkageOverride,
6320-
std::optional<llvm::CallingConv::ID> specialCallingConv) {
6327+
std::optional<llvm::CallingConv::ID> specialCallingConv,
6328+
std::optional<llvm::function_ref<void(llvm::AttributeList &)>>
6329+
transformAttrs) {
63216330
llvm::FunctionType *fnTy =
63226331
llvm::FunctionType::get(resultTy, paramTys, false);
63236332

@@ -6327,7 +6336,7 @@ llvm::Constant *IRGenModule::getOrCreateHelperFunction(
63276336

63286337
if (llvm::Function *def =
63296338
shouldDefineHelper(*this, fn, setIsNoInline, optionalLinkageOverride,
6330-
specialCallingConv)) {
6339+
specialCallingConv, transformAttrs)) {
63316340
IRGenFunction IGF(*this, def, isPerformanceConstraint);
63326341
if (DebugInfo && !forPrologue)
63336342
DebugInfo->emitArtificialFunction(IGF, def);

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,9 @@ class IRGenModule {
12411241
bool setIsNoInline = false, bool forPrologue = false,
12421242
bool isPerformanceConstraint = false,
12431243
IRLinkage *optionalLinkage = nullptr,
1244-
std::optional<llvm::CallingConv::ID> specialCallingConv = std::nullopt);
1244+
std::optional<llvm::CallingConv::ID> specialCallingConv = std::nullopt,
1245+
std::optional<llvm::function_ref<void(llvm::AttributeList &)>>
1246+
transformAttrs = std::nullopt);
12451247

12461248
llvm::Constant *getOrCreateRetainFunction(const TypeInfo &objectTI, SILType t,
12471249
llvm::Type *llvmType, Atomicity atomicity);
@@ -1947,6 +1949,8 @@ private: \
19471949
/// Add the swiftself attribute.
19481950
void addSwiftSelfAttributes(llvm::AttributeList &attrs, unsigned argIndex);
19491951

1952+
void addSwiftCoroAttributes(llvm::AttributeList &attrs, unsigned argIndex);
1953+
19501954
void addSwiftAsyncContextAttributes(llvm::AttributeList &attrs,
19511955
unsigned argIndex);
19521956

test/IRGen/coroutine_accessors.swift

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
// REQUIRES: swift_feature_CoroutineAccessors
77

8+
// CHECK-LABEL: %T19coroutine_accessors1SV = type <{ %AnyObject, %TSi }>
9+
10+
// CHECK-LABEL: @"$s19coroutine_accessors1SV3irmSivyTwc" = {{.*}}global %swift.coro_func_pointer
11+
// : sub (
12+
// CHECK-SAME: $s19coroutine_accessors1SV3irmSivy
13+
// : $s19coroutine_accessors1SV3irmSivyTwc
14+
// : )
15+
// CHECK-SAME: i32 0
16+
// CHECK-SAME: }>
817
// CHECK-LABEL: @"$s19coroutine_accessors1SV3irmSivxTwc" = {{.*}}global{{.*}} %swift.coro_func_pointer <{
918
// : sub (
1019
// CHECK-SAME: $s19coroutine_accessors1SV3irmSivx
@@ -25,7 +34,7 @@
2534
// CHECK-SAME: }
2635

2736
// CHECK-LABEL: @_swift_coro_alloc(
28-
// CHECK-SAME: ptr [[ALLOCATOR:%[^,]+]]
37+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
2938
// CHECK-SAME: [[INT]] [[SIZE:%[^)]+]]
3039
// CHECK-SAME: )
3140
// CHECK-SAME: {
@@ -40,7 +49,7 @@
4049
// CHECK: }
4150

4251
// CHECK-LABEL: @_swift_coro_dealloc(
43-
// CHECK-SAME: ptr [[ALLOCATOR:%[^,]+]]
52+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
4453
// CHECK-SAME: ptr [[ADDRESS:%[^)]+]]
4554
// CHECK-SAME: )
4655
// CHECK-SAME: {
@@ -73,16 +82,81 @@ public var o: any AnyObject
7382
public var _i: Int = 0
7483

7584
public var irm: Int {
85+
// CHECK-LABEL: declare{{.*}} swiftcc void @"$s19coroutine_accessors1SVSiIetMIlYl_TC"(ptr noalias, ptr swiftcoro)
86+
7687
// CHECK-LABEL: define{{.*}} { ptr, {{i64|i32}} } @"$s19coroutine_accessors1SV3irmSivy"(
88+
// CHECK-SAME: ptr noalias [[FRAME:%[^,]+]],
89+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]],
90+
// CHECK-SAME: ptr [[S_FIELD_O:%[^,]+]],
91+
// CHECK-SAME: [[INT]] [[S_FIELD__I:%[^)]+]]
7792
// CHECK-SAME: )
7893
// CHECK-SAME: {
94+
// CHECK: [[ID:%[^,]+]] = call token @llvm.coro.id.retcon.once.dynamic(
95+
// CHECK-SAME: i32 -1,
96+
// CHECK-SAME: i32 16,
97+
// CHECK-SAME: ptr @"$s19coroutine_accessors1SV3irmSivyTwc",
98+
// CHECK-SAME: ptr [[ALLOCATOR]],
99+
// CHECK-SAME: ptr [[FRAME]],
100+
// CHECK-SAME: ptr @"$s19coroutine_accessors1SVSiIetMIgYy_TC",
101+
// CHECK-SAME: ptr @_swift_coro_alloc,
102+
// CHECK-SAME: ptr @_swift_coro_dealloc
103+
// CHECK-SAME: )
104+
// CHECK: [[HANDLE:%[^,]+]] = call ptr @llvm.coro.begin(
105+
// CHECK-SAME: token [[ID]],
106+
// CHECK-SAME: ptr null
107+
// CHECK-SAME: )
108+
// CHECK: call ptr (...) @llvm.coro.suspend.retcon.p0([[INT]] [[S_FIELD__I]])
109+
// CHECK: br i1 false, label %[[UNWIND:[^,]+]], label %[[NORMAL:[^,]+]]
110+
// CHECK: [[NORMAL]]:
111+
// CHECK: br label %coro.end
112+
// CHECK: [[UNWIND]]:
113+
// CHECK: br label %coro.end
114+
// CHECK: coro.end:
115+
// CHECK: call i1 @llvm.coro.end(
116+
// CHECK-SAME: ptr [[HANDLE]],
117+
// CHECK-SAME: i1 false,
118+
// CHECK-SAME: token none
119+
// CHECK-SAME: )
120+
// CHECK: unreachable
79121
// CHECK: }
80122
read {
81123
yield _i
82124
}
83125
// CHECK-LABEL: define{{.*}} { ptr, ptr } @"$s19coroutine_accessors1SV3irmSivx"(
126+
// CHECK-SAME: ptr noalias [[FRAME:%[^,]+]],
127+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]],
128+
// CHECK-SAME: ptr nocapture swiftself dereferenceable({{8|16}}) [[SELF:%[^)]+]]
84129
// CHECK-SAME: )
85130
// CHECK-SAME: {
131+
// CHECK: [[ID:%[^,]+]] = call token @llvm.coro.id.retcon.once.dynamic(
132+
// CHECK-SAME: i32 -1,
133+
// CHECK-SAME: i32 16,
134+
// CHECK-SAME: ptr @"$s19coroutine_accessors1SV3irmSivxTwc",
135+
// CHECK-SAME: ptr [[ALLOCATOR]],
136+
// CHECK-SAME: ptr [[FRAME]],
137+
// CHECK-SAME: ptr @"$s19coroutine_accessors1SVSiIetMIlYl_TC",
138+
// CHECK-SAME: ptr @_swift_coro_alloc,
139+
// CHECK-SAME: ptr @_swift_coro_dealloc
140+
// CHECK-SAME: )
141+
// CHECK: [[HANDLE:%[^,]+]] = call ptr @llvm.coro.begin(
142+
// CHECK-SAME: token [[ID]],
143+
// CHECK-SAME: ptr null
144+
// CHECK-SAME: )
145+
// CHECK: [[S_FIELD__I:%[^,]+]] = getelementptr inbounds %T19coroutine_accessors1SV,
146+
// CHECK-SAME: ptr [[SELF]],
147+
// CHECK-SAME: i32 0,
148+
// CHECK-SAME: i32 1
149+
// CHECK: call ptr (...) @llvm.coro.suspend.retcon.p0(
150+
// CHECK-SAME: ptr [[S_FIELD__I]]
151+
// CHECK-SAME: )
152+
// CHECK: br i1 false, label %[[UNWIND:[^,]+]], label %[[NORMAL:[^,]+]]
153+
// CHECK: [[NORMAL]]:
154+
// CHECK: br label %coro.end
155+
// CHECK: [[UNWIND]]:
156+
// CHECK: br label %coro.end
157+
// CHECK: coro.end:
158+
// CHECK: [[REGISTER_8:%[^,]+]] = call i1 @llvm.coro.end(ptr [[HANDLE]], i1 false, token none)
159+
// CHECK: unreachable
86160
// CHECK: }
87161
modify {
88162
yield &_i
@@ -217,7 +291,7 @@ public var force_yield_once_2_convention : () {
217291
}
218292
// CHECK-LABEL: define{{.*}} { ptr, ptr } @increment_irm_yield_once_2(
219293
// ptr noalias %0
220-
// CHECK-SAME: ptr [[ALLOCATOR:%[^,]+]]
294+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
221295
// ptr nocapture swiftself dereferenceable(16) %2
222296
// CHECK-SAME: )
223297
// CHECK-SAME: {

test/IRGen/coroutine_accessors_popless.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
// REQUIRES: CPU=arm64 || CPU=arm64e || CPU=x86_64
1010
// REQUIRES: swift_feature_CoroutineAccessors
1111

12+
// CHECK-LABEL: @"$s27coroutine_accessors_popless1iSivyTwc" = {{.*}}global{{.*}} %swift.coro_func_pointer <{
13+
// : sub (
14+
// CHECK-SAME: $s27coroutine_accessors_popless1iSivy
15+
// : $s27coroutine_accessors_popless1iSivyTwc
16+
// : ),
17+
// CHECK-SAME: i32 0
18+
// CHECK-SAME: }>
1219
// CHECK-LABEL: @"$s27coroutine_accessors_popless1iSivxTwc" = {{.*}}global{{.*}} %swift.coro_func_pointer <{
1320
// : sub (
1421
// CHECK-SAME: $s27coroutine_accessors_popless1iSivx
@@ -29,7 +36,7 @@
2936
// CHECK-SAME: }
3037

3138
// CHECK-LABEL: @_swift_coro_alloc(
32-
// CHECK-SAME: ptr [[ALLOCATOR:%[^,]+]]
39+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
3340
// CHECK-SAME: i64 [[SIZE:%[^)]+]]
3441
// CHECK-SAME: )
3542
// CHECK-SAME: {
@@ -53,7 +60,7 @@
5360
// CHECK: }
5461

5562
// CHECK-LABEL: @_swift_coro_dealloc(
56-
// CHECK-SAME: ptr [[ALLOCATOR:%[^,]+]]
63+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^,]+]]
5764
// CHECK-SAME: ptr [[ADDRESS:%[^)]+]]
5865
// CHECK-SAME: )
5966
// CHECK-SAME: {
@@ -231,7 +238,7 @@ public var force_yield_once_2_convention : () {
231238
}
232239
// CHECK-LABEL: define{{.*}} { ptr, ptr } @increment_i_yield_once_2(
233240
// ptr noalias %0
234-
// CHECK-SAME: ptr [[ALLOCATOR:%[^)]+]]
241+
// CHECK-SAME: ptr swiftcoro [[ALLOCATOR:%[^)]+]]
235242
// CHECK-SAME: )
236243
// CHECK-SAME: {
237244
// : [[SIZE_32:%[^,]+]] = load i32

0 commit comments

Comments
 (0)