Skip to content

Commit 6da7a6e

Browse files
committed
Added configuration flag for StackSlotColoring & refactored comments.
1 parent 7e610af commit 6da7a6e

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ namespace llvm {
371371

372372
/// StackSlotColoring - This pass performs stack slot coloring.
373373
extern char &StackSlotColoringID;
374+
FunctionPass *
375+
createStackSlotColoring(bool preserveRegAllocNeededAnalysis = false);
374376

375377
/// This pass lays out funclets contiguously.
376378
extern char &FuncletLayoutID;

llvm/lib/CodeGen/StackSlotColoring.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ namespace {
6767
const MachineBlockFrequencyInfo *MBFI = nullptr;
6868
SlotIndexes *Indexes = nullptr;
6969

70+
// - preserves Analysis passes in case RA may be called afterwards.
71+
bool preserveRegAllocNeededAnalysis = false;
72+
7073
// SSIntervals - Spill slot intervals.
7174
std::vector<LiveInterval*> SSIntervals;
7275

@@ -142,7 +145,9 @@ namespace {
142145
public:
143146
static char ID; // Pass identification
144147

145-
StackSlotColoring() : MachineFunctionPass(ID) {
148+
StackSlotColoring(bool preserveRegAllocNeededAnalysis_ = false)
149+
: MachineFunctionPass(ID),
150+
preserveRegAllocNeededAnalysis(preserveRegAllocNeededAnalysis_) {
146151
initializeStackSlotColoringPass(*PassRegistry::getPassRegistry());
147152
}
148153

@@ -155,12 +160,14 @@ namespace {
155160
AU.addPreserved<MachineBlockFrequencyInfo>();
156161
AU.addPreservedID(MachineDominatorsID);
157162

158-
/// NOTE: As in AMDGPU pass pipeline, reg alloc is spillted into 2 phases
159-
/// and StackSlotColoring is invoked after each phase, it becomes
160-
/// important to preserve additional analyses result to be used by VGPR
161-
/// regAlloc, after being done with SGPR regAlloc and its related passes.
162-
AU.addPreserved<LiveIntervals>();
163-
AU.addPreserved<LiveDebugVariables>();
163+
// As in some Target's pipeline, register allocation (RA) might be
164+
// splitted into multiple phases based on register class. So, this pass
165+
// may be invoked multiple times requiring it to save these analyses to be
166+
// used by RA later.
167+
if (preserveRegAllocNeededAnalysis) {
168+
AU.addPreserved<LiveIntervals>();
169+
AU.addPreserved<LiveDebugVariables>();
170+
}
164171

165172
MachineFunctionPass::getAnalysisUsage(AU);
166173
}
@@ -506,9 +513,6 @@ bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) {
506513
++I;
507514
}
508515

509-
/// FIXED: As this pass preserves SlotIndexesAnalysis result, any
510-
/// addition/removal of MI needs corresponding update in SlotIndexAnalysis,
511-
/// to avoid corruption of SlotIndexesAnalysis result.
512516
for (MachineInstr *MI : toErase) {
513517
MI->eraseFromParent();
514518
Indexes->removeMachineInstrFromMaps(*MI);
@@ -565,3 +569,8 @@ bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
565569

566570
return Changed;
567571
}
572+
573+
FunctionPass *
574+
llvm::createStackSlotColoring(bool preserveRegAllocNeededAnalysis) {
575+
return new StackSlotColoring(preserveRegAllocNeededAnalysis);
576+
}

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,8 +1406,11 @@ bool GCNPassConfig::addRegAssignAndRewriteOptimized() {
14061406
// since FastRegAlloc does the replacements itself.
14071407
addPass(createVirtRegRewriter(false));
14081408

1409-
// Optimizes SGPR spills into VGPR lanes for non-interferring spill-ranges.
1410-
addPass(&StackSlotColoringID);
1409+
// As by this point SGPR's RA is done introducing SGPR spills to stack frame
1410+
// through SGPRAllocPass. So, invoking StackSlotColoring here, may allow these
1411+
// SGPR spills to re-use stack slots, before these spills is further lowered
1412+
// down via SILowerSGPRSpills(i.e. equivalent of PEI for SGPRs).
1413+
addPass(createStackSlotColoring(/*preserveRegAllocNeededAnalysis*/ true));
14111414

14121415
// Equivalent of PEI for SGPRs.
14131416
addPass(&SILowerSGPRSpillsID);

llvm/lib/Target/AMDGPU/SILowerSGPRSpills.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class SILowerSGPRSpills : public MachineFunctionPass {
5252
void calculateSaveRestoreBlocks(MachineFunction &MF);
5353
bool spillCalleeSavedRegs(MachineFunction &MF,
5454
SmallVectorImpl<int> &CalleeSavedFIs);
55-
void extendWWMVirtRegLiveness(MachineFunction &MF, SlotIndexes *Indexes,
56-
LiveIntervals *LIS);
55+
void extendWWMVirtRegLiveness(MachineFunction &MF, LiveIntervals *LIS);
5756

5857
bool runOnMachineFunction(MachineFunction &MF) override;
5958

@@ -261,7 +260,6 @@ bool SILowerSGPRSpills::spillCalleeSavedRegs(
261260
}
262261

263262
void SILowerSGPRSpills::extendWWMVirtRegLiveness(MachineFunction &MF,
264-
SlotIndexes *Indexes,
265263
LiveIntervals *LIS) {
266264
// TODO: This is a workaround to avoid the unmodelled liveness computed with
267265
// whole-wave virtual registers when allocated together with the regular VGPR
@@ -280,7 +278,6 @@ void SILowerSGPRSpills::extendWWMVirtRegLiveness(MachineFunction &MF,
280278
for (auto Reg : MFI->getSGPRSpillVGPRs()) {
281279
for (MachineBasicBlock *SaveBlock : SaveBlocks) {
282280
MachineBasicBlock::iterator InsertBefore = SaveBlock->begin();
283-
MachineInstrSpan MIS(InsertBefore, SaveBlock);
284281

285282
DebugLoc DL = SaveBlock->findDebugLoc(InsertBefore);
286283
auto MIB = BuildMI(*SaveBlock, InsertBefore, DL,
@@ -289,13 +286,8 @@ void SILowerSGPRSpills::extendWWMVirtRegLiveness(MachineFunction &MF,
289286
// Set SGPR_SPILL asm printer flag
290287
MIB->setAsmPrinterFlag(AMDGPU::SGPR_SPILL);
291288

292-
if (LIS) {
289+
if (LIS)
293290
LIS->InsertMachineInstrInMaps(*MIB);
294-
} else if (Indexes) {
295-
assert(std::distance(MIS.begin(), InsertBefore) == 1);
296-
MachineInstr &Inst = *std::prev(InsertBefore);
297-
Indexes->insertMachineInstrInMaps(Inst);
298-
}
299291
}
300292
}
301293

@@ -310,12 +302,8 @@ void SILowerSGPRSpills::extendWWMVirtRegLiveness(MachineFunction &MF,
310302
TII->get(TargetOpcode::KILL));
311303
MIB.addReg(Reg);
312304

313-
if (LIS) {
305+
if (LIS)
314306
LIS->InsertMachineInstrInMaps(*MIB);
315-
} else if (Indexes) {
316-
MachineInstr &Inst = *std::prev(InsertBefore);
317-
Indexes->insertMachineInstrInMaps(Inst);
318-
}
319307
}
320308
}
321309
}
@@ -406,7 +394,7 @@ bool SILowerSGPRSpills::runOnMachineFunction(MachineFunction &MF) {
406394
}
407395

408396
if (SpilledToVirtVGPRLanes) {
409-
extendWWMVirtRegLiveness(MF, Indexes, LIS);
397+
extendWWMVirtRegLiveness(MF, LIS);
410398
if (LIS) {
411399
// Compute the LiveInterval for the newly created virtual registers.
412400
for (auto Reg : FuncInfo->getSGPRSpillVGPRs())

0 commit comments

Comments
 (0)