Skip to content

[Reg2Mem] Add legacy pass wrapping Reg2Mem #111024

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
merged 6 commits into from
Oct 21, 2024
Merged

Conversation

Keenuts
Copy link
Contributor

@Keenuts Keenuts commented Oct 3, 2024

The SPIR-V backend will need to use Reg2Mem, hence this pass needs to be wrapped to be used with the legacy pass manager.

@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Nathan Gauër (Keenuts)

Changes

The SPIR-V backend will need to use Reg2Mem, hence this pass needs to be wrapped to be used with the legacy pass manager.


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

2 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Scalar/Reg2Mem.h (+25)
  • (modified) llvm/lib/Transforms/Scalar/Reg2Mem.cpp (+28)
diff --git a/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h b/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
index 25f6563d7dcfc2..f7815ca9a9a18c 100644
--- a/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
+++ b/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
@@ -13,7 +13,11 @@
 #ifndef LLVM_TRANSFORMS_SCALAR_REG2MEM_H
 #define LLVM_TRANSFORMS_SCALAR_REG2MEM_H
 
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/IR/Dominators.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
 
 namespace llvm {
 
@@ -22,6 +26,27 @@ class RegToMemPass : public PassInfoMixin<RegToMemPass> {
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };
 
+class RegToMemWrapperPass : public FunctionPass {
+public:
+  static char ID;
+
+  RegToMemWrapperPass();
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesAll();
+
+    AU.addPreserved<DominatorTreeWrapperPass>();
+    AU.addRequired<DominatorTreeWrapperPass>();
+
+    AU.addPreserved<LoopInfoWrapperPass>();
+    AU.addRequired<LoopInfoWrapperPass>();
+  }
+
+  bool runOnFunction(Function &F) override;
+};
+
+FunctionPass *createRegToMemWrapperPass();
+
 } // end namespace llvm
 
 #endif // LLVM_TRANSFORMS_SCALAR_REG2MEM_H
diff --git a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
index ebc5075aa36fe8..33ae5faeba1198 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -105,3 +105,31 @@ PreservedAnalyses RegToMemPass::run(Function &F, FunctionAnalysisManager &AM) {
   PA.preserve<LoopAnalysis>();
   return PA;
 }
+
+namespace llvm {
+void initializeRegToMemWrapperPassPass(PassRegistry &);
+} // namespace llvm
+
+INITIALIZE_PASS_BEGIN(RegToMemWrapperPass, "reg-to-mem", "", true, true)
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
+INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
+INITIALIZE_PASS_END(RegToMemWrapperPass, "reg-to-mem", "", true, true)
+
+char RegToMemWrapperPass::ID = 0;
+
+RegToMemWrapperPass::RegToMemWrapperPass() : FunctionPass(ID) {}
+
+bool RegToMemWrapperPass::runOnFunction(Function &F) {
+  DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+  LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+
+  unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
+  bool Changed = runPass(F);
+  if (N == 0 && !Changed)
+    return false;
+  return true;
+}
+
+FunctionPass *llvm::createRegToMemWrapperPass() {
+  return new RegToMemWrapperPass();
+}

@Keenuts Keenuts requested a review from nikic October 3, 2024 17:04
@Keenuts
Copy link
Contributor Author

Keenuts commented Oct 3, 2024

Hi! Here is some additional context on where we plan on using Reg2Mem pass if you need:
#111026

@Keenuts
Copy link
Contributor Author

Keenuts commented Oct 4, 2024

Thanks! Moved to cpp, and renamed the pass to reg2mem.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@Keenuts
Copy link
Contributor Author

Keenuts commented Oct 4, 2024

Added the header functions to be able to instantiate it.

The SPIR-V backend will need to use Reg2Mem, hence this pass
needs to be wrapped to be used with the legacy pass manager.

Signed-off-by: Nathan Gauër <[email protected]>
Signed-off-by: Nathan Gauër <[email protected]>
@Keenuts
Copy link
Contributor Author

Keenuts commented Oct 21, 2024

Back from holiday! Thanks for the review. Rebased on main, will wait for green CI and local tests & submit.

@Keenuts Keenuts merged commit e26d907 into llvm:main Oct 21, 2024
8 checks passed
@Keenuts Keenuts deleted the wrap-reg2mem branch October 21, 2024 13:00
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.

3 participants