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

Conversation

optimisan
Copy link
Contributor

No description provided.

Copy link
Contributor Author

optimisan commented Nov 18, 2024

@llvmbot
Copy link
Member

llvmbot commented Nov 18, 2024

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-regalloc

Author: Akshat Oke (optimisan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/116616.diff

9 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/EdgeBundles.h (+34-5)
  • (modified) llvm/include/llvm/CodeGen/Passes.h (+1-1)
  • (modified) llvm/include/llvm/InitializePasses.h (+1-1)
  • (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+1-1)
  • (modified) llvm/lib/CodeGen/EdgeBundles.cpp (+30-9)
  • (modified) llvm/lib/CodeGen/RegAllocGreedy.cpp (+3-3)
  • (modified) llvm/lib/CodeGen/SpillPlacement.cpp (+3-3)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Target/X86/X86FloatingPoint.cpp (+3-3)
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 708ff464b38093..5bfb71364d7870 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 7ecd59a14f709a..fb8356b9c98cb9 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 375709e0a05f4c..e4cc257623948d 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 a181a28f502f59..ad285c64a454bd 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.

@@ -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

Comment on lines 121 to 122
return !(PAC.preserved() || PAC.preservedSet<CFGAnalyses>() ||
PAC.preservedSet<AllAnalysesOn<MachineFunction>>());
Copy link
Contributor

Choose a reason for hiding this comment

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

push negation through the expression

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from a001cd1 to 74150e7 Compare November 19, 2024 11:56
Copy link

github-actions bot commented Nov 19, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

lgtm with clang-format errors fixed

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from 74150e7 to f3ca5cd Compare November 22, 2024 11:14
Copy link
Contributor Author

optimisan commented Nov 22, 2024

Merge activity

  • Nov 22, 6:17 AM EST: A user started a stack merge that includes this pull request via Graphite.
  • Nov 22, 6:19 AM EST: Graphite rebased this pull request as part of a merge.
  • Nov 22, 6:21 AM EST: A user merged this pull request with Graphite.

@optimisan optimisan force-pushed the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch from f3ca5cd to c89ffe2 Compare November 22, 2024 11:19
@optimisan optimisan merged commit cac1360 into main Nov 22, 2024
5 of 7 checks passed
@optimisan optimisan deleted the users/Akshat-Oke/11-18-_codegen_newpm_port_edgebundles_analysis_to_npm branch November 22, 2024 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants