Issue 242766
Summary [AMDGPU] Unreferenced SGPR spill slot gives a kernel a private segment with no scratch access
Labels new issue
Assignees
Reporter chelnnexy
    **Summary**

At the 256-VGPR limit, the greedy allocator spills an SGPR that holds a kernel argument (an `s_load_dwordx4` of the kernarg segment) and then rematerializes every use, so no spill or reload is left. The 16 B spill slot stays in the frame.

- `SILowerSGPRSpills` frees only the slots it moved into VGPR lanes (`removeDeadFrameIndices` walks `SGPRSpillsToVirtualVGPRLanes` / `SGPRSpillsToPhysicalVGPRLanes`), and this slot has no spill instruction left, so it is never freed.
- `SIFrameLowering::processFunctionBeforeFrameFinalized` then moves it back to the default stack, sees a live stack object, and adds the 4 B emergency scavenging slot.

The kernel ends up with `.amdhsa_private_segment_fixed_size 20` and no scratch instruction. Persistent kernels that need every workgroup resident refuse a non-zero private segment, so this kernel is refused although it never touches scratch.

**Reproduce** (`k2_orphan_sgpr_spill.ll`, a FlyDSL-generated gfx950 kernel, and `repro.sh`, which runs all three checks: https://gist.github.com/chelnnexy/78aa6695a90de940cf47383b727419ac)

```
clang-23 -x ir --target=amdgcn-amd-amdhsa -mcpu=gfx950 -nogpulib -O3 -Xclang -disable-llvm-optzns -S k2_orphan_sgpr_spill.ll -o k2.s
grep amdhsa_private_segment_fixed_size k2.s     # 20
grep -cE 'scratch_(load|store)|buffer_(load|store).*s\[0:3\]' k2.s   # 0
```

With `-mllvm -stop-after=greedy` the function has 29 stack objects; `%stack.1` (spill-slot, 16 B) is not referenced by any instruction.

**Expected:** no private segment (or one only when an instruction accesses scratch).
**Actual:** 20 B (16 B unreferenced spill slot + 4 B emergency slot), 0 scratch instructions.

**Versions**

| compiler | result |
|---|---|
| clang 23.1.6 (apt.llvm.org, `c47d7823685a`) | 20 B, 0 scratch instructions, unreferenced 16 B slot |
| AMD clang 23.0.0git (ROCm/llvm-project `8f497e0992fb`) | same |
| clang 24.0.0 trunk (`96c8c412b2aa`, 2026-11-14) | does not reproduce with this IR: 0 B, every stack object referenced |

FlyDSL pins upstream `941a04e69ee8` (2026-08-09) and hits this in its builds, so it was on trunk then. Between that and `96c8c412b2aa`, `SILowerSGPRSpills.cpp` changed only in #234514 (command line options), and the two code paths above read the same on trunk, so I think trunk's different codegen avoids the situation for this IR rather than fixing it. I could not build a trunk reproducer: MIR from 23.x after greedy doesn't round-trip faithfully. Happy to test a patch.

**Notes**

- Depends on the schedule: `-mllvm -amdgpu-sched-strategy=iterative-ilp` or `iterative-minreg` avoid it for most of our kernels; we use that as a fallback today ([ROCm/ATOM#3006](https://github.com/ROCm/ATOM/pull/3006)).
- `-amdgpu-prealloc-sgpr-spill-vgprs`, `-no-stack-slot-sharing`, `-no-stack-coloring` and `-disable-ssc` don't change it.
- The IR is not reduced (sorry for the size); the `target datalayout` line is removed so trunk accepts it (trunk's AMDGPU datalayout changed; the IR uses only address spaces 1, 3 and 8).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to