llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AMDGPU][gfx950] CoExec does not consider persistent 32x32 MFMA chains for AGPR allocation

Open bangtianliu opened this issue 2 months ago • 6 comments

Summary

On upstream llvm-project/main at 66462afa7e4a33f0d720877f2228589d5b43de81, CoExec does not consider any of the 32 32x32x16 MFMA instructions containing the persistent accumulator live ranges for its pre-RA VGPR-to-AGPR rewrite. A later post-RA pass converts 10 of the 32 instructions, but the final intrinsic lowering uses 204 B of scratch versus 68 B for a contextual mixed AGPR/VGPR constrained lowering.

This is a static code-generation report. It does not claim that the constrained result is globally optimal, that bank selection is the only source of the difference, or that a particular LLVM implementation is required.

Reproducer bundle: upstream-main revision 6321467dee2f8740df78eb1777bc91f9513bb62c

Input

  • Target: gfx950
  • Kernel: BF16 backward attention, B=1, Hq=8, Hkv=1, N=16384, D=128, non-causal
  • Launch: BLOCK_M=16, BLOCK_N=256, four warps
  • Primary input: persistent-mfma.ll, containing 96 16x16x32 and 32 32x32x16 LLVM MFMA intrinsic calls
  • Contextual baseline: constrained-baseline.ll, in which 24 persistent 32x32x16 updates use inline assembly with 16 VGPR and eight AGPR destination constraints

Both inputs retain the same 26 empty inline-assembly handoff nodes elsewhere in the kernel: 20 side-effecting nodes and six pure nodes. The changed sites are the 24 persistent MFMA updates.

Inline assembly versus direct SSA

The constrained LLVM IR makes the destination bank and fixed-delay padding explicit:

; VGPR form
%out = call <16 x float> asm sideeffect
  "s_nop 3\0Av_mfma_f32_32x32x16_bf16 $0, $1, $2, $0",
  "=&v,v,v,0"(<8 x bfloat> %a, <8 x bfloat> %b, <16 x float> %acc)
%bits = bitcast <16 x float> %out to <16 x i32>
%padded = call <16 x i32> asm sideeffect
  "s_nop 15\0As_nop 3", "=v,0"(<16 x i32> %bits)

; AGPR form uses "=a,v,v,0" and matching "=a,0" post-MFMA padding.

The corresponding direct-SSA LLVM IR keeps the MFMA visible to LLVM but carries no accumulator-bank constraint:

%out = call <16 x float>
  @llvm.amdgcn.mfma.f32.32x32x16.bf16(
      <8 x bfloat> %a, <8 x bfloat> %b, <16 x float> %acc,
      i32 0, i32 0, i32 0)

The examples are matched by debug location (!110 for VGPR and !168 for AGPR); SSA numbers differ because the files were captured independently. The direct form preserves the numerical SSA dependency and exposes the real MFMA opcode to scheduling and hazard handling. The constrained form communicates VGPR or AGPR, but hides the MFMA in a side-effecting INLINEASM node. It also adds early-clobber at the VGPR sites and a tied post-MFMA use/def that keeps the selected bank live through fixed-delay padding. Therefore this is not a pure register-class A/B. The bundle contains the matched full inputs and a detailed paired IR comparison. The s_nop sequences are static delays, not scoreboard waits or synchronization operations.

Final code generation

Both CoExec compilations pass -verify-machineinstrs and retain occupancy 1. All 96 16x16x32 MFMAs have VGPR destinations in both outputs.

Input 32x32x16 AGPR/VGPR destinations AGPR reads/writes Scratch loads/stores ScratchSize
Direct SSA MFMA intrinsics 10 / 22 138 / 128 26 / 26 204 B
Mixed constrained lowering 15 / 17 152 / 156 12 / 12 68 B

Where the upstream automatic path stops

Immediately before machine scheduling, instruction selection has selected all 128 MFMAs in VGPR-destination form. Auditing the unmodified MIR against RewriteMFMAFormStage::isRewriteCandidate gives:

MFMA form Pre-RA instructions Eligible candidates Candidate components
16x16x32 96 76 4 x 7 and 16 x 3
32x32x16 32 0 none

All 32 wide MFMAs are rejected directly by the current destination-use check:

for (const MachineInstr &UseMI : DAG.MRI.use_nodbg_instructions(DstReg)) {
  if (!TII->isMAI(UseMI) && !UseMI.isCopy())
    return false;
}

Of the 32 wide 32x32x16 MFMAs, 16 are first rejected by V_CVT_PK_BF16_F32_e64 and the other 16 by V_PK_MUL_F32. Thus the 24 persistent calls under study never enter the pre-RA bank-rewrite candidate set.

The 76 eligible candidates are unrelated 16x16x32 path prefixes. Diagnostic-only logging of upstream's unchanged cost calculation reports -4096 spill benefit, 95040 CopyForUse cost, and total cost 90944, so that complete small-MFMA rewrite is rejected. This cost is not a decision about the excluded wide MFMAs.

Immediately before AMDGPURewriteAGPRCopyMFMA, all 32 wide MFMAs still use VGPR form. That post-RA pass rewrites 10 to AGPR form; 22 remain VGPR. The final direct-SSA allocation therefore comes from opportunistic post-RA copy folding rather than a pre-RA mixed-bank decision for the persistent accumulator families.

Reproduction

BUNDLE=/path/to/reproducer-bundle
LLC=/path/to/llvm-main-build/bin/llc
COMMON=(
  -mtriple=amdgpu9.50-amd-amdhsa
  -mcpu=gfx950
  -verify-machineinstrs
  -amdgpu-sched-strategy=coexec
)

"$LLC" "${COMMON[@]}" "$BUNDLE/persistent-mfma.ll" -o direct-ssa.s
"$LLC" "${COMMON[@]}" "$BUNDLE/constrained-baseline.ll" -o constrained.s
python3 "$BUNDLE/summarize.py" direct-ssa.s constrained.s

The bundle also includes an optional LLVM_DEBUG-only patch, its filtered output, exact hashes, and commands. The diagnostic build produces byte-for-byte identical assembly to the clean upstream build.

Questions

  1. Is the exclusion of these loop-carried 32x32x16 accumulators expected when their virtual registers also feed ordinary vector consumers?
  2. Which current upstream pass is intended to choose a mixed AGPR/VGPR allocation for such persistent MFMA accumulator families?
  3. Is AMDGPURewriteAGPRCopyMFMA expected to recover this case after allocation, or is the absence of a pre-RA candidate the relevant limitation?

Scope

No runtime-performance or numerical-correctness claim is made. The constrained lowering is only evidence that a lower-scratch mixed-bank result exists in this kernel context; its opaque MFMA assembly, fixed-delay padding, extra tied use/defs, early-clobber constraints, and scheduling effects are not proposed as the desired compiler representation.

bangtianliu avatar Sep 08 '26 22:09 bangtianliu

@llvm/issue-subscribers-backend-amdgpu

Author: Bangtian Liu (bangtianliu)

## Summary

On a high-pressure gfx950 kernel with several persistent MFMA accumulator chains, the profitability model in the current version of #217328 selects zero chains for VGPR-to-AGPR conversion.

A diagnostic-only forced-prefix sweep shows that converting any of the first one through four chains reduces the final private segment from 204 bytes to 132 bytes and reduces scratch loads/stores from 26/26 to 16/16. The automatic search directly evaluates prefixes one and two, assigns them positive costs of 288 and 576, and rejects them.

This report is narrowly about the disagreement between the pre-RA rewrite cost and the final register-allocation result. It is not a request to force all MFMAs into AGPRs or expose a source-level register-allocation control.

Reproducer bundle: revision 77ab5a5c085756a0851dcb3d80958c42bb089c1a

Versions and input

  • Target: gfx950
  • LLVM main: 27bb4d2432d9e5643c7f4f5df495c1580fe07da8
  • PR #217328 head tested: 8fa20d63fe0bf74327faa034a56f6716d472aa0e
  • Input: persistent-mfma.ll from a BF16 backward-attention specialization
  • Kernel shape: B=1, Hq=8, Hkv=1, N=16384, D=128, non-causal
  • Launch configuration: BLOCK_M=16, BLOCK_N=256, four warps

The input contains 128 compiler-visible MFMAs and several simultaneously live, loop-carried accumulator chains. Full LLVM IR is attached because smaller low-pressure controls do not activate the pressure-driven rewrite decision.

Results

The current-main and unmodified-PR CoExec outputs are identical:

Compiler/mode Selected pre-RA prefix Final AGPR/VGPR MFMA destinations Private segment Scratch loads/stores
LLVM main, CoExec n/a 10 / 118 204 B 26 / 26
PR #217328, automatic CoExec 0 10 / 118 204 B 26 / 26
PR #217328, forced prefix 1 21 / 107 132 B 16 / 16
PR #217328, forced prefix 2 28 / 100 132 B 16 / 16
PR #217328, forced prefix 3 35 / 93 132 B 16 / 16
PR #217328, forced prefix 4 42 / 86 132 B 16 / 16
PR #217328, forced prefix 5 42 / 86 260 B 32 / 32

All variants complete with -verify-machineinstrs; occupancy remains one.

Rewrite decision

The PR's rewrite stage does enter and identifies 20 chains:

7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Its automatic probes report:

RewriteMFMA probe: N=20 Cost=90944
RewriteMFMA probe: N=10 Cost=70208
RewriteMFMA probe: N=5  Cost=28224
RewriteMFMA probe: N=2  Cost=576
RewriteMFMA probe: N=1  Cost=288
RewriteMFMA: best N=0

Thus this is not only a case where the binary search skips a beneficial prefix: N=1 and N=2 are directly evaluated, classified as unprofitable, and produce less final spilling when forced.

Additional diagnostic logging breaks down those two costs:

Prefix Estimated spill benefit CopyForDef CopyForUse Total
N=1 0 0 288 288
N=2 0 0 576 576

For both prefixes, every pressure-sensitive region has identical getVGPRSpills values before and after the speculative rewrite. The positive cost comes entirely from one or two CopyForUse registers in block 3, each with raw copy cost 9 scaled by the block frequency of 32.

This narrows the disagreement: chain detection succeeds, but the pre-RA regional spill estimate predicts no relief, whereas forced N=1 reduces the final private segment by 72 bytes and removes ten scratch loads and ten scratch stores.

The result is also non-monotonic. Prefixes one through four have the same 132-byte private segment, while prefix five increases it to 260 bytes. Converting every chain, or matching only a desired final AGPR/VGPR instruction count, is therefore not sufficient.

Reproduction

The bundle includes the exact PR diff and diagnostic-only force-prefix patch used for this test. With the force option unset, the diagnostic patch does not change the PR's selected prefix.

BUNDLE=/path/to/reproducer-bundle
MAIN_LLC=/path/to/llvm-main-build/bin/llc
PR_LLC=/path/to/pr-217328-build/bin/llc
REPRO="$BUNDLE/persistent-mfma.ll"

COMMON=(
  -mtriple=amdgpu9.50-amd-amdhsa
  -mcpu=gfx950
  -verify-machineinstrs
)

"$MAIN_LLC" "${COMMON[@]}" \
  -amdgpu-sched-strategy=coexec \
  "$REPRO" -o main-coexec.s

"$PR_LLC" "${COMMON[@]}" \
  -amdgpu-sched-strategy=coexec \
  "$REPRO" -o pr-auto.s

for n in 0 1 2 3 4 5; do
  "$PR_LLC" "${COMMON[@]}" \
    -amdgpu-sched-strategy=coexec \
    -amdgpu-force-rewrite-mfma-chain-count="$n" \
    "$REPRO" -o "pr-prefix-${n}.s"
done

python3 "$BUNDLE/summarize.py" \
  main-coexec.s pr-auto.s pr-prefix-{0..5}.s

To capture the automatic cost decisions with an assertions-enabled build:

"$PR_LLC" "${COMMON[@]}" \
  -amdgpu-sched-strategy=coexec \
  -debug-only=machine-scheduler \
  "$REPRO" -o /dev/null 2&gt;pr-auto.debug.txt

grep -E 'RewriteMFMA chains|RewriteMFMA probe:|RewriteMFMA: best' \
  pr-auto.debug.txt

Questions

  1. Why does the regional getVGPRSpills estimate remain unchanged for N=1 and N=2 when both reduce the final scratch allocation?
  2. Is that estimate expected to model downstream greedy-RA behavior such as tuple fragmentation and live-range interactions, or is a different signal intended for this decision?
  3. Is #217328 expected to cover this kernel, or should this be treated as a follow-up limitation of its profitability model?
  4. Which MIR stage or pressure dump would best expose where the estimated and final allocations diverge?

Scope

No runtime-performance claim is made, and the forced prefixes are not claimed to be globally optimal. The demonstrated result is only that automatic N=0 has more final scratch allocation and traffic than N=1 and N=2, two prefixes that the same cost model directly evaluates and rejects.

Correction: I replaced the issue body and reproducer with an upstream-main-only analysis. The earlier bot snapshot above, including the PR #217328 and forced-prefix discussion, is superseded. The current report compares matched inline-asm and direct-SSA IR and identifies where upstream CoExec excludes the persistent 32x32x16 accumulator chains.

bangtianliu avatar Sep 08 '26 23:09 bangtianliu

Adding @romanovvlad and @kurapov-peter for visibility.

bcahoon avatar Sep 09 '26 01:09 bcahoon

I presume that #209204 was aiming to address this issue

AlexeySachkov avatar Sep 09 '26 07:09 AlexeySachkov

I presume that #209204 was aiming to address this issue

Right, but enabling that uncovers other issues in RewriteMFMAFormStage which needs to be fixed first.

romanovvlad avatar Sep 09 '26 08:09 romanovvlad

I presume that #209204 was aiming to address this issue

Right, but enabling that uncovers other issues in RewriteMFMAFormStage which needs to be fixed first.

hi, I will stack the subreg-aware code and kick off the review as soon as possible.

xgxanq avatar Sep 18 '26 02:09 xgxanq