Skip to content

[AMDGPU] Make AMDGPURewriteAGPRCopyMFMA aware of subreg reload - #174998

Open
cdevadas wants to merge 4 commits into
users/cdevadas/make-getNumSubRegsForSpillOp-member-functionfrom
users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware
Open

cdevadas wants to merge 4 commits into
users/cdevadas/make-getNumSubRegsForSpillOp-member-functionfrom
users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware

Conversation

@cdevadas

@cdevadas cdevadas commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

AMDGPURewriteAGPRCopyMFMA pass is currently not subreg-aware.
In particular, the logic that optimizes spills into COPY
instructions assumes full register reloads. This becomes
problematic when the reload instruction partially restores
a tuple register. This patch introduces the necessary changes
to make this pass subreg-aware, for a future patch that
implements subreg reload during RA.

cdevadas commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot

llvmbot commented Jan 8, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-amdgpu

Author: Christudasan Devadasan (cdevadas)

Changes

AMDGPURewriteAGPRCopyMFMA pass is currently not subreg-aware.
In particular, the logic that optimizes spills into COPY
instructions assumes full register reloads. This becomes
problematic when the reload instruction partially restores
a tuple register. This patch introduces the necessary changes
to make this pass subreg-aware, for a future patch that
implements subreg reload during RA.


Full diff: https://github.com/llvm/llvm-project/pull/174998.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/TargetRegisterInfo.h (+3)
  • (modified) llvm/lib/CodeGen/TargetRegisterInfo.cpp (+10)
  • (modified) llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp (+41-1)
diff --git a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
index 35b14e8b8fd30..5c35cd338feb6 100644
--- a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
@@ -430,6 +430,9 @@ class LLVM_ABI TargetRegisterInfo : public MCRegisterInfo {
     return SubRegIndexLaneMasks[SubIdx];
   }
 
+  /// Try to find a matching subreg from the given lanemask.
+  unsigned getSubRegIdxFromLaneMask(LaneBitmask LaneMask) const;
+
   /// Try to find one or more subregister indexes to cover \p LaneMask.
   ///
   /// If this is possible, returns true and appends the best matching set of
diff --git a/llvm/lib/CodeGen/TargetRegisterInfo.cpp b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
index cffb3ed1b8779..2b3924e368ccd 100644
--- a/llvm/lib/CodeGen/TargetRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
@@ -546,6 +546,16 @@ TargetRegisterInfo::getRegSizeInBits(Register Reg,
   return getRegSizeInBits(*RC);
 }
 
+unsigned
+TargetRegisterInfo::getSubRegIdxFromLaneMask(LaneBitmask LaneMask) const {
+  for (unsigned Idx = 1, E = getNumSubRegIndices(); Idx < E; ++Idx) {
+    if (getSubRegIndexLaneMask(Idx) == LaneMask)
+      return Idx;
+  }
+
+  return 0 /*NoSubRegister*/;
+}
+
 bool TargetRegisterInfo::getCoveringSubRegIndexes(
     const TargetRegisterClass *RC, LaneBitmask LaneMask,
     SmallVectorImpl<unsigned> &NeededIndexes) const {
diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
index ffbb1c183ca9e..fd872751ad037 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
@@ -112,6 +112,17 @@ class AMDGPURewriteAGPRCopyMFMAImpl {
   bool tryFoldCopiesToAGPR(Register VReg, MCRegister AssignedAGPR) const;
   bool tryFoldCopiesFromAGPR(Register VReg, MCRegister AssignedAGPR) const;
 
+  /// Derives the subregister index from a spill reload pseudo instruction by
+  /// constructing a lane mask that covers the reloaded portion and finding
+  /// the matching subregister.
+  ///
+  /// \p MI the spill reload pseudo instruction containing the offset and
+  /// spill size info
+  /// \p Reg the original virtual register being spilled (mostly a tuple
+  /// register)
+  /// \return the subregister index corresponding to the reload portion.
+  unsigned getSubRegFromReload(MachineInstr &MI, Register VReg) const;
+
   /// Replace spill instruction \p SpillMI which loads/stores from/to \p SpillFI
   /// with a COPY to the replacement register value \p VReg.
   void replaceSpillWithCopyToVReg(MachineInstr &SpillMI, int SpillFI,
@@ -422,6 +433,33 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryFoldCopiesFromAGPR(
   return MadeChange;
 }
 
+unsigned
+AMDGPURewriteAGPRCopyMFMAImpl::getSubRegFromReload(MachineInstr &MI,
+                                                   Register Reg) const {
+  unsigned NumRegs = TRI.getRegSizeInBits(*MRI.getRegClass(Reg)) / 32;
+  unsigned SubReg = 0;
+  // SubReg accesses for the tuple registers are of interest here.
+  // Note: We don't support 16-bit subreg reloads. If that assuption is
+  // changed in the future, this function should be revised.
+  if (NumRegs == 1)
+    return SubReg;
+
+  unsigned NumSpilledRegs = TRI.getNumSubRegsForSpillOp(MI);
+  // Skip if the entire tuple is reloaded.
+  if (NumRegs == NumSpilledRegs)
+    return SubReg;
+
+  // Construct the covering lanes for the reloaded portion.
+  unsigned SubRegIdx =
+      TII.getNamedOperand(MI, AMDGPU::OpName::offset)->getImm() / 4;
+  // Subreg lane masks are maintained in terms of regunits and each 32-bit
+  // register consists of two regunits.
+  uint64_t Lanes = (1ULL << NumSpilledRegs * 2) - 1;
+  LaneBitmask CoveringLanes = LaneBitmask(Lanes << SubRegIdx * 2);
+  SubReg = TRI.getSubRegIdxFromLaneMask(CoveringLanes);
+  return SubReg;
+}
+
 void AMDGPURewriteAGPRCopyMFMAImpl::replaceSpillWithCopyToVReg(
     MachineInstr &SpillMI, int SpillFI, Register VReg) const {
   const DebugLoc &DL = SpillMI.getDebugLoc();
@@ -431,9 +469,11 @@ void AMDGPURewriteAGPRCopyMFMAImpl::replaceSpillWithCopyToVReg(
     NewCopy = BuildMI(MBB, SpillMI, DL, TII.get(TargetOpcode::COPY), VReg)
                   .add(SpillMI.getOperand(0));
   } else {
+    // Identify the subregs if SpillMI is really a subreg-load.
+    unsigned SubReg = getSubRegFromReload(SpillMI, VReg);
     NewCopy = BuildMI(MBB, SpillMI, DL, TII.get(TargetOpcode::COPY))
                   .add(SpillMI.getOperand(0))
-                  .addReg(VReg);
+                  .addReg(VReg, 0, SubReg);
   }
 
   LIS.ReplaceMachineInstrInMaps(SpillMI, *NewCopy);

@cdevadas
cdevadas requested review from MatzeB and qcolombet January 8, 2026 15:47
Comment thread llvm/lib/CodeGen/TargetRegisterInfo.cpp Outdated
}

unsigned
TargetRegisterInfo::getSubRegIdxFromLaneMask(LaneBitmask LaneMask) const {

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 just getCoveringSubRegIndexes, except it silently doesn't work sometimes?

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.

getCoveringSubRegIndexes isn't useful at all. There is an early exit when the LankeMask exactly matches to a SubReg index.
https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/TargetRegisterInfo.cpp#L562

@cdevadas
cdevadas force-pushed the users/cdevadas/make-getNumSubRegsForSpillOp-member-function branch from 089bb8e to 4142195 Compare January 12, 2026 13:29
@cdevadas
cdevadas force-pushed the users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware branch 2 times, most recently from 0ebaff5 to b6668cf Compare January 12, 2026 15:29
@cdevadas
cdevadas force-pushed the users/cdevadas/make-getNumSubRegsForSpillOp-member-function branch from 4142195 to 2750ee4 Compare January 12, 2026 15:29
@cdevadas
cdevadas force-pushed the users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware branch from b6668cf to a8e5297 Compare January 13, 2026 05:07
@cdevadas
cdevadas force-pushed the users/cdevadas/make-getNumSubRegsForSpillOp-member-function branch from 2750ee4 to 144d8a2 Compare January 13, 2026 05:07
@easyonaadit
easyonaadit force-pushed the users/cdevadas/make-getNumSubRegsForSpillOp-member-function branch from 144d8a2 to 836607c Compare July 31, 2026 09:20
@easyonaadit
easyonaadit force-pushed the users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware branch from a8e5297 to 2a5c8c9 Compare July 31, 2026 09:20
cdevadas and others added 3 commits September 29, 2026 17:28
AMDGPURewriteAGPRCopyMFMA pass is currently not subreg-aware.
In particular, the logic that optimizes spills into COPY
instructions assumes full register reloads. This becomes
problematic when the reload instruction partially restores
a tuple register. This patch introduces the necessary changes
to make this pass subreg-aware, for a future patch that
implements subreg reload during RA.
@easyonaadit
easyonaadit force-pushed the users/cdevadas/make-getNumSubRegsForSpillOp-member-function branch from 836607c to b2dc44e Compare September 29, 2026 13:01
@easyonaadit
easyonaadit force-pushed the users/cdevadas/make-AMDGPURewriteAGPRCopyMFMA-subreg-reload-aware branch from 2a5c8c9 to df21a78 Compare September 29, 2026 13:01
SubRegIdxRanges[Base + Idx].Size == Size)
return Idx;
}
return 0;

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.

Suggested change
return 0;
return 0 /*NoSubRegister*/;

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.

6 participants