Skip to content

Commit c8845f5

Browse files
committed
[CodeGen][NewPM] Port RegAllocGreedy to NPM
1 parent 9550da6 commit c8845f5

File tree

8 files changed

+218
-63
lines changed

8 files changed

+218
-63
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

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

901901
/// Run the current MachineFunction through the machine code verifier, useful
902902
/// for debugger use.
903+
/// TODO: Add the param LiveStks
903904
/// \returns true if no problems were found.
904905
bool verify(LiveIntervals *LiveInts, SlotIndexes *Indexes,
905906
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
@@ -167,7 +167,7 @@ namespace llvm {
167167
extern char &LiveRangeShrinkID;
168168

169169
/// Greedy register allocator.
170-
extern char &RAGreedyID;
170+
extern char &RAGreedyLegacyID;
171171

172172
/// Basic register allocator.
173173
extern char &RABasicID;

llvm/lib/CodeGen/RegAllocGreedy.h renamed to llvm/include/llvm/CodeGen/RegAllocGreedy.h

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@
1212
#ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_
1313
#define LLVM_CODEGEN_REGALLOCGREEDY_H_
1414

15-
#include "InterferenceCache.h"
16-
#include "RegAllocBase.h"
17-
#include "SplitKit.h"
1815
#include "llvm/ADT/ArrayRef.h"
1916
#include "llvm/ADT/BitVector.h"
2017
#include "llvm/ADT/IndexedMap.h"
2118
#include "llvm/ADT/SetVector.h"
2219
#include "llvm/ADT/SmallVector.h"
2320
#include "llvm/ADT/StringRef.h"
2421
#include "llvm/CodeGen/CalcSpillWeights.h"
22+
#include "llvm/CodeGen/InterferenceCache.h"
2523
#include "llvm/CodeGen/LiveDebugVariables.h"
2624
#include "llvm/CodeGen/LiveInterval.h"
2725
#include "llvm/CodeGen/LiveRangeEdit.h"
26+
#include "llvm/CodeGen/LiveStacks.h"
2827
#include "llvm/CodeGen/MachineFunction.h"
2928
#include "llvm/CodeGen/MachineFunctionPass.h"
29+
#include "llvm/CodeGen/RegAllocBase.h"
30+
#include "llvm/CodeGen/RegAllocEvictionAdvisor.h"
3031
#include "llvm/CodeGen/RegAllocPriorityAdvisor.h"
3132
#include "llvm/CodeGen/RegisterClassInfo.h"
3233
#include "llvm/CodeGen/SpillPlacement.h"
3334
#include "llvm/CodeGen/Spiller.h"
35+
#include "llvm/CodeGen/SplitKit.h"
3436
#include "llvm/CodeGen/TargetRegisterInfo.h"
37+
#include "llvm/IR/PassManager.h"
3538
#include <algorithm>
3639
#include <cstdint>
3740
#include <memory>
@@ -56,11 +59,30 @@ class SlotIndexes;
5659
class TargetInstrInfo;
5760
class VirtRegMap;
5861

59-
class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
60-
public RegAllocBase,
62+
class LLVM_LIBRARY_VISIBILITY RAGreedy : public RegAllocBase,
6163
private LiveRangeEdit::Delegate {
62-
// Interface to eviction advisers
6364
public:
65+
struct RequiredAnalyses {
66+
VirtRegMap *VRM = nullptr;
67+
LiveIntervals *LIS = nullptr;
68+
LiveRegMatrix *LRM = nullptr;
69+
SlotIndexes *Indexes = nullptr;
70+
MachineBlockFrequencyInfo *MBFI = nullptr;
71+
MachineDominatorTree *DomTree = nullptr;
72+
MachineLoopInfo *Loops = nullptr;
73+
MachineOptimizationRemarkEmitter *ORE = nullptr;
74+
EdgeBundles *Bundles = nullptr;
75+
SpillPlacement *SpillPlacer = nullptr;
76+
LiveDebugVariables *DebugVars = nullptr;
77+
78+
// Used by InlineSpiller
79+
LiveStacks *LSS;
80+
// Proxies for eviction and priority advisors
81+
RegAllocEvictionAdvisorProvider *EvictProvider;
82+
RegAllocPriorityAdvisorProvider *PriorityProvider;
83+
};
84+
85+
// Interface to eviction advisers
6486
/// Track allocation stage and eviction loop prevention during allocation.
6587
class ExtraRegInfo final {
6688
// RegInfo - Keep additional information about each live range.
@@ -178,6 +200,10 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
178200
EdgeBundles *Bundles = nullptr;
179201
SpillPlacement *SpillPlacer = nullptr;
180202
LiveDebugVariables *DebugVars = nullptr;
203+
LiveStacks *LSS = nullptr; // Used by InlineSpiller
204+
// Proxy for the advisors
205+
RegAllocEvictionAdvisorProvider *EvictProvider = nullptr;
206+
RegAllocPriorityAdvisorProvider *PriorityProvider = nullptr;
181207

182208
// state
183209
std::unique_ptr<Spiller> SpillerInstance;
@@ -282,13 +308,11 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
282308

283309
public:
284310
RAGreedy(const RegAllocFilterFunc F = nullptr);
311+
// Evict and priority advisors use this object, so we can construct those
312+
// first and pass them here.
313+
// Not required once legacy PM is removed.
314+
void setAnalyses(RequiredAnalyses &Analyses);
285315

286-
/// Return the pass name.
287-
StringRef getPassName() const override { return "Greedy Register Allocator"; }
288-
289-
/// RAGreedy analysis usage.
290-
void getAnalysisUsage(AnalysisUsage &AU) const override;
291-
void releaseMemory() override;
292316
Spiller &spiller() override { return *SpillerInstance; }
293317
void enqueueImpl(const LiveInterval *LI) override;
294318
const LiveInterval *dequeue() override;
@@ -297,19 +321,9 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
297321
void aboutToRemoveInterval(const LiveInterval &) override;
298322

299323
/// Perform register allocation.
300-
bool runOnMachineFunction(MachineFunction &mf) override;
301-
302-
MachineFunctionProperties getRequiredProperties() const override {
303-
return MachineFunctionProperties().set(
304-
MachineFunctionProperties::Property::NoPHIs);
305-
}
306-
307-
MachineFunctionProperties getClearedProperties() const override {
308-
return MachineFunctionProperties().set(
309-
MachineFunctionProperties::Property::IsSSA);
310-
}
324+
bool run(MachineFunction &mf);
311325

312-
static char ID;
326+
void releaseMemory();
313327

314328
private:
315329
MCRegister selectOrSplitImpl(const LiveInterval &,
@@ -451,5 +465,23 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public MachineFunctionPass,
451465
/// Report the statistic for each loop.
452466
void reportStats();
453467
};
468+
469+
class RAGreedyPass : public PassInfoMixin<RAGreedyPass> {
470+
RegAllocFilterFunc Filter;
471+
472+
public:
473+
RAGreedyPass(RegAllocFilterFunc F = nullptr) : Filter(F) {}
474+
PreservedAnalyses run(MachineFunction &F, MachineFunctionAnalysisManager &AM);
475+
476+
MachineFunctionProperties getRequiredProperties() const {
477+
return MachineFunctionProperties().set(
478+
MachineFunctionProperties::Property::NoPHIs);
479+
}
480+
481+
MachineFunctionProperties getClearedProperties() const {
482+
return MachineFunctionProperties().set(
483+
MachineFunctionProperties::Property::IsSSA);
484+
}
485+
};
454486
} // namespace llvm
455487
#endif // #ifndef LLVM_CODEGEN_REGALLOCGREEDY_H_

llvm/include/llvm/InitializePasses.h

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

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ MACHINE_FUNCTION_PASS_WITH_PARAMS(
187187
return parseRegAllocFastPassOptions(*PB, Params);
188188
},
189189
"filter=reg-filter;no-clear-vregs")
190+
191+
MACHINE_FUNCTION_PASS_WITH_PARAMS(
192+
"regallocgreedy", "RAGreedy",
193+
[](RegAllocFilterFunc F) { return RAGreedyPass(F); },
194+
[PB = this](StringRef Params) {
195+
// TODO: parseRegAllocFilter(*PB, Params);
196+
return Expected<RegAllocFilterFunc>(nullptr);
197+
}, ""
198+
)
190199
#undef MACHINE_FUNCTION_PASS_WITH_PARAMS
191200

192201
// 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
@@ -111,7 +111,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
111111
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
112112
initializeProcessImplicitDefsPass(Registry);
113113
initializeRABasicPass(Registry);
114-
initializeRAGreedyPass(Registry);
114+
initializeRAGreedyLegacyPass(Registry);
115115
initializeRegAllocFastPass(Registry);
116116
initializeRegUsageInfoCollectorLegacyPass(Registry);
117117
initializeRegUsageInfoPropagationLegacyPass(Registry);

0 commit comments

Comments
 (0)