Repository navigation
[AMDGPU] Make AMDGPURewriteAGPRCopyMFMA aware of subreg reload - #174998
Conversation
|
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.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
@llvm/pr-subscribers-backend-amdgpu Author: Christudasan Devadasan (cdevadas) ChangesAMDGPURewriteAGPRCopyMFMA pass is currently not subreg-aware. 3 Files Affected:
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);
|
| } | ||
|
|
||
| unsigned | ||
| TargetRegisterInfo::getSubRegIdxFromLaneMask(LaneBitmask LaneMask) const { |
There was a problem hiding this comment.
This is just getCoveringSubRegIndexes, except it silently doesn't work sometimes?
There was a problem hiding this comment.
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
089bb8e to
4142195
Compare
0ebaff5 to
b6668cf
Compare
4142195 to
2750ee4
Compare
b6668cf to
a8e5297
Compare
2750ee4 to
144d8a2
Compare
144d8a2 to
836607c
Compare
a8e5297 to
2a5c8c9
Compare
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.
836607c to
b2dc44e
Compare
2a5c8c9 to
df21a78
Compare
| SubRegIdxRanges[Base + Idx].Size == Size) | ||
| return Idx; | ||
| } | ||
| return 0; |
There was a problem hiding this comment.
| return 0; | |
| return 0 /*NoSubRegister*/; |

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.