Skip to content

Commit 6963db4

Browse files
committed
Named operand and stable partition applied
(cherry picked from commit ee6d876fcc3d84d6ea3a68b3eee1ce97e714b6e6)
1 parent ca100cf commit 6963db4

File tree

6 files changed

+206
-193
lines changed

6 files changed

+206
-193
lines changed

llvm/include/llvm/CodeGen/MachineRegisterInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ class MachineRegisterInfo {
915915
/// VRM is the current virtual register map showing allocations made so far.
916916
void getPhysRegAntiHints(Register VReg,
917917
SmallVectorImpl<MCPhysReg> &PhysAntiHints,
918-
const VirtRegMap *VRM) const;
918+
const VirtRegMap &VRM) const;
919919

920920
/// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
921921
/// specified register as undefined which causes the DBG_VALUE to be

llvm/lib/CodeGen/AllocationOrder.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ AllocationOrder AllocationOrder::create(Register VirtReg, const VirtRegMap &VRM,
4848

4949
// Get anti-hints
5050
SmallVector<MCPhysReg, 16> AntiHintedPhysRegs;
51-
MRI.getPhysRegAntiHints(VirtReg, AntiHintedPhysRegs, &VRM);
51+
MRI.getPhysRegAntiHints(VirtReg, AntiHintedPhysRegs, VRM);
5252

5353
LLVM_DEBUG({
5454
if (!AntiHintedPhysRegs.empty()) {
@@ -84,29 +84,34 @@ AllocationOrder AllocationOrder::create(Register VirtReg, const VirtRegMap &VRM,
8484

8585
void AllocationOrder::applyAntiHints(ArrayRef<MCPhysReg> AntiHintedPhysRegs,
8686
const TargetRegisterInfo *TRI) {
87+
// Helper to check if a register overlaps with any anti-hint
88+
auto isAntiHinted = [&](MCPhysReg Reg) {
89+
return std::any_of(
90+
AntiHintedPhysRegs.begin(), AntiHintedPhysRegs.end(),
91+
[&](MCPhysReg AntiHint) { return TRI->regsOverlap(Reg, AntiHint); });
92+
};
93+
8794
// Create filtered order
8895
FilteredOrderStorage.clear();
89-
FilteredOrderStorage.reserve(Order.size());
90-
91-
// Add non-anti-hinted registers first
92-
for (MCPhysReg PhysReg : Order) {
93-
if (!is_contained(AntiHintedPhysRegs, PhysReg)) {
94-
FilteredOrderStorage.push_back(PhysReg);
95-
}
96-
}
96+
FilteredOrderStorage.assign(Order.begin(), Order.end());
9797

98-
// Add anti-hinted registers at the end as last resort
99-
for (MCPhysReg PhysReg : Order) {
100-
if (is_contained(AntiHintedPhysRegs, PhysReg)) {
101-
FilteredOrderStorage.push_back(PhysReg);
102-
}
103-
}
98+
// Partition: non-anti-hinted registers go first
99+
auto PartitionPoint = std::stable_partition(
100+
FilteredOrderStorage.begin(), FilteredOrderStorage.end(),
101+
[&](MCPhysReg Reg) { return !isAntiHinted(Reg); });
104102

105103
// Update Order
106104
Order = FilteredOrderStorage;
107105

108106
LLVM_DEBUG({
109-
dbgs() << "moved " << AntiHintedPhysRegs.size()
110-
<< " anti-hinted registers to end of allocation order\n";
107+
size_t NonAntiHintedCount =
108+
std::distance(FilteredOrderStorage.begin(), PartitionPoint);
109+
size_t AntiHintedCount =
110+
std::distance(PartitionPoint, FilteredOrderStorage.end());
111+
dbgs() << " Added " << NonAntiHintedCount
112+
<< " non-anti-hinted registers first\n"
113+
<< " Added " << AntiHintedCount
114+
<< " anti-hinted registers at the end\n"
115+
<< " Anti-hint filtering complete\n";
111116
});
112117
}

llvm/lib/CodeGen/MachineRegisterInfo.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -682,22 +682,19 @@ bool MachineRegisterInfo::isReservedRegUnit(unsigned Unit) const {
682682

683683
void MachineRegisterInfo::getPhysRegAntiHints(
684684
Register VReg, SmallVectorImpl<MCPhysReg> &PhysAntiHints,
685-
const VirtRegMap *VRM) const {
685+
const VirtRegMap &VRM) const {
686686
assert(VReg.isVirtual());
687-
if (!AntiHintRegs.inBounds(VReg) || !VRM)
687+
if (!AntiHintRegs.inBounds(VReg))
688688
return;
689689

690690
const SmallVector<Register, 4> &AntiHints = AntiHintRegs[VReg];
691-
const TargetRegisterInfo *TRI = getTargetRegisterInfo();
692691

693692
for (Register AntiHintVReg : AntiHints) {
694693
// Check if the anti-hinted register has been allocated
695-
if (VRM->hasPhys(AntiHintVReg)) {
696-
MCPhysReg PhysReg = VRM->getPhys(AntiHintVReg);
697-
// Add the physical register and all its aliases
698-
for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
699-
PhysAntiHints.push_back(*AI);
700-
}
694+
if (VRM.hasPhys(AntiHintVReg)) {
695+
MCPhysReg PhysReg = VRM.getPhys(AntiHintVReg);
696+
// Add the physical register
697+
PhysAntiHints.push_back(PhysReg);
701698
}
702699
}
703700

llvm/lib/Target/AMDGPU/GCNPreRAOptimizations.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,29 @@ bool GCNPreRAOptimizationsImpl::run(MachineFunction &MF) {
269269
// Handle MFMA instructions
270270
if (SIInstrInfo::isMFMA(MI)) {
271271
SmallVector<Register, 4> MFMARegisters;
272-
auto collectMFMARegister = [&](unsigned OpIdx) {
273-
if (OpIdx >= MI.getNumOperands())
272+
// Helper to get named operand
273+
auto collectNamedOperand = [&](AMDGPU::OpName OpName,
274+
const char *OpNameStr) {
275+
unsigned Opc = MI.getOpcode();
276+
int OpIdx = AMDGPU::getNamedOperandIdx(Opc, OpName);
277+
if (OpIdx == -1) {
278+
LLVM_DEBUG(dbgs() << " Named operand " << OpNameStr
279+
<< " not found\n");
274280
return;
275-
281+
}
276282
const MachineOperand &MO = MI.getOperand(OpIdx);
277-
if (MO.isReg() && MO.getReg().isVirtual())
283+
if (MO.isReg() && MO.getReg().isVirtual()) {
278284
MFMARegisters.push_back(MO.getReg());
285+
LLVM_DEBUG(dbgs()
286+
<< " Collected " << OpNameStr << " (Op" << OpIdx
287+
<< "): " << printReg(MO.getReg(), TRI) << "\n");
288+
}
279289
};
280-
// Only collect Matrix C (operand 3) and destination (operand 0)
281-
// registers
282-
collectMFMARegister(0);
283-
collectMFMARegister(3);
290+
291+
// Collect destination and source C registers
292+
collectNamedOperand(AMDGPU::OpName::vdst, "vdst"); // Destination
293+
collectNamedOperand(AMDGPU::OpName::src2,
294+
"src2"); // Matrix C (accumulator)
284295

285296
if (!MFMARegisters.empty()) {
286297
RecentMFMAs.emplace_back(CurrentSlot, std::move(MFMARegisters));

0 commit comments

Comments
 (0)