[llvm] [CodeGen][NewPM] Port EdgeBundles analysis to NPM (PR #116616)

Akshat Oke via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 19 03:56:41 PST 2024


https://github.com/optimisan updated https://github.com/llvm/llvm-project/pull/116616

>From a355437692c848dcadd1dc4d2e738eba280498a6 Mon Sep 17 00:00:00 2001
From: Akshat Oke <Akshat.Oke at amd.com>
Date: Mon, 18 Nov 2024 05:27:12 +0000
Subject: [PATCH 1/2] [CodeGen][NewPM] Port EdgeBundles analysis to NPM

---
 llvm/include/llvm/CodeGen/EdgeBundles.h       | 39 ++++++++++++++++---
 llvm/include/llvm/CodeGen/Passes.h            |  2 +-
 llvm/include/llvm/InitializePasses.h          |  2 +-
 .../llvm/Passes/MachinePassRegistry.def       |  2 +-
 llvm/lib/CodeGen/EdgeBundles.cpp              | 39 ++++++++++++++-----
 llvm/lib/CodeGen/RegAllocGreedy.cpp           |  6 +--
 llvm/lib/CodeGen/SpillPlacement.cpp           |  6 +--
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 llvm/lib/Target/X86/X86FloatingPoint.cpp      |  6 +--
 9 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/EdgeBundles.h b/llvm/include/llvm/CodeGen/EdgeBundles.h
index b844bd307c1970..6e0c301a651e3e 100644
--- a/llvm/include/llvm/CodeGen/EdgeBundles.h
+++ b/llvm/include/llvm/CodeGen/EdgeBundles.h
@@ -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:
@@ -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]; }
@@ -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
diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index 7698f557c58a04..d1fac4a304cffe 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -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.
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index af93d5e989f654..e883aae2758688 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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 &);
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 851561f6b769b1..cb1c295d824787 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -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())
 MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
 MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
 MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
@@ -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())
diff --git a/llvm/lib/CodeGen/EdgeBundles.cpp b/llvm/lib/CodeGen/EdgeBundles.cpp
index d3d2bfc616eb5c..d164e9661c53d6 100644
--- a/llvm/lib/CodeGen/EdgeBundles.cpp
+++ b/llvm/lib/CodeGen/EdgeBundles.cpp
@@ -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());
 
@@ -64,8 +79,6 @@ bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
     if (b1 != b0)
       Blocks[b1].push_back(i);
   }
-
-  return false;
 }
 
 namespace llvm {
@@ -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>>());
+}
diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp
index 80f7af1eaebbe0..3542bfe18af46f 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -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)
@@ -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>();
@@ -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>();
 
diff --git a/llvm/lib/CodeGen/SpillPlacement.cpp b/llvm/lib/CodeGen/SpillPlacement.cpp
index 9f91ee49341595..318e2b19322bb4 100644
--- a/llvm/lib/CodeGen/SpillPlacement.cpp
+++ b/llvm/lib/CodeGen/SpillPlacement.cpp
@@ -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);
 }
 
@@ -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()];
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index bc6b449d22abe8..cf7ceed63607a6 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -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"
diff --git a/llvm/lib/Target/X86/X86FloatingPoint.cpp b/llvm/lib/Target/X86/X86FloatingPoint.cpp
index ea94a4be32b2fa..34d8b774a186a4 100644
--- a/llvm/lib/Target/X86/X86FloatingPoint.cpp
+++ b/llvm/lib/Target/X86/X86FloatingPoint.cpp
@@ -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);
@@ -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)
 
@@ -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.

>From 74150e7c4cf2aee1e8d9f371278c8dc443ba36d3 Mon Sep 17 00:00:00 2001
From: Akshat Oke <Akshat.Oke at amd.com>
Date: Tue, 19 Nov 2024 11:55:25 +0000
Subject: [PATCH 2/2] use conjunction

---
 llvm/lib/CodeGen/EdgeBundles.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/EdgeBundles.cpp b/llvm/lib/CodeGen/EdgeBundles.cpp
index d164e9661c53d6..70729b162bfcaf 100644
--- a/llvm/lib/CodeGen/EdgeBundles.cpp
+++ b/llvm/lib/CodeGen/EdgeBundles.cpp
@@ -118,6 +118,6 @@ 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>>());
+  return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
+           !PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
 }



More information about the llvm-commits mailing list