Skip to content

[InlineSpiller][AMDGPU] Skip undef lanes when spilling a partial-def super-register - #226689

Open
xgxanq wants to merge 1 commit into
llvm:mainfrom
xgxanq:spill-partial-def-fix
Open

xgxanq wants to merge 1 commit into
llvm:mainfrom
xgxanq:spill-partial-def-fix

Conversation

@xgxanq

@xgxanq xgxanq commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

When the greedy allocator live-range-splits a wide register (e.g. an sgpr_128 buffer descriptor) on a single sub-register, it produces sibling vregs each defined on one sub-lane and undef elsewhere. InlineSpiller shares one stack slot among all descendants of the Original value, so a full-width store of such a partially-defined sibling writes its undef lanes over a live sibling value in the same slot and corrupts it. On gfx950 this was observed (via asm data-flow analysis) as an in-loop buffer-descriptor lane being clobbered by an unrelated in-loop spill sharing the slot.

The hazard is target-independent (InlineSpiller's shared-slot logic is generic; SGPR-spill-to-VGPR-lane exists on every AMDGPU that spills SGPRs). Fix it in two places:

  1. hoistSpillInsideBB: bail out unless every sub-range of the hoisted value is live at the hoist point, mirroring the cross-BB guard in isSpillCandBB ([InlineSpiller] Hoist spills only when all of its subranges are available #177703) that the in-BB path was missing.

  2. insertSpill: for a real (partial) spill, compute the defined-lane mask and hand it to the target via a new TargetInstrInfo::setSpillDefinedLaneMask hook (default no-op). AMDGPU records it in the SI_SPILL_S*_SAVE $lanemask operand; spillSGPR then writelanes only the defined dwords, leaving the slot's other lanes (a sibling value) intact. A -1 mask preserves the original full-width behavior.

Existing SGPR-spill tests are regenerated to carry the new -1 $lanemask operand. Adds inline-spiller-partial-subreg-shared-slot.ll as a gfx950 end-to-end regression check.

@github-actions

Copy link
Copy Markdown

Hello @xgxanq 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@llvmorg-github-actions

llvmorg-github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-xtensa
@llvm/pr-subscribers-backend-mips
@llvm/pr-subscribers-backend-powerpc
@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-backend-msp430
@llvm/pr-subscribers-backend-hexagon
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-regalloc

Author: xgxanq

Changes

When the greedy allocator live-range-splits a wide register (e.g. an sgpr_128 buffer descriptor) on a single sub-register, it produces sibling vregs each defined on one sub-lane and undef elsewhere. InlineSpiller shares one stack slot among all descendants of the Original value, so a full-width store of such a partially-defined sibling writes its undef lanes over a live sibling value in the same slot and corrupts it. On gfx950 this was observed (via asm data-flow analysis) as an in-loop buffer-descriptor lane being clobbered by an unrelated in-loop spill sharing the slot.

The hazard is target-independent (InlineSpiller's shared-slot logic is generic; SGPR-spill-to-VGPR-lane exists on every AMDGPU that spills SGPRs). Fix it in two places:

  1. hoistSpillInsideBB: bail out unless every sub-range of the hoisted value is live at the hoist point, mirroring the cross-BB guard in isSpillCandBB (#177703) that the in-BB path was missing.

  2. insertSpill: for a real (partial) spill, compute the defined-lane mask and hand it to the target via a new TargetInstrInfo::setSpillDefinedLaneMask hook (default no-op). AMDGPU records it in the SI_SPILL_S*_SAVE $lanemask operand; spillSGPR then writelanes only the defined dwords, leaving the slot's other lanes (a sibling value) intact. A -1 mask preserves the original full-width behavior.

Existing SGPR-spill tests are regenerated to carry the new -1 $lanemask operand. Adds inline-spiller-partial-subreg-shared-slot.ll as a gfx950 end-to-end regression check.


Patch is 95.32 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/226689.diff

40 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/TargetInstrInfo.h (+6)
  • (modified) llvm/lib/CodeGen/InlineSpiller.cpp (+32-2)
  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+33-4)
  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.h (+3)
  • (modified) llvm/lib/Target/AMDGPU/SIInstructions.td (+8-2)
  • (modified) llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp (+47-4)
  • (modified) llvm/test/CodeGen/AMDGPU/bug-undef-spilled-agpr.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/greedy-alloc-fail-sgpr1024-spill.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/infloop-subrange-spill-inspect-subrange.mir (+1-1)
  • (added) llvm/test/CodeGen/AMDGPU/inline-spiller-partial-subreg-shared-slot.ll (+257)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-dead-frame-in-dbg-value.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-fi-skip-processing-stack-arg-dbg-value-list.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-fi-skip-processing-stack-arg-dbg-value.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-overlap-wwm-reserve.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-partially-undef.mir (+4-14)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-to-vmem-scc-clobber-unhandled.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-to-vmem-scc-clobber.mir (+6-6)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-vmem-large-frame.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill-wrong-stack-id.mir (+5-5)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-spill.mir (+11-11)
  • (modified) llvm/test/CodeGen/AMDGPU/si-lower-sgpr-spills-cycle-header.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/si-lower-sgpr-spills-initial-insert-in-body.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/si-lower-sgpr-spills-initial-insert-in-latch.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/si-lower-sgpr-spills-multi-entry-cycle.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/spill-reg-tuple-super-reg-use.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/spill-scavenge-offset.ll (+7-7)
  • (modified) llvm/test/CodeGen/AMDGPU/spill-sgpr-to-virtual-vgpr.mir (+6-6)
  • (modified) llvm/test/CodeGen/AMDGPU/spill-special-sgpr.mir (+2-2)
  • (modified) llvm/test/CodeGen/AMDGPU/spill192.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/spill224.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/spill288.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/spill320.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/spill352.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/spill384.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/stack-slot-color-sgpr-vgpr-spills.mir (+1-1)
  • (modified) llvm/test/CodeGen/AMDGPU/undefined-physreg-sgpr-spill.mir (+3-3)
  • (modified) llvm/test/CodeGen/AMDGPU/wwm-regalloc-partial-pool.mir (+3-3)
  • (modified) llvm/test/CodeGen/AMDGPU/wwm-regalloc-preallocation-guard.mir (+9-9)
  • (modified) llvm/test/CodeGen/AMDGPU/wwm-spill-superclass-pseudo.mir (+1-1)
  • (modified) llvm/test/CodeGen/Hexagon/regalloc-bad-undef.mir (+1-1)
diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index b013511d33313..022c39d0ca92d 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1226,6 +1226,12 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
                      "TargetInstrInfo::storeRegToStackSlot!");
   }
 
+  /// Tell the target which lanes of spill \p SpillMI hold a real value
+  /// (\p DefinedLanes); it can skip the undef ones when lowering, so a shared
+  /// slot's neighbor isn't stomped. Default: no-op.
+  virtual void setSpillDefinedLaneMask(MachineInstr &SpillMI,
+                                       LaneBitmask DefinedLanes) const {}
+
   /// Load the specified register of the given register class from the specified
   /// stack frame index. The load instruction is to be added to the given
   /// machine basic block before the specified machine instruction. If \p
diff --git a/llvm/lib/CodeGen/InlineSpiller.cpp b/llvm/lib/CodeGen/InlineSpiller.cpp
index f3682a2e24808..a7a960a0cf441 100644
--- a/llvm/lib/CodeGen/InlineSpiller.cpp
+++ b/llvm/lib/CodeGen/InlineSpiller.cpp
@@ -454,6 +454,17 @@ bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
   if (DefMBB != CopyMI.getParent() || !SrcQ.isKill())
     return false;
 
+  // Every sub-range of the hoisted value must be live at the hoist point. The
+  // store below is full-width, so a dead sub-range would write a stale sibling
+  // value into the shared spill slot and clobber it. This mirrors the sub-range
+  // liveness guard on the cross-BB path in isSpillCandBB (#177703), which the
+  // in-BB path here was missing.
+  if (SrcLI.hasSubRanges() &&
+      !all_of(SrcLI.subranges(), [&](const LiveInterval::SubRange &SR) {
+        return SR.getVNInfoAt(Idx) != nullptr;
+      }))
+    return false;
+
   MachineBasicBlock *MBB = DefMBB;
   MachineBasicBlock::iterator MII;
   if (SrcVNI->isPHIDef())
@@ -1285,10 +1296,29 @@ void InlineSpiller::insertSpill(Register NewVReg, bool isKill,
   MachineBasicBlock::iterator SpillBefore = std::next(MI);
   bool IsRealSpill = isRealSpill(*MI);
 
-  if (IsRealSpill)
+  if (IsRealSpill) {
     TII.storeRegToStackSlot(MBB, SpillBefore, NewVReg, isKill, StackSlot,
                             MRI.getRegClass(NewVReg), Register());
-  else
+
+    MachineInstr &SpillMI = *std::next(MI);
+
+    // Compute the lanes of NewVReg defined by MI. For a partial def (undef
+    // lanes remain in the full-width store above), pass the defined-lane mask
+    // so the target skips them and avoids clobbering a sibling in the shared
+    // stack slot. A fully-undef def never reaches here (filtered by
+    // isRealSpill).
+    LaneBitmask FullMask = MRI.getMaxLaneMaskForVReg(NewVReg);
+    LaneBitmask DefinedLanes;
+    for (const MachineOperand &MO : MI->all_defs()) {
+      if (MO.getReg() != NewVReg)
+        continue;
+      DefinedLanes |= MO.getSubReg()
+                          ? TRI.getSubRegIndexLaneMask(MO.getSubReg())
+                          : FullMask;
+    }
+    if (DefinedLanes != FullMask)
+      TII.setSpillDefinedLaneMask(SpillMI, DefinedLanes);
+  } else
     // Don't spill undef value.
     // Anything works for undef, in particular keeping the memory
     // uninitialized is a viable option and it saves code size and
diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index 0346ebc254361..28a06d6921aa4 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -1800,10 +1800,11 @@ void SIInstrInfo::storeRegToStackSlotImpl(
     }
 
     BuildMI(MBB, MI, DL, OpDesc)
-      .addReg(SrcReg, getKillRegState(isKill)) // data
-      .addFrameIndex(FrameIndex)               // addr
-      .addMemOperand(MMO)
-      .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
+        .addReg(SrcReg, getKillRegState(isKill)) // data
+        .addFrameIndex(FrameIndex)               // addr
+        .addImm(-1) // lanemask (all lanes; refined by spiller)
+        .addMemOperand(MMO)
+        .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit);
 
     return;
   }
@@ -1837,6 +1838,34 @@ void SIInstrInfo::storeRegToStackSlotCFI(MachineBasicBlock &MBB,
                           MachineInstr::NoFlags, true);
 }
 
+void SIInstrInfo::setSpillDefinedLaneMask(MachineInstr &SpillMI,
+                                          LaneBitmask DefinedLanes) const {
+  // Only SGPR spill saves carry a $lanemask operand (see SI_SPILL_SGPR).
+  int Idx =
+      AMDGPU::getNamedOperandIdx(SpillMI.getOpcode(), AMDGPU::OpName::lanemask);
+  if (Idx == -1)
+    return;
+
+  // Build the per-dword mask consumed by spillSGPR: bit i is set when dword i
+  // of the spilled super-register intersects a defined lane. spillSGPR lowers
+  // the spill dword by dword and consults this mask to skip the undef ones.
+  const MachineRegisterInfo &MRI = SpillMI.getMF()->getRegInfo();
+  Register Data = SpillMI.getOperand(0).getReg();
+  const TargetRegisterClass *RC = MRI.getRegClass(Data);
+  unsigned NumDwords = RI.getRegSizeInBits(*RC) / 32;
+  // DwordMask is a u32; the widest SGPR tuple (SReg_1024) has 32 dwords, so
+  // this holds and keeps the 1u << i shift below well-defined.
+  assert(NumDwords <= 32 && "SGPR spill wider than DwordMask can represent");
+  unsigned DwordMask = 0;
+  for (unsigned i = 0; i != NumDwords; ++i) {
+    LaneBitmask DwordLanes =
+        RI.getSubRegIndexLaneMask(RI.getSubRegFromChannel(i));
+    if ((DefinedLanes & DwordLanes).any())
+      DwordMask |= 1u << i;
+  }
+  SpillMI.getOperand(Idx).setImm(DwordMask);
+}
+
 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) {
   switch (Size) {
   case 4:
diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.h b/llvm/lib/Target/AMDGPU/SIInstrInfo.h
index 48eba6b2a567d..42afa3cd8c60e 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.h
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.h
@@ -356,6 +356,9 @@ class SIInstrInfo final : public AMDGPUGenInstrInfo {
       bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg,
       MachineInstr::MIFlag Flags = MachineInstr::NoFlags) const override;
 
+  void setSpillDefinedLaneMask(MachineInstr &SpillMI,
+                               LaneBitmask DefinedLanes) const override;
+
   void loadRegFromStackSlot(
       MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg,
       int FrameIndex, const TargetRegisterClass *RC, Register VReg,
diff --git a/llvm/lib/Target/AMDGPU/SIInstructions.td b/llvm/lib/Target/AMDGPU/SIInstructions.td
index 3cf20ffb1fcf4..3faae31302689 100644
--- a/llvm/lib/Target/AMDGPU/SIInstructions.td
+++ b/llvm/lib/Target/AMDGPU/SIInstructions.td
@@ -1178,14 +1178,20 @@ def V_INDIRECT_REG_READ_GPR_IDX_B32_V32 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VRe
 
 multiclass SI_SPILL_SGPR <RegisterClass sgpr_class> {
   let UseNamedOperandTable = 1, Spill = 1, SALU = 1, Uses = [EXEC] in {
+    // $lanemask: per-dword bitmask of which dwords of the spilled super-register
+    // are defined. Undef dwords must not be written into the (possibly shared)
+    // stack slot. -1 (all dwords defined) is the ordinary full-width spill; a
+    // partial mask is set by the spiller for partially-defined values. i32imm's
+    // 32 bits cover the widest SGPR tuple SReg_1024 (32 dwords).
     def _SAVE : PseudoInstSI <
       (outs),
-      (ins sgpr_class:$data, i32imm:$addr)> {
+      (ins sgpr_class:$data, i32imm:$addr, i32imm:$lanemask)> {
       let mayStore = 1;
       let mayLoad = 0;
     }
 
-    def _CFI_SAVE : PseudoInstSI<(outs), (ins sgpr_class:$data, i32imm:$addr)> {
+    def _CFI_SAVE : PseudoInstSI<(outs),
+      (ins sgpr_class:$data, i32imm:$addr, i32imm:$lanemask)> {
       let mayStore = 1;
       let mayLoad = 0;
     }
diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
index 1aa423e1da647..5a8dd05c239ab 100644
--- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
@@ -2180,6 +2180,18 @@ bool SIRegisterInfo::spillSGPR(MachineBasicBlock::iterator MI, int Index,
   if (OnlyToVGPR && !SpillToVGPR)
     return false;
 
+  // A partial-def spill (see InlineSpiller / setSpillDefinedLaneMask) records
+  // which dwords are real values in its $lanemask operand; a cleared bit means
+  // that dword is undef and must NOT be written to the (possibly shared) stack
+  // slot, so it does not clobber a sibling value living in the same slot. A
+  // mask of -1 (default) means "all lanes defined" = original behavior.
+  const MachineOperand *LaneMask =
+      SB.TII.getNamedOperand(*MI, AMDGPU::OpName::lanemask);
+  int64_t DefinedDwordMask = LaneMask ? LaneMask->getImm() : -1;
+  auto DwordDefined = [&](unsigned i) {
+    return (DefinedDwordMask & (int64_t(1) << i)) != 0;
+  };
+
   const SIFrameLowering *TFL = ST.getFrameLowering();
 
   assert(SpillToVGPR || (SB.SuperReg != SB.MFI.getStackPtrOffsetReg() &&
@@ -2195,15 +2207,29 @@ bool SIRegisterInfo::spillSGPR(MachineBasicBlock::iterator MI, int Index,
            "Num of SGPRs spilled should be less than or equal to num of "
            "the VGPR lanes.");
 
+    // With a partial-def mask, find the first/last dword that is actually
+    // defined, so the ImplicitDefine of the super-register and the kill flag
+    // can be re-anchored onto emitted writelanes (undef dwords are skipped).
+    unsigned FirstDefined = 0, LastDefined = SB.NumSubRegs - 1;
+    while (FirstDefined < SB.NumSubRegs && !DwordDefined(FirstDefined))
+      ++FirstDefined;
+    while (LastDefined > 0 && !DwordDefined(LastDefined))
+      --LastDefined;
+
     for (unsigned i = 0, e = SB.NumSubRegs; i < e; ++i) {
+      // Skip undef dwords: not writing them to a (possibly shared) stack slot
+      // preserves a sibling value living in the same slot.
+      if (!DwordDefined(i))
+        continue;
+
       Register SubReg =
           SB.NumSubRegs == 1
               ? SB.SuperReg
               : Register(getSubReg(SB.SuperReg, SB.SplitParts[i]));
       SpilledReg Spill = VGPRSpills[i];
 
-      bool IsFirstSubreg = i == 0;
-      bool IsLastSubreg = i == SB.NumSubRegs - 1;
+      bool IsFirstSubreg = i == FirstDefined;
+      bool IsLastSubreg = i == LastDefined;
       bool UseKill = SB.IsKill && IsLastSubreg;
 
 
@@ -2259,13 +2285,30 @@ bool SIRegisterInfo::spillSGPR(MachineBasicBlock::iterator MI, int Index,
     // Per VGPR helper data
     auto PVD = SB.getPerVGPRData();
 
+    // For a partial-def spill (undef dwords, see $lanemask), the whole TmpVGPR
+    // is stored at once, so first load the slot's current contents; the skipped
+    // dwords then keep the sibling value already there instead of writing
+    // undef.
+    bool IsPartialDef = SB.NumSubRegs > 1 && DefinedDwordMask != -1;
+
     for (unsigned Offset = 0; Offset < PVD.NumVGPRs; ++Offset) {
       RegState TmpVGPRFlags = RegState::Undef;
 
+      if (IsPartialDef) {
+        // Seed TmpVGPR with the slot's current contents, so the dwords we skip
+        // (undef in this partial def) keep the sibling value already living in
+        // the slot when the whole TmpVGPR is stored back below.
+        SB.readWriteTmpVGPR(Offset, /*IsLoad*/ true);
+        TmpVGPRFlags = {};
+      }
+
       // Write sub registers into the VGPR
       for (unsigned i = Offset * PVD.PerVGPR,
                     e = std::min((Offset + 1) * PVD.PerVGPR, SB.NumSubRegs);
            i < e; ++i) {
+        if (IsPartialDef && !DwordDefined(i))
+          continue; // Undef dword: keep the sibling value seeded from the slot.
+
         Register SubReg =
             SB.NumSubRegs == 1
                 ? SB.SuperReg
@@ -2286,8 +2329,8 @@ bool SIRegisterInfo::spillSGPR(MachineBasicBlock::iterator MI, int Index,
             Indexes->insertMachineInstrInMaps(*WriteLane);
         }
 
-        // There could be undef components of a spilled super register.
-        // TODO: Can we detect this and skip the spill?
+        // Undef components of the spilled super-register are detected via the
+        // $lanemask operand and skipped above (see IsPartialDef).
         if (SB.NumSubRegs > 1) {
           // The last implicit use of the SB.SuperReg carries the "Kill" flag.
           RegState SuperKillState = {};
diff --git a/llvm/test/CodeGen/AMDGPU/bug-undef-spilled-agpr.mir b/llvm/test/CodeGen/AMDGPU/bug-undef-spilled-agpr.mir
index 5c564decd1e7d..a026d13e0cce5 100644
--- a/llvm/test/CodeGen/AMDGPU/bug-undef-spilled-agpr.mir
+++ b/llvm/test/CodeGen/AMDGPU/bug-undef-spilled-agpr.mir
@@ -75,7 +75,7 @@ body:             |
     liveins: $sgpr12, $sgpr13, $sgpr14, $sgpr15
 
     %45:vgpr_32 = IMPLICIT_DEF
-    SI_SPILL_S32_SAVE $sgpr15, %stack.0, implicit $exec, implicit $sgpr32 :: (store (s32) into %stack.0, addrspace 5)
+    SI_SPILL_S32_SAVE $sgpr15, %stack.0, -1, implicit $exec, implicit $sgpr32 :: (store (s32) into %stack.0, addrspace 5)
     %16:vgpr_32 = V_AND_B32_e32 1, %45, implicit $exec
 
   bb.1:
diff --git a/llvm/test/CodeGen/AMDGPU/greedy-alloc-fail-sgpr1024-spill.mir b/llvm/test/CodeGen/AMDGPU/greedy-alloc-fail-sgpr1024-spill.mir
index 94c22b1aa8664..5c186b9a0470b 100644
--- a/llvm/test/CodeGen/AMDGPU/greedy-alloc-fail-sgpr1024-spill.mir
+++ b/llvm/test/CodeGen/AMDGPU/greedy-alloc-fail-sgpr1024-spill.mir
@@ -57,7 +57,7 @@ body:             |
   ; CHECK-NEXT:   renamable $sgpr86 = S_MOV_B32 0
   ; CHECK-NEXT:   renamable $sgpr87 = S_MOV_B32 0
   ; CHECK-NEXT:   renamable $sgpr88 = S_MOV_B32 0
-  ; CHECK-NEXT:   SI_SPILL_S1024_SAVE renamable $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75_sgpr76_sgpr77_sgpr78_sgpr79_sgpr80_sgpr81_sgpr82_sgpr83_sgpr84_sgpr85_sgpr86_sgpr87_sgpr88_sgpr89_sgpr90_sgpr91_sgpr92_sgpr93_sgpr94_sgpr95_sgpr96_sgpr97_sgpr98_sgpr99, %stack.0, implicit $exec, implicit $sgpr32 :: (store (s1024) into %stack.0, align 4, addrspace 5)
+  ; CHECK-NEXT:   SI_SPILL_S1024_SAVE renamable $sgpr68_sgpr69_sgpr70_sgpr71_sgpr72_sgpr73_sgpr74_sgpr75_sgpr76_sgpr77_sgpr78_sgpr79_sgpr80_sgpr81_sgpr82_sgpr83_sgpr84_sgpr85_sgpr86_sgpr87_sgpr88_sgpr89_sgpr90_sgpr91_sgpr92_sgpr93_sgpr94_sgpr95_sgpr96_sgpr97_sgpr98_sgpr99, %stack.0, 65535, implicit $exec, implicit $sgpr32 :: (store (s1024) into %stack.0, align 4, addrspace 5)
   ; CHECK-NEXT:   renamable $sgpr52_sgpr53 = IMPLICIT_DEF
   ; CHECK-NEXT:   ADJCALLSTACKUP 0, 0, implicit-def dead $scc, implicit-def $sgpr32, implicit $sgpr32
   ; CHECK-NEXT:   dead $sgpr30_sgpr31 = SI_CALL renamable $sgpr52_sgpr53, 0, csr_amdgpu, implicit $sgpr0_sgpr1_sgpr2_sgpr3
diff --git a/llvm/test/CodeGen/AMDGPU/infloop-subrange-spill-inspect-subrange.mir b/llvm/test/CodeGen/AMDGPU/infloop-subrange-spill-inspect-subrange.mir
index 63ee4473a56e5..43669338dd308 100644
--- a/llvm/test/CodeGen/AMDGPU/infloop-subrange-spill-inspect-subrange.mir
+++ b/llvm/test/CodeGen/AMDGPU/infloop-subrange-spill-inspect-subrange.mir
@@ -55,7 +55,7 @@ body:             |
   ; CHECK-NEXT:   liveins: $sgpr24_sgpr25_sgpr26_sgpr27:0x000000000000000F, $sgpr4_sgpr5_sgpr6_sgpr7_sgpr8_sgpr9_sgpr10_sgpr11_sgpr12_sgpr13_sgpr14_sgpr15_sgpr16_sgpr17_sgpr18_sgpr19:0x000000000000FFFF, $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43_sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51:0x000000000000FFFF
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   renamable $sgpr12 = IMPLICIT_DEF
-  ; CHECK-NEXT:   SI_SPILL_S512_SAVE renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43_sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51, %stack.0, implicit $exec, implicit $sgpr32 :: (store (s512) into %stack.0, align 4, addrspace 5)
+  ; CHECK-NEXT:   SI_SPILL_S512_SAVE renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43_sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51, %stack.0, 255, implicit $exec, implicit $sgpr32 :: (store (s512) into %stack.0, align 4, addrspace 5)
   ; CHECK-NEXT:   renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43_sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51 = IMPLICIT_DEF
   ; CHECK-NEXT:   dead undef [[IMAGE_SAMPLE_LZ_V1_V2_2:%[0-9]+]].sub0:vreg_96 = IMAGE_SAMPLE_LZ_V1_V2 undef [[DEF2]], killed renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43, renamable $sgpr12_sgpr13_sgpr14_sgpr15, 1, 0, 0, 0, 0, 0, 0, 0, implicit $exec :: (dereferenceable load (s32), addrspace 8)
   ; CHECK-NEXT:   renamable $sgpr36_sgpr37_sgpr38_sgpr39_sgpr40_sgpr41_sgpr42_sgpr43_sgpr44_sgpr45_sgpr46_sgpr47_sgpr48_sgpr49_sgpr50_sgpr51 = SI_SPILL_S512_RESTORE %stack.0, implicit $exec, implicit $sgpr32 :: (load (s512) from %stack.0, align 4, addrspace 5)
diff --git a/llvm/test/CodeGen/AMDGPU/inline-spiller-partial-subreg-shared-slot.ll b/llvm/test/CodeGen/AMDGPU/inline-spiller-partial-subreg-shared-slot.ll
new file mode 100644
index 0000000000000..bbff66888c652
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/inline-spiller-partial-subreg-shared-slot.ll
@@ -0,0 +1,257 @@
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -stop-after=greedy %s -o - | FileCheck %s
+;
+; Regression test for the InlineSpiller shared-stack-slot partial-subregister
+; hazard.
+;
+; The hazard is target-independent: InlineSpiller's shared-slot logic is generic,
+; and SGPR-spill-to-VGPR-lane exists on every AMDGPU that spills SGPRs. When the
+; greedy allocator live-range-splits an sgpr_128 descriptor on one sub-register,
+; it produces sibling vregs each defined on a single sub-lane
+; (`undef %V.sub0:sgpr_128 = ...`), undef elsewhere. Since InlineSpiller shares
+; one stack slot among descendants of the Original value, a full-width store of
+; such a sibling writes its undef lanes over a live sibling in the SAME slot. This
+; input is a gfx950 reproducer (there the clobber corrupts an in-loop buffer
+; descriptor -- the kind of corruption that leads to an HSA memory fault), but the
+; fix and this check apply to all AMDGPU.
+;
+; The fix records in the save pseudo's $lanemask operand which dwords the store
+; defines; spillSGPR writelanes only those, leaving the slot's other lanes intact.
+; This pins that the two partial siblings sharing one slot each store with a
+; partial mask (matched as a non-negative immediate: the buggy full-width store
+; used -1, which the {{[0-9]+}} pattern cannot match).
+;
+; Driven from IR through -stop-after=greedy: a .mir + -run-pass=greedy form does
+; not reproduce, since the trigger needs pre-greedy PreRARemat pressure state that
+; MIR serialization drops. The CHECK block is vreg/slot-number independent.
+;
+; First partial sibling (only .sub0 defined), stored with a partial mask:
+; CHECK:      undef [[DESC:%[0-9]+]].sub0:sgpr_128 = S_MOV_B32 0
+; CHECK-NEXT: SI_SPILL_S128_SAVE [[DESC]], [[SLOT:%stack\.[0-9]+]], {{[0-9]+}},
+; Second partial sibling into the SAME slot, also with a partial mask:
+; CHECK:      SI_SPILL_S128_SAVE %{{[0-9]+}}, [[SLOT]], {{[0-9]+}},
+;
+target datalayout = "e-m:e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9-p10:32:32-p11:32:32-p12:32:32-p13:32:32-p14:32:32-p15:32:32"
+target triple = "amdgcn-amd-amdhsa"
+
+define amdgpu_kernel void @_attn_fwd_IS_CAUSAL_1_NUM_Q_HEADS_8_NUM_K_HEADS_1_BLOCK_M_256_BLOCK_N_64_BLOCK_DMODEL_64_RETURN_SCORES_1_ENABLE_DROPOUT_1_IS_FP8_0_VARLEN_1_NUM_XCD_8_USE_INT64_STRIDES_1_ENABLE_SINK_0_SLIDING_WINDOW_0(ptr addrspace(1) inreg %0, ptr addrspace(1) inreg %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, <1 x i32> %7, i32 %8, i32 %9, i32 %10, i32 %11, i32 %12, i32 %13, i32 %14, i32 %15, i32 %16, i32 %17, i32 %18, i32 %19, i32 %20, i32 %21, i32 %22, i32 %23, i32 %24, i32 %25, i32 %26, i64 %27, i64 %28, i64 %29, i64 %30, i64 %31, i64 %32, i64 %33, i64 %34, i64 %35, i64 %36, i64 %37, i1 %38, i1 %39, i1 %40, i1 %41, i1 %42, i1 %43, i1 %44, i64 %sext452, i64 %.pn241.in795, i64 %sext496, i64 %.pn233.in799, i64 %sext500, i64 %.pn231.in800, i64 %sext501, i64 %.pn229.in801, i64 %sext503, i64 %.pn223.in804, i64 %sext505, i64 %.pn221.in805, i64 %sext506) #0 {
+..loopexit_crit_edge:
+  %45 = icmp slt i32 %8, 0
+  %46 = icmp slt i32 %6, 0
+  %47 = icmp slt i32 %9, 0
+  %48 = icmp slt i32 %10, 0
+  %49 = icmp slt i32 %12, 0
+  %50 = icmp slt i32 %2, 0
+  br label %51
+
+51:                                               ; preds = ...
[truncated]

@Muhamed-Husic

Copy link
Copy Markdown

Thanks for working on this. We hit the same hazard on MI350 (gfx950) in an attention kernel generated by rocmlirTriton, and I tested this PR (c3e394a) against it. It doesn't fix our case, and with this PR applied instead of our downstream fix (see ROCm/rocmlirTriton#552), the kernel still faults on hardware in 3 of 3 runs, while our fix passes 3 of 3 on the same GPU.

Our spill goes through hoistSpillInsideBB, but the partial sibling is defined by a COPY from another sibling, and it has no subrange at all for the lanes it doesn't define. After greedy, with this PR:

1764B  SI_SPILL_S128_SAVE %664:sgpr_128, %stack.3, -1, ...          ; before the loop
3816B  %667:sgpr_128 = SI_SPILL_S128_RESTORE %stack.3, ...           ; loop top
6104B  undef %668.sub2_sub3:sgpr_128 = COPY %667.sub2_sub3:sgpr_128  ; in the loop
6108B  SI_SPILL_S128_SAVE %668:sgpr_128, %stack.3, -1, ...

-debug-only=regalloc:

selectOrSplit SGPR_128:%668 [6104r,6132r:0) 0@6104r  L0000000000000030 [6104r,6132r:0) 0@6104r  L00000000000000C0 [6104r,6132r:0) 0@6104r  weight:3.020561e-01
	hoisted: 6104r	SI_SPILL_S128_SAVE %668:sgpr_128, %stack.3, -1, implicit $exec, implicit $sgpr32 :: (store (s128) into %stack.3, align 4, addrspace 5)

So the new guard passes (the only subranges are sub2 and sub3, and both are live at the hoist point). The store hoistSpillInsideBB builds also keeps $lanemask = -1, since setSpillDefinedLaneMask is only called from insertSpill. In the final code the loop overwrites the descriptor's base in the slot:

  s_load_dwordx4 s[4:7], s[2:3], 0x0       ; %0 in s[4:5], %1 in s[6:7]
  s_and_b32 s5, s5, 0xffff
  s_and_b32 s53, s7, 0xffff
  s_mov_b32 s52, s6                        ; s[52:53] = base of %1, not written again
  s_mov_b32 s7, 0x27000
  s_mov_b32 s6, 0x7ffffffe
  v_writelane_b32 v122, s4, 2              ; %stack.3 = v122 lanes 2-5 = rsrc(%0)
  v_writelane_b32 v122, s5, 3
  v_writelane_b32 v122, s6, 4
  v_writelane_b32 v122, s7, 5
  ...
.LBB0_1:
  v_readlane_b32 s76, v122, 2              ; reload rsrc(%0)
  v_readlane_b32 s77, v122, 3
  v_readlane_b32 s78, v122, 4
  v_readlane_b32 s79, v122, 5
  buffer_load_ushort v0, v0, s[76:79], 0 offen
  ...
  s_mov_b64 s[54:55], s[78:79]
  ...
  v_writelane_b32 v122, s52, 2             ; base of %1 written over rsrc(%0)
  v_writelane_b32 v122, s53, 3
  v_writelane_b32 v122, s54, 4
  v_writelane_b32 v122, s55, 5
  ...
  s_cbranch_vccnz .LBB0_1

Both descriptors are loop-invariant in the IR (make.buffer.rsrc of kernel arguments, no phi), so from the second iteration on, the load that should read %0 reads through %1's base.

Reproducers
kernel.ll.txt
reduced.ll.txt

  • reduced.ll.txt (llvm-reduce'd from the full kernel): llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -mattr=+sramecc,-xnack -O3 reduced.ll.txt -o reduced.s. The clobber is visible in the final assembly as above. On our tree it needs -mattr=+sramecc,-xnack (without it, the bug doesn't reproduce).
  • kernel.ll.txt: the unreduced kernel IR as our compiler hands it to LLVM's code generator. This is the one that faults on hardware. Compiling it with llc gives nearly the same assembly as our compiler produced (a few dozen lines differ, mostly in instruction order), and the faulting spill is the same one: undef %4700.sub2_sub3:sgpr_128 = COPY %4699.sub2_sub3, then a hoisted SI_SPILL_S128_SAVE %4700:sgpr_128, %stack.5, -1, with subranges 0x30/0xC0 only.

For context, I tested onn our Triton-pinned LLVM (based on 62b7cf9) with a few downstream cherry-picks, including #211809 (which also touches hoistSpillInsideBB), #204648, #209704 and #214725. I haven't tried main. Your PR applied cleanly to our tree except for one context-only conflict in SIInstrInfo::storeRegToStackSlotImpl, which I applied by hand; the result matches the PR line for line. FYI, inline-spiller-partial-subreg-shared-slot.ll doesn't match its CHECK lines on our tree, presumably because our allocation differs from main.

Our downstream fix, for reference, is InlineSpiller-only, with no target changes (see ROCm/rocmlirTriton#552). The hoist guard in hoistSpillInsideBB refuses to hoist if, at the source's def, some lane is live in the Original but not in the source. A lane with no subrange counts as dead, which is exactly our case. And when spillAroundUses spills an undef partial def while the slot is live and the Original is live in the other lanes, it reloads the slot first, so the full-width store writes those lanes back unchanged.

@qcolombet

Copy link
Copy Markdown
Contributor

I think this is going in the right direction, but I would rather give the LaneMask directly to the TII hooks that handles the insertion of load and stores than creating a separate API for that. The -1 index looks hacky BTW.

Regarding the issue we're trying to solve (#225054; for some reason I can't comment there directly), I think there one issue and one opportunity:

  1. The issue is that when we rolled out the subregister liveness, we didn't account for it "in memory". What this means is that either we would need to rematerialize all the lanes before we do a store, or we must ensure that we store ONLY the lanes that are live. Doing the latter is the preferred option, but it may be difficult to do it in a generic manner. In a way, I'm surprised it didn't break earlier!
  2. In the motivating example of [InlineSpiller] hoistSpillInsideBB: partial-subregister split shares spill slot→ full-width store corrupts loop-carried sibling (gfx950 miscompile) #225054, if I'm not mistaken, we wouldn't actually need the store at all. We're just materializing a split point so we shouldn't have to do the reload->store dance. That's just a missed opportunity.

@xgxanq , I asked my agent to do patches for both, I am sharing them if you want to take a look and productize them.
I eyeballed them and the patch for #2 (split-copy) looked reasonable. The patch for #1 (i.e., the proper fix and a variant of what you're doing (partial-spills)) was going in the right direction but would require more work.

cc: @Muhamed-Husic , @GMNGeoffrey FYI.

PS: I believe that although the hoisting heuristic needs to be fixed, it is not the source of the problem. It just makes it more likely to manifest.
llvm225054-partial-spills.patch
llvm225054-split-copy.patch

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this recreating #175002 and some of its associated patches

@xgxanq

xgxanq commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Is this recreating #175002 and some of its associated patches

No, it is not. It stems from a newly encountered issue. #225054

This PR (partial spill, the store side) and PR #175002 (partial reload, the load side) address the same underlying gap — subregister liveness not being reflected in how tuples are spilled to and reloaded from memory — but they are opposite halves with different goals.

This PR(partial spill) , following qcolombet's review feedback, ultimately implements subregister-granular spilling through a pair of opt-in target hooks (canStoreRegToStackSlotWithLanes /storeRegToStackSlotWithLanes). It makes the spiller store only the lanes that are actually defined. It is a correctness fix: today, a full-width store of a partially-defined sibling writes its undef lanes over another sibling's value living in the same shared stack slot, producing a miscompile. Because the store touches the shared slot, there is a real clobber hazard, so the optimization is scoped to word-aligned SGPR tuples.

#175002 (partial reload) makes the spiller restore only the sub-registers that are actually used, instead of the whole tuple. It is a performance optimization: reloading an entire tuple when only part of it is needed wastes instructions and register pressure. Since it only reads a subset of the slot, there is no clobber hazard, so it safely covers both SGPR and VGPR (its
test uses vreg_128).

@xgxanq

xgxanq commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

I think this is going in the right direction, but I would rather give the LaneMask directly to the TII hooks that handles the insertion of load and stores than creating a separate API for that. The -1 index looks hacky BTW.

Regarding the issue we're trying to solve (#225054; for some reason I can't comment there directly), I think there one issue and one opportunity:

  1. The issue is that when we rolled out the subregister liveness, we didn't account for it "in memory". What this means is that either we would need to rematerialize all the lanes before we do a store, or we must ensure that we store ONLY the lanes that are live. Doing the latter is the preferred option, but it may be difficult to do it in a generic manner. In a way, I'm surprised it didn't break earlier!
  2. In the motivating example of [InlineSpiller] hoistSpillInsideBB: partial-subregister split shares spill slot→ full-width store corrupts loop-carried sibling (gfx950 miscompile) #225054, if I'm not mistaken, we wouldn't actually need the store at all. We're just materializing a split point so we shouldn't have to do the reload->store dance. That's just a missed opportunity.

@xgxanq , I asked my agent to do patches for both, I am sharing them if you want to take a look and productize them. I eyeballed them and the patch for #2 (split-copy) looked reasonable. The patch for #1 (i.e., the proper fix and a variant of what you're doing (partial-spills)) was going in the right direction but would require more work.

cc: @Muhamed-Husic , @GMNGeoffrey FYI.

PS: I believe that although the hoisting heuristic needs to be fixed, it is not the source of the problem. It just makes it more likely to manifest. llvm225054-partial-spills.patch llvm225054-split-copy.patch

I think this is going in the right direction, but I would rather give the LaneMask directly to the TII hooks that handles the insertion of load and stores than creating a separate API for that. The -1 index looks hacky BTW.

Regarding the issue we're trying to solve (#225054; for some reason I can't comment there directly), I think there one issue and one opportunity:

  1. The issue is that when we rolled out the subregister liveness, we didn't account for it "in memory". What this means is that either we would need to rematerialize all the lanes before we do a store, or we must ensure that we store ONLY the lanes that are live. Doing the latter is the preferred option, but it may be difficult to do it in a generic manner. In a way, I'm surprised it didn't break earlier!
  2. In the motivating example of [InlineSpiller] hoistSpillInsideBB: partial-subregister split shares spill slot→ full-width store corrupts loop-carried sibling (gfx950 miscompile) #225054, if I'm not mistaken, we wouldn't actually need the store at all. We're just materializing a split point so we shouldn't have to do the reload->store dance. That's just a missed opportunity.

@xgxanq , I asked my agent to do patches for both, I am sharing them if you want to take a look and productize them. I eyeballed them and the patch for #2 (split-copy) looked reasonable. The patch for #1 (i.e., the proper fix and a variant of what you're doing (partial-spills)) was going in the right direction but would require more work.

cc: @Muhamed-Husic , @GMNGeoffrey FYI.

PS: I believe that although the hoisting heuristic needs to be fixed, it is not the source of the problem. It just makes it more likely to manifest. llvm225054-partial-spills.patch llvm225054-split-copy.patch

Thanks Quentin, this is really helpful — agreed on the direction and the framing (issue vs. opportunity).

Confirming your "hoist heuristic isn't the source" point with data. An external team hit the same hazard on MI350 (gfx950) in a rocmlirTriton attention kernel and tested my current patch
(the setSpillDefinedLaneMask + $lanemask=-1 form) against it: it does not fix their case — 3/3 hardware faults, vs. 3/3 pass with their InlineSpiller-only downstream fix. Root cause of the
escape matches what you said:

  • The clobbering spill goes through hoistSpillInsideBB, but my lane-mask is only wired into insertSpill, so the hoisted store stays full-width (-1).
  • The partial sibling is undef %.sub2_sub3 = COPY ... with no subrange at all for the undefined lanes, so a subrange-iterating guard treats them as "not present" rather than dead and lets
    the hoist through.

So I'm going to drop the setSpillDefinedLaneMask/-1 approach and productize your #1 instead: the opt-in can/storeRegToStackSlotWithLanes hook pair (no new param on the existing hook, no sentinel), getLiveSpillLanes covering the no-subrange lanes, the mask threaded through all three emission sites (hoist / insertSpill / spillAroundUses), and PartialSpillSlots guarding the whole-value motion/removal. Your patch applies cleanly on our tree (only a few test-regen context conflicts).

On scope: I looked into whether VGPR/AGPR tuples need the same treatment. The lowering has no inherent guard (full-width store writes undef lanes), but in practice VGPR tuples rarely get per-subreg split into partial-def siblings (their lanes are usually defined/used together), and I couldn't even construct a synthetic repro because the AMDGPU allocator prefers lowering occupancy over spilling. So limiting the opt-in to word-aligned SGPR tuples looks like the right call — happy to revisit if a real VGPR case shows up.

One coordination question: do you want to drive #1 upstream yourself, or should I productize it (align the unit tests, verify on the reduced reproducer, CI) and put up the PR? Either way I'll contribute the gfx950 lit test I already have (sgpr-spill-partial-def-lanemask.mir) rebased onto your SI_SPILL_S32_SAVE_PARTIAL form. For #2 (split-copy) the patch looks good to me too; I can take that as a separate PR. @qcolombet

@qcolombet

Copy link
Copy Markdown
Contributor

I think #175002 will be helpful to have a true subreg aware spilling, but narrowing reloads could be seen as just an optimization so I'm okay to pursue that later. I'd encourage you @xgxanq to still read the conversation.

@xgxanq please drive both #1 and #2, I won't have the bandwidth to do it.
For #1, ideally I would prefer that we keep the same hook for both lane aware and not lane aware stores/loads. Hence why I believe the current patch is not that great but didn't have time to push the agent in that direction.

@xgxanq

xgxanq commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

I think #175002 will be helpful to have a true subreg aware spilling, but narrowing reloads could be seen as just an optimization so I'm okay to pursue that later. I'd encourage you @xgxanq to still read the conversation.

@xgxanq please drive both #1 and #2, I won't have the bandwidth to do it. For #1, ideally I would prefer that we keep the same hook for both lane aware and not lane aware stores/loads. Hence why I believe the current patch is not that great but didn't have time to push the agent in that direction.

Thanks @qcolombet — happy to drive both #1 and #2. I've read the #175002 thread.

I've reworked #1 along the lines you asked for:

  • The LaneMask now goes directly into the existing storeRegToStackSlot hook
    (new trailing LaneBitmask Lanes = LaneBitmask::getAll() param, getAll() == the
    ordinary full-width store). The separate storeRegToStackSlotWithLanes API is gone,
    and so is the -1 sentinel.
  • Loads are untouched: narrowing reloads are the optimization you're fine deferring
    (no clobber hazard on a read), and that work lives in [InlineSpiller][AMDGPU] Implement subreg reload during RA spill #175002.

One design point I'd like your call on: I kept a single capability predicate canStoreRegToStackSlotWithLanes(RC, Lanes). I saw your "the target can just ignore the subreg" comment on #175002's reload hook, and I agree for reloads — over-reading a full tuple is safe. Stores are asymmetric, though: if a target can't honor the mask and falls back to a full-width store of a partial-def value into a shared slot, that's exactly the #225054 clobber. The generic spiller therefore has to know the capability before it commits — in hoistSpillInsideBB the choice is "emit a partial store (supported)" vs "don't hoist (unsupported)", both safe — so unlike the reload side the target can't simply ignore the mask. Let me know if you'd prefer a different shape for that query.

@zhentaocc

zhentaocc commented Nov 3, 2026 •

Copy link
Copy Markdown

For reference, I opened #238213 for the same miscompile (#225054), as a possible complement to this PR rather than a replacement.

Difference in short:

If this PR lands, #238213 could be reduced to the fallback for classes that do not opt in, or dropped. Happy to adjust either way.

Assisted-by: Claude Code

@qcolombet qcolombet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @xgxanq,

At the high level I feel that there's some feature creep happening in this PR so let me suggest some path forward.
I would split the work in three parts, i.e., independent PRs.
a. Fix the general partial store/load issue
b. (Re) enable spill hoisting on partial stores
c. Optimize away the useless materialization of the values when we're just moving things around

The patches I shared in #226689 (comment) were doing a. and b. in #1 and c. in #2 if I'm making any sense.

For a., here is my recommendation:

  1. Disable hoistspill when you detect partial spill, you should be able to leverage the isStoreToStackSlot with the store size for that, like check if the stack slot is as large as the MemBytes.
  2. Enable partial spill when it's supported, but when it's not supported, we'd need something like what @zhentaocc mentioned in #226689 (comment). I didn't actually check the patch but we'd need to make sure we restore all the live lanes (i.e., #238213).

For b., that's when you can relax the hoisting to be allowed as long as the spills that you are merging touching the same lanes. Right now you're trying to keep this enabled in this PR and there's where the feature creep happens IMHO.

For c., you got this, just need to be a separate patch.

One other comment, I see that you kept some of the AI slop comments that were in my AI generated patch. I highlighted a couple of them, but please make sure to go through all of them.

Note: I understand that a. is a big ask, this is fixing a fundamental issue in how we handle the memory when subreg liveness is enabled, so let me know if you need help/would like someone else to carry this over.

/// register. A target opting in via canStoreRegToStackSlotWithLanes may get a
/// narrower mask and must store exactly those lanes, preserving the rest of
/// the slot. It must not widen the store, nor report the partial store as
/// complete via isStoreToStackSlot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part about isStoreToStackSlot sounds suspicious to me.
This is going to be a store to stack slot by definition (this function is about inserting such a store!) so that requirement sounds wrong to me.

Maybe instead we want to say that the isStoreToStackSlot variable with the MemSize returns a smaller size than the full stack slot?

I guess to step back, what issues are you running into without that exclusion?
It sounds to me we may be working around some lurking issues in how isStoreToStackSlot is used.

@xgxanq xgxanq Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qcolombet

You're right, and the isStoreToStackSlot requirement in that comment is a workaround.

What breaks without the exclusion: InlineSpiller::eliminateRedundantSpills()

if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
// treat the spill as redundant and turn it into a KILL
MI.setDesc(TII.get(TargetOpcode::KILL));
...
}

I'll work through these based on my own understanding, rather than just integrating your earlier patches with CC.


/// Whether the target can spill exactly Lanes of RC while preserving the
/// other lanes of the original stack slot. Lanes uses RC's lane numbering;
/// it is not a byte offset. In particular, a target must not round this mask

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Lanes uses RC's lane numbering; it is not a byte offset."

That sounds good to call out that Lanes is given with respect to RC, but calling out the "not a byte offset" is unnecessary/confusing because that would mean we abuse the LaneBitmask type.

/// other lanes of the original stack slot. Lanes uses RC's lane numbering;
/// it is not a byte offset. In particular, a target must not round this mask
/// up to a wider store. A target which opts in receives the narrower mask
/// through storeRegToStackSlot's \p Lanes parameter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are missing something is this sentence:
"A target which opts in receives the narrower mask through storeRegToStackSlot's \p Lanes parameter."

The "which opts in" is connected to nothing regarding this one hook.
I understand what you're saying "if this returns true the mask will be used in storeRegToStackSlot", but I think it makes sense only because I know what this PR is about.
Could you try to rephrase?

MergeableSpillsMap MergeableSpills;

// Whole-value equivalence does not describe a slot with partial updates.
DenseSet<int> PartialSpillSlots;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand what this comment is calling out.

} // end anonymous namespace

// Existing subranges alone do not describe the whole register: a split child
// can have no subrange at all for lanes which remain live in its shared slot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what the function is doing, but I don't understand what the comment tries to call out.

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated
/// same block. Do not infer slot contents from the original register alone: a
/// real definition or an intervening store can make a sibling's value differ
/// from the value currently in memory.
bool InlineSpiller::isCopyFromStackSlot(const MachineInstr &CopyMI) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you leave this out of this PR?
If I'm not mistaken this address point #2 I was making: the missed optimization where we shouldn't need to rematerialize the value in register (#226689 (comment)).
If yes, my point was that point #1 and #2 should go separately but if you have the bandwidth I'd appreciate that you drive both :).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right — isCopyFromStackSlot is point #2 (the redundant rematerialization / split-copy optimization), and it shouldn't be bundled here. I'll pull it out of this PR so this one stays focused on #1, and move the split-copy work into a separate PR. Happy to drive both — I'll put up the #2 PR once #1 settles.

// Query the source at the actual insertion point, not at the later copy.
// A lane that dies before the copy can still be available here.
// Dead definitions are not live after the defining instruction.
SlotIndex InsertIdx =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need that?
The insertion point is MII, we should use that slot index.
What am I missing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — and at this site the two are actually equivalent, so I'll switch to MII.

Non-PHI, MII is the instruction immediately after DefMI and the store is inserted right before it. def.getDeadSlot() and getInstructionIndex(*MII) fall on either side of the gap between those two adjacent instructions, with no instruction (hence no liveness segment boundary) in between, so liveAt() — and therefore getLiveSpillLanes() — returns the same mask at both.

The comment is also just wrong: a lane that is dead-defined at DefMI has its segment end at DefMI's dead slot, so it isn't live at def.getDeadSlot() either — the code never actually preserved such lanes, and there's no "later copy" at this insertion point. This one came over from the shared #1 patch; I should have caught it when I carried it in.

Comment thread llvm/lib/CodeGen/InlineSpiller.cpp Outdated
MRI.getRegClass(SrcReg), Register());
// Insert spill without kill flag immediately after def. A partial-liveness
// mask requests a lane-preserving partial store; a full mask is the ordinary
// full-width spill. Both go through the same hook.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"A partial-liveness mask requests a lane-preserving partial store; a full mask is the ordinary full-width spill. Both go through the same hook."

This is AI-slop, please remove

// other words of the shared slot. Otherwise emit the ordinary full-width
// store, including when this class does not support partial spills.
if (Lanes != LaneBitmask::getAll() &&
canStoreRegToStackSlotWithLanes(RC, Lanes)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we assert that canStoreRegToStackSlotWithLanes otherwise we violate the contract of how this API is being called, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — you're right.

@arsenm arsenm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot more code than I expect to solve this issue, can you try with a different approach

@xgxanq

xgxanq commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

This is a lot more code than I expect to solve this issue, can you try with a different approach

Different approach?Handling the partial spill problem systematically would require guarding every entry point that relies on full access or full value-equivalence — a systematic,large-scale overhaul. I'll refine it incrementally from the angles of correctness and completeness.

yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 4, 2026
With Triton 3.9 (LLVM 18f9e623), bwd_kernel_fused_causal miscompiles on
gfx942 for varlen inputs with a padded head dim (33, 53): InlineSpiller
writes a partially defined sibling over the spill slot holding the hoisted
Q base pointer, so the Q buffer descriptor base becomes Q_hi << 32 | <an
unrelated offset>. MI300 CI reads garbage (8 test_mha_dao_ai_backward_head_size
failures); MI308X page-faults and hangs. The LLVM fix is
llvm/llvm-project#226689, verified locally with llc on the same IR.

Until Triton's LLVM carries that fix, keep the varlen path on per-head
offsets (bid * stride_b + hqid * stride_h + q_start * stride_m) instead of
hoisting the sequence bases, which perturbs register allocation enough to
avoid the bad spill. The selection is on the IS_VARLEN constexpr, so dense
kernels compile to the same ISA as before.

Verified on MI308X with Triton 3.9 and 3.8: all 24 head-size variants plus 8
extra varlen shapes match the reference, with errors identical to Triton 3.8.

Co-Authored-By: Claude <[email protected]>
@xgxanq

xgxanq commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Hi @xgxanq,

At the high level I feel that there's some feature creep happening in this PR so let me suggest some path forward. I would split the work in three parts, i.e., independent PRs. a. Fix the general partial store/load issue b. (Re) enable spill hoisting on partial stores c. Optimize away the useless materialization of the values when we're just moving things around

The patches I shared in #226689 (comment) were doing a. and b. in #1 and c. in #2 if I'm making any sense.

For a., here is my recommendation:

  1. Disable hoistspill when you detect partial spill, you should be able to leverage the isStoreToStackSlot with the store size for that, like check if the stack slot is as large as the MemBytes.
  2. Enable partial spill when it's supported, but when it's not supported, we'd need something like what @zhentaocc mentioned in #226689 (comment). I didn't actually check the patch but we'd need to make sure we restore all the live lanes (i.e., [InlineSpiller] Preserve slot lanes when spilling a partial split def #238213).

For b., that's when you can relax the hoisting to be allowed as long as the spills that you are merging touching the same lanes. Right now you're trying to keep this enabled in this PR and there's where the feature creep happens IMHO.

For c., you got this, just need to be a separate patch.

One other comment, I see that you kept some of the AI slop comments that were in my AI generated patch. I highlighted a couple of them, but please make sure to go through all of them.

Note: I understand that a. is a big ask, this is fixing a fundamental issue in how we handle the memory when subreg liveness is enabled, so let me know if you need help/would like someone else to carry this over.

Taking a more comprehensive view of the partial-spill problem is definitely the right call. I'm happy to keep driving this forward.

yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 13, 2026
Triton 3.9's LLVM (InlineSpiller, fixed by llvm/llvm-project#226689)
miscompiles the fused causal bwd attention kernel:
- MI300X: 8 varlen head-dim 33/53 cases of test_mha_dao_ai_backward_head_size
  read a clobbered Q pointer; deselect them in the MI300X shard.
- MI35X: FA test_flash_attn_output[0.0-0.17-2048-2048-96-True-False-True-False-mha-dtype0-True]
  aborts with a GPU memory fault; exclude it from the -k filter.
Restore both once Triton ships the LLVM fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 13, 2026
…on triton 3.9

Triton 3.9's LLVM (InlineSpiller, fixed by llvm/llvm-project#226689)
miscompiles the varlen fused causal bwd attention kernel on gfx942: 8
head-dim 33/53 cases of test_mha_dao_ai_backward_head_size read a clobbered
Q pointer. Deselect them in the MI300X shard; restore once Triton ships the
LLVM fix.

Co-Authored-By: Claude <[email protected]>
@xgxanq
xgxanq force-pushed the spill-partial-def-fix branch from 2e5909c to 946e8cf Compare November 13, 2026 11:45
@xgxanq
xgxanq requested a review from qcolombet November 13, 2026 11:52
mengfei-jiang pushed a commit to ROCm/aiter that referenced this pull request Nov 16, 2026
With Triton 3.9 (LLVM 18f9e623), bwd_kernel_fused_causal miscompiles on
gfx942 for varlen inputs with a padded head dim (33, 53): InlineSpiller
writes a partially defined sibling over the spill slot holding the hoisted
Q base pointer, so the Q buffer descriptor base becomes Q_hi << 32 | <an
unrelated offset>. MI300 CI reads garbage (8 test_mha_dao_ai_backward_head_size
failures); MI308X page-faults and hangs. The LLVM fix is
llvm/llvm-project#226689, verified locally with llc on the same IR.

Until Triton's LLVM carries that fix, keep the varlen path on per-head
offsets (bid * stride_b + hqid * stride_h + q_start * stride_m) instead of
hoisting the sequence bases, which perturbs register allocation enough to
avoid the bad spill. The selection is on the IS_VARLEN constexpr, so dense
kernels compile to the same ISA as before.

Verified on MI308X with Triton 3.9 and 3.8: all 24 head-size variants plus 8
extra varlen shapes match the reference, with errors identical to Triton 3.8.

Co-Authored-By: Claude <[email protected]>
mengfei-jiang pushed a commit to ROCm/aiter that referenced this pull request Nov 16, 2026
…on triton 3.9

Triton 3.9's LLVM (InlineSpiller, fixed by llvm/llvm-project#226689)
miscompiles the varlen fused causal bwd attention kernel on gfx942: 8
head-dim 33/53 cases of test_mha_dao_ai_backward_head_size read a clobbered
Q pointer. Deselect them in the MI300X shard; restore once Triton ships the
LLVM fix.

Co-Authored-By: Claude <[email protected]>
Retain the lane-aware stack store interface and AMDGPU partial SGPR
emission, with conservative guards on spill hoisting and merging.

Preservation reload fallback for targets without partial stores and
remaining spiller entry-point audits are still required.
@xgxanq
xgxanq force-pushed the spill-partial-def-fix branch from b4c7bf8 to e0c121b Compare November 16, 2026 03:59
mengfei-jiang pushed a commit to ROCm/aiter that referenced this pull request Nov 19, 2026
- test_mha_dao_ai_capture: deselected on MI35X and MI300X.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases: deselected
  on MI300X (gfx942 LLVM InlineSpiller miscompile, llvm/llvm-project#226689).

Co-Authored-By: Claude <[email protected]>
@xgxanq

xgxanq commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

I am splitting this work into three stacked PRs (A/B/C) following Quentin’s approach. This PR addresses the requirements for part A.

image

#243100
@qcolombet @reble @arsenm

yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
Only [mha-cpu] and [mha-cuda] fault on triton 3.9 (bwd_kernel_fused_causal,
MHA backward with dropout); the gqa variants pass. Link the LLVM fix,
llvm/llvm-project#226689, next to both deselect lists.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout).
  The gqa variants pass.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942.

Both are LLVM InlineSpiller miscompiles in the Triton toolchain, fixed by
llvm/llvm-project#226689 (verified on gfx950 by
rebuilding the triton 3.9 codegen LLVM with it). Restore once triton ships
the fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout).
  The gqa variants pass.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942.

Both are LLVM InlineSpiller miscompiles in the Triton toolchain, fixed by
llvm/llvm-project#226689 (verified on gfx950 by
rebuilding the triton 3.9 codegen LLVM with it). Restore once triton ships
the fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout).
  The gqa variants pass.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942.

Both are LLVM InlineSpiller miscompiles in the Triton toolchain, fixed by
llvm/llvm-project#226689 (verified on gfx950 by
rebuilding the triton 3.9 codegen LLVM with it). Restore once triton ships
the fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout),
  an LLVM InlineSpiller miscompile. The gqa variants pass. Fix pending in
  llvm/llvm-project#226689, verified on gfx950 by
  rebuilding the triton 3.9 codegen LLVM with it.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942; not root-caused.

Restore them once triton ships a fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 20, 2026
- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout),
  an LLVM InlineSpiller miscompile. The gqa variants pass. Fix pending in
  llvm/llvm-project#226689, verified on gfx950 by
  rebuilding the triton 3.9 codegen LLVM with it.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942; not root-caused.

Restore them once triton ships a fix.

Co-Authored-By: Claude <[email protected]>
yuyzhang512 added a commit to yuyzhang512/aiter that referenced this pull request Nov 22, 2026
…ests

With triton 3.9 (f1bf9132) these cases fault or hang in
bwd_kernel_fused_causal, the same LLVM miscompile as test_mha_dao_ai_capture:

- test_flash_attn_output[0.0-0.17-2048-2048-128-True-False-False-False-mha-dtype0-True]
- test_flash_attn_output[0.0-0.17-2048-2048-128-True-False-False-False-mha-dtype0-False]
- test_flash_attn_varlen_output[0.0-0.17-1024-1024-32-True-False-True-False-mha-dtype0-False]

They pass on triton 3.8 and pass once the triton 3.9 codegen LLVM has
llvm/llvm-project#226689 (still open). On MI350X,
all 396 dropout cases of both tests were run one per process; only these
three fail, every time. One of them hangs, which --timeout=120 does not
stop, so CI would hit the step limit.

Deselect them where test.sh runs these tests (v2-pr and v2-full).

Co-Authored-By: Claude <[email protected]>
mengfei-jiang pushed a commit to ROCm/aiter that referenced this pull request Nov 23, 2026
…with gfx950 fixes and config retune (#8266)

* ci: install triton 3.9.0 (f1bf9132) from the release_1031 channel

Move the Triton used by CI and by `pip install aiter` (setup.py runs
install_triton.sh) from triton 3.8.0 (release_0914, 7cb7b059) to triton
3.9.0 (release_1031, f1bf9132).

- Python 3.11+ only: release_1031 has cp311-cp314 wheels. On Python 3.10
  install_triton.sh prints a reminder and exits 0, keeping the existing
  triton; setup.py prints the same reminder instead of "Replacing existing
  triton", and the README notes the requirement.
- The ROCm version comes from rocm-core (dpkg or rpm) or, on pip ROCm SDK
  images, the pip `rocm` package; a failed rpm query is ignored.
- Candidate indices: the derived rocm-X.Y.0, then the newest one of the
  same ROCm major (rocm-7.2.0 for 7.x such as 7.12, rocm-10.2.0 for 10.x),
  then the default. An index is used only if it lists a wheel with this
  interpreter's full tag (cpXY[t]-cpXY[t]-linux_<arch>), probed with
  python3 urllib (no curl): HTTP 403/404 tries the next candidate, a network
  error is retried and then stops.
- A local wheelhouse is used only if it holds a matching wheel. Nothing is
  uninstalled until a usable source is found, so a failure leaves the
  existing triton in place.

download_triton_wheel.sh reuses the same lookup via
`install_triton.sh --resolve` instead of a copy of it.

Co-Authored-By: Claude <[email protected]>

* [Triton] Fix mxfp4 quant compile on triton 3.9

Triton 3.9 rejects `scalar[None, :]` ("too many indices for tensor of
rank 0"). _dynamic_mxfp4_quant_kernel_asm_layout indexes the scalar pid_n
that way in its block-scale stores. Use `pid_n + tl.arange(0, 1)`: a
rank-1, size-1 range that broadcasts identically and compiles on triton
3.8 and 3.9.

Co-Authored-By: Claude <[email protected]>

* [Triton][gfx950] Retune FF fused small-M tiles to fit LDS on triton 3.9

The M_LEQ_4 / M_LEQ_8 buckets of 7 FF-A16W16-fused-N=*-K=* configs, and the
M_LEQ_64 bucket of 4 of them (N=11264/K=2048, N=16384/K=1280,2048,8192),
used BM=32, BN=128, BK=256, ns=2, which needs 168864 B of LDS on triton 3.9
(limit 163840) and fails to compile. The ungated kernel already overflowed
on triton 3.8.

- M_LEQ_4, M_LEQ_8 (all 7 files): BM=16, BN=128, BK=256, ns=2, nw=4.
- M_LEQ_64 (those 4 files): BK=128, ns=3 (BM=32, BN=128, nw=8 unchanged).
  The other 3 files' M_LEQ_64 (BM=64, BN=256, BK=64, ns=3) is unchanged.

Picked from tile sweeps on triton 3.9 (gfx950). BN stays 128: the ungated
kernel adds its output with bf16 atomic adds, one per N-tile, and BN=64
doubled the misses against the torch reference. The new tiles fit LDS on
triton 3.8 and 3.9. On MI350X, gated shapes are +3.0% TFLOPS over triton
3.8 with the old configs (median; +13.2% at M 16-64), and the ungated
shapes, which failed before, now run. With triton 3.8 alone the new
configs are +3.4% over the old ones.

The gfx950 launch tests now also cover the retuned buckets: gated M=4/64
(N=16384, K=8192) and ungated M=8/64 (N=16384, K=1280), checked against the
torch reference. get_x_vals() does not reach these buckets.

Co-Authored-By: Claude <[email protected]>

* ci: deselect triton 3.9 known-issue attention cases

- test_mha_dao_ai_capture[mha-cpu] and [mha-cuda] (MI35X and MI300X):
  GPU memory fault in bwd_kernel_fused_causal (MHA backward with dropout),
  an LLVM InlineSpiller miscompile. The gqa variants pass. Fix pending in
  llvm/llvm-project#226689, verified on gfx950 by
  rebuilding the triton 3.9 codegen LLVM with it.
- 8 varlen head-dim 33/53 test_mha_dao_ai_backward_head_size cases
  (MI300X): wrong gradients or a fault on gfx942; not root-caused.

Restore them once triton ships a fix.

Co-Authored-By: Claude <[email protected]>

* [Triton][gfx942] unified_attention: 3D config for head sizes >= 256 fits LDS on triton 3.9

Heads above 256 pad to a 512-wide tile. With the default attn_3d config
(num_warps=2, num_stages=2) the 3D kernel needs 128 KiB of LDS on triton 3.9,
over gfx942's 64 KiB, so test_triton_unified_attn_prefill_head_sizes
[q_dtype0-kv_dtype0-None-320-seq_lens0] fails on MI300X (OutOfResources).
Use the D_GEQ_512 settings (num_warps=4, num_stages=1) from head 256 up.

Co-Authored-By: Claude <[email protected]>

* ci: deselect three triton 3.9 known failures in the flash-attention tests

With triton 3.9 (f1bf9132) these cases fault or hang in
bwd_kernel_fused_causal, the same LLVM miscompile as test_mha_dao_ai_capture:

- test_flash_attn_output[0.0-0.17-2048-2048-128-True-False-False-False-mha-dtype0-True]
- test_flash_attn_output[0.0-0.17-2048-2048-128-True-False-False-False-mha-dtype0-False]
- test_flash_attn_varlen_output[0.0-0.17-1024-1024-32-True-False-True-False-mha-dtype0-False]

They pass on triton 3.8 and pass once the triton 3.9 codegen LLVM has
llvm/llvm-project#226689 (still open). On MI350X,
all 396 dropout cases of both tests were run one per process; only these
three fail, every time. One of them hangs, which --timeout=120 does not
stop, so CI would hit the step limit.

Deselect them where test.sh runs these tests (v2-pr and v2-full).

Co-Authored-By: Claude <[email protected]>

* ci: install triton from the release-1031 index

Switch the AMD Triton index path from release_1031 to release-1031. The
new index serves triton 3.9.0 git24d5394c (release_1031 had f1bf9132) with
the same layout: cp311-cp314 wheels and triton-kernels 1.0.0 for ROCm 7.0,
7.1, 7.2, 10.1 and 10.2, and HTTP 403 for ROCm versions without an index.

Co-Authored-By: Claude <[email protected]>

* [CI] Raise triton test timeouts to 90 minutes

#8214 (unified get_shapes for GEMMs) multiplied the GEMM test case counts
(e.g. test_fused_gemm_a8w8_blockscale_mul_add.py 642 -> 4866 cases,
test_gemm_a8w8.py 1845 -> 8141), while split-test-times/triton.tsv still
holds the pre-#8214 times. The shards are unbalanced: MI300X shard 5 took
56.7 min of tests and hit the 60-minute job limit, and MI35X shard 7 took
54.3 min against its 60-minute test step limit.

Raise the MI300X job limit and the MI35X "Triton Tests" step limit from
60 to 90 minutes.

Co-Authored-By: Claude <[email protected]>

* [Triton] dao_ai bwd: smaller RDNA tiles to avoid an LLVM crash on gfx1100

Triton 3.9's LLVM aborts while compiling bwd_kernel_fused_causal for
gfx1100 at head dim 112 with varlen (InlineSpiller::reMaterializeFor
"corrupted sub-interval" assertion). This killed the RDNA3 MHA bench at
kimik25-tp4 d=112 bwd_varlen.

Halve BLOCK_M1 and BLOCK_N2 (32 -> 16) in the RDNA bwd configs, autotune on
and off. Only the arch.is_rdna branches change; gfx942/gfx950 configs are
untouched. Cross-compiled for gfx1100, all 160 bench bwd/bwd_varlen cases
now compile. Runtime behaviour on RDNA3 is checked by the RDNA3 CI job.

Co-Authored-By: Claude <[email protected]>

* ci: deselect a triton 3.9 RDNA3 failure in the FA output test

test_flash_attn_output[0.0-0.17-2048-2048-96-False-False-False-False-mha-dtype0-True]
fails on the RDNA3 (gfx1100) runner with triton 3.9. Deselect it in the
v2-pr-rdna3 run only; other archs still run it.

Co-Authored-By: Claude <[email protected]>

* ci: skip the triton 3.9 RDNA3 flash-attention output failures by pattern

On RDNA3 (gfx1100) 30 test_flash_attn_output -k 2048-2048 cases fail with
triton 3.9, some by crashing the pytest process. All are mha (no mqa or gqa),
in two groups:

- dropout 0.0, causal, head dim 32/59/64: all alibi x kvpacked combinations (12)
- dropout 0.17, head dim 96/111/128: 18 of 24 causal x alibi x kvpacked combinations

Skip both groups with one -k pattern instead of ids, which also covers the
96-False-False-False-False case deselected before. The pattern skips 36 of the
528 cases: the 30 failures, the 2 96-False-False-False-False cases, and 4
dropout cases that passed in this run (96 causal+alibi, 128 non-causal without
alibi), which look like dropout luck rather than a different rule. 492 cases
still run.

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
@cdevadas

cdevadas commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@xgxanq Thanks for working on this. I agree with the fix and the underlying issue in #225054. However, I think we can simplify the approach by aligning it with the subreg reload stack (#174996–#175002) rather than introducing a separate lane-mask mechanism. I originally planned this work as well and wanted to do it after the subreg reload stack lands.

As you figured out, we need to spill only the live part of the register without overwriting sibling lanes sharing the same slot. The reload stack already provides most of the machinery for representing a part of a stack slot using a subregister and offset and the same thing can be extended to the spill equivalents.
I would prefer:

  1. storeRegToStackSlot(..., SubReg, Flags) mirroring the existing load interface ([CodeGen][InlineSpiller] Add SubReg argument to loadRegFromStackSlot for subreg-reload #175581, already landed).
  2. A spill-side equivalent of isReloadableSubRegIdx.
  3. Reuse the existing SI_SPILL_* pseudos plus an offset (which [AMDGPU] Introduce Offset field in SGPR spill Pseudos #174996 already adds for restores).
  4. Reusing the existing spill pseudos with an offset rather than introducing SI_SPILL_S32_SAVE_PARTIAL. This also optimizes the need for multi-S32 spill insertions. We could have a single spill for contiguous subreg ranges. Non-contiguous live lanes will definitely need separate subregister spills.

This approach would make the fix work naturally for SGPR, VGPR, AGPR and AV tuples, while keeping the mechanism generic for other targets with subreg liveness. It would simplify the patch by avoiding the additional PartialSpillSlots bookkeeping, the separate lane-mask API, and the need to hide partial stores from isStoreToStackSlot.

I have prototyped this on top of the reload stack, and that seemed to fix the problem. I prefer to have the subreg reload stack (#174996–#175002) get landed first and then this PR.

Sorry for giving my thoughts so late.

BrianHarrisonAMD added a commit to ROCm/rocm-libraries that referenced this pull request Dec 3, 2026
… pitch reasons

- dQ and merged _COMPILE_HINTS cite ALMIOPEN-3684 and llvm/llvm-project#226689 (AUTH-5, DESIGN-1); dK/dV states why it has no region-split bound (IMPACT-1).
- strides_of comment and message give outputs the store-suppression reason; README difference list names it (CORR-1, CORR-2).
- Comments and one message string only; comment-stripped ASTs are unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.