[llvm] [Reg2Mem] Add legacy pass wrapping Reg2Mem (PR #111024)

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 04:50:44 PDT 2024


https://github.com/Keenuts updated https://github.com/llvm/llvm-project/pull/111024

>From 8cdf6cc1c4f6b7ebc92b1f89f05d062ad8b59d57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Thu, 3 Oct 2024 18:26:10 +0200
Subject: [PATCH 1/6] [Reg2Mem] Add legacy pass wrapping Reg2Mem
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 <brioche at google.com>
---
 llvm/include/llvm/Transforms/Scalar/Reg2Mem.h | 25 +++++++++++++++++
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp        | 28 +++++++++++++++++++
 2 files changed, 53 insertions(+)

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();
+}

>From 4bc971dd68e1f0e664ba4cde4d9ad6d8c525249f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Thu, 3 Oct 2024 19:01:18 +0200
Subject: [PATCH 2/6] fold condition

---
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
index 33ae5faeba1198..ec6c6ca60591fe 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -125,9 +125,7 @@ bool RegToMemWrapperPass::runOnFunction(Function &F) {
 
   unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
   bool Changed = runPass(F);
-  if (N == 0 && !Changed)
-    return false;
-  return true;
+  return N != 0 || Changed;
 }
 
 FunctionPass *llvm::createRegToMemWrapperPass() {

>From 26b362c76eeb6d94e1c7d46cb1bb219d20d97de6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Fri, 4 Oct 2024 09:13:31 +0200
Subject: [PATCH 3/6] move to cpp file
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Nathan Gauër <brioche at google.com>
---
 llvm/include/llvm/Transforms/Scalar/Reg2Mem.h | 25 -----------------
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp        | 28 +++++++++++++++++--
 2 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h b/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
index f7815ca9a9a18c..25f6563d7dcfc2 100644
--- a/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
+++ b/llvm/include/llvm/Transforms/Scalar/Reg2Mem.h
@@ -13,11 +13,7 @@
 #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 {
 
@@ -26,27 +22,6 @@ 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 ec6c6ca60591fe..0bc8fd0c469925 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -18,6 +18,7 @@
 #include "llvm/Transforms/Scalar/Reg2Mem.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/InitializePasses.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Dominators.h"
@@ -107,13 +108,36 @@ PreservedAnalyses RegToMemPass::run(Function &F, FunctionAnalysisManager &AM) {
 }
 
 namespace llvm {
+
 void initializeRegToMemWrapperPassPass(PassRegistry &);
+
+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();
+
 } // namespace llvm
 
-INITIALIZE_PASS_BEGIN(RegToMemWrapperPass, "reg-to-mem", "", true, true)
+INITIALIZE_PASS_BEGIN(RegToMemWrapperPass, "reg2mem", "", true, true)
 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
-INITIALIZE_PASS_END(RegToMemWrapperPass, "reg-to-mem", "", true, true)
+INITIALIZE_PASS_END(RegToMemWrapperPass, "reg2mem", "", true, true)
 
 char RegToMemWrapperPass::ID = 0;
 

>From ad30131ae588fb23326c8df0800c5e00bcbf54bf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Fri, 4 Oct 2024 09:17:11 +0200
Subject: [PATCH 4/6] clang-format

---
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
index 0bc8fd0c469925..b9cf968afd2427 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -18,7 +18,6 @@
 #include "llvm/Transforms/Scalar/Reg2Mem.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/LoopInfo.h"
-#include "llvm/InitializePasses.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Dominators.h"
@@ -26,6 +25,7 @@
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/InitializePasses.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"

>From dde6fbc475be0ca76de18416fa0eb3365fb4a7cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Fri, 4 Oct 2024 15:04:39 +0200
Subject: [PATCH 5/6] fold implem in class

---
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
index b9cf968afd2427..0ac37798cef7f1 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -115,7 +115,7 @@ class RegToMemWrapperPass : public FunctionPass {
 public:
   static char ID;
 
-  RegToMemWrapperPass();
+  RegToMemWrapperPass() : FunctionPass(ID) {}
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesAll();
@@ -127,7 +127,14 @@ class RegToMemWrapperPass : public FunctionPass {
     AU.addRequired<LoopInfoWrapperPass>();
   }
 
-  bool runOnFunction(Function &F) override;
+  bool runOnFunction(Function &F) override {
+    DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+    LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+
+    unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
+    bool Changed = runPass(F);
+    return N != 0 || Changed;
+  }
 };
 
 FunctionPass *createRegToMemWrapperPass();
@@ -141,17 +148,6 @@ INITIALIZE_PASS_END(RegToMemWrapperPass, "reg2mem", "", 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);
-  return N != 0 || Changed;
-}
-
 FunctionPass *llvm::createRegToMemWrapperPass() {
   return new RegToMemWrapperPass();
 }

>From 4c4d5590b2fd6c990c00b933930f79fe8d2105d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Fri, 4 Oct 2024 15:36:28 +0200
Subject: [PATCH 6/6] add createRegToMem functions to shared headers

---
 llvm/include/llvm/InitializePasses.h   | 1 +
 llvm/include/llvm/LinkAllPasses.h      | 1 +
 llvm/include/llvm/Transforms/Utils.h   | 8 ++++++++
 llvm/lib/Transforms/Scalar/Reg2Mem.cpp | 3 ---
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 1374880b6a716b..e50cb0dd7541a2 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -256,6 +256,7 @@ void initializeRegAllocFastPass(PassRegistry &);
 void initializeRegAllocPriorityAdvisorAnalysisPass(PassRegistry &);
 void initializeRegAllocScoringPass(PassRegistry &);
 void initializeRegBankSelectPass(PassRegistry &);
+void initializeRegToMemWrapperPassPass(PassRegistry &);
 void initializeRegUsageInfoCollectorPass(PassRegistry &);
 void initializeRegUsageInfoPropagationPass(PassRegistry &);
 void initializeRegionInfoPassPass(PassRegistry &);
diff --git a/llvm/include/llvm/LinkAllPasses.h b/llvm/include/llvm/LinkAllPasses.h
index 92b59a66567c95..3516b47d29ef36 100644
--- a/llvm/include/llvm/LinkAllPasses.h
+++ b/llvm/include/llvm/LinkAllPasses.h
@@ -98,6 +98,7 @@ struct ForcePassLinking {
     (void)llvm::createNaryReassociatePass();
     (void)llvm::createObjCARCContractPass();
     (void)llvm::createPromoteMemoryToRegisterPass();
+    (void)llvm::createRegToMemWrapperPass();
     (void)llvm::createPostDomOnlyPrinterWrapperPassPass();
     (void)llvm::createPostDomPrinterWrapperPassPass();
     (void)llvm::createPostDomOnlyViewerWrapperPassPass();
diff --git a/llvm/include/llvm/Transforms/Utils.h b/llvm/include/llvm/Transforms/Utils.h
index 677cc3d128c3a3..ff306dbe358038 100644
--- a/llvm/include/llvm/Transforms/Utils.h
+++ b/llvm/include/llvm/Transforms/Utils.h
@@ -81,6 +81,14 @@ extern char &LCSSAID;
 //
 FunctionPass *createPromoteMemoryToRegisterPass();
 
+//===----------------------------------------------------------------------===//
+//
+// RegToMemWrapperPass - This pass is used to demote registers to memory
+// references. In basically undoes the PromoteMemoryToRegister pass to make cfg
+// hacking easier.
+//
+FunctionPass *createRegToMemWrapperPass();
+
 //===----------------------------------------------------------------------===//
 //
 // LoopSimplify - Insert Pre-header blocks into the CFG for every function in
diff --git a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
index 0ac37798cef7f1..30b27cb19b4ad9 100644
--- a/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
+++ b/llvm/lib/Transforms/Scalar/Reg2Mem.cpp
@@ -136,9 +136,6 @@ class RegToMemWrapperPass : public FunctionPass {
     return N != 0 || Changed;
   }
 };
-
-FunctionPass *createRegToMemWrapperPass();
-
 } // namespace llvm
 
 INITIALIZE_PASS_BEGIN(RegToMemWrapperPass, "reg2mem", "", true, true)



More information about the llvm-commits mailing list