[AMDGPU] Pre-RA scheduling increases register pressure and reduces occupancy on gfx950
[WIP]
@llvm/issue-subscribers-backend-amdgpu
Author: Bangtian Liu (bangtianliu)
Some hand-scheduled AMDGPU kernels have a dataflow boundary where a value moves between software-pipeline phases. The value is unchanged, but the incoming and outgoing virtual-register intervals should be independently allocatable.
This does not reduce pointwise liveness. It gives register allocation an explicit split and recoloring point. The allocator may reuse the same physical register or choose another one when surrounding interference makes that useful. The outgoing interval may also need a hard VGPR or AGPR class constraint without changing allocation policy for the entire function.
We currently approximate this with an empty tied inline asm:
%in_vgpr = call float asm sideeffect "", "=v,0"(float %value)
%in_agpr = call float asm sideeffect "", "=a,0"(float %value)
This is incomplete: same-class operands are coalesced back into one virtual register, while sideeffect introduces unrelated scheduling and hazard effects.
Reproducer
This was reproduced for gfx950 with AMD LLVM 20.0.0git and 22.0.0git:
target triple = "amdgcn-amd-amdhsa"
declare i32 @<!-- -->llvm.amdgcn.workitem.id.x()
define amdgpu_kernel void @<!-- -->local_vgpr_placement(
ptr addrspace(1) %in, ptr addrspace(1) %out) #<!-- -->0 {
entry:
%tid = call i32 @<!-- -->llvm.amdgcn.workitem.id.x()
%in.ptr = getelementptr float, ptr addrspace(1) %in, i32 %tid
%out.ptr = getelementptr float, ptr addrspace(1) %out, i32 %tid
%x = load float, ptr addrspace(1) %in.ptr, align 4
%before = fmul float %x, 2.0
store volatile float %before, ptr addrspace(1) %out.ptr, align 4
%placed = call float asm sideeffect "", "=v,0"(float %x)
%after = fmul float %placed, 3.0
%out.next = getelementptr float, ptr addrspace(1) %out.ptr, i32 256
store volatile float %after, ptr addrspace(1) %out.next, align 4
ret void
}
attributes #<!-- -->0 = { "target-cpu"="gfx950" }
llc -mcpu=gfx950 -stop-after=finalize-isel handoff.ll -o -
llc -mcpu=gfx950 -stop-after=register-coalescer handoff.ll -o -
llc -mcpu=gfx950 handoff.ll -o -
At finalize-isel, the input and output are distinct:
%input:vgpr_32 = GLOBAL_LOAD_DWORD_SADDR ...
%copy:vgpr_32 = COPY %input
INLINEASM &"", sideeffect, regdef:VGPR_32, def %placed,
reguse tiedto:$0, %copy(tied-def 3)
After register-coalescer, they are one virtual register:
%value:vgpr_32 = GLOBAL_LOAD_DWORD_SADDR ...
INLINEASM &"", sideeffect, regdef:VGPR_32, def %value,
reguse tiedto:$0, %value(tied-def 3)
Thus a tied operand does not preserve a same-class allocation boundary.
Changing the constraint to =a,0 does preserve a local VGPR-to-AGPR transition, but may require v_accvgpr_write_b32 and v_accvgpr_read_b32 around users that require VGPRs.
The side-effecting empty asm also produces an unrelated hazard no-op:
global_store_dword ...
;;#ASMSTART
;;#ASMEND
s_nop 0
v_mul_f32_e32 ...
Removing sideeffect and adding memory(none) removes the s_nop, but LLVM can move the asm away from its source position. The operation remains opaque and the same-class operands are still coalesced.
Requested behavior
Could AMDGPU provide a first-class operation that:
- Returns its input unchanged.
- Creates distinct incoming and outgoing virtual-register intervals that survive register coalescing.
- Optionally imposes a hard VGPR or AGPR class on the outgoing interval.
- Keeps the request local rather than changing function-wide allocation policy.
- Allows compatible intervals to receive the same physical register, but permits different assignments when useful.
- Emits a physical copy only when the assignments require one.
- Has no memory effects and is not treated as opaque inline assembly by AMDGPU hazard recognition or scheduling.
- Preserves distinct handoff occurrences until register allocation without becoming a general memory barrier.
Existing controls
amdgpu-agpr-alloccontrols the function-wide AGPR budget; it cannot express a local class request in a kernel that intentionally uses both VGPR and AGPR regions.- AV register classes allow automatic VGPR/AGPR selection, but do not express a source-selected split or hard class for only the outgoing interval.
AMDGPURewriteAGPRCopyMFMAoperates after register allocation on eligible MFMA chains. It cannot create a pre-RA split or constrain an arbitrary value.
Possible backend direction
One option is an AMDGPU intrinsic with an immediate destination-class operand:
%next = call float @<!-- -->llvm.amdgcn.regalloc.handoff(float %value, i32 0)
; 0 = unconstrained, 1 = VGPR, 2 = AGPR (illustrative only)
It could lower to a target pseudo with separate, untied source and destination operands. The pseudo would not be reported as a copy, would carry no memory effects, and would remain present through register allocation. A register-allocation hint could prefer the source physical register when classes are compatible. Post-RA expansion would erase the pseudo when both operands receive the same physical register and emit the required copy otherwise.
STRICT_WWM is precedent only for retaining a pseudo until post-RA expansion. Its @<!-- -->earlyclobber constraint intentionally prevents source/destination reuse and therefore must not be copied here.
The main design question is how to preserve the IR occurrence until codegen without introducing generic side effects or an unrelated scheduling barrier.
Tests
- SelectionDAG and GlobalISel tests for each destination-class mode.
- A register-coalescer MIR test proving that the intervals remain distinct.
- Same-class tests showing no final instruction when one physical register is selected.
- VGPR-to-AGPR and AGPR-to-VGPR tests showing only the required transition.
- Scheduling tests showing no memory dependency or inline-asm hazard behavior.
Granularity
The original workaround grouped adjacent 32-bit values into one asm tuple. Three A/B benchmark sweeps found no measurable aggregate difference between grouped tuples and independent native-register handoffs (0.9993x). A single native 32-bit register per handoff is sufficient; no grouping interface is requested.
FYI @qcolombet
Hi @bangtianliu,
Yeah the tie-operand is not what you want here. I'm not sure I fully understand what you're trying to achieve. Are you trying to pin specific values to AGPR or VGPR register class?
Could you share the original motivating example and highlight what goes wrong with it? In other words, share an example where the produce asm departs from what you were expecting without using the tie-operand.
Here you're suggesting a solution, but I'd rather understand the problem to see what solution would make sense.
Hi @bangtianliu,
Yeah the tie-operand is not what you want here. I'm not sure I fully understand what you're trying to achieve. Are you trying to pin specific values to AGPR or VGPR register class?
Could you share the original motivating example and highlight what goes wrong with it? In other words, share an example where the produce asm departs from what you were expecting without using the tie-operand.
Here you're suggesting a solution, but I'd rather understand the problem to see what solution would make sense.
This issue originated from a review discussion on Triton PR #2843, where we were considering whether LLVM could replace or generalize the empty-inline-asm workaround. In my response there, and subsequently in this issue, I described the requirement as an allocator handoff.
After rechecking the motivating kernel, that description was too strong: the concrete case does not require a non-coalescible live-range split. It requires a local VGPR-class constraint on one value before its MFMA consumer. The tied operand is only part of the current empty-inline-asm encoding of that constraint.
The case is the gfx950 D64 causal GQA backward-attention kernel from Triton PR #2843. The relevant dataflow is:
P = exp2(scores)
dP = dO @ V^T + delta
dS = P * dP
dS = constrain_to_vgpr(dS) // current IR uses asm sideeffect "", "=v,0"
dK += dS^T @ Q
The producer and constraint feed this dK MFMA.
I ran a backend-only A/B with the same generated LLVM IR and the same llc for gfx950. In the second variant I removed each empty tied asm and replaced its result uses with the input SSA value. At finalize-isel, the constrained fragments are in VGPR_32; without the constraint they remain eligible for AV_32. The final resource usage is:
| Variant | VGPRs | AGPRs | Reported total vector registers | Occupancy |
|---|---|---|---|---|
Empty asm "=v,0" |
240 | 0 | 240 | 2 |
| Direct SSA | 254 | 4 | 260 | 1 |
Both variants contain 768 MFMAs and have zero spills and zero scratch allocation. On (B,Hq,Hkv,SQ,SKV,D)=(4,48,6,4096,4096,64), the isolated dK/dV kernel changes from about 0.941 ms to 1.087 ms when the constraint is removed, a 15.5% regression. Thus the observed departure is the allocator's class/assignment decision crossing an occupancy boundary, not a missing machine copy.
For the current workaround, =v provides the local VGPR constraint and matching constraint 0 makes the empty asm value-preserving. Coalescing the input and output is acceptable; I do not need the tie to create independently allocatable same-class intervals.
with-inline-asm.ll.txt direct-ssa.ll.txt
From an LLVM checkout at 77a6638e4f61e1827001cce202d8f785815a341b, the following commands reproduce the result (LLVM_BUILD_DIR is that checkout's build directory):
LLVM_BUILD_DIR=/path/to/llvm-project/build
"${LLVM_BUILD_DIR}/bin/llc" \
-mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -filetype=asm \
with-inline-asm.ll -o with-inline-asm.s
"${LLVM_BUILD_DIR}/bin/llc" \
-mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -filetype=asm \
direct-ssa.ll -o direct-ssa.s
grep -H -E '^; (NumVgprs|NumAgprs|TotalNumVgprs|ScratchSize|Occupancy):' \
with-inline-asm.s direct-ssa.s
| Variant | VGPR | AGPR | Total | Occupancy |
|---|---|---|---|---|
| Inline asm | 240 | 0 | 240 | 2 |
| Direct SSA | 254 | 4 | 260 | 1 |
The constrained dS values are already VGPR_32 without the asm. The four AGPRs instead hold unrelated do_nd LDS loads feeding the dV MFMAs. The tied asm operands are also coalesced to the same vreg.
The scheduler-disabled control is:
"${LLVM_BUILD_DIR}/bin/llc" \
-mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -enable-misched=false \
-filetype=asm with-inline-asm.ll -o with-inline-asm-no-misched.s
"${LLVM_BUILD_DIR}/bin/llc" \
-mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -enable-misched=false \
-filetype=asm direct-ssa.ll -o direct-ssa-no-misched.s
grep -H -E '^; (NumVgprs|NumAgprs|TotalNumVgprs|ScratchSize|Occupancy):' \
with-inline-asm-no-misched.s direct-ssa-no-misched.s
Both control variants use 234 VGPRs, 0 AGPRs, and occupancy 2. This localizes the difference to pre-RA scheduling/register-pressure decisions rather than a missing IR-level VGPR constraint or handoff semantic.
One correction: the previously quoted 0.941/1.087 ms timings came from a source-level toggle, not these exact backend-only variants. They demonstrate the end-to-end effect, but not a backend-only 15.5% result.
Hi @bangtianliu , thanks for that example. I'd like to take a deeper look at this to understand why the compiler ends up at occupancy 1, but occupancy 2 with the inline asm. I think the compiler can be improved here.
Hi @bangtianliu , thanks for that example. I'd like to take a deeper look at this to understand why the compiler ends up at occupancy 1, but occupancy 2 with the inline asm. I think the compiler can be improved here.
Yes, I am asking the agent to prepare the LLVM-specific part.
From my understanding: Triton needs to express an explicit semantic task handoff, while LLVM implements it as a register-allocation split.
so:
- the task handoff needs to be preserved through LLVm until regalloc.
- this will have to be an interface change, we do not want to expose regalloc knobs.
- but we still need to expose some internals, like agpr or vgpr to handoff.
@bangtianliu is my understanding right here?
Hi @bangtianliu , thanks for that example. I'd like to take a deeper look at this to understand why the compiler ends up at occupancy 1, but occupancy 2 with the inline asm. I think the compiler can be improved here.
Yes, I am asking the agent to prepare the LLVM-specific part.
Thanks - much appreciated.
Thanks, @qcolombet @bcahoon and @lialan, I updated repro here: https://github.com/llvm/llvm-project/issues/219377#issuecomment-5510810499
I see, the original approach to use intrinsic is probably not the way to go. The right fix likely belongs inside the backend—likely occupancy-aware flexible VGPR/AGPR assignment.
A targeted fix is in #220975. It conservatively avoids pending candidates near unified VGPR/AGPR occupancy boundaries. Feedback is welcome.
Thanks for checking this. I reproduced the original result at llvm-project commit 27bb4d2432d9e5643c7f4f5df495c1580fe07da8: direct-ssa.ll with -amdgpu-sched-strategy=coexec uses 255 VGPRs, 0 AGPRs, and reports occupancy 2. Thus CoExec resolves the occupancy cliff in that specific reproducer.
I then generated three additional backend-only A/B pairs from current TLX D64 causal GQA8 dK/dV producer specializations:
https://gist.github.com/bangtianliu/a01e4ce5722b2b8d77498775b6670d2e
For every pair, the direct-ssa input was derived mechanically from the same with-handoff LLVM IR by deleting only the FP32 empty tied-VGPR calls (asm sideeffect "", "=v,0") and replacing their result uses with the input SSA values. The README contains the exact commands and all six .ll files.
Using the same LLVM commit and CoExec:
| Specialization | Variant | VGPR | AGPR | Total vector registers | Occupancy |
|---|---|---|---|---|---|
| Shallow/interleaved D32 | With handoff | 237 | 0 | 237 | 2 |
| Direct SSA | 255 | 8 | 264 | 1 | |
| Direct D64 | With handoff | 225 | 0 | 225 | 2 |
| Direct SSA | 253 | 4 | 260 | 1 | |
| Batched/XCD | With handoff | 235 | 0 | 235 | 2 |
| Direct SSA | 255 | 4 | 260 | 1 |
All six variants report zero scratch allocation. These are compiler-only resource reproducers; I have not attached runtime claims to them.
CoExec fixes the original repro but not these current GQA cases.
I think the main issue here is that the register pressure tracker in the scheduler is underestimating register pressure. The number of vgprs needed is very close to the occupancy drop boundary. Using the AMDGPU specific register pressure tracker helps, -amdgpu-use-amdgpu-trackers=true, but occupancy still drops in direct-ssa.ll. It requires another option to help, -amdgpu-vgpr-threshold-percent=90. We've been working to enable the AMDGPU register pressure by default, but that is still a work in progress. The question remains if it possible to improve the accuracy of the register pressure.
I'm guessing the inline asm helps because it acts like a scheduling barrier and reduces overall register pressure. Replacing the inline asm with __builtin_amdgcn_sched_barrier provides a similar benefit.
Using the AMDGPU specific register pressure tracker helps,
-amdgpu-use-amdgpu-trackers=true, but occupancy still drops indirect-ssa.ll. It requires another option to help,-amdgpu-vgpr-threshold-percent=90.Replacing the inline asm with
__builtin_amdgcn_sched_barrierprovides a similar benefit.
I think we should consider these approaches over https://github.com/llvm/llvm-project/pull/220975 -- this PR indirectly solves the issue here and seems unreliable.
Aside from Brendon's recommendation, another option is to look into the coexec scheduler. The workload here looks to be some compute dense ML operator -- which is what the scheduler is optimized for. It is true the coexec scheduler tries to optimize for instruction interleaving, but it also has some advanced RP controls, including using the amdgpu trackers. So the downside is a mismatch between usecase and optimization strategy, but the upside is advanced RP handling. If we use amdgpu-waves-per-eu=2,2 then hitting occupancy 2 will become a top priority and the optimization strategy more aligns with the usecase.
When going through Triton, I believe we now have access to the full featured coexec scheduler, whereas when going through upstream, we have a limited in-progress version .
The results for direct-ssa are interesting
| run | Occupancy | Spill |
|---|---|---|
| full featured coexec + waves-per-eu: | Occupancy: 2 | (vgpr spill 1, sgpr spill 5) |
| full featured coexec: | Occupancy: 2 | (vgpr spill 0, sgpr spill 5) |
| limited featured coexec + waves-per-eu: | Occupancy: 2 | (vgpr spill 0, sgpr spill 7) |
| limited featured coexec: | Occupancy: 2 | (vgpr spill 0, sgpr spill 7) |
The 1 spill is unfortunate. Note, when using --greedy-regclass-priority-trumps-globalness=1 the 1 spill goes away for the full featured coexec + waves-per-eu
Using the AMDGPU specific register pressure tracker helps,
-amdgpu-use-amdgpu-trackers=true, but occupancy still drops indirect-ssa.ll. It requires another option to help,-amdgpu-vgpr-threshold-percent=90.Replacing the inline asm with__builtin_amdgcn_sched_barrierprovides a similar benefit.I think we should consider these approaches over #220975 -- this PR indirectly solves the issue here and seems unreliable.
This is cleaner interface, and I do think this is a good way to change.
However I tested with the reduced MIR test and it does not work for ihat.
Also the 90% guard is such a wide guard that I believe is much more intrusive. We should find a middle ground if possible.
This is cleaner interface, and I do think this is a good way to change.
However I tested with the reduced MIR test and it does not work for ihat.
Also the 90% guard is such a wide guard that I believe is much more intrusive. We should find a middle ground if possible.
When using upstream compiler or using the llvm branch Triton uses https://github.com/ROCm/llvm-project/tree/CoExecScheduler - I see that when using "amdgpu-waves-per-eu"="2,2" "amdgpu-agpr-alloc"="0, 0" attributes on the direct-ssa.ll , we achieve the occupancy 2 and no spills - with or without the amdgpu tracker. This is without the 90% guard.
The waves-per-eu control is specifically designed to tell the scheduler + RA to try harder to hit occupancy 2, and the amdgpu-agpr-alloc is usually coupled with this to avoid bad RA resulting from mixing AGPR + VGPR. For this kernel, I suggest we use this to address the issue, along with the amdgpu trackers. It is interesting that we no longer need the amdgpu-vgpr-threshold-percent to achieve this result - possibly it is worth looking into the history to see what influenced this -- to get a sense of how stable this approach will be going forward. If anything, these fluctuations make me less inclined to rely on the indirect result of the PR. I suspect that the inline asm is causing instability, since many places in CodeGen do not handle it well.
Since the MIR test is a bit contrived with the inline asm usage, I think that we should be designing more around the intent of the pending queue, rather than for the result of this specific test.
The investigation bisects to commit: https://github.com/llvm/llvm-project/commit/6286f77214be257eed1cff70d3e2e16f024e3dfb where earlier LiveIntervals computation changes allocation order, avoiding four AGPRs and restoring two-wave register occupancy.