Skip to content

Commit 9bae84b

Browse files
LuoYuankeYuanke Luo
andauthored
[RegAlloc] Relax the split constrain on MBB prolog (#168259)
https://reviews.llvm.org/D52052 is to prevent register split on the MBB which have prolog instructions defining the exec register (or mask register that activate the threads of a warp in GPU). The constrain seems too strict, because 1) If the split is allowed, it may fit the free live range of a physical register, and no spill will happen; 2) The register class of register that is under splitting may not be the same to the register that is defined in prolog, so there is no interference with the register being defined in prolog. The current code has another small issue. The MBB->getFirstNonDebugInstr() just skip debug instructions, but SA->getFirstSplitPoint(Number) would skip label and phi instructions. This cause some MBB with label instruction being taken as prolog. This patch is to relax the split constrain on MMB with prolog by checking if the register defined in prolog has the common register class with the register being split. It allow the split if the register defined in prolog is physical register or there is no common register class. --------- Co-authored-by: Yuanke Luo <[email protected]>
1 parent 99addbf commit 9bae84b

11 files changed

+3519
-3244
lines changed

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,7 @@ bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
774774
// Abort if the spill cannot be inserted at the MBB' start
775775
if (((BC.Entry == SpillPlacement::MustSpill) ||
776776
(BC.Entry == SpillPlacement::PrefSpill)) &&
777-
SlotIndex::isEarlierInstr(BI.FirstInstr,
778-
SA->getFirstSplitPoint(BC.Number)))
777+
!SA->canSplitBeforeProlog(BC.Number))
779778
return false;
780779
}
781780

@@ -830,11 +829,7 @@ bool RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
830829
BCS[B].Number = Number;
831830

832831
// Abort if the spill cannot be inserted at the MBB' start
833-
MachineBasicBlock *MBB = MF->getBlockNumbered(Number);
834-
auto FirstNonDebugInstr = MBB->getFirstNonDebugInstr();
835-
if (FirstNonDebugInstr != MBB->end() &&
836-
SlotIndex::isEarlierInstr(LIS->getInstructionIndex(*FirstNonDebugInstr),
837-
SA->getFirstSplitPoint(Number)))
832+
if (!SA->canSplitBeforeProlog(Number))
838833
return false;
839834
// Interference for the live-in value.
840835
if (Intf.first() <= Indexes->getMBBStartIdx(Number))

llvm/lib/CodeGen/SplitKit.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,54 @@ InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
147147
return LIS.getInstructionFromIndex(LIP);
148148
}
149149

150+
bool InsertPointAnalysis::canSplitBeforeProlog(const LiveInterval &CurLI,
151+
const MachineBasicBlock &MBB) {
152+
const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo();
153+
154+
for (auto &MI : MBB) {
155+
if (MI.isPHI() || MI.isPosition() || MI.isDebugInstr() ||
156+
MI.isPseudoProbe())
157+
continue;
158+
159+
if (!TII->isBasicBlockPrologue(MI))
160+
return true;
161+
162+
for (auto &MO : MI.operands()) {
163+
if (!MO.isReg() || !MO.isDef() || !MO.getReg().isVirtual())
164+
continue;
165+
166+
// For the AMDGPU target if a MBB contains exec mask restore preamble,
167+
// SplitEditor may get state when it cannot insert a spill instruction
168+
// at the begin of the MBB.
169+
// E.g. for a MIR
170+
// bb.100:
171+
// %1 = S_OR_SAVEEXEC_B64 %2, implicit-def $exec, implicit-def $scc,
172+
// implicit $exec
173+
// ...
174+
// use %1
175+
// If the regalloc try to allocate a virtreg to the physreg already
176+
// assigned to virtreg %1 and the pyhsreg is computed as the best
177+
// candidate for split, it may insert COPY instruction.
178+
// bb.100:
179+
// %1 = S_OR_SAVEEXEC_B64 %2, implicit-def $exec, implicit-def $scc,
180+
// implicit $exec
181+
// %2 = COPY %orig
182+
// ...
183+
// use %1
184+
// Thus %1 and %orig still have interference. We may add cost for the
185+
// physreg candidate or abandon the candidate.
186+
const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
187+
const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
188+
const TargetRegisterClass *RC = MRI.getRegClass(MO.getReg());
189+
const TargetRegisterClass *CurRC = MRI.getRegClass(CurLI.reg());
190+
if (TRI->getCommonSubClass(RC, CurRC))
191+
return false;
192+
}
193+
}
194+
195+
return true;
196+
}
197+
150198
//===----------------------------------------------------------------------===//
151199
// Split Analysis
152200
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/SplitKit.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis {
8989
return Res;
9090
}
9191

92+
/// Return true if we can split \pCurLI before \pMBB's prolog.
93+
bool canSplitBeforeProlog(const LiveInterval &CurLI,
94+
const MachineBasicBlock &MBB);
9295
};
9396

9497
/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
@@ -247,6 +250,11 @@ class LLVM_LIBRARY_VISIBILITY SplitAnalysis {
247250
SlotIndex getFirstSplitPoint(unsigned Num) {
248251
return IPA.getFirstInsertPoint(*MF.getBlockNumbered(Num));
249252
}
253+
254+
bool canSplitBeforeProlog(unsigned Num) {
255+
MachineBasicBlock *BB = MF.getBlockNumbered(Num);
256+
return IPA.canSplitBeforeProlog(*CurLI, *BB);
257+
}
250258
};
251259

252260
/// SplitEditor - Edit machine code and LiveIntervals for live range

0 commit comments

Comments
 (0)