[AMDGPU][gfx950] Tied 8-VGPR boundary changes HSTU dR codegen versus ordinary SSA
On gfx950, adding a numerical-identity tied <8 x i32> "=v,0" boundary around an FP32 dR value changes LLVM O3 vectorization and the final code for a production HSTU backward kernel. A freshly collected balanced-order pass measures the resident form about 4% faster, although both variants use 256 VGPRs, occupancy 1, and no scratch.
This issue now asks whether ordinary SSA at this dR site can recover the same result, and which part of the boundary matters: vector shape, the tied VGPR constraint, inline-assembly opacity, or changed use-def/liveness structure.
Unresolved HSTU dR case
The only source-level toggle is:
dr = tlx.amd_register_resident(
dr,
register_class="vgpr",
registers_per_group=8,
)
The source is here. For every thread-local group of eight FP32 values, the lowering builds an <8 x i32> value, passes it through asm "", "=v,0", and unpacks it without changing the bits.
The operation changes all of those properties together. The current evidence does not isolate tuple grouping or coalescing as the cause.
“Ordinary SSA” is local to this dR site. The direct kernel still has 319 other inline-assembly calls, including 24 other tied <8 x i32> "=v,0" calls.
Where the variants diverge
Before LLVM O3, the resident module has 12 inlined dR wrappers. Each wrapper adds eight inserts, two bitcasts, one tied inline-assembly call, and eight extracts. Removing those wrappers and rewiring their 96 outputs makes the pre-O3 modules identical under llvm-diff.
The pre-O3 arithmetic is identical: 576 fadd, 1,416 fmul, and 96 fsub operations in each variant. In a standalone O3 replay with the bundled LLVM:
SLPVectorizerPassfirst chooses different scalar/vector arithmetic forms.VectorCombinePassfirst changes total arithmetic counts and forms four eight-wide FP32dRgroups in the resident variant.
The captured post-O3 IR already differs before standalone llc:
| LLVM IR opcode | Direct | Resident |
|---|---|---|
fadd |
480 | 468 |
fmul |
948 | 924 |
fsub |
48 | 36 |
shufflevector |
876 | 984 |
This is therefore a whole-pipeline observation, not an isolated AMDGPU scheduler or register-allocation reproducer.
Runtime and final assembly
Workload: BF16, B=512, H=4, max_seq_len=2048, D=128, 995,460 tokens, lower-triangular HSTU mask.
The balanced ABBA pass was collected after the fixed-AB pass in the same process.
| Method | Direct median | Resident median | Direct / resident |
|---|---|---|---|
| Fixed AB, 31 pairs | 16.5826 ms | 15.9530 ms | 1.0395x |
| Balanced ABBA, 32 pairs | 16.5907 ms | 15.9428 ms | 1.0406x |
These are cross-variant comparisons, not checks against an independent reference. At the full shape, dK and dV are storage-bit identical. dQ is finite with 60.99 dB SNR (direct output as the signal) and maximum absolute difference 1.0914e-11. A smaller (B,H,N,D)=(8,4,1024,128) control produces finite, storage-bit-identical dQ, dK, and dV.
| Metric | Direct | Resident |
|---|---|---|
| VGPR | 256 | 256 |
| AGPR | 126 | 128 |
| Total vector registers | 382 | 384 |
| Scratch bytes | 0 | 0 |
| Compiler-reported occupancy | 1 | 1 |
v_perm_b32 |
72 | 40 |
v_pk_mul_f32 |
374 | 410 |
| Static MFMA sites | 392 | 392 |
Other instructions also change, so these counts are correlation rather than a claim that any one code-generation difference causes the speedup.
Exact pre/post-O3 IR, bundled assembly, benchmark source, and raw timing/correctness results: https://gist.github.com/bangtianliu/46792c16bc698fe387a82d031255c318/cf17e6c8382dea0f6e6b4bc6a5adad3364eda0fa
Provenance:
Triton source: c54ae5685d9f3d8a881e3a5cbe46bb9587d3b880
Bundled LLVM: 850a2b1b975c061ae0fc982ba68064d305485cb2
Compile-only upstream LLVM: 2de38b4861ed4a7be7ddd42fc803e91d1fe81b14
GPU: AMD Instinct MI350X, gfx950
Driver / ROCm: 6.16.6 / 7.1.1
PyTorch / HIP: 2.13.0+rocm7.1 / 7.1.52802
Both post-O3 files compile with the pinned upstream LLVM revision. This report makes no upstream-runtime claim.
Questions
- Is ordinary SSA expected to reach the useful vectorization/codegen seen with this tied boundary?
- Because the first divergence appears in SLP/vector combining, should this be treated primarily as a generic vectorization problem, an AMDGPU lowering problem, or an interaction between both?
- What additional ablation or reduced IR would best separate vector grouping, register constraints, opacity, and liveness effects?
This report does not propose a new LLVM API or prescribe an implementation. Its goal is to identify what information is missing and which compiler stage should own it.
Resolved original BF16/AGPR source case
The issue originally asked whether a reused BF16 MFMA A/B source should be forced into AGPRs. The replies here and here explain that AMDGPU normally selects VGPR forms and rewrites to AGPRs later when register pressure makes that useful.
Removing only the hard BF16 =a preserve constraints from Triton PR #2709 retained correctness, occupancy 1, and zero scratch/spills. It measured 30.027 ms with normal allocation versus 30.182 ms with hard =a. The integer bitcast was also unnecessary for LLVM type legality.
That original case therefore does not motivate explicit AGPR residency. Its immutable reproducer remains at: https://gist.github.com/bangtianliu/a6719f44caefbe119be4f719e384c296/34be3cfc3038c24e363d5ee59280ff18ff39e3e6
@llvm/issue-subscribers-backend-amdgpu
Author: Bangtian Liu (bangtianliu)
Two otherwise matched controls pass the same 128 bits through one tied
=a,0 inline-assembly tuple. One uses native <8 x bfloat> and the other
round-trips through <4 x i32>. Both selectors allocate a[0:3] for A and B
in both controls, and the native and packed outputs are equivalent. The
integer bitcast is therefore not required for LLVM type legality.
The GlobalISel mapping for this intrinsic explicitly says:
// FIXME: Should we eventually add an alternative mapping with AGPR src
// for srcA/srcB?
OpdsMapping[2] = getVGPROpMapping(MI.getOperand(2).getReg(), MRI, *TRI);
OpdsMapping[3] = getVGPROpMapping(MI.getOperand(3).getReg(), MRI, *TRI);
These are
AMDGPURegisterBankInfo.cpp:5096-5111
at the frozen commit. Machine operands 2 and 3 are A and B. The instruction
definitions accept AVSrc_128, so AGPR and VGPR tuples are both legal.
SelectionDAG likewise accepts the AV operands, but these controls did not
reveal an automatic persistent-source decision.
Which existing AMDGPU analysis is expected to infer that a repeatedly reused MFMA A/B value should remain in AGPRs? Is the current intended behavior that ordinary IR remains VGPR-resident unless an explicit constraint or other producer already establishes AGPR residency?
The immutable reproducer bundle is: https://gist.github.com/bangtianliu/a6719f44caefbe119be4f719e384c296/7c8e7a731c65809a3c5dab0b2f5991fc7078df9d
The full-kernel context preserves 96 MFMAs and has two static rendezvous call sites associated with loop-carried uses. Packed-i32 and native-BF16 constrained forms are byte-identical. Removing the BF16 constraints changes liveness and scheduling and produces operand-specific A/B redistribution plus scratch/resource changes, so it is not a pure bank comparison.
| Selector | Form | A in AGPR | B in AGPR | Scratch bytes |
|---|---|---|---|---|
| SelectionDAG | packed/native | 36/96 | 32/96 | 8 |
| SelectionDAG | direct SSA | 48/96 | 30/96 | 0 |
| GlobalISel | packed/native | 22/96 | 36/96 | 492 |
| GlobalISel | direct SSA | 29/96 | 30/96 | 316 |
This report is limited to MFMA A/B source residency. It excludes F32 accumulator-bank allocation, hazard-padding correctness, and runtime-performance claims. I am not proposing an LLVM API or implementation here.
Using AGPRs is strictly worse than VGPRs; AGPRs are only to be used as a last resort. The current pipeline selects to VGPR forms of the instructions, and rewrites to AGPR later as necessary. The rewrite is implemented in 2 places. RewriteMFMAForm does some mid-schedule rewrites when the occupancy is sufficiently low. Additionally AMDGPURewriteAGPRCopyMFMA does post-RA rewrites and tries to fold out the associated copies and spills
The AGPR vs. VGPR problem is equivalent to RA split/spill - so the bulk of the problem is left to regular RA
Using AGPRs is strictly worse than VGPRs; AGPRs are only to be used as a last resort. The current pipeline selects to VGPR forms of the instructions, and rewrites to AGPR later as necessary. The rewrite is implemented in 2 places. RewriteMFMAForm does some mid-schedule rewrites when the occupancy is sufficiently low. Additionally AMDGPURewriteAGPRCopyMFMA does post-RA rewrites and tries to fold out the associated copies and spills
The original BF16/AGPR source-residency question is now resolved.
In the small reproducer, ordinary SSA places the reused <8 x bf16> MFMA source in VGPRs, while tied =a,0 assembly forces it into AGPRs. Native BF16 and <4 x i32> packed forms generate identical code, so the bitcast is not required for LLVM legality.
As explained above, LLVM normally starts with VGPR MFMA forms and later moves values to AGPRs when register pressure makes that profitable.
I also removed the sixteen BF16 =a preserve constraints from the D128 GQA kernel in Triton PR #2709, while retaining the F32 MFMA-completion boundaries. LLVM selected a mixed VGPR/AGPR allocation automatically. The direct-SSA kernel passed correctness, retained occupancy 1 with zero scratch/spills, and measured 30.027 ms versus 30.182 ms with hard AGPR residency.
Therefore, these sites do not require explicit AGPR residency or integer packing. This conclusion is specific to the original BF16 source case and does not imply that every register-handoff constraint can be removed.
Reproducer: https://gist.github.com/bangtianliu/a6719f44caefbe119be4f719e384c296/34be3cfc3038c24e363d5ee59280ff18ff39e3e6
I narrowed the report to the unresolved HSTU FP32 dR case and moved the original BF16/AGPR question into a resolved background section.
The updated reproducer now includes the exact pre-O3 pair, post-O3 IR, bundled assembly, benchmark source, and raw fixed-AB and balanced-ABBA results. The key new finding is that removing the 12 tied dR wrappers makes the pre-O3 modules identical, while the first arithmetic-form divergence appears in SLPVectorizerPass, before standalone llc.
The freshly collected balanced-order pass in the same process still measures about a 1.041x resident/direct improvement.
Reproducer: https://gist.github.com/bangtianliu/46792c16bc698fe387a82d031255c318/cf17e6c8382dea0f6e6b4bc6a5adad3364eda0fa
The inline asm appears to act as a hint to SLP/VectorCombine to produce more wide (8x) vector operations. Testing showed that it's the 8x wide vector ops that is making the difference, not the lower V_PERM count. Raising SLP's look-ahead depth from 2 to 3 closes this gap, but not sure if that is the right fix -- might meet resistance from adding extra compile time overhead. I'm currently looking into if @lialan 's change with cost neutral transform can address this with adding extra logic for VectorCombine to recognize commutatively equivalent sibling trees.
It seems that while AMDGPU doesn't have native <8 x float> instruction, having 8-lane wide vector makes downstream more conducive to more v_pk_*_f32 generation. From ATT:
Variant IR v2/v4/v8 fmul Median ATT time
━━━━━━━━━ ━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━ ━━━━━━━━━━━
v2 436 / 0 / 0 16.451 ms 16.139 ms
───────── ────────────────── ─────────── ───────────
v4 384 / 26 / 0 16.228 ms 15.948 ms
───────── ────────────────── ─────────── ───────────
v8 528 / 26 / 8 15.469 ms 15.295 ms
Thus:
- v4 is only 1.35% faster than v2.
- v8 is 4.68% faster than v4.
- v8 is 5.97% faster than v2.
The important static ISA changes are:
Instruction v2 v4 v8
━━━━━━━━━━━━━━━━━━━━ ━━━━━━━ ━━━━━━━ ━━━━━━━
Total instructions 5,874 5,869 5,434
──────────────────── ─────── ─────── ───────
v_pk_mul_f32 374 373 442
──────────────────── ─────── ─────── ───────
v_mul_f32_e32 224 224 0
──────────────────── ─────── ─────── ───────
v_pk_fma_f32 131 131 224
──────────────────── ─────── ─────── ───────
v_fmaak_f32 192 192 0
──────────────────── ─────── ─────── ───────
v_fmamk_f32 96 96 0
──────────────────── ─────── ─────── ───────
v_mov_b32_e32 314 314 245
──────────────────── ─────── ─────── ───────
s_nop 430 417 349
──────────────────── ─────── ─────── ───────
v_cvt_pk_bf16_f32 212 212 212
I tried to coax VectorCombine on top of @lialan 's change to generate more widening to v8 from narrow v2 trees, but it seems that the resulting change actually didn't increase v_pk_* count. Instead, it still improved the runtime numbers, but for entirely different reasons ie backend scheduling delta.
Increasing SLP look-ahead depth from 2 to 3 achieves similar effect, generating a lot more narrow v2 trees, thus a lot more v_pk_* instructions and removing scalar ops. This is controllable via -slp-max-look-ahead-depth=3. I'm not sure how this default value=2 was determined, but changing that might be harder task.
• All three were measured on gfx950 with the same full HSTU workload, seed, and balanced ABBA timing procedure.
### Profile comparison
Metric Baseline depth 2 SLP depth 3 Depth 3 + Alan Resident, baseline compiler
━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Runtime median 16.4420 ms 15.8581 ms 15.5094 ms 15.7669 ms
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Speedup vs baseline — 3.68% 6.01% 4.28%
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Dynamic instructions 2.826B 2.619B 2.409B 2.690B
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Dynamic VALU 1.813B 1.562B 1.562B 1.823B
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Dynamic MFMA 266.85M 266.85M 266.85M 266.85M
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Dynamic LDS 287.57M 287.57M 287.57M 287.57M
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Dynamic VMEM 58.55M 58.55M 58.55M 58.55M
────────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
Waves 131,072 131,072 131,072 131,072
### Static ISA
Opcode/count Baseline depth 2 SLP depth 3 Depth 3 + Alan Resident, baseline compiler
━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total instructions 5,874 5,609 5,434 5,895
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
v_pk_mul_f32 374 434 442 410
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
v_pk_fma_f32 131 254 224 125
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
v_mul_f32_e32 224 0 0 192
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
v_fmaak_f32 192 0 0 192
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
v_fmamk_f32 96 0 0 96
──────────────────── ────────────────── ───────────── ──────────────── ─────────────────────────────
s_nop 430 475 349 450
This makes the comparison more revealing:
- Resident is 4.28% faster than baseline despite executing only 4.8% fewer total instructions and slightly more VALU instructions.
- Depth 3 gets close to Resident by forming packed arithmetic.
- Depth 3 + Alan surpasses Resident by producing both packed arithmetic and a tighter schedule with many fewer dynamically executed filler instructions.
This still doesn't establish that there is direct benefit of widening to 8-lane vector since AMDGPU lacks native instructions for it.
Hi @choikwa, I think it would be good to understand why the more packed instructions are created with the 8 x float instructions vs the 2 x float versions? Shouldn't the backend generate the packed versions in either case?
Hi @choikwa, I think it would be good to understand why the more packed instructions are created with the 8 x float instructions vs the 2 x float versions? Shouldn't the backend generate the packed versions in either case?
It turns out the uses for those narrow v2 trees are asymmetric enough that 32 instances of them remain in scalar fmul. Attaching inline asm that forces it into v8 using shuffles causes this asymmetry to be repaired and allows for vector fmul. I.e.
%x0 = extractelement <2 x float> %x_half, 0
%u0 = fmul float %x0, %x0
; Later:
%tmp = insertelement <2 x float> ..., float %u0, 0
Meanwhile, another lane has already been transformed into:
%u2 = fmul <2 x float> %x_half2, %x_half2
%x1 = extractelement <2 x float> %u2, 1
Thus the conceptual v2 sibling trees are not represented symmetrically in IR. One lane is:
extract → scalar multiply → insert
while another is:
vector multiply → extract
VectorCombine has some facility to recognize this pattern of fmul (extract, extract) -> extract(fmul) but only recognizes immediate def-use, not more complex subtrees which this case had. I.e.
%r0 = extractelement <2 x float> %r, 0
%xhalf0 = fmul float %r0, 0.5
%u0 = fmul float %xhalf0, %xhalf0
foldExtractExtract only handles an immediate pattern such as:
%a = extractelement %va, i
%b = extractelement %vb, i
%x = fmul %a, %b
which it can turn into:
%vx = fmul %va, %vb
%x = extractelement %vx, i
But for %u0, its operands are %xhalf0, not extractelement instructions. VectorCombine would have to look through another arithmetic level and transform both operations together:
(extract r × 0.5)²
into:
extract ((r × splat(0.5))²)
Trying to see if greedy reordering v2's by lane and by operand helps -- initial results are showing that it does help construct more packed vector ops but still needs Alan's change to reproduce the performance gain of inline asm. Will still need to verify if the gains can be attributed to the change.
There appears to be some evidence to suggest that SLP chose to construct heterogeneous vectors from an external-use-score tie breaker. This ended up creating more scalar operations in the baseline case. Preferring to construct homogeneous vectors removes the need for creating scalar ops and gains the performance of the inline-asm case. I.e.
• The reduced HSTU case contains two equivalent chains:
%b1 = fmul %b0, 0.5
%b4 = fadd ..., 1.0
%b5 = fmul %b1, %b4
%a1 = fmul %a0, 0.5
%a4 = fadd ..., 1.0
%a5 = fmul %a1, %a4
When vectorizing %b5 and %a5, the desired operand pairing is:
[%b1, %a1] = [fmul, fmul]
[%b4, %a4] = [fadd, fadd]
Baseline instead chose the crossed pairing:
[%b4, %a1] = [fadd, fmul]
[%b1, %a4] = [fmul, fadd]
because its scores were:
[%b1, %a1] → 50
[%b4, %a1] → 51 // +1 external-use bonus
That single crossed choice misaligned the two chains. SLP then needed extracts/inserts and left scalar operations such as:
%lane = extractelement <2 x float> ...
%half = fmul float %lane, 0.5
%square = fmul float %half, %half
%zeroed = fmul float %square, 0.0
The fix makes immediate opcode compatibility break an otherwise equal recursive score. SLP therefore chooses the homogeneous pairing and produces:
%half = fmul <2 x float> ...
%square = fmul <2 x float> %half, %half
%zeroed = fmul <2 x float> %square, zeroinitializer
%plus_one = fadd <2 x float> %zeroed, splat (float 1.0)
%result = fmul <2 x float> %half, %plus_one
In short:
baseline: external-use bonus causes crossed fadd/fmul pairing
fix: equal trees are resolved as fmul/fmul and fadd/fadd
result: the entire two-lane chain remains vectorized
It looks like 6053d19 by @alexey-bataev fixes this already by making homogeneous structures with constants score higher.
### HSTU comparison (SLP depth 2)
Configuration Runtime vs. direct Scalar fmul after SLP <2 x float> fmul v_pk_*
━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━ ━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━ ━━━━━━━━
Direct baseline 16.243 ms — 464 484 591
───────────────────── ─────────── ────────────── ─────────────────────── ────────────────── ────────
Resident inline asm 15.893 ms 2.15% faster — — 645
───────────────────── ─────────── ────────────── ─────────────────────── ────────────────── ────────
Direct + 6053d19 15.804 ms 2.70% faster 0 708 735
6053d19 does not increase SLP look-ahead depth. It retunes look-ahead scores so same-opcode operations
and identical constants are distinguished from weaker constant/splat matches. In HSTU, this prevents an
external-use bonus from selecting crossed fadd/fmul pairs over homogeneous fmul/fmul pairs.
On the full captured HSTU module, 850a2b1 + 6053d19 produces byte-for-byte identical post-SLP IR to our
pairing fix. The resulting speedup is associated with more packed operations and fewer scalar VALU instructions:
- v_pk_*: 591 → 735
- v_mul_f32: 254 → 68
- Dynamic VALU instructions: −13.9%
- Total dynamic instructions: −6.7%
- VGPR/SGPR allocation: unchanged
Thus 6053d19 fixes the HSTU SLP pairing problem through its intended repeated-constant score retuning and
provides performance comparable to—and slightly better than—the resident workaround in this run.
@bangtianliu can this issue be closed? I believe above commit addresses this issue.
@bangtianliu can this issue be closed? I believe above commit addresses this issue.
Let me check it from my end
@alex-katranov @choikwa
Thanks for the fix. I confirmed that 6053d19eb3bc2391fcab76eb20a8ea0462568224 resolves the reported SLP vectorization problem. I validated it in TLX by rebuilding the SLP implementation with the fix backported onto our LLVM 850a2b1b pin and relinking the native extension. Fresh compilation confirmed that the patched compiler was being used.
With LLVM 850a2b1b plus that change, at look-ahead depth 2, the original direct reproducer goes from 464 scalar / 484 two-lane vector fmul operations after SLP to 0 / 708. The resident variant also reaches 0 / 708.
This issue can be closed.
During follow-up testing, I found a separate HSTU performance/code-generation difference involving later vector grouping and AMDGPU backend behavior. The current evidence does not indicate that the original SLP problem remains. I’ve opened #227456 with a standalone LLVM IR/MIR reproducer, the tested toolchain details, and the supporting measurements.