Skip to content

Commit 96b5b2c

Browse files
authored
Use a dedicated parameter attribute to identify the gstack arg. (#59059)
Otherwise, on systems without SwitfCC support (i.e. RISC-V) `getPGCstack` may return null, disabling the final GC pass.
1 parent 4718f43 commit 96b5b2c

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

src/codegen.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6788,9 +6788,12 @@ static Function *emit_modifyhelper(jl_codectx_t &ctx2, const jl_cgval_t &op, con
67886788
rhs.promotion_point = nullptr;
67896789
rhs.promotion_ssa = -1;
67906790
if (gcstack_arg) {
6791-
w->setCallingConv(CallingConv::Swift);
67926791
AttrBuilder param(ctx.builder.getContext());
6793-
param.addAttribute(Attribute::SwiftSelf);
6792+
if (ctx.emission_context.use_swiftcc) {
6793+
w->setCallingConv(CallingConv::Swift);
6794+
param.addAttribute(Attribute::SwiftSelf);
6795+
}
6796+
param.addAttribute("gcstack");
67946797
param.addAttribute(Attribute::NonNull);
67956798
Argument *gcstackarg = &*AI++;
67966799
gcstackarg->addAttrs(param);
@@ -7966,6 +7969,7 @@ static jl_returninfo_t get_specsig_function(jl_codegen_params_t &params, Module
79667969
AttrBuilder param(M->getContext());
79677970
if (params.use_swiftcc)
79687971
param.addAttribute(Attribute::SwiftSelf);
7972+
param.addAttribute("gcstack");
79697973
param.addAttribute(Attribute::NonNull);
79707974
attrs.push_back(AttributeSet::get(M->getContext(), param));
79717975
fsig.push_back(PointerType::get(M->getContext(), 0));

src/llvm-pass-helpers.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ llvm::Value *JuliaPassContext::getPGCstack(llvm::Function &F) const
8888
}
8989
}
9090
}
91-
if (F.getCallingConv() == CallingConv::Swift) {
92-
for (auto &arg : F.args()) {
93-
if (arg.hasSwiftSelfAttr())
94-
return &arg;
91+
for (auto &arg : F.args()) {
92+
// Check for the "gcstack" attribute
93+
AttributeSet attrs = F.getAttributes().getParamAttrs(arg.getArgNo());
94+
if (attrs.hasAttribute("gcstack")) {
95+
return &arg;
9596
}
9697
}
9798
return nullptr;

src/llvm-ptls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ bool LowerPTLS::run(bool *CFGModified)
344344
auto f = call->getCaller();
345345
Value *pgcstack = NULL;
346346
for (Function::arg_iterator arg = f->arg_begin(); arg != f->arg_end(); ++arg) {
347-
if (arg->hasSwiftSelfAttr()) {
347+
AttributeSet attrs = f->getAttributes().getParamAttrs(arg->getArgNo());
348+
if (attrs.hasAttribute("gcstack")) {
348349
pgcstack = &*arg;
349350
break;
350351
}

test/llvmpasses/alloc-opt-pass.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ union_move9: ; No predecessors!
197197
@1 = private unnamed_addr constant i64 0, align 8
198198

199199
; CHECK-LABEL: @cmpxchg
200-
; CHECK: alloca
200+
; CHECK: alloca
201201
; CHECK: alloca
202202
; CHECK: %20 = cmpxchg ptr %2,
203-
define swiftcc i64 @"cmpxchg"(ptr nonnull swiftself %0) #0 {
203+
define swiftcc i64 @"cmpxchg"(ptr nonnull swiftself "gcstack" %0) #0 {
204204
%2 = alloca i64, align 16
205205
%3 = call ptr @julia.get_pgcstack()
206206
%4 = getelementptr inbounds i8, ptr %3, i32 -152
@@ -229,7 +229,7 @@ define swiftcc i64 @"cmpxchg"(ptr nonnull swiftself %0) #0 {
229229

230230
19: ; preds = %19, %1
231231
%20 = phi i64 [ %17, %1 ], [ %23, %19 ]
232-
%21 = call swiftcc i64 @"jlsys_+_47"(ptr nonnull swiftself %3, i64 signext %20, i64 signext 1)
232+
%21 = call swiftcc i64 @"jlsys_+_47"(ptr nonnull swiftself "gcstack" %3, i64 signext %20, i64 signext 1)
233233
%22 = cmpxchg ptr addrspace(11) %16, i64 %20, i64 %21 seq_cst monotonic, align 8, !tbaa !25, !alias.scope !23, !noalias !24
234234
%23 = extractvalue { i64, i1 } %22, 0
235235
%24 = extractvalue { i64, i1 } %22, 1
@@ -241,7 +241,7 @@ define swiftcc i64 @"cmpxchg"(ptr nonnull swiftself %0) #0 {
241241
; CHECK: alloca
242242
; CHECK: alloca
243243
; CHECK: atomicrmw xchg ptr %2,
244-
define swiftcc i64 @"atomicrmw"(ptr nonnull swiftself %0) #0 {
244+
define swiftcc i64 @"atomicrmw"(ptr nonnull swiftself "gcstack" %0) #0 {
245245
%2 = alloca i64, align 16
246246
%3 = call ptr @julia.get_pgcstack()
247247
%4 = getelementptr inbounds i8, ptr %3, i32 -152
@@ -263,7 +263,7 @@ define swiftcc i64 @"atomicrmw"(ptr nonnull swiftself %0) #0 {
263263
call void @llvm.memcpy.p11.p0.i64(ptr addrspace(11) align 8 %15, ptr align 8 @1, i64 8, i1 false), !tbaa !20, !alias.scope !23, !noalias !24
264264
%16 = addrspacecast ptr addrspace(10) %14 to ptr addrspace(11)
265265
%17 = load atomic i64, ptr addrspace(11) %16 monotonic, align 8, !tbaa !25, !alias.scope !23, !noalias !24
266-
%18 = call swiftcc i64 @"jlsys_+_47"(ptr nonnull swiftself %3, i64 signext %17, i64 signext 1)
266+
%18 = call swiftcc i64 @"jlsys_+_47"(ptr nonnull swiftself "gcstack" %3, i64 signext %17, i64 signext 1)
267267
%19 = atomicrmw xchg ptr addrspace(11) %16, i64 %18 seq_cst, align 8, !tbaa !25, !alias.scope !23, !noalias !24 ; preds = %19
268268
ret i64 %19
269269
}

test/llvmpasses/late-lower-gc-sret.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare ptr @julia.get_pgcstack()
66

77
declare swiftcc void @sret_call(ptr noalias nocapture noundef nonnull sret([3 x ptr addrspace(10)]), ptr nonnull swiftself, ptr addrspace(10) nonnull)
88

9-
define hidden swiftcc nonnull ptr addrspace(10) @sret_select(ptr nonnull swiftself %0, ptr addrspace(10) noundef nonnull align 8 dereferenceable(88) %1, i1 %unpredictable) {
9+
define hidden swiftcc nonnull ptr addrspace(10) @sret_select(ptr nonnull swiftself "gcstack" %0, ptr addrspace(10) noundef nonnull align 8 dereferenceable(88) %1, i1 %unpredictable) {
1010
; CHECK-LABEL: @sret_select
1111
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 6)
1212
; CHECK: call ptr @julia.get_gc_frame_slot(ptr %gcframe, i32 3)
@@ -17,12 +17,12 @@ define hidden swiftcc nonnull ptr addrspace(10) @sret_select(ptr nonnull swiftse
1717
%3 = alloca [3 x i64], align 8
1818
%4 = alloca [3 x i64], align 8
1919
%5 = select i1 %unpredictable, ptr %3, ptr %4
20-
call swiftcc void @sret_call(ptr noalias nocapture noundef nonnull sret([3 x ptr addrspace(10)]) %5, ptr nonnull swiftself %0, ptr addrspace(10) nonnull %1)
20+
call swiftcc void @sret_call(ptr noalias nocapture noundef nonnull sret([3 x ptr addrspace(10)]) %5, ptr nonnull swiftself "gcstack" %0, ptr addrspace(10) nonnull %1)
2121
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
2222
ret ptr addrspace(10) %1
2323
}
2424

25-
define hidden swiftcc nonnull ptr addrspace(10) @sret_phi(ptr nonnull swiftself %0, ptr addrspace(10) noundef nonnull align 8 dereferenceable(88) %1, i1 %unpredictable) {
25+
define hidden swiftcc nonnull ptr addrspace(10) @sret_phi(ptr nonnull swiftself "gcstack" %0, ptr addrspace(10) noundef nonnull align 8 dereferenceable(88) %1, i1 %unpredictable) {
2626
top:
2727
; CHECK-LABEL: @sret_phi
2828
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 6)
@@ -43,14 +43,14 @@ false: ; preds = %top
4343

4444
ret: ; preds = %false, %true
4545
%4 = phi ptr [ %2, %true ], [ %3, %false ]
46-
call swiftcc void @sret_call(ptr noalias nocapture noundef nonnull sret([3 x ptr addrspace(10)]) %4, ptr nonnull swiftself %0, ptr addrspace(10) nonnull %1)
46+
call swiftcc void @sret_call(ptr noalias nocapture noundef nonnull sret([3 x ptr addrspace(10)]) %4, ptr nonnull swiftself "gcstack" %0, ptr addrspace(10) nonnull %1)
4747
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
4848
ret ptr addrspace(10) %1
4949
}
5050

5151
declare swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }), ptr noalias nocapture noundef, ptr nonnull swiftself)
5252

53-
define hidden swiftcc void @sret_gc_root_phi(ptr nonnull swiftself %0, i1 %unpredictable) {
53+
define hidden swiftcc void @sret_gc_root_phi(ptr nonnull swiftself "gcstack" %0, i1 %unpredictable) {
5454
top:
5555
; CHECK-LABEL: @sret_gc_root_phi
5656
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 2)
@@ -75,13 +75,13 @@ false: ; preds = %top
7575

7676
ret: ; preds = %false, %true
7777
%4 = phi ptr [ %2, %true ], [ %3, %false ]
78-
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %4, ptr nonnull swiftself %0)
78+
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %4, ptr nonnull swiftself "gcstack" %0)
7979
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
8080
ret void
8181
}
8282

8383

84-
define hidden swiftcc void @sret_gc_root_phi_select(ptr nonnull swiftself %0, i1 %unpredictable, i1 %unpredictable2) {
84+
define hidden swiftcc void @sret_gc_root_phi_select(ptr nonnull swiftself "gcstack" %0, i1 %unpredictable, i1 %unpredictable2) {
8585
top:
8686
; CHECK-LABEL: @sret_gc_root_phi_select
8787
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 3)
@@ -110,12 +110,12 @@ false: ; preds = %top
110110
ret: ; preds = %false, %true
111111
%5 = phi ptr [ %2, %true ], [ %3, %false ]
112112
%6 = select i1 %unpredictable2, ptr %4, ptr %5
113-
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %6, ptr nonnull swiftself %0)
113+
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %6, ptr nonnull swiftself "gcstack" %0)
114114
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
115115
ret void
116116
}
117117

118-
define hidden swiftcc void @sret_gc_root_select_phi(ptr nonnull swiftself %0, i1 %unpredictable, i1 %unpredictable2) {
118+
define hidden swiftcc void @sret_gc_root_select_phi(ptr nonnull swiftself "gcstack" %0, i1 %unpredictable, i1 %unpredictable2) {
119119
top:
120120
; CHECK-LABEL: @sret_gc_root_select_phi
121121
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 3)
@@ -145,7 +145,7 @@ false: ; preds = %top
145145
ret: ; preds = %false, %true
146146
%6 = phi ptr [ %2, %true ], [ %5, %false ]
147147

148-
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %6, ptr nonnull swiftself %0)
148+
call swiftcc void @sret_call_gc(ptr noalias nocapture noundef sret({ ptr addrspace(10), i64, i64 }) %1, ptr noalias nocapture noundef %6, ptr nonnull swiftself "gcstack" %0)
149149
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
150150
ret void
151151
}

test/llvmpasses/late-lower-gc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ define void @decayar([2 x {} addrspace(10)* addrspace(11)*] %ar) {
199199
; CHECK: %r = call i32 @callee_root(ptr addrspace(10) %l0, ptr addrspace(10) %l1)
200200
; CHECK: call void @julia.pop_gc_frame(ptr %gcframe)
201201

202-
define swiftcc ptr addrspace(10) @insert_element(ptr swiftself %0) {
202+
define swiftcc ptr addrspace(10) @insert_element(ptr swiftself "gcstack" %0) {
203203
; CHECK-LABEL: @insert_element
204204
%2 = alloca [10 x i64], i32 1, align 8
205205
; CHECK: %gcframe = call ptr @julia.new_gc_frame(i32 10)

0 commit comments

Comments
 (0)