[InlineSpiller] hoistSpillInsideBB: partial-subregister split shares spill slot→ full-width store corrupts loop-carried sibling (gfx950 miscompile)
On gfx950 (CDNA3 / MI300-class), a fused multi-head-attention forward kernel (_attn_fwd, from ROCm aiter, compiled via Triton 3.8.0) is miscompiled by the register allocator. A loop-carried buffer-descriptor value is silently overwritten by an unrelated value inside the K/V inner loop, so buffer_load reads K from the wrong address on every iteration after the first.
The defect is in InlineSpiller: when greedy live-range-splits only a subset of the sub-registers of a wide value (sgpr_128), the resulting sibling shares the same spill stack slot as the original, but a full-width store writes the undefined sub-registers back over the slot, clobbering the loop-carried value still live there.
This is the same class of hazard that #177703 (75f03a62) fixed for the cross-BB hoist path (HoistSpillHelper::isSpillCandBB). The symmetric same-BB path (InlineSpiller::hoistSpillInsideBB) was not covered, and is the one this kernel hits.
Miscompile (observable)
Inside the K/V loop (.LBB0_12):
- The K buffer descriptor s[0:3] is rebuilt at the loop top by v_readlane from VGPR lanes, feeding buffer_load_dwordx4 — source location mha.py:138 (descriptor definition).
- The same lanes are later overwritten in-loop by v_writelane on the dropout / RETURN_SCORES path — source location mha.py:228 (Philox dropout store).
- There is no in-loop save-back of those lanes before the back-edge, so the next iteration reads the dropout value as if it were the K descriptor address.
The .loc metadata is decisive: the lanes are defined by the descriptor (mha.py:138) but overwritten by dropout (mha.py:228, random.py), with no restore. The exact VGPR/lane is not essential — the defect reproduces on different registers/lanes under different pressure layouts (it is register/lane-agnostic).
Root cause
llvm/lib/CodeGen/InlineSpiller.cpp shares one stack slot among all descendants of an Original value:
// Share a stack slot among all descendants of Original. Original = VRM.getOriginal(edit.getReg()); StackSlot = VRM.getStackSlot(Original);
The K descriptor is an sgpr_128:
- sub0_sub1 = buffer base address — loop-carried, spilled and reloaded every trip;
- sub2_sub3 = buffer config constants.
The dropout path only needs the config, so greedy live-range-splits sub2_sub3 into a descendant vreg that only defines sub2_sub3 (sub0_sub1 is undef). Because the descendant shares Original, the spiller reuses the same stack slot, then emits a full-width SI_SPILL_S128_SAVE, writing the undef sub0_sub1 over the loop-carried base address still living in that slot. The next iteration reloads a corrupted descriptor.
The correctness precondition the spiller assumes — that descendants sharing a slot hold "equal values defined by Original's VNI" — is false for a partial-subregister split: the descendant equals Original only on sub2_sub3, and differs (undef) on sub0_sub1, yet a full-width store touches the whole slot.
Three-layer evidence (Debug+asserts llc):
- post-greedy MIR: undef %N.sub0:sgpr_128 = ... immediately followed by SI_SPILL_S128_SAVE %N, %stack.K, where %stack.K is shared with the loop-carried descriptor's siblings.
- post-rewriter: only sub2_sub3 is redefined, but a full-width SI_SPILL_S128_SAVE of the whole sgpr_128 is emitted; sub0_sub1 has no nearby def → stale/undef.
- asm: dropout writes all four descriptor lanes; the next loop-top read gets the polluted lanes.
Relation to #177703
75f03a62 ("[InlineSpiller] Hoist spills only when all of its subranges are available") added, to HoistSpillHelper::isSpillCandBB (the cross-BB hoist path), a check that all sub-ranges are live at the prospective slot index. The symmetric same-BB path, InlineSpiller::hoistSpillInsideBB, has no such guard. This kernel hits the same-BB path (spill inside a self-looping loop body), so the #177703 fix does not apply.
Candidate fix
Mirror the #177703 guard on the same-BB path: in hoistSpillInsideBB, bail out of the hoist when not all sub-ranges of the sibling value are live at the hoist point, so the full-width store that would write back undefined sub-registers is not emitted.
// All sub-ranges of the sibling value must be live at the hoist point; otherwise // the full-width store would write back undefined sub-registers into the shared // spill slot, corrupting the loop-carried sibling. Mirrors the isSpillCandBB fix (#177703). bool partialDead = SrcLI.hasSubRanges() && !all_of(SrcLI.subranges(), [&](const LiveInterval::SubRange &SR) { return SR.getVNInfoAt(Idx) != nullptr; }); if (partialDead) return false;
Measured effect (original kernel, controlled single-variable toggle). Same llc, the only variable is the guard on/off; input is the original _attn_fwd.llir:
┌───────┬────────────────────────────────────────────────────┐ │ Guard │ Final asm (dataflow judge) │ ├───────┼────────────────────────────────────────────────────┤ │ off │ BUG DETECTED (K-desc lane clobbered, no save-back) │ ├───────┼────────────────────────────────────────────────────┤ │ on │ CLEAN │ └───────┴────────────────────────────────────────────────────┘
The guard fires 97 times on this input, and the miscompile disappears.
Known gaps (disclosed up front)
- No reduced .ll test yet for this (hoist) path. The defect requires the allocator, under critical register pressure, to place the partial split's config and the non-restored dropout lane in the same slot with no in-loop rebuild — an emergent RA behavior. Hand-written minimal IR reaches the necessary conditions (shared-slot full-width store of a partial-def descendant) but not the sufficient one (the actual final-asm lane collision): such attempts are BUG at the MIR layer but CLEAN at the asm layer. llvm-reduce on the real kernel either drops the dropout store that creates the collision, or converges onto a different exit of the same root cause (see gap 2). Reproduction currently requires the original kernel.
- This root cause has a second exit not covered by the hoist guard. Under higher pressure (e.g. waves-per-eu raised, or llvm-reduce output), the polluting full-width store is emitted on the insertSpill / isRealSpill main path rather than via hoistSpillInsideBB. On such inputs the hoist guard still fires but does not change the final asm (measured: a reduced test triggers the guard 6 times yet stays BUG, versus 97 fires → CLEAN on the original kernel). A complete fix for that exit needs insertSpill to use a private slot for a partially-defined super-register vreg instead of the shared Original slot. Tracked separately.
Environment
- Target: amdgcn-amd-amdhsa, -mcpu=gfx950, -O3.
- Kernel specialization: IS_CAUSAL=1, BLOCK_M=256, BLOCK_N=64, BLOCK_DMODEL=64, RETURN_SCORES=1, ENABLE_DROPOUT=1, VARLEN=1 (dropout pulls in the Philox RNG, whose live state collides with the descriptor).
- Bisect: the miscompile is exposed/hidden by 8a9c0755 (#214725), which is pure pressure perturbation, not a fix — it hides the bug on the original kernel but exposes it on a waves-per-eu-raised variant. The latent InlineSpiller defect is independent of that commit.
I'm working on this
https://github.com/xgxanq/llvm-project/tree/inline-spiller-hoist-subrange
We found a reduced reproducer from a FlexAttention illegal-memory-access failure on MI350.
For context, we were testing updating Triton pin on Pytorch. In the pin PR pytorch/pytorch#190440, three FlexAttention tests consistently crash with HSA_STATUS_ERROR_MEMORY_FAULT (fault address e.g. 0xc000) in the generated kernel triton_tem_fused_flex_attention_0:
test/inductor/test_flex_attention.py
TestFlexAttentionCUDA.test_load_from_bias_seq_only_cuda_float16
TestFlexAttentionCUDA.test_load_from_bias_seq_batch_cuda_float16
TestFlexAttentionCUDA.test_load_from_bias_head_seq_batch_cuda_float16
- Failing MI350 CI with the new Triton pin: https://github.com/pytorch/pytorch/actions/runs/35206071680
- Same PyTorch commit with the previous Triton pin, passing: https://github.com/pytorch/pytorch/actions/runs/35205904400
Bisecting Triton points to triton-lang/triton#11485, which moved the AMD backend to a newer LLVM. Bisecting LLVM points to 8a9c0755eb6c (#214725, AMDGPU pre-RA remat) which raises register pressure enough to trigger the spill.
The reduced reproducer I attached below shows the same partial-subregister / shared-spill-slot problem described here on upstream b864af16.
llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -O3 -relocation-model=pic -trap-unreachable \
-amdgpu-use-amdgpu-trackers -stop-after=greedy repro-final-201.ll -o out.mir
Applying the proposed hoistSpillInsideBB guard rejects the hoist, but does not fix our reproducer: the fallback insertSpill path still emits a full-width spill when only sub2 is defined:
undef %860.sub2 = lr-split COPY %859.sub2
SI_SPILL_S128_SAVE %860, %stack.19
This overwrites the live lanes of the buffer descriptor in the shared stack slot, which causes the memory fault above.
We found a reduced reproducer from a FlexAttention illegal-memory-access failure on MI350.
For context, we were testing updating Triton pin on Pytorch. In the pin PR pytorch/pytorch#190440, three FlexAttention tests consistently crash with
HSA_STATUS_ERROR_MEMORY_FAULT(fault address e.g.0xc000) in the generated kerneltriton_tem_fused_flex_attention_0:test/inductor/test_flex_attention.py TestFlexAttentionCUDA.test_load_from_bias_seq_only_cuda_float16 TestFlexAttentionCUDA.test_load_from_bias_seq_batch_cuda_float16 TestFlexAttentionCUDA.test_load_from_bias_head_seq_batch_cuda_float16
- Failing MI350 CI with the new Triton pin: https://github.com/pytorch/pytorch/actions/runs/35206071680
- Same PyTorch commit with the previous Triton pin, passing: https://github.com/pytorch/pytorch/actions/runs/35205904400
Bisecting Triton points to triton-lang/triton#11485, which moved the AMD backend to a newer LLVM. Bisecting LLVM points to 8a9c075 (#214725, AMDGPU pre-RA remat) which raises register pressure enough to trigger the spill.
The reduced reproducer I attached below shows the same partial-subregister / shared-spill-slot problem described here on upstream
b864af16.llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -O3 -relocation-model=pic -trap-unreachable \ -amdgpu-use-amdgpu-trackers -stop-after=greedy repro-final-201.ll -o out.mirApplying the proposed hoistSpillInsideBB guard rejects the hoist, but does not fix our reproducer: the fallback insertSpill path still emits a full-width spill when only sub2 is defined:
undef %860.sub2 = lr-split COPY %859.sub2 SI_SPILL_S128_SAVE %860, %stack.19This overwrites the live lanes of the buffer descriptor in the shared stack slot, which causes the memory fault above.
The prior hoistSpillInsideBB fix only covers the operator case I mentioned. Other operators may hit separate related issues. Give me a moment — I will reproduce your problem and check if it falls into the same category of issues I’ve already encountered but haven’t upstreamed yet. If so, I will submit the corresponding patch
We found a reduced reproducer from a FlexAttention illegal-memory-access failure on MI350.
For context, we were testing updating Triton pin on Pytorch. In the pin PR pytorch/pytorch#190440, three FlexAttention tests consistently crash with
HSA_STATUS_ERROR_MEMORY_FAULT(fault address e.g.0xc000) in the generated kerneltriton_tem_fused_flex_attention_0:test/inductor/test_flex_attention.py TestFlexAttentionCUDA.test_load_from_bias_seq_only_cuda_float16 TestFlexAttentionCUDA.test_load_from_bias_seq_batch_cuda_float16 TestFlexAttentionCUDA.test_load_from_bias_head_seq_batch_cuda_float16
- Failing MI350 CI with the new Triton pin: https://github.com/pytorch/pytorch/actions/runs/35206071680
- Same PyTorch commit with the previous Triton pin, passing: https://github.com/pytorch/pytorch/actions/runs/35205904400
Bisecting Triton points to triton-lang/triton#11485, which moved the AMD backend to a newer LLVM. Bisecting LLVM points to 8a9c075 (#214725, AMDGPU pre-RA remat) which raises register pressure enough to trigger the spill.
The reduced reproducer I attached below shows the same partial-subregister / shared-spill-slot problem described here on upstream
b864af16.llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -O3 -relocation-model=pic -trap-unreachable \ -amdgpu-use-amdgpu-trackers -stop-after=greedy repro-final-201.ll -o out.mirApplying the proposed hoistSpillInsideBB guard rejects the hoist, but does not fix our reproducer: the fallback insertSpill path still emits a full-width spill when only sub2 is defined:
undef %860.sub2 = lr-split COPY %859.sub2 SI_SPILL_S128_SAVE %860, %stack.19This overwrites the live lanes of the buffer descriptor in the shared stack slot, which causes the memory fault above.
Confirmed on my side. Building your upstream base b864af16 (llc, gfx950, same flags incl. -amdgpu-use-amdgpu-trackers) and running -stop-after=greedy on the flex-attention kernel, I reproduce the exact pathology you describe. In our numbering it's %stack.19, shared by %852/%854/%859:
undef %852.sub0_sub1 = COPY $sgpr10_sgpr11 ; descriptor base %852.sub2/sub3 = S_MOV (flags 0x7ffffffe,1) SI_SPILL_S128_SAVE %852, %stack.19 ; full descriptor stored ... undef %859.sub2_sub3 = lr-split COPY %857.sub2_sub3 ; only flags redefined, sub0_sub1 UNDEF SI_SPILL_S128_SAVE %859, %stack.19 ; full-width store writes undef base over the slot
%859 here is your %860. This is the isRealSpill/insertSpill main-path store, not the hoist path — so, as you found, the hoistSpillInsideBB guard correctly rejects the hoist but the fallback still emits the full-width spill. The guard is necessary but not sufficient; the real fix has to mask the store to the defined sub-lanes (or use a private slot for the partially-defined vreg).
One discrepancy we'd like to close with you. On both b864af16 and our newer trees, the final assembly for this kernel comes out clean: the undef sub0_sub1 lanes get lowered to a save-back of the descriptor's own value, so no wrong address reaches the buffer_load. That's consistent with the source IR — in the .ll we have, the loop-body descriptor (%46 =make.buffer.rsrc(%1, ...)) is loop-invariant (defined in the preheader, read-only in the loop), so refilling the undef lane with its own constant base is still semantically correct.
Could you share the original, pre-reduction IR — the full Triton-generated kernel that actually faults on MI350 — rather than repro-final-201.ll? The reduced file is an llvm-reduce artifact: its interesting-ness predicate almost certainly keys on the MIR-level pathology (the partial-def full-width spill), so reduction can legally delete the very code path that turns the shared-slot clobber into a wrong runtime address. That's exactly why we see the structural precondition but not the fault on our copy. We need the unreduced IR straight out of the Triton cache (the .llir/.ll before any llvm-reduce run), so we can byte-diff it against our error_924.llir — because I need to confirm on the real input whether the failing descriptor is loop-carried and whether the corrupting store lands an unrelated value. The reduced repro-final-201.ll is useful as the MIR-level signature, but it can't tell us why the final asm faults.
@warrendeng https://github.com/llvm/llvm-project/pull/226689 You can try this one
We're hitting this on MI350 too, in a generated attention kernel, where it causes a memory access fault. The pattern is that after greedy, a sibling that defines only part of an SGPR tuple is spilled full-width into the shared stack slot. That store overwrites the base address of a loop-invariant buffer descriptor, which is reloaded from the slot at the top of each iteration. From the second iteration on, loads through that descriptor use the wrong base address.
We're carrying a downstream workaround for now: https://github.com/ROCm/rocmlirTriton/pull/552 (changes only InlineSpiller). It doesn't hoist the spill when the source doesn't define every lane that is live in the original value, and it reloads the slot before storing an undef partial def.
I'm hitting what I believe is a bug with the same root cause from FlexAttention-generated Triton kernels on gfx1151 (RDNA 3.5).
You asked above for the unreduced Triton-generated IR: reduced_triton380_gfx1151.ll.txt (.txt extension because of GitHub restrictions). In this IR the corrupting store writes an unrelated value over a live descriptor, and it fails at runtime. This was generated by Triton 3.8.0. On main, Triton has re-enabled real-true16 (https://github.com/triton-lang/triton/pull/11188), which avoids the spilling that triggers the bug, but the same binary fails with the main runtime.
Agent-generated analysis
# llvm-project b010a18d2b648cab83c83967ff26b8fde11acdc6 (assertions build)
llc -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1151 -mattr=-real-true16 \
reduced_triton380_gfx1151.ll -o out.s -print-after=greedy
(-real-true16 matches what Triton 3.8.0 passed for gfx11.)
After the first (SGPR) greedy run, bb.6 is the loop and %stack.8 holds K's buffer descriptor:
16036B SI_SPILL_S128_SAVE %5856:sgpr_128, %stack.8 ; full descriptor, preheader
30280B %5882:sgpr_128 = SI_SPILL_S128_RESTORE %stack.8 ; bb.6, every iteration
30288B %1320:vreg_128 = BUFFER_LOAD_DWORDX4_OFFEN %1313, %5882, ...
30408B undef %5883.sub2_sub3:sgpr_128 = lr-split COPY %5882.sub2_sub3
58632B undef %5884.sub2_sub3:sgpr_128 = lr-split COPY %5883.sub2_sub3
58636B SI_SPILL_S128_SAVE %5884:sgpr_128, %stack.8 ; sub0_sub1 undef
With -debug-only=regalloc, this store comes from the hoistSpillInsideBB path: hoisted: 58632r SI_SPILL_S128_SAVE %5884 ... while spilling sibling %6042.
In the final ISA, the undef lanes are not filled with the descriptor's own value. They get whatever is in s4/s5 at that point, the results of unrelated s_mul_hi_i32 index arithmetic. The out.s from the command above shows the same thing: in the second iteration, the K descriptor's low dword traces back to an s_mul_hi_i32. This excerpt is from the binary Triton 3.8.0 built for this kernel, the one that fails:
v_readlane_b32 s4, v253, 8 ; loop top: reload K descriptor
v_readlane_b32 s5, v253, 9
v_readlane_b32 s6, v253, 10
v_readlane_b32 s7, v253, 11
buffer_load_b128 v[0:3], v0, s[4:7], 0 offen ; 8 K loads
...
s_mul_hi_i32 s4, s11, 0x2aaaaaab
s_mul_hi_i32 s5, s9, 0x2aaaaaab
v_writelane_b32 v253, s4, 8 ; over the descriptor base (low)
v_writelane_b32 v253, s5, 9 ; over the base (high), stride, swizzle
v_writelane_b32 v253, s6, 10
v_writelane_b32 v253, s7, 11
From the second iteration on, the K loads use that as their base address. On the GPU, the result is unreadable, and every later kernel in the process returns wrong values. A related variant of the kernel returns wrong results instead, without breaking the device; I haven't traced which spilled value that one clobbers.
GPU check on gfx1151, with the same runtime for both runs: deleting only the two v_writelane lines for lanes 8 and 9 from that binary makes the kernel pass (max abs error 0.00177 against an fp64 reference). The unedited binary fails and breaks all future GPU work in the process: a trivial sum kernel afterwards returns 0 instead of the expected value.
The same pattern (undef %5778.sub2_sub3 spilled full-width into the shared slot) also shows up with a newer AMD LLVM, triton-lang/llvm-project 6bc4aaf6. I have not tested #226689 or the ROCm/rocmlirTriton#552 change on this input.
Thanks @Muhamed-Husic @GMNGeoffrey!
Do we also plan on upstreaming the InlineSpiller change from https://github.com/ROCm/rocmlirTriton/pull/552 anytime soon? I had drafted https://github.com/llvm/llvm-project/pull/227492 to unblock some PyTorch Triton pin failures for our upcoming release which uses a similar approach, but happy to defer to your implementation/coordinate
Another real-world hit, plus a small reproducer that still triggers on current main (fc16f151) and a note on why the subrange-based guards miss it.
Where it shows up. Gluon flash-attention prefill kernels on gfx950 (MI355X), compiled with Triton's pinned AMD LLVM (triton-lang/llvm-project 08b05a33, a merge of llvm/llvm-project 05e432a8; the only diff from upstream in llvm/lib/CodeGen is in MachineScheduler). The loop-invariant K buffer resource is spilled to VGPR lanes before a persistent loop and read back in each iteration. On one path inside the loop, undef %9951.sub2_sub3:sgpr_128 = lr-split COPY %9950.sub2_sub3 is spilled. hoistSpillInsideBB hoists SI_SPILL_S128_SAVE %9951, %stack.167 onto that copy, and %stack.167 still holds the resource (live across the loop). Final asm (pinned LLVM, llc -O3 -mtriple=amdgpu9.50-amd-amdhsa reproduces the shipped kernel byte for byte):
; preheader: K resource s[4:7] -> v250 lanes 27..30
v_writelane_b32 v250, s4, 27
v_writelane_b32 v250, s5, 28
...
; inside the loop, one path: only s[94:95] (sub2_sub3) is defined
s_mov_b64 s[94:95], s[6:7]
v_writelane_b32 v250, s92, 27 ; s92/s93 are stale: overwrite the K base address
v_writelane_b32 v250, s93, 28
v_writelane_b32 v250, s94, 29
v_writelane_b32 v250, s95, 30
; next iteration, other path
v_readlane_b32 s4, v250, 27 ; wrong base
...
buffer_load_dwordx4 v[8:11], v8, s[4:7], 0 offen
On hardware this gives wrong attention output (or a memory fault) for the sequences processed after that path in a batch. Expected behaviour: lanes 27..30 still hold the resource at the next reload.
Reproducer on main. On current main the same IR no longer triggers by default; the allocation is perturbed by SlotIndexes compaction (#235806 / #224031). The code involved is unchanged, though, and with compaction disabled the same pattern shows up again. Reduced with llvm-reduce from one of the kernels:
llc -mtriple=amdgpu9.50-amd-amdhsa -greedy-compact-slot-indexes=false -split-spill-mode=speed \
-verify-machineinstrs -stop-after=greedy repro.ll -o -
bb.0:
SI_SPILL_S128_SAVE %398, %stack.0 ; buffer resource
bb.1: ; loop
%408:sgpr_128 = SI_SPILL_S128_RESTORE %stack.0
%112:av_128_align2 = BUFFER_LOAD_DWORDX4_OFFEN %106, %408, 0, 0, 0, 0, implicit $exec
undef %410.sub3:sgpr_128 = lr-split COPY %408.sub3
SI_SPILL_S128_SAVE %410, %stack.0 ; sub0..sub2 undefined, slot still live (back edge)
...
S_CBRANCH_VCCNZ %bb.1
In this reduced case register assignment happens to leave the right values in the undefined lanes, so the asm looks benign. The MIR store is still wrong in the same way as in the full kernels.
repro.ll
define amdgpu_kernel void @partial_split_def_spill(ptr addrspace(1) inreg %0, ptr addrspace(1) %1, i32 %2, <4 x float> %3) #0 {
.lr.ph:
%4 = tail call i32 @llvm.amdgcn.workitem.id.x()
%5 = insertelement <4 x i32> zeroinitializer, i32 %4, i64 0
br label %6
6: ; preds = %6, %.lr.ph
%7 = phi i32 [ 1, %.lr.ph ], [ 0, %6 ]
%8 = tail call ptr addrspace(8) @llvm.amdgcn.make.buffer.rsrc.p8.p1.i32(ptr addrspace(1) %0, i16 0, i32 2147483646, i32 1)
%9 = load <1 x i32>, ptr addrspace(1) null, align 4
%10 = extractelement <1 x i32> %9, i64 0
%11 = tail call <4 x i32> @llvm.amdgcn.raw.ptr.buffer.load.v4i32(ptr addrspace(8) %8, i32 %10, i32 0, i32 0)
%12 = sdiv i32 %7, %2
%13 = insertelement <4 x i32> zeroinitializer, i32 %12, i64 0
%14 = shufflevector <4 x i32> %13, <4 x i32> zeroinitializer, <4 x i32> zeroinitializer
%15 = shufflevector <4 x i32> %5, <4 x i32> zeroinitializer, <4 x i32> zeroinitializer
%16 = or <4 x i32> %15, <i32 40, i32 41, i32 42, i32 43>
%17 = or <4 x i32> %14, %16
%18 = or <4 x i32> %5, <i32 48, i32 49, i32 50, i32 51>
%19 = or <4 x i32> %14, %18
%20 = or <4 x i32> %15, <i32 56, i32 57, i32 58, i32 59>
%21 = or <4 x i32> %14, %20
%22 = tail call ptr addrspace(8) @llvm.amdgcn.make.buffer.rsrc.p8.p1.i32(ptr addrspace(1) %1, i16 0, i32 1, i32 0)
tail call void @llvm.amdgcn.raw.ptr.buffer.load.async.lds(ptr addrspace(8) %22, ptr addrspace(3) null, i32 1, i32 0, i32 0, i32 0, i32 0)
%23 = or <4 x i32> %15, <i32 24, i32 25, i32 26, i32 27>
%24 = or <4 x i32> %14, %23
%25 = sub <4 x i32> zeroinitializer, %24
%26 = icmp ult <4 x i32> %25, splat (i32 101)
%27 = or <4 x i32> %15, <i32 32, i32 33, i32 34, i32 35>
%28 = or <4 x i32> %14, %27
%29 = sub <4 x i32> zeroinitializer, %28
%30 = icmp ult <4 x i32> %29, splat (i32 101)
%31 = sub <4 x i32> zeroinitializer, %17
%32 = icmp ult <4 x i32> %31, splat (i32 101)
%33 = sub <4 x i32> zeroinitializer, %19
%34 = icmp ult <4 x i32> %33, splat (i32 101)
%35 = sub <4 x i32> zeroinitializer, %21
%36 = icmp ult <4 x i32> %35, splat (i32 101)
%37 = icmp slt <4 x i32> %24, zeroinitializer
%38 = icmp slt <4 x i32> %28, zeroinitializer
%39 = icmp slt <4 x i32> %17, zeroinitializer
%40 = icmp slt <4 x i32> %19, zeroinitializer
%41 = icmp slt <4 x i32> %21, zeroinitializer
%42 = and <4 x i1> %37, %26
%43 = and <4 x i1> %38, %30
%44 = and <4 x i1> %40, %34
%45 = and <4 x i1> %41, %36
%46 = select <4 x i1> %42, <4 x float> %3, <4 x float> zeroinitializer
%47 = bitcast <4 x i32> %11 to <8 x bfloat>
%48 = tail call <16 x float> @llvm.amdgcn.mfma.f32.32x32x16.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <16 x float> zeroinitializer, i32 0, i32 0, i32 0)
%49 = tail call <16 x float> @llvm.amdgcn.mfma.f32.32x32x16.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> %47, <16 x float> %48, i32 0, i32 0, i32 0)
%50 = shufflevector <16 x float> %49, <16 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
%51 = bitcast <4 x i32> %13 to <4 x float>
%52 = fadd <4 x float> %50, %51
%53 = select <4 x i1> %43, <4 x float> %52, <4 x float> zeroinitializer
%54 = and <4 x i1> %39, %32
%55 = shufflevector <16 x float> %48, <16 x float> zeroinitializer, <4 x i32> <i32 12, i32 13, i32 14, i32 15>
%56 = select <4 x i1> <i1 false, i1 true, i1 false, i1 false>, <4 x float> %3, <4 x float> zeroinitializer
%57 = fadd <4 x float> %55, %56
%58 = fmul <4 x float> %46, %53
%59 = select <4 x i1> %54, <4 x float> zeroinitializer, <4 x float> splat (float -inf)
%60 = fmul <4 x float> %58, %59
%61 = select <4 x i1> %44, <4 x float> splat (float +qnan), <4 x float> zeroinitializer
%62 = fmul <4 x float> %60, %61
%63 = select <4 x i1> %45, <4 x float> %57, <4 x float> zeroinitializer
%64 = fmul <4 x float> %62, %63
%65 = tail call float @llvm.vector.reduce.fmaximum.v4f32(<4 x float> %64)
tail call void @llvm.amdgcn.raw.ptr.buffer.store.f32(float %65, ptr addrspace(8) null, i32 0, i32 0, i32 0)
%66 = tail call ptr addrspace(8) @llvm.amdgcn.make.buffer.rsrc.p8.p1.i32(ptr addrspace(1) null, i16 0, i32 0, i32 1)
tail call void @llvm.amdgcn.raw.ptr.buffer.store.v4i32(<4 x i32> zeroinitializer, ptr addrspace(8) %66, i32 0, i32 0, i32 0)
br label %6
}
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare noundef range(i32 0, 1024) i32 @llvm.amdgcn.workitem.id.x() #1
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare <4 x i32> @llvm.amdgcn.raw.ptr.buffer.load.v4i32(ptr addrspace(8) readonly captures(none), i32, i32, i32 immarg) #2
; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.amdgcn.raw.ptr.buffer.load.async.lds(ptr addrspace(8) readonly captures(none), ptr addrspace(3) writeonly captures(none), i32 immarg, i32, i32, i32 immarg, i32 immarg) #3
; Function Attrs: convergent nocallback nocreateundeforpoison nofree nosync nounwind willreturn memory(none)
declare <16 x float> @llvm.amdgcn.mfma.f32.32x32x16.bf16(<8 x bfloat>, <8 x bfloat>, <16 x float>, i32 immarg, i32 immarg, i32 immarg) #4
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.amdgcn.raw.ptr.buffer.store.f32(float, ptr addrspace(8) writeonly captures(none), i32, i32, i32 immarg) #5
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.amdgcn.raw.ptr.buffer.store.v4i32(<4 x i32>, ptr addrspace(8) writeonly captures(none), i32, i32, i32 immarg) #5
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare ptr addrspace(8) @llvm.amdgcn.make.buffer.rsrc.p8.p1.i32(ptr addrspace(1) readnone, i16, i32, i32) #6
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.vector.reduce.fmaximum.v4f32(<4 x float>) #6
; uselistorder directives
uselistorder ptr @llvm.amdgcn.mfma.f32.32x32x16.bf16, { 1, 0 }
uselistorder ptr @llvm.amdgcn.make.buffer.rsrc.p8.p1.i32, { 2, 1, 0 }
attributes #0 = { "amdgpu-agpr-alloc"="0" "amdgpu-no-queue-ptr" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { convergent nocallback nocreateundeforpoison nofree nosync nounwind willreturn memory(none) }
attributes #5 = { nocallback nofree nosync nounwind willreturn memory(argmem: write) }
attributes #6 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
Why the existing / proposed guards miss it. The #177703 guard in isSpillCandBB and the hoistSpillInsideBB candidate guard above both check that every existing subrange is live. A split sibling like undef %9951.sub2_sub3 = COPY ... has subranges only for sub2 and sub3. There is no subrange at all for the undefined lanes, so all_of(subranges, live) is true and the hoist goes ahead. Blocking the hoist is also not sufficient on its own. As noted above, the regular store after the partial copy writes the same undefined lanes.
Fix directions checked against these kernels. Both of the following remove the clobber, and the kernels then pass on MI355X:
- PR #226689 (lane-masked SGPR spill stores plus hoist guard), applied on top of
05e432a8. - A local change in
spillAroundUses. When a sibling copy defines only some lanes (undefsubreg def) and the stack slot is live there, it does not hoist or fold. Instead it reloads the slot into the new register before the copy, drops theundefflag, and keeps the slot live from the reload to the copy. The full-width store then writes the other lanes back unchanged. With this change the AMDGPU CodeGen lit tests pass unchanged, and the reproducer above is clean.
Fix proposed in #238213.
Assisted-by: Claude Code
https://github.com/llvm/llvm-project/pull/243100
The product team previously worked around this RA spil bug by rewriting the kernel. I am now splitting my earlier large, mixed LLVM change into a sequence of smaller incremental commits.
Seeing the same miscompile on Radeon: gfx1100 (RX 7900 XTX) and gfx1201 (Radeon AI PRO R9700).
- Setup: a Triton 3.9 split-K attention kernel (fp8 K/V, buffer ops on, one warp). Triton's AMD codegen LLVM is llvm/llvm-project
6bc4aaf66. - Symptom: illegal memory access, deterministic on both GPUs. From the second loop iteration the K data
buffer_loads use the base address of a different buffer. - Cause: same as described here.
hoistSpillInsideBBhoistsSI_SPILL_S128_SAVEontoundef %x.sub2_sub3:sgpr_128 = lr-split COPY ..., and the full-width store overwritessub0_sub1of the loop-carried descriptor in the shared slot. - Reproduces with standalone
llc -O3 -mcpu=gfx1201at6bc4aaf66from the dumped IR.-verify-machineinstrsis clean.-sgpr-regalloc=basicavoids it. main(da201e071) does not trigger it for this kernel, with or without-greedy-compact-slot-indexes=false -split-spill-mode=speed.
Seeing the same miscompile on Radeon: gfx1100 (RX 7900 XTX) and gfx1201 (Radeon AI PRO R9700).
- Setup: a Triton 3.9 split-K attention kernel (fp8 K/V, buffer ops on, one warp). Triton's AMD codegen LLVM is llvm/llvm-project
6bc4aaf66.- Symptom: illegal memory access, deterministic on both GPUs. From the second loop iteration the K data
buffer_loads use the base address of a different buffer.- Cause: same as described here.
hoistSpillInsideBBhoistsSI_SPILL_S128_SAVEontoundef %x.sub2_sub3:sgpr_128 = lr-split COPY ..., and the full-width store overwritessub0_sub1of the loop-carried descriptor in the shared slot.- Reproduces with standalone
llc -O3 -mcpu=gfx1201at6bc4aaf66from the dumped IR.-verify-machineinstrsis clean.-sgpr-regalloc=basicavoids it.main(da201e071) does not trigger it for this kernel, with or without-greedy-compact-slot-indexes=false -split-spill-mode=speed.
Don‘t worry the PR is reviewed.