Skip to content

Commit 36d5e74

Browse files
committed
[CodeGen][NewPM] Port RegAllocGreedy to NPM
1 parent d13349b commit 36d5e74

20 files changed

+250
-105
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class LLVM_LIBRARY_VISIBILITY InterferenceCache {
142142

143143
// Point to an entry for each physreg. The entry pointed to may not be up to
144144
// date, and it may have been reused for a different physreg.
145-
unsigned char* PhysRegEntries = nullptr;
145+
unsigned char *PhysRegEntries = nullptr;
146146
size_t PhysRegEntriesCount = 0;
147147

148148
// Next round-robin entry to be picked.
@@ -158,9 +158,7 @@ class LLVM_LIBRARY_VISIBILITY InterferenceCache {
158158
InterferenceCache() = default;
159159
InterferenceCache &operator=(const InterferenceCache &other) = delete;
160160
InterferenceCache(const InterferenceCache &other) = delete;
161-
~InterferenceCache() {
162-
free(PhysRegEntries);
163-
}
161+
~InterferenceCache() { free(PhysRegEntries); }
164162

165163
void reinitPhysRegEntries();
166164

@@ -194,9 +192,7 @@ class LLVM_LIBRARY_VISIBILITY InterferenceCache {
194192
/// Cursor - Create a dangling cursor.
195193
Cursor() = default;
196194

197-
Cursor(const Cursor &O) {
198-
setEntry(O.CacheEntry);
199-
}
195+
Cursor(const Cursor &O) { setEntry(O.CacheEntry); }
200196

201197
Cursor &operator=(const Cursor &O) {
202198
setEntry(O.CacheEntry);
@@ -220,21 +216,15 @@ class LLVM_LIBRARY_VISIBILITY InterferenceCache {
220216
}
221217

222218
/// hasInterference - Return true if the current block has any interference.
223-
bool hasInterference() {
224-
return Current->First.isValid();
225-
}
219+
bool hasInterference() { return Current->First.isValid(); }
226220

227221
/// first - Return the starting index of the first interfering range in the
228222
/// current block.
229-
SlotIndex first() {
230-
return Current->First;
231-
}
223+
SlotIndex first() { return Current->First; }
232224

233225
/// last - Return the ending index of the last interfering range in the
234226
/// current block.
235-
SlotIndex last() {
236-
return Current->Last;
237-
}
227+
SlotIndex last() { return Current->Last; }
238228
};
239229
};
240230

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/RegAllocBase.h renamed to llvm/include/llvm/CodeGen/RegAllocBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class LiveIntervals;
4848
class LiveRegMatrix;
4949
class MachineInstr;
5050
class MachineRegisterInfo;
51-
template<typename T> class SmallVectorImpl;
51+
template <typename T> class SmallVectorImpl;
5252
class Spiller;
5353
class TargetRegisterInfo;
5454
class VirtRegMap;

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/lib/CodeGen/SplitKit.h renamed to llvm/include/llvm/CodeGen/SplitKit.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis {
8888
}
8989
return Res;
9090
}
91-
9291
};
9392

9493
/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
@@ -387,7 +386,7 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
387386

388387
/// removeBackCopies - Remove the copy instructions that defines the values
389388
/// in the vector in the complement interval.
390-
void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
389+
void removeBackCopies(SmallVectorImpl<VNInfo *> &Copies);
391390

392391
/// getShallowDominator - Returns the least busy dominator of MBB that is
393392
/// also dominated by DefMBB. Busy is measured by loop depth.
@@ -430,8 +429,9 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
430429
/// \p InsertBefore. This can be invoked with a \p LaneMask which may make it
431430
/// necessary to construct a sequence of copies to cover it exactly.
432431
SlotIndex buildCopy(Register FromReg, Register ToReg, LaneBitmask LaneMask,
433-
MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
434-
bool Late, unsigned RegIdx);
432+
MachineBasicBlock &MBB,
433+
MachineBasicBlock::iterator InsertBefore, bool Late,
434+
unsigned RegIdx);
435435

436436
SlotIndex buildSingleSubRegCopy(Register FromReg, Register ToReg,
437437
MachineBasicBlock &MB,
@@ -448,7 +448,7 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
448448
VirtRegAuxInfo &VRAI);
449449

450450
/// reset - Prepare for a new split.
451-
void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
451+
void reset(LiveRangeEdit &, ComplementSpillMode = SM_Partition);
452452

453453
/// Create a new virtual register and live interval.
454454
/// Return the interval index, starting from 1. Interval index 0 is the
@@ -533,9 +533,9 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
533533
/// @param LeaveBefore When set, leave IntvIn before this point.
534534
/// @param IntvOut Interval index leaving the block.
535535
/// @param EnterAfter When set, enter IntvOut after this point.
536-
void splitLiveThroughBlock(unsigned MBBNum,
537-
unsigned IntvIn, SlotIndex LeaveBefore,
538-
unsigned IntvOut, SlotIndex EnterAfter);
536+
void splitLiveThroughBlock(unsigned MBBNum, unsigned IntvIn,
537+
SlotIndex LeaveBefore, unsigned IntvOut,
538+
SlotIndex EnterAfter);
539539

540540
/// splitRegInBlock - Split CurLI in the given block such that it enters the
541541
/// block in IntvIn and leaves it on the stack (or not at all). Split points
@@ -545,8 +545,8 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
545545
/// @param BI Block descriptor.
546546
/// @param IntvIn Interval index entering the block. Not 0.
547547
/// @param LeaveBefore When set, leave IntvIn before this point.
548-
void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
549-
unsigned IntvIn, SlotIndex LeaveBefore);
548+
void splitRegInBlock(const SplitAnalysis::BlockInfo &BI, unsigned IntvIn,
549+
SlotIndex LeaveBefore);
550550

551551
/// splitRegOutBlock - Split CurLI in the given block such that it enters the
552552
/// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
@@ -557,8 +557,8 @@ class LLVM_LIBRARY_VISIBILITY SplitEditor {
557557
/// @param BI Block descriptor.
558558
/// @param IntvOut Interval index leaving the block.
559559
/// @param EnterAfter When set, enter IntvOut after this point.
560-
void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
561-
unsigned IntvOut, SlotIndex EnterAfter);
560+
void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, unsigned IntvOut,
561+
SlotIndex EnterAfter);
562562
};
563563

564564
} // end namespace llvm

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);

llvm/lib/CodeGen/InlineSpiller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "SplitKit.h"
1514
#include "llvm/ADT/ArrayRef.h"
1615
#include "llvm/ADT/DenseMap.h"
1716
#include "llvm/ADT/MapVector.h"
@@ -36,6 +35,7 @@
3635
#include "llvm/CodeGen/MachineRegisterInfo.h"
3736
#include "llvm/CodeGen/SlotIndexes.h"
3837
#include "llvm/CodeGen/Spiller.h"
38+
#include "llvm/CodeGen/SplitKit.h"
3939
#include "llvm/CodeGen/StackMaps.h"
4040
#include "llvm/CodeGen/TargetInstrInfo.h"
4141
#include "llvm/CodeGen/TargetOpcodes.h"

llvm/lib/CodeGen/InterferenceCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "InterferenceCache.h"
13+
#include "llvm/CodeGen/InterferenceCache.h"
1414
#include "llvm/ADT/ArrayRef.h"
1515
#include "llvm/CodeGen/LiveIntervals.h"
1616
#include "llvm/CodeGen/MachineBasicBlock.h"

llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "AllocationOrder.h"
14-
#include "RegAllocGreedy.h"
1514
#include "llvm/Analysis/InteractiveModelRunner.h"
1615
#include "llvm/Analysis/MLModelRunner.h"
1716
#include "llvm/Analysis/TensorSpec.h"
1817
#include "llvm/CodeGen/RegAllocEvictionAdvisor.h"
18+
#include "llvm/CodeGen/RegAllocGreedy.h"
1919
#if defined(LLVM_HAVE_TF_AOT_REGALLOCEVICTMODEL) || defined(LLVM_HAVE_TFLITE)
2020
#include "llvm/Analysis/ModelUnderTrainingRunner.h"
2121
#include "llvm/Analysis/NoInferenceModelRunner.h"

llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "AllocationOrder.h"
14-
#include "RegAllocGreedy.h"
1514
#include "llvm/Analysis/AliasAnalysis.h"
1615
#include "llvm/Analysis/InteractiveModelRunner.h"
1716
#include "llvm/Analysis/MLModelRunner.h"
@@ -24,6 +23,7 @@
2423
#include "llvm/CodeGen/MachineLoopInfo.h"
2524
#include "llvm/CodeGen/MachineRegisterInfo.h"
2625
#include "llvm/CodeGen/Passes.h"
26+
#include "llvm/CodeGen/RegAllocGreedy.h"
2727
#include "llvm/CodeGen/RegAllocPriorityAdvisor.h"
2828
#include "llvm/CodeGen/RegisterClassInfo.h"
2929
#include "llvm/CodeGen/SlotIndexes.h"

llvm/lib/CodeGen/RegAllocBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "RegAllocBase.h"
14+
#include "llvm/CodeGen/RegAllocBase.h"
1515
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/ADT/Statistic.h"
1717
#include "llvm/CodeGen/LiveInterval.h"

0 commit comments

Comments
 (0)