Skip to content

Commit ffd4bcc

Browse files
authored
[AMDGPU] Fix 160181. Be less optimistic when allocating module scope … (llvm#4474)
2 parents 9653571 + 7bfba6d commit ffd4bcc

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ class AMDGPULowerModuleLDS {
647647
ModuleScopeVariables.insert(GV);
648648
} else if (K.second.size() == 1) {
649649
KernelAccessVariables.insert(GV);
650-
} else if (set_is_subset(K.second, HybridModuleRootKernels)) {
650+
} else if (K.second == HybridModuleRootKernels &&
651+
set_is_subset(K.second, HybridModuleRootKernels)) {
651652
ModuleScopeVariables.insert(GV);
652653
} else {
653654
TableLookupVariables.insert(GV);
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
2+
; RUN: opt -S -mtriple=amdgcn-- -passes=amdgpu-lower-module-lds < %s | FileCheck %s
3+
4+
; Regression test for issue 160181
5+
; One variable is chosen to be assigned at zero. Here, that's @both
6+
; Then other variables should be allocated at fixed offsets from that provided
7+
; they are allocated by all the other kernels that presently allocate the
8+
; variable at address zero.
9+
; The failure mode was in that second check - variables could be added to
10+
; the module scope zero address struct even when some of the kernels allocating
11+
; that struct do not need the additional variable.
12+
13+
; With current llvm, all three of these integers are put in the module scope struct, when
14+
; neither kern_one or kern_two access all three.
15+
16+
@both = addrspace(3) global i32 poison
17+
@both_second = addrspace(3) global i16 poison ; a second field in the module struct
18+
@one = addrspace(3) global i32 poison
19+
@two = addrspace(3) global i32 poison
20+
21+
22+
;.
23+
; CHECK: @llvm.amdgcn.module.lds = internal addrspace(3) global %llvm.amdgcn.module.lds.t poison, align 4, !absolute_symbol [[META0:![0-9]+]]
24+
; CHECK: @llvm.compiler.used = appending addrspace(1) global [1 x ptr] [ptr addrspacecast (ptr addrspace(3) @llvm.amdgcn.module.lds to ptr)], section "llvm.metadata"
25+
; CHECK: @llvm.amdgcn.kernel.kern_one.lds = internal addrspace(3) global %llvm.amdgcn.kernel.kern_one.lds.t poison, align 4, !absolute_symbol [[META1:![0-9]+]]
26+
; CHECK: @llvm.amdgcn.kernel.kern_two.lds = internal addrspace(3) global %llvm.amdgcn.kernel.kern_two.lds.t poison, align 4, !absolute_symbol [[META1]]
27+
; CHECK: @llvm.amdgcn.kernel.kern_block_direct_allocation.lds = internal addrspace(3) global %llvm.amdgcn.kernel.kern_block_direct_allocation.lds.t poison, align 4, !absolute_symbol [[META1]]
28+
29+
;.
30+
define void @func_one() {
31+
; CHECK-LABEL: define {{[^@]+}}@func_one() {
32+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.amdgcn.lds.kernel.id()
33+
; CHECK-NEXT: [[VAL0:%.*]] = load i32, ptr addrspace(3) @llvm.amdgcn.module.lds, align 4, !noalias [[META2:![0-9]+]]
34+
; CHECK-NEXT: [[ONE:%.*]] = getelementptr inbounds [3 x [2 x i32]], ptr addrspace(4) @llvm.amdgcn.lds.offset.table, i32 0, i32 [[TMP1]], i32 0
35+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(4) [[ONE]], align 4
36+
; CHECK-NEXT: [[ONE1:%.*]] = inttoptr i32 [[TMP2]] to ptr addrspace(3)
37+
; CHECK-NEXT: store i32 [[VAL0]], ptr addrspace(3) [[ONE1]], align 4
38+
; CHECK-NEXT: store i16 10, ptr addrspace(3) getelementptr inbounds ([[LLVM_AMDGCN_MODULE_LDS_T:%.*]], ptr addrspace(3) @llvm.amdgcn.module.lds, i32 0, i32 1), align 4, !noalias [[META11:![0-9]+]]
39+
; CHECK-NEXT: ret void
40+
;
41+
%val0 = load i32, ptr addrspace(3) @both
42+
store i32 %val0, ptr addrspace(3) @one
43+
store i16 10, ptr addrspace(3) @both_second
44+
ret void
45+
}
46+
47+
define amdgpu_kernel void @kern_one() {
48+
; CHECK-LABEL: define {{[^@]+}}@kern_one
49+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] !llvm.amdgcn.lds.kernel.id [[META16:![0-9]+]] {
50+
; CHECK-NEXT: entry:
51+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.kernel.kern_one.lds) ]
52+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.module.lds) ], !noalias [[META17:![0-9]+]]
53+
; CHECK-NEXT: call void @func_one()
54+
; CHECK-NEXT: ret void
55+
;
56+
entry:
57+
call void @func_one()
58+
ret void
59+
}
60+
61+
define void @func_two() {
62+
; CHECK-LABEL: define {{[^@]+}}@func_two() {
63+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.amdgcn.lds.kernel.id()
64+
; CHECK-NEXT: [[VAL0:%.*]] = load i32, ptr addrspace(3) @llvm.amdgcn.module.lds, align 4, !noalias [[META2]]
65+
; CHECK-NEXT: [[TWO:%.*]] = getelementptr inbounds [3 x [2 x i32]], ptr addrspace(4) @llvm.amdgcn.lds.offset.table, i32 0, i32 [[TMP1]], i32 1
66+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(4) [[TWO]], align 4
67+
; CHECK-NEXT: [[TWO1:%.*]] = inttoptr i32 [[TMP2]] to ptr addrspace(3)
68+
; CHECK-NEXT: store i32 [[VAL0]], ptr addrspace(3) [[TWO1]], align 4
69+
; CHECK-NEXT: store i16 20, ptr addrspace(3) getelementptr inbounds ([[LLVM_AMDGCN_MODULE_LDS_T:%.*]], ptr addrspace(3) @llvm.amdgcn.module.lds, i32 0, i32 1), align 4, !noalias [[META11]]
70+
; CHECK-NEXT: ret void
71+
;
72+
%val0 = load i32, ptr addrspace(3) @both
73+
store i32 %val0, ptr addrspace(3) @two
74+
store i16 20, ptr addrspace(3) @both_second
75+
ret void
76+
}
77+
78+
define amdgpu_kernel void @kern_two() {
79+
; CHECK-LABEL: define {{[^@]+}}@kern_two
80+
; CHECK-SAME: () #[[ATTR0]] !llvm.amdgcn.lds.kernel.id [[META18:![0-9]+]] {
81+
; CHECK-NEXT: entry:
82+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.kernel.kern_two.lds) ]
83+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.module.lds) ], !alias.scope [[META19:![0-9]+]], !noalias [[META20:![0-9]+]]
84+
; CHECK-NEXT: call void @func_two()
85+
; CHECK-NEXT: ret void
86+
;
87+
entry:
88+
call void @func_two()
89+
ret void
90+
}
91+
92+
; Unrelated to the bug at hand, but if a variable is only
93+
; reachable from a single kernel, it gets allocated to a fixed
94+
; address independent of the module scope struct. This kernel
95+
; means the key variables miss that optimisation while @both
96+
; remains the best candidate for address zero allocation.
97+
define void @func_block_direct_allocation() {
98+
; CHECK-LABEL: define {{[^@]+}}@func_block_direct_allocation() {
99+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.amdgcn.lds.kernel.id()
100+
; CHECK-NEXT: [[ONE:%.*]] = getelementptr inbounds [3 x [2 x i32]], ptr addrspace(4) @llvm.amdgcn.lds.offset.table, i32 0, i32 [[TMP1]], i32 0
101+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(4) [[ONE]], align 4
102+
; CHECK-NEXT: [[ONE1:%.*]] = inttoptr i32 [[TMP2]] to ptr addrspace(3)
103+
; CHECK-NEXT: [[VAL1:%.*]] = load i32, ptr addrspace(3) [[ONE1]], align 4
104+
; CHECK-NEXT: [[TWO:%.*]] = getelementptr inbounds [3 x [2 x i32]], ptr addrspace(4) @llvm.amdgcn.lds.offset.table, i32 0, i32 [[TMP1]], i32 1
105+
; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr addrspace(4) [[TWO]], align 4
106+
; CHECK-NEXT: [[TWO2:%.*]] = inttoptr i32 [[TMP3]] to ptr addrspace(3)
107+
; CHECK-NEXT: [[VAL2:%.*]] = load i32, ptr addrspace(3) [[TWO2]], align 4
108+
; CHECK-NEXT: [[SUM:%.*]] = add i32 [[VAL1]], [[VAL2]]
109+
; CHECK-NEXT: store i32 [[SUM]], ptr addrspace(3) @llvm.amdgcn.module.lds, align 4, !noalias [[META2]]
110+
; CHECK-NEXT: store i16 30, ptr addrspace(3) getelementptr inbounds ([[LLVM_AMDGCN_MODULE_LDS_T:%.*]], ptr addrspace(3) @llvm.amdgcn.module.lds, i32 0, i32 1), align 4, !noalias [[META11]]
111+
; CHECK-NEXT: ret void
112+
;
113+
%val1 = load i32, ptr addrspace(3) @one
114+
%val2 = load i32, ptr addrspace(3) @two
115+
%sum = add i32 %val1, %val2
116+
store i32 %sum, ptr addrspace(3) @both
117+
store i16 30, ptr addrspace(3) @both_second
118+
ret void
119+
}
120+
121+
define amdgpu_kernel void @kern_block_direct_allocation() {
122+
; CHECK-LABEL: define {{[^@]+}}@kern_block_direct_allocation
123+
; CHECK-SAME: () #[[ATTR1:[0-9]+]] !llvm.amdgcn.lds.kernel.id [[META21:![0-9]+]] {
124+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.kernel.kern_block_direct_allocation.lds) ], !alias.scope [[META22:![0-9]+]], !noalias [[META25:![0-9]+]]
125+
; CHECK-NEXT: call void @llvm.donothing() [ "ExplicitUse"(ptr addrspace(3) @llvm.amdgcn.module.lds) ]
126+
; CHECK-NEXT: call void @func_block_direct_allocation()
127+
; CHECK-NEXT: call void @func_one()
128+
; CHECK-NEXT: call void @func_two()
129+
; CHECK-NEXT: ret void
130+
;
131+
call void @func_block_direct_allocation()
132+
call void @func_one()
133+
call void @func_two()
134+
ret void
135+
}
136+
;.
137+
; CHECK: attributes #[[ATTR0]] = { "amdgpu-lds-size"="12" }
138+
; CHECK: attributes #[[ATTR1]] = { "amdgpu-lds-size"="16" }
139+
; CHECK: attributes #[[ATTR2:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(none) }
140+
; CHECK: attributes #[[ATTR3:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

0 commit comments

Comments
 (0)