Skip to content

Commit 492312f

Browse files
committed
[CodeGen][NewPM] Port RegAllocGreedy to NPM
1 parent 2d6330f commit 492312f

File tree

8 files changed

+196
-63
lines changed

8 files changed

+196
-63
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ class LLVM_ABI MachineFunction {
927927

928928
/// Run the current MachineFunction through the machine code verifier, useful
929929
/// for debugger use.
930+
/// TODO: Add the param LiveStks
930931
/// \returns true if no problems were found.
931932
bool verify(LiveIntervals *LiveInts, SlotIndexes *Indexes,
932933
const char *Banner = nullptr, raw_ostream *OS = nullptr,

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ namespace llvm {
171171
extern char &LiveRangeShrinkID;
172172

173173
/// Greedy register allocator.
174-
extern char &RAGreedyID;
174+
extern char &RAGreedyLegacyID;
175175

176176
/// Basic register allocator.
177177
extern char &RABasicID;

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void initializeProfileSummaryInfoWrapperPassPass(PassRegistry &);
248248
void initializePromoteLegacyPassPass(PassRegistry &);
249249
void initializeRABasicPass(PassRegistry &);
250250
void initializePseudoProbeInserterPass(PassRegistry &);
251-
void initializeRAGreedyPass(PassRegistry &);
251+
void initializeRAGreedyLegacyPass(PassRegistry &);
252252
void initializeReachingDefAnalysisPass(PassRegistry &);
253253
void initializeReassociateLegacyPassPass(PassRegistry &);
254254
void initializeRegAllocEvictionAdvisorAnalysisLegacyPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ MACHINE_FUNCTION_PASS_WITH_PARAMS(
194194
return parseRegAllocFastPassOptions(*PB, Params);
195195
},
196196
"filter=reg-filter;no-clear-vregs")
197+
198+
MACHINE_FUNCTION_PASS_WITH_PARAMS(
199+
"regallocgreedy", "RAGreedy",
200+
[](RegAllocFilterFunc F) { return RAGreedyPass(F); },
201+
[PB = this](StringRef Params) {
202+
// TODO: parseRegAllocFilter(*PB, Params);
203+
return Expected<RegAllocFilterFunc>(nullptr);
204+
}, ""
205+
)
197206
#undef MACHINE_FUNCTION_PASS_WITH_PARAMS
198207

199208
// After a pass is converted to new pass manager, its entry should be moved from

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
112112
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
113113
initializeProcessImplicitDefsPass(Registry);
114114
initializeRABasicPass(Registry);
115-
initializeRAGreedyPass(Registry);
115+
initializeRAGreedyLegacyPass(Registry);
116116
initializeRegAllocFastPass(Registry);
117117
initializeRegUsageInfoCollectorLegacyPass(Registry);
118118
initializeRegUsageInfoPropagationLegacyPass(Registry);

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 147 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#include "llvm/CodeGen/MachineLoopInfo.h"
4444
#include "llvm/CodeGen/MachineOperand.h"
4545
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
46+
#include "llvm/CodeGen/MachinePassManager.h"
4647
#include "llvm/CodeGen/MachineRegisterInfo.h"
4748
#include "llvm/CodeGen/RegAllocEvictionAdvisor.h"
49+
#include "llvm/CodeGen/RegAllocGreedyPass.h"
4850
#include "llvm/CodeGen/RegAllocPriorityAdvisor.h"
4951
#include "llvm/CodeGen/RegAllocRegistry.h"
5052
#include "llvm/CodeGen/RegisterClassInfo.h"
@@ -55,6 +57,7 @@
5557
#include "llvm/CodeGen/TargetRegisterInfo.h"
5658
#include "llvm/CodeGen/TargetSubtargetInfo.h"
5759
#include "llvm/CodeGen/VirtRegMap.h"
60+
#include "llvm/IR/Analysis.h"
5861
#include "llvm/IR/DebugInfoMetadata.h"
5962
#include "llvm/IR/Function.h"
6063
#include "llvm/IR/LLVMContext.h"
@@ -146,11 +149,134 @@ static cl::opt<unsigned> SplitThresholdForRegWithHint(
146149
static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
147150
createGreedyRegisterAllocator);
148151

149-
char RAGreedy::ID = 0;
150-
char &llvm::RAGreedyID = RAGreedy::ID;
152+
namespace {
153+
class RAGreedyLegacy : public MachineFunctionPass {
154+
RegAllocFilterFunc F;
151155

152-
INITIALIZE_PASS_BEGIN(RAGreedy, "greedy",
153-
"Greedy Register Allocator", false, false)
156+
public:
157+
RAGreedyLegacy(const RegAllocFilterFunc F = nullptr);
158+
159+
static char ID;
160+
/// Return the pass name.
161+
StringRef getPassName() const override { return "Greedy Register Allocator"; }
162+
163+
/// RAGreedy analysis usage.
164+
void getAnalysisUsage(AnalysisUsage &AU) const override;
165+
/// Perform register allocation.
166+
bool runOnMachineFunction(MachineFunction &mf) override;
167+
168+
MachineFunctionProperties getRequiredProperties() const override {
169+
return MachineFunctionProperties().set(
170+
MachineFunctionProperties::Property::NoPHIs);
171+
}
172+
173+
MachineFunctionProperties getClearedProperties() const override {
174+
return MachineFunctionProperties().set(
175+
MachineFunctionProperties::Property::IsSSA);
176+
}
177+
};
178+
179+
} // end anonymous namespace
180+
181+
RAGreedyLegacy::RAGreedyLegacy(const RegAllocFilterFunc F)
182+
: MachineFunctionPass(ID), F(F) {
183+
initializeRAGreedyLegacyPass(*PassRegistry::getPassRegistry());
184+
}
185+
186+
RAGreedy::RAGreedy(const RegAllocFilterFunc F) : RegAllocBase(F) {}
187+
188+
void RAGreedy::setAnalyses(RequiredAnalyses &Analyses) {
189+
VRM = Analyses.VRM;
190+
LIS = Analyses.LIS;
191+
Matrix = Analyses.LRM;
192+
Indexes = Analyses.Indexes;
193+
MBFI = Analyses.MBFI;
194+
DomTree = Analyses.DomTree;
195+
Loops = Analyses.Loops;
196+
ORE = Analyses.ORE;
197+
Bundles = Analyses.Bundles;
198+
SpillPlacer = Analyses.SpillPlacer;
199+
DebugVars = Analyses.DebugVars;
200+
LSS = Analyses.LSS;
201+
EvictProvider = Analyses.EvictProvider;
202+
PriorityProvider = Analyses.PriorityProvider;
203+
}
204+
205+
PreservedAnalyses RAGreedyPass::run(MachineFunction &MF,
206+
MachineFunctionAnalysisManager &MFAM) {
207+
MFPropsModifier _(*this, MF);
208+
209+
RAGreedy Impl(Filter);
210+
RAGreedy::RequiredAnalyses Analyses;
211+
212+
Analyses.VRM = &MFAM.getResult<VirtRegMapAnalysis>(MF);
213+
Analyses.LIS = &MFAM.getResult<LiveIntervalsAnalysis>(MF);
214+
Analyses.LRM = &MFAM.getResult<LiveRegMatrixAnalysis>(MF);
215+
Analyses.LSS = &MFAM.getResult<LiveStacksAnalysis>(MF);
216+
Analyses.Indexes = &MFAM.getResult<SlotIndexesAnalysis>(MF);
217+
Analyses.MBFI = &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
218+
Analyses.DomTree = &MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
219+
Analyses.ORE = &MFAM.getResult<MachineOptimizationRemarkEmitterAnalysis>(MF);
220+
Analyses.Loops = &MFAM.getResult<MachineLoopAnalysis>(MF);
221+
Analyses.Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
222+
Analyses.SpillPlacer = &MFAM.getResult<SpillPlacementAnalysis>(MF);
223+
Analyses.DebugVars = &MFAM.getResult<LiveDebugVariablesAnalysis>(MF);
224+
Analyses.EvictProvider =
225+
MFAM.getResult<RegAllocEvictionAdvisorAnalysis>(MF).Provider;
226+
Analyses.PriorityProvider =
227+
MFAM.getResult<RegAllocPriorityAdvisorAnalysis>(MF).Provider;
228+
229+
Impl.setAnalyses(Analyses);
230+
bool Changed = Impl.run(MF);
231+
if (!Changed)
232+
return PreservedAnalyses::all();
233+
auto PA = getMachineFunctionPassPreservedAnalyses();
234+
PA.preserveSet<CFGAnalyses>();
235+
PA.preserve<MachineBlockFrequencyAnalysis>();
236+
PA.preserve<LiveIntervalsAnalysis>();
237+
PA.preserve<SlotIndexesAnalysis>();
238+
PA.preserve<LiveDebugVariablesAnalysis>();
239+
PA.preserve<LiveStacksAnalysis>();
240+
PA.preserve<MachineDominatorTreeAnalysis>();
241+
PA.preserve<MachineLoopAnalysis>();
242+
PA.preserve<VirtRegMapAnalysis>();
243+
PA.preserve<LiveRegMatrixAnalysis>();
244+
return PA;
245+
}
246+
247+
bool RAGreedyLegacy::runOnMachineFunction(MachineFunction &MF) {
248+
RAGreedy Impl(F);
249+
250+
RAGreedy::RequiredAnalyses Analyses;
251+
Analyses.VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
252+
Analyses.LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
253+
Analyses.LSS = &getAnalysis<LiveStacksWrapperLegacy>().getLS();
254+
Analyses.LRM = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
255+
Analyses.Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
256+
Analyses.MBFI =
257+
&getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
258+
Analyses.DomTree =
259+
&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
260+
Analyses.ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
261+
Analyses.Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
262+
Analyses.Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
263+
Analyses.SpillPlacer =
264+
&getAnalysis<SpillPlacementWrapperLegacy>().getResult();
265+
Analyses.DebugVars = &getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV();
266+
Analyses.EvictProvider =
267+
getAnalysis<RegAllocEvictionAdvisorAnalysisLegacy>().getProvider().get();
268+
Analyses.PriorityProvider =
269+
&getAnalysis<RegAllocPriorityAdvisorAnalysisLegacy>().getProvider();
270+
271+
Impl.setAnalyses(Analyses);
272+
return Impl.run(MF);
273+
}
274+
275+
char RAGreedyLegacy::ID = 0;
276+
char &llvm::RAGreedyLegacyID = RAGreedyLegacy::ID;
277+
278+
INITIALIZE_PASS_BEGIN(RAGreedyLegacy, "greedy", "Greedy Register Allocator",
279+
false, false)
154280
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperLegacy)
155281
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
156282
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
@@ -166,8 +292,8 @@ INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy)
166292
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
167293
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysisLegacy)
168294
INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysisLegacy)
169-
INITIALIZE_PASS_END(RAGreedy, "greedy",
170-
"Greedy Register Allocator", false, false)
295+
INITIALIZE_PASS_END(RAGreedyLegacy, "greedy", "Greedy Register Allocator",
296+
false, false)
171297

172298
#ifndef NDEBUG
173299
const char *const RAGreedy::StageName[] = {
@@ -186,17 +312,14 @@ const char *const RAGreedy::StageName[] = {
186312
const float Hysteresis = (2007 / 2048.0f); // 0.97998046875
187313

188314
FunctionPass* llvm::createGreedyRegisterAllocator() {
189-
return new RAGreedy();
315+
return new RAGreedyLegacy();
190316
}
191317

192318
FunctionPass *llvm::createGreedyRegisterAllocator(RegAllocFilterFunc Ftor) {
193-
return new RAGreedy(Ftor);
319+
return new RAGreedyLegacy(Ftor);
194320
}
195321

196-
RAGreedy::RAGreedy(RegAllocFilterFunc F)
197-
: MachineFunctionPass(ID), RegAllocBase(F) {}
198-
199-
void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
322+
void RAGreedyLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
200323
AU.setPreservesCFG();
201324
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
202325
AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>();
@@ -1057,7 +1180,8 @@ void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
10571180
}
10581181

10591182
if (VerifyEnabled)
1060-
MF->verify(this, "After splitting live range around region", &errs());
1183+
MF->verify(LIS, Indexes, "After splitting live range around region",
1184+
&errs());
10611185
}
10621186

10631187
MCRegister RAGreedy::tryRegionSplit(const LiveInterval &VirtReg,
@@ -1326,7 +1450,8 @@ Register RAGreedy::tryBlockSplit(const LiveInterval &VirtReg,
13261450
}
13271451

13281452
if (VerifyEnabled)
1329-
MF->verify(this, "After splitting live range around basic blocks", &errs());
1453+
MF->verify(LIS, Indexes, "After splitting live range around basic blocks",
1454+
&errs());
13301455
return Register();
13311456
}
13321457

@@ -2524,7 +2649,7 @@ MCRegister RAGreedy::selectOrSplitImpl(const LiveInterval &VirtReg,
25242649
DebugVars->splitRegister(r, LRE.regs(), *LIS);
25252650

25262651
if (VerifyEnabled)
2527-
MF->verify(this, "After spilling", &errs());
2652+
MF->verify(LIS, Indexes, "After spilling", &errs());
25282653
}
25292654

25302655
// The live virtual register requesting allocation was spilled, so tell
@@ -2720,37 +2845,26 @@ bool RAGreedy::hasVirtRegAlloc() {
27202845
return false;
27212846
}
27222847

2723-
bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
2848+
bool RAGreedy::run(MachineFunction &mf) {
27242849
LLVM_DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
27252850
<< "********** Function: " << mf.getName() << '\n');
27262851

27272852
MF = &mf;
27282853
TII = MF->getSubtarget().getInstrInfo();
27292854

27302855
if (VerifyEnabled)
2731-
MF->verify(this, "Before greedy register allocator", &errs());
2856+
MF->verify(LIS, Indexes, "Before greedy register allocator", &errs());
27322857

2733-
RegAllocBase::init(getAnalysis<VirtRegMapWrapperLegacy>().getVRM(),
2734-
getAnalysis<LiveIntervalsWrapperPass>().getLIS(),
2735-
getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM());
2858+
RegAllocBase::init(*this->VRM, *this->LIS, *this->Matrix);
27362859

27372860
// Early return if there is no virtual register to be allocated to a
27382861
// physical register.
27392862
if (!hasVirtRegAlloc())
27402863
return false;
27412864

2742-
Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
27432865
// Renumber to get accurate and consistent results from
27442866
// SlotIndexes::getApproxInstrDistance.
27452867
Indexes->packIndexes();
2746-
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
2747-
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2748-
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
2749-
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
2750-
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
2751-
SpillPlacer = &getAnalysis<SpillPlacementWrapperLegacy>().getResult();
2752-
DebugVars = &getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV();
2753-
auto &LSS = getAnalysis<LiveStacksWrapperLegacy>().getLS();
27542868

27552869
initializeCSRCost();
27562870

@@ -2766,17 +2880,12 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27662880

27672881
ExtraInfo.emplace();
27682882

2769-
auto &EvictAdvisorProvider =
2770-
getAnalysis<RegAllocEvictionAdvisorAnalysisLegacy>().getProvider();
2771-
EvictAdvisor = EvictAdvisorProvider.getAdvisor(*MF, *this, MBFI, Loops);
2772-
2773-
PriorityAdvisor = getAnalysis<RegAllocPriorityAdvisorAnalysisLegacy>()
2774-
.getProvider()
2775-
.getAdvisor(*MF, *this, *Indexes);
2883+
EvictAdvisor = EvictProvider->getAdvisor(*MF, *this, MBFI, Loops);
2884+
PriorityAdvisor = PriorityProvider->getAdvisor(*MF, *this, *Indexes);
27762885

27772886
VRAI = std::make_unique<VirtRegAuxInfo>(*MF, *LIS, *VRM, *Loops, *MBFI);
27782887
SpillerInstance.reset(
2779-
createInlineSpiller({*LIS, LSS, *DomTree, *MBFI}, *MF, *VRM, *VRAI));
2888+
createInlineSpiller({*LIS, *LSS, *DomTree, *MBFI}, *MF, *VRM, *VRAI));
27802889

27812890
VRAI->calculateSpillWeightsAndHints();
27822891

@@ -2793,7 +2902,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27932902
tryHintsRecoloring();
27942903

27952904
if (VerifyEnabled)
2796-
MF->verify(this, "Before post optimization", &errs());
2905+
MF->verify(LIS, Indexes, "Before post optimization", &errs());
27972906
postOptimization();
27982907
reportStats();
27992908

0 commit comments

Comments
 (0)