Skip to content

[CodeGen][NewPM] Port EdgeBundles analysis to NPM #116616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions llvm/include/llvm/CodeGen/EdgeBundles.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntEqClasses.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/IR/PassManager.h"

namespace llvm {
class EdgeBundlesWrapperLegacy;
class EdgeBundlesAnalysis;

class EdgeBundles {
friend class EdgeBundlesWrapperLegacy;
friend class EdgeBundlesAnalysis;

class EdgeBundles : public MachineFunctionPass {
const MachineFunction *MF = nullptr;

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

public:
static char ID;
EdgeBundles() : MachineFunctionPass(ID) {}
void init();
EdgeBundles(MachineFunction &MF);

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

// Handle invalidation for the new pass manager
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv);
};

class EdgeBundlesWrapperLegacy : public MachineFunctionPass {
public:
static char ID;
EdgeBundlesWrapperLegacy() : MachineFunctionPass(ID) {}

EdgeBundles &getEdgeBundles() { return *Impl; }
const EdgeBundles &getEdgeBundles() const { return *Impl; }

private:
bool runOnMachineFunction(MachineFunction&) override;
std::unique_ptr<EdgeBundles> Impl;
bool runOnMachineFunction(MachineFunction &MF) override;
void getAnalysisUsage(AnalysisUsage&) const override;
};

class EdgeBundlesAnalysis : public AnalysisInfoMixin<EdgeBundlesAnalysis> {
friend AnalysisInfoMixin<EdgeBundlesAnalysis>;
static AnalysisKey Key;

public:
using Result = EdgeBundles;
EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
};

} // end namespace llvm

#endif
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace llvm {
extern char &MachineRegionInfoPassID;

/// EdgeBundles analysis - Bundle machine CFG edges.
extern char &EdgeBundlesID;
extern char &EdgeBundlesWrapperLegacyID;

/// LiveVariables pass - This pass computes the set of blocks in which each
/// variable is life and sets machine operand kill flags.
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void initializeEarlyIfConverterLegacyPass(PassRegistry &);
void initializeEarlyIfPredicatorPass(PassRegistry &);
void initializeEarlyMachineLICMPass(PassRegistry &);
void initializeEarlyTailDuplicateLegacyPass(PassRegistry &);
void initializeEdgeBundlesPass(PassRegistry &);
void initializeEdgeBundlesWrapperLegacyPass(PassRegistry &);
void initializeEHContGuardCatchretPass(PassRegistry &);
void initializeExpandLargeFpConvertLegacyPassPass(PassRegistry &);
void initializeExpandLargeDivRemLegacyPassPass(PassRegistry &);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass())
// LiveVariables can be removed completely, and LiveIntervals can be directly
// computed. (We still either need to regenerate kill flags after regalloc, or
// preferably fix the scavenger to not depend on them).
MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
Copy link
Contributor

@paperchalice paperchalice Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need a printer pass like dot-edge-bundles to call the view method to dump edge bundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can introduce in a new patch

MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
Expand All @@ -114,7 +115,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
// MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",
// LazyMachineBlockFrequencyInfoAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())
Expand Down
39 changes: 30 additions & 9 deletions llvm/lib/CodeGen/EdgeBundles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@ static cl::opt<bool>
ViewEdgeBundles("view-edge-bundles", cl::Hidden,
cl::desc("Pop up a window to show edge bundle graphs"));

char EdgeBundles::ID = 0;
char EdgeBundlesWrapperLegacy::ID = 0;

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

char &llvm::EdgeBundlesID = EdgeBundles::ID;
char &llvm::EdgeBundlesWrapperLegacyID = EdgeBundlesWrapperLegacy::ID;

void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
MachineFunctionPass::getAnalysisUsage(AU);
}

bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
MF = &mf;
AnalysisKey EdgeBundlesAnalysis::Key;

EdgeBundles EdgeBundlesAnalysis::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
EdgeBundles Impl(MF);
return Impl;
}

bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
Impl.reset(new EdgeBundles(MF));
return false;
}

EdgeBundles::EdgeBundles(MachineFunction &MF) : MF(&MF) { init(); }

void EdgeBundles::init() {
EC.clear();
EC.grow(2 * MF->getNumBlockIDs());

Expand All @@ -64,8 +79,6 @@ bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
if (b1 != b0)
Blocks[b1].push_back(i);
}

return false;
}

namespace llvm {
Expand Down Expand Up @@ -100,3 +113,11 @@ raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
void EdgeBundles::view() const {
ViewGraph(*this, "EdgeBundles");
}

bool EdgeBundles::invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv) {
// Invalidated when CFG is not preserved
auto PAC = PA.getChecker<EdgeBundlesAnalysis>();
return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
}
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/RegAllocGreedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis)
Expand Down Expand Up @@ -216,7 +216,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<VirtRegMapWrapperLegacy>();
AU.addRequired<LiveRegMatrixWrapperLegacy>();
AU.addPreserved<LiveRegMatrixWrapperLegacy>();
AU.addRequired<EdgeBundles>();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addRequired<SpillPlacement>();
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
Expand Down Expand Up @@ -2730,7 +2730,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
Bundles = &getAnalysis<EdgeBundles>();
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
SpillPlacer = &getAnalysis<SpillPlacement>();
DebugVars = &getAnalysis<LiveDebugVariables>();

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/SpillPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ char &llvm::SpillPlacementID = SpillPlacement::ID;

INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE,
"Spill Code Placement Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE,
"Spill Code Placement Analysis", true, true)

void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
AU.addRequiredTransitive<EdgeBundles>();
AU.addRequiredTransitive<EdgeBundlesWrapperLegacy>();
MachineFunctionPass::getAnalysisUsage(AU);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ struct SpillPlacement::Node {

bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
MF = &mf;
bundles = &getAnalysis<EdgeBundles>();
bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();

assert(!nodes && "Leaking node array");
nodes = new Node[bundles->getNumBundles()];
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
#include "llvm/CodeGen/DwarfEHPrepare.h"
#include "llvm/CodeGen/EarlyIfConversion.h"
#include "llvm/CodeGen/EdgeBundles.h"
#include "llvm/CodeGen/ExpandLargeDivRem.h"
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
#include "llvm/CodeGen/ExpandMemCmp.h"
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/X86/X86FloatingPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace {

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<EdgeBundles>();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addPreservedID(MachineLoopInfoID);
AU.addPreservedID(MachineDominatorsID);
MachineFunctionPass::getAnalysisUsage(AU);
Expand Down Expand Up @@ -303,7 +303,7 @@ char FPS::ID = 0;

INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier",
false, false)
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier",
false, false)

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

Bundles = &getAnalysis<EdgeBundles>();
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
TII = MF.getSubtarget().getInstrInfo();

// Prepare cross-MBB liveness.
Expand Down
Loading