Skip to content

Commit 9f102f0

Browse files
committed
[CodeGen][NewPM] Port SlotIndexes to new pass manager
- Add `SlotIndexesAnalysis`. - Add `SlotIndexesPrinterPass`. - Use `SlotIndexesWrapperPass` in legacy pass.
1 parent 167c860 commit 9f102f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+187
-105
lines changed

llvm/include/llvm/CodeGen/SlotIndexes.h

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/CodeGen/MachineFunctionPass.h"
2929
#include "llvm/CodeGen/MachineInstr.h"
3030
#include "llvm/CodeGen/MachineInstrBundle.h"
31+
#include "llvm/CodeGen/MachinePassManager.h"
3132
#include "llvm/Support/Allocator.h"
3233
#include <algorithm>
3334
#include <cassert>
@@ -293,7 +294,9 @@ class raw_ostream;
293294
/// SlotIndexes pass.
294295
///
295296
/// This pass assigns indexes to each instruction.
296-
class SlotIndexes : public MachineFunctionPass {
297+
class SlotIndexes {
298+
friend class SlotIndexesWrapperPass;
299+
297300
private:
298301
// IndexListEntry allocator.
299302
BumpPtrAllocator ileAllocator;
@@ -313,6 +316,13 @@ class raw_ostream;
313316
/// and MBB id.
314317
SmallVector<IdxMBBPair, 8> idx2MBBMap;
315318

319+
// For legacy pass manager.
320+
SlotIndexes() = default;
321+
322+
void clear();
323+
324+
void analyze(MachineFunction &MF);
325+
316326
IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
317327
IndexListEntry *entry =
318328
static_cast<IndexListEntry *>(ileAllocator.Allocate(
@@ -327,16 +337,18 @@ class raw_ostream;
327337
void renumberIndexes(IndexList::iterator curItr);
328338

329339
public:
330-
static char ID;
340+
SlotIndexes(SlotIndexes &&) = default;
331341

332-
SlotIndexes();
342+
SlotIndexes(MachineFunction &MF) { analyze(MF); }
333343

334-
~SlotIndexes() override;
344+
~SlotIndexes();
335345

336-
void getAnalysisUsage(AnalysisUsage &au) const override;
337-
void releaseMemory() override;
346+
void reanalyze(MachineFunction &MF) {
347+
clear();
348+
analyze(MF);
349+
}
338350

339-
bool runOnMachineFunction(MachineFunction &fn) override;
351+
void print(raw_ostream &OS) const;
340352

341353
/// Dump the indexes.
342354
void dump() const;
@@ -629,6 +641,44 @@ class raw_ostream;
629641
struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
630642
};
631643

644+
class SlotIndexesAnalysis : public AnalysisInfoMixin<SlotIndexesAnalysis> {
645+
friend AnalysisInfoMixin<SlotIndexesAnalysis>;
646+
static AnalysisKey Key;
647+
648+
public:
649+
using Result = SlotIndexes;
650+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &);
651+
};
652+
653+
class SlotIndexesPrinterPass : public PassInfoMixin<SlotIndexesPrinterPass> {
654+
raw_ostream &OS;
655+
656+
public:
657+
explicit SlotIndexesPrinterPass(raw_ostream &OS) : OS(OS) {}
658+
PreservedAnalyses run(MachineFunction &MF,
659+
MachineFunctionAnalysisManager &MFAM);
660+
static bool isRequired() { return true; }
661+
};
662+
663+
class SlotIndexesWrapperPass : public MachineFunctionPass {
664+
SlotIndexes SI;
665+
666+
public:
667+
static char ID;
668+
669+
SlotIndexesWrapperPass();
670+
671+
void getAnalysisUsage(AnalysisUsage &au) const override;
672+
void releaseMemory() override { SI.clear(); }
673+
674+
bool runOnMachineFunction(MachineFunction &fn) override {
675+
SI.analyze(fn);
676+
return false;
677+
}
678+
679+
SlotIndexes &getSI() { return SI; }
680+
};
681+
632682
} // end namespace llvm
633683

634684
#endif // LLVM_CODEGEN_SLOTINDEXES_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void initializeShrinkWrapPass(PassRegistry&);
279279
void initializeSingleLoopExtractorPass(PassRegistry&);
280280
void initializeSinkingLegacyPassPass(PassRegistry&);
281281
void initializeSjLjEHPreparePass(PassRegistry&);
282-
void initializeSlotIndexesPass(PassRegistry&);
282+
void initializeSlotIndexesWrapperPassPass(PassRegistry &);
283283
void initializeSpeculativeExecutionLegacyPassPass(PassRegistry&);
284284
void initializeSpillPlacementPass(PassRegistry&);
285285
void initializeStackColoringPass(PassRegistry&);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
9595
MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
9696
MachinePostDominatorTreeAnalysis())
9797
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
98+
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
9899
// LiveVariables currently requires pure SSA form.
99100
// FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
100101
// LiveVariables can be removed completely, and LiveIntervals can be directly
@@ -103,7 +104,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
103104
// MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
104105

105106
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
106-
// MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
107107
// MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
108108
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",
109109
// LazyMachineBlockFrequencyInfoAnalysis())
@@ -138,6 +138,7 @@ MACHINE_FUNCTION_PASS("print<machine-dom-tree>",
138138
MachineDominatorTreePrinterPass(dbgs()))
139139
MACHINE_FUNCTION_PASS("print<machine-post-dom-tree>",
140140
MachinePostDominatorTreePrinterPass(dbgs()))
141+
MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(dbgs()))
141142
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
142143
RequireAllMachineFunctionPropertiesPass())
143144
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
123123
initializeShadowStackGCLoweringPass(Registry);
124124
initializeShrinkWrapPass(Registry);
125125
initializeSjLjEHPreparePass(Registry);
126-
initializeSlotIndexesPass(Registry);
126+
initializeSlotIndexesWrapperPassPass(Registry);
127127
initializeStackColoringPass(Registry);
128128
initializeStackFrameLayoutAnalysisPassPass(Registry);
129129
initializeStackMapLivenessPass(Registry);

llvm/lib/CodeGen/LiveIntervals.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ char &llvm::LiveIntervalsID = LiveIntervals::ID;
6262
INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", "Live Interval Analysis",
6363
false, false)
6464
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
65-
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
65+
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
6666
INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
6767
"Live Interval Analysis", false, false)
6868

@@ -89,8 +89,8 @@ void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
8989
AU.addPreservedID(MachineLoopInfoID);
9090
AU.addRequiredTransitiveID(MachineDominatorsID);
9191
AU.addPreservedID(MachineDominatorsID);
92-
AU.addPreserved<SlotIndexes>();
93-
AU.addRequiredTransitive<SlotIndexes>();
92+
AU.addPreserved<SlotIndexesWrapperPass>();
93+
AU.addRequiredTransitive<SlotIndexesWrapperPass>();
9494
MachineFunctionPass::getAnalysisUsage(AU);
9595
}
9696

@@ -122,7 +122,7 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
122122
MRI = &MF->getRegInfo();
123123
TRI = MF->getSubtarget().getRegisterInfo();
124124
TII = MF->getSubtarget().getInstrInfo();
125-
Indexes = &getAnalysis<SlotIndexes>();
125+
Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
126126
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
127127

128128
if (!LICalc)

llvm/lib/CodeGen/LiveStacks.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ using namespace llvm;
2323
char LiveStacks::ID = 0;
2424
INITIALIZE_PASS_BEGIN(LiveStacks, DEBUG_TYPE,
2525
"Live Stack Slot Analysis", false, false)
26-
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
26+
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
2727
INITIALIZE_PASS_END(LiveStacks, DEBUG_TYPE,
2828
"Live Stack Slot Analysis", false, false)
2929

3030
char &llvm::LiveStacksID = LiveStacks::ID;
3131

3232
void LiveStacks::getAnalysisUsage(AnalysisUsage &AU) const {
3333
AU.setPreservesAll();
34-
AU.addPreserved<SlotIndexes>();
35-
AU.addRequiredTransitive<SlotIndexes>();
34+
AU.addPreserved<SlotIndexesWrapperPass>();
35+
AU.addRequiredTransitive<SlotIndexesWrapperPass>();
3636
MachineFunctionPass::getAnalysisUsage(AU);
3737
}
3838

llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class ReleaseModePriorityAdvisorAnalysis final
134134
private:
135135
void getAnalysisUsage(AnalysisUsage &AU) const override {
136136
AU.setPreservesAll();
137-
AU.addRequired<SlotIndexes>();
137+
AU.addRequired<SlotIndexesWrapperPass>();
138138
RegAllocPriorityAdvisorAnalysis::getAnalysisUsage(AU);
139139
}
140140

@@ -151,7 +151,7 @@ class ReleaseModePriorityAdvisorAnalysis final
151151
InteractiveChannelBaseName + ".in");
152152
}
153153
return std::make_unique<MLPriorityAdvisor>(
154-
MF, RA, &getAnalysis<SlotIndexes>(), Runner.get());
154+
MF, RA, &getAnalysis<SlotIndexesWrapperPass>().getSI(), Runner.get());
155155
}
156156
std::unique_ptr<MLModelRunner> Runner;
157157
};
@@ -215,7 +215,7 @@ class DevelopmentModePriorityAdvisorAnalysis final
215215
private:
216216
void getAnalysisUsage(AnalysisUsage &AU) const override {
217217
AU.setPreservesAll();
218-
AU.addRequired<SlotIndexes>();
218+
AU.addRequired<SlotIndexesWrapperPass>();
219219
RegAllocPriorityAdvisorAnalysis::getAnalysisUsage(AU);
220220
}
221221

@@ -266,7 +266,8 @@ class DevelopmentModePriorityAdvisorAnalysis final
266266
}
267267

268268
return std::make_unique<DevelopmentModePriorityAdvisor>(
269-
MF, RA, &getAnalysis<SlotIndexes>(), Runner.get(), Log.get());
269+
MF, RA, &getAnalysis<SlotIndexesWrapperPass>().getSI(), Runner.get(),
270+
Log.get());
270271
}
271272

272273
std::unique_ptr<MLModelRunner> Runner;

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,8 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(
11621162
<< printMBBReference(*Succ) << '\n');
11631163

11641164
LiveIntervals *LIS = P.getAnalysisIfAvailable<LiveIntervals>();
1165-
SlotIndexes *Indexes = P.getAnalysisIfAvailable<SlotIndexes>();
1165+
auto *SIWrapper = P.getAnalysisIfAvailable<SlotIndexesWrapperPass>();
1166+
SlotIndexes *Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
11661167
if (LIS)
11671168
LIS->insertMBBInMaps(NMBB);
11681169
else if (Indexes)

llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ struct MachineFunctionPrinterPass : public MachineFunctionPass {
3939

4040
void getAnalysisUsage(AnalysisUsage &AU) const override {
4141
AU.setPreservesAll();
42-
AU.addUsedIfAvailable<SlotIndexes>();
42+
AU.addUsedIfAvailable<SlotIndexesWrapperPass>();
4343
MachineFunctionPass::getAnalysisUsage(AU);
4444
}
4545

4646
bool runOnMachineFunction(MachineFunction &MF) override {
4747
if (!isFunctionInPrintList(MF.getName()))
4848
return false;
4949
OS << "# " << Banner << ":\n";
50-
MF.print(OS, getAnalysisIfAvailable<SlotIndexes>());
50+
auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
51+
MF.print(OS, SIWrapper ? &SIWrapper->getSI() : nullptr);
5152
return false;
5253
}
5354
};

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ INITIALIZE_PASS_BEGIN(MachineScheduler, DEBUG_TYPE,
268268
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
269269
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
270270
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
271-
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
271+
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
272272
INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
273273
INITIALIZE_PASS_END(MachineScheduler, DEBUG_TYPE,
274274
"Machine Instruction Scheduler", false, false)
@@ -283,8 +283,8 @@ void MachineScheduler::getAnalysisUsage(AnalysisUsage &AU) const {
283283
AU.addRequired<MachineLoopInfo>();
284284
AU.addRequired<AAResultsWrapperPass>();
285285
AU.addRequired<TargetPassConfig>();
286-
AU.addRequired<SlotIndexes>();
287-
AU.addPreserved<SlotIndexes>();
286+
AU.addRequired<SlotIndexesWrapperPass>();
287+
AU.addPreserved<SlotIndexesWrapperPass>();
288288
AU.addRequired<LiveIntervals>();
289289
AU.addPreserved<LiveIntervals>();
290290
MachineFunctionPass::getAnalysisUsage(AU);

0 commit comments

Comments
 (0)