1 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=fiji -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
3 ; Check that we properly realign the stack. While 4-byte access is all
4 ; that is ever needed, some transformations rely on the known bits from the alignment of the pointer (e.g.
8 ; 4 byte emergency stack slot
9 ; = 144 bytes with padding between them
11 ; GCN-LABEL: {{^}}needs_align16_default_stack_align:
12 ; GCN-DAG: v_lshlrev_b32_e32 [[SCALED_IDX:v[0-9]+]], 4, v0
13 ; GCN-DAG: v_lshrrev_b32_e64 [[FRAMEDIFF:v[0-9]+]], 6, s32
14 ; GCN: v_add_u32_e32 [[FI:v[0-9]+]], vcc, [[FRAMEDIFF]], [[SCALED_IDX]]
18 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
19 ; GCN: v_or_b32_e32 v{{[0-9]+}}, 12
20 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
21 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
22 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
26 ; GCN: ; ScratchSize: 144
27 define void @needs_align16_default_stack_align(i32 %idx) #0 {
28 %alloca.align16 = alloca [8 x <4 x i32>], align 16, addrspace(5)
29 %gep0 = getelementptr inbounds [8 x <4 x i32>], ptr addrspace(5) %alloca.align16, i32 0, i32 %idx
30 store volatile <4 x i32> <i32 1, i32 2, i32 3, i32 4>, ptr addrspace(5) %gep0, align 16
34 ; GCN-LABEL: {{^}}needs_align16_stack_align4:
35 ; GCN: s_add_i32 [[SCRATCH_REG:s[0-9]+]], s32, 0x3c0{{$}}
36 ; GCN: s_and_b32 s33, [[SCRATCH_REG]], 0xfffffc00
38 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
39 ; GCN: v_or_b32_e32 v{{[0-9]+}}, 12
40 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
41 ; GCN: s_addk_i32 s32, 0x2800{{$}}
42 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
43 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
45 ; GCN: s_addk_i32 s32, 0xd800
47 ; GCN: ; ScratchSize: 160
48 define void @needs_align16_stack_align4(i32 %idx) #2 {
49 %alloca.align16 = alloca [8 x <4 x i32>], align 16, addrspace(5)
50 %gep0 = getelementptr inbounds [8 x <4 x i32>], ptr addrspace(5) %alloca.align16, i32 0, i32 %idx
51 store volatile <4 x i32> <i32 1, i32 2, i32 3, i32 4>, ptr addrspace(5) %gep0, align 16
55 ; GCN-LABEL: {{^}}needs_align32:
56 ; GCN: s_add_i32 [[SCRATCH_REG:s[0-9]+]], s32, 0x7c0{{$}}
57 ; GCN: s_and_b32 s33, [[SCRATCH_REG]], 0xfffff800
59 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
60 ; GCN: v_or_b32_e32 v{{[0-9]+}}, 12
61 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
62 ; GCN: s_addk_i32 s32, 0x3000{{$}}
63 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
64 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
66 ; GCN: s_addk_i32 s32, 0xd000
68 ; GCN: ; ScratchSize: 192
69 define void @needs_align32(i32 %idx) #0 {
70 %alloca.align16 = alloca [8 x <4 x i32>], align 32, addrspace(5)
71 %gep0 = getelementptr inbounds [8 x <4 x i32>], ptr addrspace(5) %alloca.align16, i32 0, i32 %idx
72 store volatile <4 x i32> <i32 1, i32 2, i32 3, i32 4>, ptr addrspace(5) %gep0, align 32
76 ; GCN-LABEL: {{^}}force_realign4:
77 ; GCN: s_add_i32 [[SCRATCH_REG:s[0-9]+]], s32, 0xc0{{$}}
78 ; GCN: s_and_b32 s33, [[SCRATCH_REG]], 0xffffff00
79 ; GCN: s_addk_i32 s32, 0xd00{{$}}
81 ; GCN: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[0:3], 0 offen
82 ; GCN: s_addk_i32 s32, 0xf300
84 ; GCN: ; ScratchSize: 52
85 define void @force_realign4(i32 %idx) #1 {
86 %alloca.align16 = alloca [8 x i32], align 4, addrspace(5)
87 %gep0 = getelementptr inbounds [8 x i32], ptr addrspace(5) %alloca.align16, i32 0, i32 %idx
88 store volatile i32 3, ptr addrspace(5) %gep0, align 4
92 ; GCN-LABEL: {{^}}kernel_call_align16_from_8:
93 ; GCN: s_movk_i32 s32, 0x400{{$}}
96 define amdgpu_kernel void @kernel_call_align16_from_8() #0 {
97 %alloca = alloca i32, align 4, addrspace(5)
98 store volatile i32 2, ptr addrspace(5) %alloca
99 call void @needs_align16_default_stack_align(i32 1)
103 ; The call sequence should keep the stack on call aligned to 4
104 ; GCN-LABEL: {{^}}kernel_call_align16_from_5:
105 ; GCN: s_movk_i32 s32, 0x400
107 define amdgpu_kernel void @kernel_call_align16_from_5() {
108 %alloca0 = alloca i8, align 1, addrspace(5)
109 store volatile i8 2, ptr addrspace(5) %alloca0
111 call void @needs_align16_default_stack_align(i32 1)
115 ; GCN-LABEL: {{^}}kernel_call_align4_from_5:
116 ; GCN: s_movk_i32 s32, 0x400
118 define amdgpu_kernel void @kernel_call_align4_from_5() {
119 %alloca0 = alloca i8, align 1, addrspace(5)
120 store volatile i8 2, ptr addrspace(5) %alloca0
122 call void @needs_align16_stack_align4(i32 1)
126 ; GCN-LABEL: {{^}}default_realign_align128:
127 ; GCN: s_mov_b32 [[FP_COPY:s[0-9]+]], s33
128 ; GCN-NEXT: s_add_i32 s33, s32, 0x1fc0
129 ; GCN-NEXT: s_and_b32 s33, s33, 0xffffe000
130 ; GCN-NEXT: s_addk_i32 s32, 0x4000
132 ; GCN: buffer_store_dword v0, off, s[0:3], s33{{$}}
133 ; GCN: s_addk_i32 s32, 0xc000
134 ; GCN: s_mov_b32 s33, [[FP_COPY]]
135 define void @default_realign_align128(i32 %idx) #0 {
136 %alloca.align = alloca i32, align 128, addrspace(5)
137 store volatile i32 9, ptr addrspace(5) %alloca.align, align 128
141 ; GCN-LABEL: {{^}}disable_realign_align128:
143 ; GCN: buffer_store_dword v0, off, s[0:3], s32{{$}}
145 define void @disable_realign_align128(i32 %idx) #3 {
146 %alloca.align = alloca i32, align 128, addrspace(5)
147 store volatile i32 9, ptr addrspace(5) %alloca.align, align 128
151 declare void @extern_func(<32 x i32>, i32) #0
152 define void @func_call_align1024_bp_gets_vgpr_spill(<32 x i32> %a, i32 %b) #0 {
153 ; The test forces the stack to be realigned to a new boundary
154 ; since there is a local object with an alignment of 1024.
155 ; Should use BP to access the incoming stack arguments.
156 ; The BP value is saved/restored with a VGPR spill.
158 ; GCN-LABEL: func_call_align1024_bp_gets_vgpr_spill:
159 ; GCN: s_mov_b32 [[FP_SCRATCH_COPY:s[0-9]+]], s33
160 ; GCN-NEXT: s_add_i32 [[SCRATCH_REG:s[0-9]+]], s32, 0xffc0
161 ; GCN-NEXT: s_and_b32 s33, [[SCRATCH_REG]], 0xffff0000
162 ; GCN-NEXT: s_or_saveexec_b64 s[18:19], -1
163 ; GCN-NEXT: buffer_store_dword [[VGPR_REG:v[0-9]+]], off, s[0:3], s33 offset:1028 ; 4-byte Folded Spill
164 ; GCN-NEXT: s_mov_b64 exec, s[18:19]
165 ; GCN-NEXT: v_mov_b32_e32 v32, 0
166 ; GCN-DAG: v_writelane_b32 [[VGPR_REG]], s34, 3
167 ; GCN: s_mov_b32 s34, s32
168 ; GCN: buffer_store_dword v32, off, s[0:3], s33 offset:1024
169 ; GCN-NEXT: s_waitcnt vmcnt(0)
170 ; GCN-NEXT: buffer_load_dword v{{[0-9]+}}, off, s[0:3], s34
171 ; GCN-DAG: s_add_i32 s32, s32, 0x30000
172 ; GCN: v_writelane_b32 [[VGPR_REG]], [[FP_SCRATCH_COPY]], 2
173 ; GCN: buffer_store_dword v{{[0-9]+}}, off, s[0:3], s32
174 ; GCN: s_swappc_b64 s[30:31],
176 ; GCN: v_readlane_b32 s31, [[VGPR_REG]], 1
177 ; GCN: v_readlane_b32 s30, [[VGPR_REG]], 0
178 ; GCN-NEXT: v_readlane_b32 s34, [[VGPR_REG]], 3
179 ; GCN-NEXT: v_readlane_b32 [[FP_SCRATCH_COPY:s[0-9]+]], [[VGPR_REG]], 2
180 ; GCN-NEXT: s_or_saveexec_b64 s[6:7], -1
181 ; GCN-NEXT: buffer_load_dword [[VGPR_REG]], off, s[0:3], s33 offset:1028 ; 4-byte Folded Reload
182 ; GCN-NEXT: s_mov_b64 exec, s[6:7]
183 ; GCN-NEXT: s_add_i32 s32, s32, 0xfffd0000
184 ; GCN-NEXT: s_mov_b32 s33, [[FP_SCRATCH_COPY]]
185 ; GCN: s_setpc_b64 s[30:31]
186 %temp = alloca i32, align 1024, addrspace(5)
187 store volatile i32 0, ptr addrspace(5) %temp, align 1024
188 call void @extern_func(<32 x i32> %a, i32 %b)
192 %struct.Data = type { [9 x i32] }
193 define i32 @needs_align1024_stack_args_used_inside_loop(ptr addrspace(5) nocapture readonly byval(%struct.Data) align 8 %arg) local_unnamed_addr #4 {
194 ; The local object allocation needed an alignment of 1024.
195 ; Since the function argument is accessed in a loop with an
196 ; index variable, the base pointer first get loaded into a VGPR
197 ; and that value should be further referenced to load the incoming values.
198 ; The BP value will get saved/restored in an SGPR at the prolgoue/epilogue.
200 ; GCN-LABEL: needs_align1024_stack_args_used_inside_loop:
201 ; GCN: s_mov_b32 [[FP_COPY:s[0-9]+]], s33
202 ; GCN-NEXT: s_add_i32 s33, s32, 0xffc0
203 ; GCN-NEXT: s_mov_b32 [[BP_COPY:s[0-9]+]], s34
204 ; GCN-NEXT: s_mov_b32 s34, s32
205 ; GCN-NEXT: s_and_b32 s33, s33, 0xffff0000
206 ; GCN-NEXT: v_lshrrev_b32_e64 [[VGPR_REG:v[0-9]+]], 6, s34
207 ; GCN-NEXT: v_mov_b32_e32 v{{[0-9]+}}, 0
208 ; GCN: s_add_i32 s32, s32, 0x30000
209 ; GCN: buffer_store_dword v{{[0-9]+}}, off, s[0:3], s33 offset:1024
210 ; GCN: buffer_load_dword v{{[0-9]+}}, [[VGPR_REG]], s[0:3], 0 offen
211 ; GCN: v_add_u32_e32 [[VGPR_REG]], vcc, 4, [[VGPR_REG]]
212 ; GCN: s_mov_b32 s34, [[BP_COPY]]
213 ; GCN-NEXT: s_add_i32 s32, s32, 0xfffd0000
214 ; GCN-NEXT: s_mov_b32 s33, [[FP_COPY]]
215 ; GCN-NEXT: s_setpc_b64 s[30:31]
217 %local_var = alloca i32, align 1024, addrspace(5)
218 store volatile i32 0, ptr addrspace(5) %local_var, align 1024
221 loop_end: ; preds = %loop_body
222 %idx_next = add nuw nsw i32 %lp_idx, 1
223 %lp_exit_cond = icmp eq i32 %idx_next, 9
224 br i1 %lp_exit_cond, label %exit, label %loop_body
226 loop_body: ; preds = %loop_end, %begin
227 %lp_idx = phi i32 [ 0, %begin ], [ %idx_next, %loop_end ]
228 %ptr = getelementptr inbounds %struct.Data, ptr addrspace(5) %arg, i32 0, i32 0, i32 %lp_idx
229 %val = load i32, ptr addrspace(5) %ptr, align 8
230 %lp_cond = icmp eq i32 %val, %lp_idx
231 br i1 %lp_cond, label %loop_end, label %exit
233 exit: ; preds = %loop_end, %loop_body
234 %out = phi i32 [ 0, %loop_body ], [ 1, %loop_end ]
238 define void @no_free_scratch_sgpr_for_bp_copy(<32 x i32> %a, i32 %b) #0 {
239 ; GCN-LABEL: no_free_scratch_sgpr_for_bp_copy:
241 ; GCN: v_writelane_b32 [[VGPR_REG:v[0-9]+]], s34, 0
242 ; GCN-NEXT: s_mov_b32 s34, s32
243 ; GCN-NEXT: buffer_load_dword v{{[0-9]+}}, off, s[0:3], s34
244 ; GCN: v_readlane_b32 s34, [[VGPR_REG:v[0-9]+]], 0
245 ; GCN: buffer_store_dword v{{[0-9]+}}, off, s[0:3], s33 offset:128
246 ; GCN-NEXT: s_waitcnt vmcnt(0)
247 ; GCN-NEXT: ;;#ASMSTART
248 ; GCN-NEXT: ;;#ASMEND
249 ; GCN: s_setpc_b64 s[30:31]
250 %local_val = alloca i32, align 128, addrspace(5)
251 store volatile i32 %b, ptr addrspace(5) %local_val, align 128
252 ; Use all clobberable registers, so BP has to spill to a VGPR.
253 call void asm sideeffect "",
254 "~{s0},~{s1},~{s2},~{s3},~{s4},~{s5},~{s6},~{s7},~{s8},~{s9}
255 ,~{s10},~{s11},~{s12},~{s13},~{s14},~{s15},~{s16},~{s17},~{s18},~{s19}
256 ,~{s20},~{s21},~{s22},~{s23},~{s24},~{s25},~{s26},~{s27},~{s28},~{s29}
261 define void @no_free_regs_spill_bp_to_memory(<32 x i32> %a, i32 %b) #5 {
262 ; If there are no free SGPRs or VGPRs available we must spill the BP to memory.
264 ; GCN-LABEL: no_free_regs_spill_bp_to_mem
265 ; GCN: s_mov_b32 [[FP_SCRATCH_COPY:s[0-9]+]], s33
266 ; GCN: s_xor_saveexec_b64 s[6:7], -1
267 ; GCN: buffer_store_dword v39, off, s[0:3], s33
268 ; GCN: v_mov_b32_e32 v0, s34
269 ; GCN: buffer_store_dword v0, off, s[0:3], s33
270 ; GCN: v_mov_b32_e32 v0, [[FP_SCRATCH_COPY]]
271 ; GCN-DAG: buffer_store_dword v0, off, s[0:3], s33
272 %local_val = alloca i32, align 128, addrspace(5)
273 store volatile i32 %b, ptr addrspace(5) %local_val, align 128
275 call void asm sideeffect "; clobber nonpreserved SGPRs and 64 CSRs",
276 "~{s4},~{s5},~{s6},~{s7},~{s8},~{s9}
277 ,~{s10},~{s11},~{s12},~{s13},~{s14},~{s15},~{s16},~{s17},~{s18},~{s19}
278 ,~{s20},~{s21},~{s22},~{s23},~{s24},~{s25},~{s26},~{s27},~{s28},~{s29}
279 ,~{s40},~{s41},~{s42},~{s43},~{s44},~{s45},~{s46},~{s47},~{s48},~{s49}
280 ,~{s50},~{s51},~{s52},~{s53},~{s54},~{s55},~{s56},~{s57},~{s58},~{s59}
281 ,~{s60},~{s61},~{s62},~{s63},~{s64},~{s65},~{s66},~{s67},~{s68},~{s69}
282 ,~{s70},~{s71},~{s72},~{s73},~{s74},~{s75},~{s76},~{s77},~{s78},~{s79}
283 ,~{s80},~{s81},~{s82},~{s83},~{s84},~{s85},~{s86},~{s87},~{s88},~{s89}
284 ,~{s90},~{s91},~{s92},~{s93},~{s94},~{s95},~{s96},~{s97},~{s98},~{s99}
285 ,~{s100},~{s101},~{s102},~{s39},~{vcc}"() #0
287 call void asm sideeffect "; clobber all VGPRs",
288 "~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9}
289 ,~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19}
290 ,~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29}
291 ,~{v30},~{v31},~{v32},~{v33},~{v34},~{v35},~{v36},~{v37},~{v38}" () #0
295 define void @spill_bp_to_memory_scratch_reg_needed_mubuf_offset(<32 x i32> %a, i32 %b, ptr addrspace(5) byval([4096 x i8]) align 4 %arg) #5 {
296 ; If the size of the offset exceeds the MUBUF offset field we need another
297 ; scratch VGPR to hold the offset.
299 ; GCN-LABEL: spill_bp_to_memory_scratch_reg_needed_mubuf_offset
300 ; GCN: s_mov_b32 [[FP_SCRATCH_COPY:s[0-9]+]], s33
301 ; GCN-NEXT: s_add_i32 s33, s32, 0x1fc0
302 ; GCN-NEXT: s_and_b32 s33, s33, 0xffffe000
303 ; GCN-NEXT: s_xor_saveexec_b64 s[6:7], -1
304 ; GCN-NEXT: s_add_i32 s5, s33, 0x42100
305 ; GCN-NEXT: buffer_store_dword v39, off, s[0:3], s5 ; 4-byte Folded Spill
306 ; GCN-NEXT: s_mov_b64 exec, s[6:7]
307 ; GCN-NEXT: v_mov_b32_e32 v0, s34
308 ; GCN-NOT: v_mov_b32_e32 v0, 0x108c
309 ; GCN-NEXT: s_add_i32 s5, s33, 0x42300
310 ; GCN-NEXT: buffer_store_dword v0, off, s[0:3], s5 ; 4-byte Folded Spill
311 ; GCN-NEXT: v_mov_b32_e32 v0, [[FP_SCRATCH_COPY]]
312 ; GCN-NOT: v_mov_b32_e32 v0, 0x1088
313 ; GCN-NEXT: s_add_i32 s5, s33, 0x42200
314 ; GCN-NEXT: s_mov_b32 s34, s32
315 ; GCN-NEXT: buffer_store_dword v0, off, s[0:3], s5 ; 4-byte Folded Spill
316 %local_val = alloca i32, align 128, addrspace(5)
317 store volatile i32 %b, ptr addrspace(5) %local_val, align 128
319 call void asm sideeffect "; clobber nonpreserved SGPRs and 64 CSRs",
320 "~{s4},~{s5},~{s6},~{s7},~{s8},~{s9}
321 ,~{s10},~{s11},~{s12},~{s13},~{s14},~{s15},~{s16},~{s17},~{s18},~{s19}
322 ,~{s20},~{s21},~{s22},~{s23},~{s24},~{s25},~{s26},~{s27},~{s28},~{s29}
323 ,~{s40},~{s41},~{s42},~{s43},~{s44},~{s45},~{s46},~{s47},~{s48},~{s49}
324 ,~{s50},~{s51},~{s52},~{s53},~{s54},~{s55},~{s56},~{s57},~{s58},~{s59}
325 ,~{s60},~{s61},~{s62},~{s63},~{s64},~{s65},~{s66},~{s67},~{s68},~{s69}
326 ,~{s70},~{s71},~{s72},~{s73},~{s74},~{s75},~{s76},~{s77},~{s78},~{s79}
327 ,~{s80},~{s81},~{s82},~{s83},~{s84},~{s85},~{s86},~{s87},~{s88},~{s89}
328 ,~{s90},~{s91},~{s92},~{s93},~{s94},~{s95},~{s96},~{s97},~{s98},~{s99}
329 ,~{s100},~{s101},~{s102},~{s39},~{vcc}"() #0
331 call void asm sideeffect "; clobber all VGPRs",
332 "~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9}
333 ,~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19}
334 ,~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29}
335 ,~{v30},~{v31},~{v32},~{v33},~{v34},~{v35},~{v36},~{v37},~{v38}"() #0
339 attributes #0 = { noinline nounwind }
340 attributes #1 = { noinline nounwind "stackrealign" }
341 attributes #2 = { noinline nounwind alignstack=4 }
342 attributes #3 = { noinline nounwind "no-realign-stack" }
343 attributes #4 = { noinline nounwind "frame-pointer"="all"}
344 attributes #5 = { noinline nounwind "amdgpu-waves-per-eu"="6,6" }