Skip to content

Commit cac1360

Browse files
authored
[CodeGen][NewPM] Port EdgeBundles analysis to NPM (#116616)
1 parent 68aa6ac commit cac1360

File tree

9 files changed

+77
-26
lines changed

9 files changed

+77
-26
lines changed

llvm/include/llvm/CodeGen/EdgeBundles.h

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/IntEqClasses.h"
2020
#include "llvm/CodeGen/MachineFunctionPass.h"
21+
#include "llvm/IR/PassManager.h"
2122

2223
namespace llvm {
24+
class EdgeBundlesWrapperLegacy;
25+
class EdgeBundlesAnalysis;
26+
27+
class EdgeBundles {
28+
friend class EdgeBundlesWrapperLegacy;
29+
friend class EdgeBundlesAnalysis;
2330

24-
class EdgeBundles : public MachineFunctionPass {
2531
const MachineFunction *MF = nullptr;
2632

2733
/// EC - Each edge bundle is an equivalence class. The keys are:
@@ -32,10 +38,10 @@ class EdgeBundles : public MachineFunctionPass {
3238
/// Blocks - Map each bundle to a list of basic block numbers.
3339
SmallVector<SmallVector<unsigned, 8>, 4> Blocks;
3440

35-
public:
36-
static char ID;
37-
EdgeBundles() : MachineFunctionPass(ID) {}
41+
void init();
42+
EdgeBundles(MachineFunction &MF);
3843

44+
public:
3945
/// getBundle - Return the ingoing (Out = false) or outgoing (Out = true)
4046
/// bundle number for basic block #N
4147
unsigned getBundle(unsigned N, bool Out) const { return EC[2 * N + Out]; }
@@ -52,11 +58,34 @@ class EdgeBundles : public MachineFunctionPass {
5258
/// view - Visualize the annotated bipartite CFG with Graphviz.
5359
void view() const;
5460

61+
// Handle invalidation for the new pass manager
62+
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
63+
MachineFunctionAnalysisManager::Invalidator &Inv);
64+
};
65+
66+
class EdgeBundlesWrapperLegacy : public MachineFunctionPass {
67+
public:
68+
static char ID;
69+
EdgeBundlesWrapperLegacy() : MachineFunctionPass(ID) {}
70+
71+
EdgeBundles &getEdgeBundles() { return *Impl; }
72+
const EdgeBundles &getEdgeBundles() const { return *Impl; }
73+
5574
private:
56-
bool runOnMachineFunction(MachineFunction&) override;
75+
std::unique_ptr<EdgeBundles> Impl;
76+
bool runOnMachineFunction(MachineFunction &MF) override;
5777
void getAnalysisUsage(AnalysisUsage&) const override;
5878
};
5979

80+
class EdgeBundlesAnalysis : public AnalysisInfoMixin<EdgeBundlesAnalysis> {
81+
friend AnalysisInfoMixin<EdgeBundlesAnalysis>;
82+
static AnalysisKey Key;
83+
84+
public:
85+
using Result = EdgeBundles;
86+
EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
87+
};
88+
6089
} // end namespace llvm
6190

6291
#endif

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace llvm {
118118
extern char &MachineRegionInfoPassID;
119119

120120
/// EdgeBundles analysis - Bundle machine CFG edges.
121-
extern char &EdgeBundlesID;
121+
extern char &EdgeBundlesWrapperLegacyID;
122122

123123
/// LiveVariables pass - This pass computes the set of blocks in which each
124124
/// variable is life and sets machine operand kill flags.

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void initializeEarlyIfConverterLegacyPass(PassRegistry &);
102102
void initializeEarlyIfPredicatorPass(PassRegistry &);
103103
void initializeEarlyMachineLICMPass(PassRegistry &);
104104
void initializeEarlyTailDuplicateLegacyPass(PassRegistry &);
105-
void initializeEdgeBundlesPass(PassRegistry &);
105+
void initializeEdgeBundlesWrapperLegacyPass(PassRegistry &);
106106
void initializeEHContGuardCatchretPass(PassRegistry &);
107107
void initializeExpandLargeFpConvertLegacyPassPass(PassRegistry &);
108108
void initializeExpandLargeDivRemLegacyPassPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass())
9797
// LiveVariables can be removed completely, and LiveIntervals can be directly
9898
// computed. (We still either need to regenerate kill flags after regalloc, or
9999
// preferably fix the scavenger to not depend on them).
100+
MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
100101
MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
101102
MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
102103
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
@@ -114,7 +115,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
114115
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
115116
MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
116117
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
117-
// MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
118118
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",
119119
// LazyMachineBlockFrequencyInfoAnalysis())
120120
// MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())

llvm/lib/CodeGen/EdgeBundles.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,35 @@ static cl::opt<bool>
2626
ViewEdgeBundles("view-edge-bundles", cl::Hidden,
2727
cl::desc("Pop up a window to show edge bundle graphs"));
2828

29-
char EdgeBundles::ID = 0;
29+
char EdgeBundlesWrapperLegacy::ID = 0;
3030

31-
INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
32-
/* cfg = */true, /* is_analysis = */ true)
31+
INITIALIZE_PASS(EdgeBundlesWrapperLegacy, "edge-bundles",
32+
"Bundle Machine CFG Edges",
33+
/* cfg = */ true, /* is_analysis = */ true)
3334

34-
char &llvm::EdgeBundlesID = EdgeBundles::ID;
35+
char &llvm::EdgeBundlesWrapperLegacyID = EdgeBundlesWrapperLegacy::ID;
3536

36-
void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
37+
void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
3738
AU.setPreservesAll();
3839
MachineFunctionPass::getAnalysisUsage(AU);
3940
}
4041

41-
bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
42-
MF = &mf;
42+
AnalysisKey EdgeBundlesAnalysis::Key;
43+
44+
EdgeBundles EdgeBundlesAnalysis::run(MachineFunction &MF,
45+
MachineFunctionAnalysisManager &MFAM) {
46+
EdgeBundles Impl(MF);
47+
return Impl;
48+
}
49+
50+
bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
51+
Impl.reset(new EdgeBundles(MF));
52+
return false;
53+
}
54+
55+
EdgeBundles::EdgeBundles(MachineFunction &MF) : MF(&MF) { init(); }
56+
57+
void EdgeBundles::init() {
4358
EC.clear();
4459
EC.grow(2 * MF->getNumBlockIDs());
4560

@@ -64,8 +79,6 @@ bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
6479
if (b1 != b0)
6580
Blocks[b1].push_back(i);
6681
}
67-
68-
return false;
6982
}
7083

7184
namespace llvm {
@@ -100,3 +113,11 @@ raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
100113
void EdgeBundles::view() const {
101114
ViewGraph(*this, "EdgeBundles");
102115
}
116+
117+
bool EdgeBundles::invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
118+
MachineFunctionAnalysisManager::Invalidator &Inv) {
119+
// Invalidated when CFG is not preserved
120+
auto PAC = PA.getChecker<EdgeBundlesAnalysis>();
121+
return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
122+
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
123+
}

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
161161
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
162162
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
163163
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
164-
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
164+
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
165165
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
166166
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
167167
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis)
@@ -216,7 +216,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
216216
AU.addPreserved<VirtRegMapWrapperLegacy>();
217217
AU.addRequired<LiveRegMatrixWrapperLegacy>();
218218
AU.addPreserved<LiveRegMatrixWrapperLegacy>();
219-
AU.addRequired<EdgeBundles>();
219+
AU.addRequired<EdgeBundlesWrapperLegacy>();
220220
AU.addRequired<SpillPlacement>();
221221
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
222222
AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
@@ -2730,7 +2730,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27302730
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
27312731
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
27322732
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
2733-
Bundles = &getAnalysis<EdgeBundles>();
2733+
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
27342734
SpillPlacer = &getAnalysis<SpillPlacement>();
27352735
DebugVars = &getAnalysis<LiveDebugVariables>();
27362736

llvm/lib/CodeGen/SpillPlacement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ char &llvm::SpillPlacementID = SpillPlacement::ID;
5050

5151
INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE,
5252
"Spill Code Placement Analysis", true, true)
53-
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
53+
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
5454
INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE,
5555
"Spill Code Placement Analysis", true, true)
5656

5757
void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
5858
AU.setPreservesAll();
5959
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
60-
AU.addRequiredTransitive<EdgeBundles>();
60+
AU.addRequiredTransitive<EdgeBundlesWrapperLegacy>();
6161
MachineFunctionPass::getAnalysisUsage(AU);
6262
}
6363

@@ -191,7 +191,7 @@ struct SpillPlacement::Node {
191191

192192
bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
193193
MF = &mf;
194-
bundles = &getAnalysis<EdgeBundles>();
194+
bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
195195

196196
assert(!nodes && "Leaking node array");
197197
nodes = new Node[bundles->getNumBundles()];

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
8686
#include "llvm/CodeGen/DwarfEHPrepare.h"
8787
#include "llvm/CodeGen/EarlyIfConversion.h"
88+
#include "llvm/CodeGen/EdgeBundles.h"
8889
#include "llvm/CodeGen/ExpandLargeDivRem.h"
8990
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
9091
#include "llvm/CodeGen/ExpandMemCmp.h"

llvm/lib/Target/X86/X86FloatingPoint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace {
6767

6868
void getAnalysisUsage(AnalysisUsage &AU) const override {
6969
AU.setPreservesCFG();
70-
AU.addRequired<EdgeBundles>();
70+
AU.addRequired<EdgeBundlesWrapperLegacy>();
7171
AU.addPreservedID(MachineLoopInfoID);
7272
AU.addPreservedID(MachineDominatorsID);
7373
MachineFunctionPass::getAnalysisUsage(AU);
@@ -303,7 +303,7 @@ char FPS::ID = 0;
303303

304304
INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier",
305305
false, false)
306-
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
306+
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
307307
INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier",
308308
false, false)
309309

@@ -337,7 +337,7 @@ bool FPS::runOnMachineFunction(MachineFunction &MF) {
337337
// Early exit.
338338
if (!FPIsUsed) return false;
339339

340-
Bundles = &getAnalysis<EdgeBundles>();
340+
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
341341
TII = MF.getSubtarget().getInstrInfo();
342342

343343
// Prepare cross-MBB liveness.

0 commit comments

Comments
 (0)