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

[AMDGPU] MachineSink increases WMMA operand pressure and introduces VGPR spills on gfx1201

Open bakon11 opened this issue 3 months ago • 3 comments

Summary

On gfx1201, the generic MachineSink pass moves a chain of AMDGPU WMMA recurrence instructions into a successor block while leaving their wide global/LDS-loaded operands in the source block. This extends many wide virtual-register live ranges across the CFG edge and increases peak VGPR pressure from 201 to 368 in the reduced case. Greedy register allocation then spills.

Disabling MachineSink eliminates the spills without removing any WMMA operations:

Reduced reproducer VGPR VGPR spills Private bytes Scratch loads/stores WMMA
LLVM 21 default 256 106 264 50 / 83 40
--disable-machine-sink=true 180 0 0 0 / 0 40

The original full-module kernel shows the same discriminator:

Original full-module kernel VGPR VGPR spills Private bytes Scratch loads/stores WMMA
LLVM 21 default 256 134 480 74 / 74 48
--disable-machine-sink=true 156 0 0 0 / 0 48

The full-module no-sink result was also reproduced through the real hipcc device compilation/LTO path and verified in the extracted gfx1201 HSACO.

Affected toolchain

  • Target: amdgcn-amd-amdhsa, gfx1201, wave32
  • Stock corroboration: Ubuntu LLVM 21.1.8 (/usr/lib/llvm-21/bin/llc) and HIP 7.1.52801-9999 / Ubuntu clang 21.1.8
  • Current shipping AMD toolchain reproduction: ROCm 7.2.4, AMD clang 23.0.0git / AMD LLVM 22.0.0git at commit 46fcb339fb61119b337f973c7ca9e710a319fdd0 (/opt/rocm-7.2.4/lib/llvm/bin/llc)
  • Source audit checkout: llvm-project commit 46fcb339fb61119b337f973c7ca9e710a319fdd0
  • This reproduces on both stock Ubuntu LLVM 21.1.8 and the current shipping AMD LLVM 22 toolchain; it is neither LLVM-21-only nor specific to one downstream package.

Reproducer

Public reduced LLVM IR: https://gist.github.com/bakon11/7218dc92e661ab3e65345c5362e4f300 (cm1-spill-reduced-6960.ll).

Artifact identities:

  • cm1-spill-reduced-checkpoint-6960-c3232608.bc
    • size: 6,960 bytes
    • SHA-256: c32326084e4f9997e4c3a57c7fbdbbae137d23413e5f1a13ed22b9f6b4cc70c4
  • cm1-spill-reduced-6960.ll
    • size: 26,418 bytes
    • SHA-256: 9f150ae6944114b82de1e06de0553e87f598eaff13b7eb4dfec039a965b547b9

The module has one defined kernel and 40 llvm.amdgcn.wmma.f32.16x16x16.bf16 calls.

LLC=/usr/lib/llvm-21/bin/llc
BC=cm1-spill-reduced-checkpoint-6960-c3232608.bc

$LLC -O3 \
  -mtriple=amdgcn-amd-amdhsa \
  -mcpu=gfx1201 \
  -filetype=asm \
  "$BC" -o default.s

$LLC -O3 \
  -mtriple=amdgcn-amd-amdhsa \
  -mcpu=gfx1201 \
  --disable-machine-sink=true \
  -filetype=asm \
  "$BC" -o nosink.s

Inspect .amdhsa.kernels metadata and scratch instructions:

grep -E '\.vgpr_count:|\.vgpr_spill_count:|\.private_segment_fixed_size:' default.s nosink.s
grep -Eic '\bscratch_load|\bscratch_store' default.s nosink.s
grep -Eic '\bv_wmma' default.s nosink.s

Expected result:

  • default.s: 256 VGPR, 106 VGPR spills, 264 private bytes, 50 scratch loads, 83 scratch stores, 40 WMMA
  • nosink.s: 180 VGPR, 0 VGPR spills, 0 private bytes, no scratch operations, 40 WMMA

The same reduced module on ROCm 7.2.4's shipping AMD LLVM 22 toolchain reproduces the transition:

LLC=/opt/rocm-7.2.4/lib/llvm/bin/llc
$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  -filetype=asm "$BC" -o default23.s
$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  --disable-machine-sink=true -filetype=asm "$BC" -o nosink23.s
  • default23.s: 256 VGPR, 91 VGPR spills, 236 private bytes, 116 scratch operations, 40 WMMA; SHA-256 60e54498737dd1e3aa94887fca58c1d56089668da834cdb0b3b7177d3c9c41ed
  • nosink23.s: 180 VGPR, 0 VGPR spills, 0 private bytes, no scratch operations, 40 WMMA; SHA-256 d498717ec1581bb670dfc72089ca39f847ad6e2f10ee30805c17819d5bc1bf49

For the pass-level delta:

$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  -stop-before=machine-sink,0 -filetype=asm "$BC" -o before-machine-sink.mir

$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  -stop-after=machine-sink,0 -filetype=asm "$BC" -o after-machine-sink.mir

Actual pass behavior

Before machine-sink,0, the relevant source block interleaves 26 WMMA recurrence operations with 26 GLOBAL_LOAD_DWORDX4 and 27 DS_READ_B128 operations. Fourteen later WMMA operations are already in the successor preheader.

After machine-sink,0:

  • the 26 recurrence WMMA operations move from the source block into the successor preheader;
  • four COPY operations and one REG_SEQUENCE setup operation move with them;
  • the 26 global and 27 LDS wide loads remain in the source block;
  • the successor now contains all 40 WMMA operations;
  • the loaded WMMA operand values remain simultaneously live across the CFG edge.

AMDGPU register-pressure reporting changes from 201 VGPR immediately before MachineSink to 368 immediately after it: an increase of 167 VGPR (83.1%). The spill insertion happens later at greedy RA, but RA is responding to the pressure shape created by MachineSink.

This is not a loss of WMMA instructions or an output-residency-only effect. All WMMA operations remain in both final assemblies.

Source-level analysis

The relevant generic flow is in llvm/lib/CodeGen/MachineSink.cpp:

  1. SinkInstruction() consults TII->shouldSink(MI) (around lines 1845–1848). AMDGPU does not currently veto these WMMA instructions.
  2. FindSuccToSinkTo() treats virtual-register uses as safe to sink past and continues without incorporating their pressure cost (around lines 1443–1446).
  3. Profitability is evaluated for the result definition via isProfitableToSinkTo() (around lines 1482–1486).
  4. isProfitableToSinkTo() immediately returns true when the destination does not post-dominate the source (around lines 1280–1282).
  5. The later source-operand checks (around lines 1314–1358) are therefore bypassed for this branch shape. They are also not an accumulated ordinary cross-block pressure-delta test for a chain of instructions.

The existing FIXME around lines 1864–1870 describes the underlying hazard: sinking a result may lengthen the live ranges of its source operands and increase register pressure. This reproducer demonstrates a severe instance involving wide WMMA fragments and repeated sinking into the same successor.

Targeted diagnostic

As a no-build diagnostic, I simulated an AMDGPU SIInstrInfo::shouldSink(MI) == false decision only for the affected WMMA instructions in the reduced MIR while leaving the rest of MachineSink enabled.

Result:

  • all 26 WMMA recurrence operations remain interleaved with their loads;
  • unrelated MachineSink transformations remain enabled;
  • final codegen uses 189 VGPR;
  • VGPR spills, private bytes, and scratch traffic all fall to zero;
  • all 40 WMMA operations remain.

This distinguishes the causal instruction movement from a global MachineSink-disable side effect.

Diagnostic artifacts, if useful:

  • before-machinesink0.mir: SHA-256 e7f90f5a4427145af570e7ca4f41fb7ba023ad1252a5d0c48f17778476c271fe
  • after-machinesink0.mir: SHA-256 76eea11e1a1d325b5571ac3b6f405d50d96aea18d536fba18ec8f46bb350c98e
  • after-machinesink0-wmma-hook-clean.mir: SHA-256 3165cef68ff86061ffd9895d89ce317e80cf27b22c203544df7e6ebaa3adc2f8

Relation to existing issue #131954

This is distinct from #131954. That report concerns MFMA AGPR/VGPR copies, tied operands, register coalescing, and greedy assignment on gfx942. In this gfx1201 reproducer, 40 WMMA operations are already present and the causal discriminator is earlier: generic MachineSink moves 26 consumers away from 53 wide loads, raising pre-RA VGPR pressure from 201 to 368. Disabling only that movement eliminates all spills.

Possible fixes

Narrow AMDGPU mitigation

Override SIInstrInfo::shouldSink(const MachineInstr &MI) const and reject the affected WMMA class, initially using the existing isWMMA(MI) classification as a conservative discriminator.

Prediction:

  • the 26 recurrence WMMA instructions stay interleaved with their operand loads;
  • pre-RA pressure remains below the 256-VGPR physical ceiling;
  • the original full-module kernel emits zero VGPR spills/private/scratch while retaining all 48 WMMA operations.

This is intentionally conservative and could suppress profitable WMMA sinking in other CFG shapes. It is a useful bounded target fix or diagnostic, but may not be the best general upstream policy.

Principled generic direction

Make ordinary cross-block MachineSink profitability account for pressure added by extending source-operand live ranges before taking the non-postdominating early return. The decision likely needs an accumulated pressure delta for a sequence targeting the same successor; evaluating each instruction independently may still admit the pathological chain.

Reviewer guidance would be useful on whether the generic pressure guard or a target hook is preferred.

Workaround

The current HIP driver accepts the global workaround through device compilation and device LTO:

hipcc -O3 --offload-arch=gfx1201 -mllvm -disable-machine-sink source.hip -o output

This is not recommended as a general production setting because disabling MachineSink globally may regress unrelated kernels.

Scope and runtime note

This report establishes a code-generation defect and pass-level cause. It does not claim a general application speedup.

A separate product attention kernel that did not have this 256-VGPR/134-spill signature retained an identical 192-VGPR/100-scratch resource envelope with MachineSink disabled and measured 1.004x, i.e. noise. That negative result is expected and does not exercise this reproducer's 106-to-0 spill transition. Runtime impact for the affected spill kernel should be evaluated separately from correctness of the compiler transformation.

bakon11 avatar Aug 14 '26 00:08 bakon11

@llvm/issue-subscribers-backend-amdgpu

Author: baKon (bakon11)

## Summary

On gfx1201, the generic MachineSink pass moves a chain of AMDGPU WMMA recurrence instructions into a successor block while leaving their wide global/LDS-loaded operands in the source block. This extends many wide virtual-register live ranges across the CFG edge and increases peak VGPR pressure from 201 to 368 in the reduced case. Greedy register allocation then spills.

Disabling MachineSink eliminates the spills without removing any WMMA operations:

Reduced reproducer VGPR VGPR spills Private bytes Scratch loads/stores WMMA
LLVM 21 default 256 106 264 50 / 83 40
--disable-machine-sink=true 180 0 0 0 / 0 40

The original full-module kernel shows the same discriminator:

Original full-module kernel VGPR VGPR spills Private bytes Scratch loads/stores WMMA
LLVM 21 default 256 134 480 74 / 74 48
--disable-machine-sink=true 156 0 0 0 / 0 48

The full-module no-sink result was also reproduced through the real hipcc device compilation/LTO path and verified in the extracted gfx1201 HSACO.

Affected toolchain

  • Target: amdgcn-amd-amdhsa, gfx1201, wave32
  • llc: Ubuntu LLVM 21.1.8
  • hipcc: HIP 7.1.52801-9999 / Ubuntu clang 21.1.8
  • Source audit checkout: llvm-project commit 46fcb339fb61119b337f973c7ca9e710a319fdd0
  • A separate LLVM 22 codegen check retained the spill behavior, so this does not appear limited to the LLVM 21 package.

Reproducer

Public reduced LLVM IR: https://gist.github.com/bakon11/7218dc92e661ab3e65345c5362e4f300 (cm1-spill-reduced-6960.ll).

Artifact identities:

  • cm1-spill-reduced-checkpoint-6960-c3232608.bc
    • size: 6,960 bytes
    • SHA-256: c32326084e4f9997e4c3a57c7fbdbbae137d23413e5f1a13ed22b9f6b4cc70c4
  • cm1-spill-reduced-6960.ll
    • size: 26,418 bytes
    • SHA-256: 9f150ae6944114b82de1e06de0553e87f598eaff13b7eb4dfec039a965b547b9

The module has one defined kernel and 40 llvm.amdgcn.wmma.f32.16x16x16.bf16 calls.

LLC=/usr/lib/llvm-21/bin/llc
BC=cm1-spill-reduced-checkpoint-6960-c3232608.bc

$LLC -O3 \
  -mtriple=amdgcn-amd-amdhsa \
  -mcpu=gfx1201 \
  -filetype=asm \
  "$BC" -o default.s

$LLC -O3 \
  -mtriple=amdgcn-amd-amdhsa \
  -mcpu=gfx1201 \
  --disable-machine-sink=true \
  -filetype=asm \
  "$BC" -o nosink.s

Inspect .amdhsa.kernels metadata and scratch instructions:

grep -E '\.vgpr_count:|\.vgpr_spill_count:|\.private_segment_fixed_size:' default.s nosink.s
grep -Eic '\bscratch_load|\bscratch_store' default.s nosink.s
grep -Eic '\bv_wmma' default.s nosink.s

Expected result:

  • default.s: 256 VGPR, 106 VGPR spills, 264 private bytes, 50 scratch loads, 83 scratch stores, 40 WMMA
  • nosink.s: 180 VGPR, 0 VGPR spills, 0 private bytes, no scratch operations, 40 WMMA

For the pass-level delta:

$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  -stop-before=machine-sink,0 -filetype=asm "$BC" -o before-machine-sink.mir

$LLC -O3 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1201 \
  -stop-after=machine-sink,0 -filetype=asm "$BC" -o after-machine-sink.mir

Actual pass behavior

Before machine-sink,0, the relevant source block interleaves 26 WMMA recurrence operations with 26 GLOBAL_LOAD_DWORDX4 and 27 DS_READ_B128 operations. Fourteen later WMMA operations are already in the successor preheader.

After machine-sink,0:

  • the 26 recurrence WMMA operations move from the source block into the successor preheader;
  • four COPY operations and one REG_SEQUENCE setup operation move with them;
  • the 26 global and 27 LDS wide loads remain in the source block;
  • the successor now contains all 40 WMMA operations;
  • the loaded WMMA operand values remain simultaneously live across the CFG edge.

AMDGPU register-pressure reporting changes from 201 VGPR immediately before MachineSink to 368 immediately after it: an increase of 167 VGPR (83.1%). The spill insertion happens later at greedy RA, but RA is responding to the pressure shape created by MachineSink.

This is not a loss of WMMA instructions or an output-residency-only effect. All WMMA operations remain in both final assemblies.

Source-level analysis

The relevant generic flow is in llvm/lib/CodeGen/MachineSink.cpp:

  1. SinkInstruction() consults TII->shouldSink(MI) (around lines 1845–1848). AMDGPU does not currently veto these WMMA instructions.
  2. FindSuccToSinkTo() treats virtual-register uses as safe to sink past and continues without incorporating their pressure cost (around lines 1443–1446).
  3. Profitability is evaluated for the result definition via isProfitableToSinkTo() (around lines 1482–1486).
  4. isProfitableToSinkTo() immediately returns true when the destination does not post-dominate the source (around lines 1280–1282).
  5. The later source-operand checks (around lines 1314–1358) are therefore bypassed for this branch shape. They are also not an accumulated ordinary cross-block pressure-delta test for a chain of instructions.

The existing FIXME around lines 1864–1870 describes the underlying hazard: sinking a result may lengthen the live ranges of its source operands and increase register pressure. This reproducer demonstrates a severe instance involving wide WMMA fragments and repeated sinking into the same successor.

Targeted diagnostic

As a no-build diagnostic, I simulated an AMDGPU SIInstrInfo::shouldSink(MI) == false decision only for the affected WMMA instructions in the reduced MIR while leaving the rest of MachineSink enabled.

Result:

  • all 26 WMMA recurrence operations remain interleaved with their loads;
  • unrelated MachineSink transformations remain enabled;
  • final codegen uses 189 VGPR;
  • VGPR spills, private bytes, and scratch traffic all fall to zero;
  • all 40 WMMA operations remain.

This distinguishes the causal instruction movement from a global MachineSink-disable side effect.

Diagnostic artifacts, if useful:

  • before-machinesink0.mir: SHA-256 e7f90f5a4427145af570e7ca4f41fb7ba023ad1252a5d0c48f17778476c271fe
  • after-machinesink0.mir: SHA-256 76eea11e1a1d325b5571ac3b6f405d50d96aea18d536fba18ec8f46bb350c98e
  • after-machinesink0-wmma-hook-clean.mir: SHA-256 3165cef68ff86061ffd9895d89ce317e80cf27b22c203544df7e6ebaa3adc2f8

Relation to existing issue #131954

This is distinct from #131954. That report concerns MFMA AGPR/VGPR copies, tied operands, register coalescing, and greedy assignment on gfx942. In this gfx1201 reproducer, 40 WMMA operations are already present and the causal discriminator is earlier: generic MachineSink moves 26 consumers away from 53 wide loads, raising pre-RA VGPR pressure from 201 to 368. Disabling only that movement eliminates all spills.

Possible fixes

Narrow AMDGPU mitigation

Override SIInstrInfo::shouldSink(const MachineInstr &MI) const and reject the affected WMMA class, initially using the existing isWMMA(MI) classification as a conservative discriminator.

Prediction:

  • the 26 recurrence WMMA instructions stay interleaved with their operand loads;
  • pre-RA pressure remains below the 256-VGPR physical ceiling;
  • the original full-module kernel emits zero VGPR spills/private/scratch while retaining all 48 WMMA operations.

This is intentionally conservative and could suppress profitable WMMA sinking in other CFG shapes. It is a useful bounded target fix or diagnostic, but may not be the best general upstream policy.

Principled generic direction

Make ordinary cross-block MachineSink profitability account for pressure added by extending source-operand live ranges before taking the non-postdominating early return. The decision likely needs an accumulated pressure delta for a sequence targeting the same successor; evaluating each instruction independently may still admit the pathological chain.

Reviewer guidance would be useful on whether the generic pressure guard or a target hook is preferred.

Workaround

The current HIP driver accepts the global workaround through device compilation and device LTO:

hipcc -O3 --offload-arch=gfx1201 -mllvm -disable-machine-sink source.hip -o output

This is not recommended as a general production setting because disabling MachineSink globally may regress unrelated kernels.

Scope and runtime note

This report establishes a code-generation defect and pass-level cause. It does not claim a general application speedup.

A separate product attention kernel that did not have this 256-VGPR/134-spill signature retained an identical 192-VGPR/100-scratch resource envelope with MachineSink disabled and measured 1.004x, i.e. noise. That negative result is expected and does not exercise this reproducer's 106-to-0 spill transition. Runtime impact for the affected spill kernel should be evaluated separately from correctness of the compiler transformation.

This has been fixed with the following PR https://github.com/llvm/llvm-project/pull/165602, which landed in Nov. 25.

bcahoon avatar Aug 14 '26 21:08 bcahoon

Closing as this has been fixed. Please reopen if anything additional is needed.

bcahoon avatar Aug 19 '26 18:08 bcahoon