[polly] 4c64d8e - [Polly] Port ForwardOpTree to the NewPM.

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 21:58:53 PST 2021


Author: Michael Kruse
Date: 2021-02-09T23:56:19-06:00
New Revision: 4c64d8ee3a871ef4bd7823d23ab009a19e89287f

URL: https://github.com/llvm/llvm-project/commit/4c64d8ee3a871ef4bd7823d23ab009a19e89287f
DIFF: https://github.com/llvm/llvm-project/commit/4c64d8ee3a871ef4bd7823d23ab009a19e89287f.diff

LOG: [Polly] Port ForwardOpTree to the NewPM.

Added: 
    

Modified: 
    polly/include/polly/ForwardOpTree.h
    polly/include/polly/LinkAllPasses.h
    polly/lib/Support/PollyPasses.def
    polly/lib/Support/RegisterPasses.cpp
    polly/lib/Transform/ForwardOpTree.cpp
    polly/test/ForwardOpTree/forward_load.ll

Removed: 
    


################################################################################
diff  --git a/polly/include/polly/ForwardOpTree.h b/polly/include/polly/ForwardOpTree.h
index d4f879c89686..dfa11bea9414 100644
--- a/polly/include/polly/ForwardOpTree.h
+++ b/polly/include/polly/ForwardOpTree.h
@@ -13,18 +13,35 @@
 #ifndef POLLY_FORWARDOPTREE_H
 #define POLLY_FORWARDOPTREE_H
 
-namespace llvm {
+#include "polly/ScopPass.h"
 
+namespace llvm {
 class PassRegistry;
 
-void initializeForwardOpTreePass(PassRegistry &);
+void initializeForwardOpTreeWrapperPassPass(PassRegistry &);
 } // namespace llvm
 
 namespace polly {
+Pass *createForwardOpTreeWrapperPass();
+
+struct ForwardOpTreePass : llvm::PassInfoMixin<ForwardOpTreePass> {
+  ForwardOpTreePass() {}
+
+  llvm::PreservedAnalyses run(Scop &S, ScopAnalysisManager &SAM,
+                              ScopStandardAnalysisResults &SAR, SPMUpdater &U);
+};
+
+struct ForwardOpTreePrinterPass
+    : llvm::PassInfoMixin<ForwardOpTreePrinterPass> {
+  ForwardOpTreePrinterPass(raw_ostream &OS) : OS(OS) {}
+
+  PreservedAnalyses run(Scop &S, ScopAnalysisManager &,
+                        ScopStandardAnalysisResults &SAR, SPMUpdater &);
 
-class ScopPass;
+private:
+  llvm::raw_ostream &OS;
+};
 
-ScopPass *createForwardOpTreePass();
 } // namespace polly
 
 #endif // POLLY_FORWARDOPTREE_H

diff  --git a/polly/include/polly/LinkAllPasses.h b/polly/include/polly/LinkAllPasses.h
index ed05f836d457..3fe255d8ff2c 100644
--- a/polly/include/polly/LinkAllPasses.h
+++ b/polly/include/polly/LinkAllPasses.h
@@ -57,6 +57,7 @@ createManagedMemoryRewritePassPass(GPUArch Arch = GPUArch::NVPTX64,
 #endif
 llvm::Pass *createIslScheduleOptimizerPass();
 llvm::Pass *createFlattenSchedulePass();
+llvm::Pass *createForwardOpTreeWrapperPass();
 llvm::Pass *createDeLICMPass();
 llvm::Pass *createMaximalStaticExpansionPass();
 
@@ -95,6 +96,7 @@ struct PollyForcePassLinking {
     polly::createIslScheduleOptimizerPass();
     polly::createMaximalStaticExpansionPass();
     polly::createFlattenSchedulePass();
+    polly::createForwardOpTreeWrapperPass();
     polly::createDeLICMPass();
     polly::createDumpModulePass("", true);
     polly::createSimplifyPass();
@@ -121,6 +123,7 @@ void initializeIslScheduleOptimizerPass(llvm::PassRegistry &);
 void initializeMaximalStaticExpanderPass(llvm::PassRegistry &);
 void initializePollyCanonicalizePass(llvm::PassRegistry &);
 void initializeFlattenSchedulePass(llvm::PassRegistry &);
+void initializeForwardOpTreeWrapperPassPass(llvm::PassRegistry &);
 void initializeDeLICMPass(llvm::PassRegistry &);
 } // namespace llvm
 

diff  --git a/polly/lib/Support/PollyPasses.def b/polly/lib/Support/PollyPasses.def
index 2c0592af28cc..1b6095f1800e 100644
--- a/polly/lib/Support/PollyPasses.def
+++ b/polly/lib/Support/PollyPasses.def
@@ -30,4 +30,6 @@ SCOP_PASS("print<polly-dependences>", DependenceInfoPrinterPass(outs()))
 SCOP_PASS("polly-codegen", CodeGenerationPass())
 SCOP_PASS("polly-simplify", SimplifyPass())
 SCOP_PASS("print<polly-simplify>", SimplifyPrinterPass(outs()))
+SCOP_PASS("polly-optree", ForwardOpTreePass())
+SCOP_PASS("print<polly-optree>", ForwardOpTreePrinterPass(outs()))
 #undef SCOP_PASS

diff  --git a/polly/lib/Support/RegisterPasses.cpp b/polly/lib/Support/RegisterPasses.cpp
index 0d2a5801bac1..37dedd7107f2 100644
--- a/polly/lib/Support/RegisterPasses.cpp
+++ b/polly/lib/Support/RegisterPasses.cpp
@@ -261,7 +261,7 @@ void initializePollyPasses(PassRegistry &Registry) {
   initializeRewriteByrefParamsPass(Registry);
   initializeCodegenCleanupPass(Registry);
   initializeFlattenSchedulePass(Registry);
-  initializeForwardOpTreePass(Registry);
+  initializeForwardOpTreeWrapperPassPass(Registry);
   initializeDeLICMPass(Registry);
   initializeSimplifyLegacyPassPass(Registry);
   initializeDumpModulePass(Registry);
@@ -321,7 +321,7 @@ void registerPollyPasses(llvm::legacy::PassManagerBase &PM) {
   if (EnableSimplify)
     PM.add(polly::createSimplifyPass(0));
   if (EnableForwardOpTree)
-    PM.add(polly::createForwardOpTreePass());
+    PM.add(polly::createForwardOpTreeWrapperPass());
   if (EnableDeLICM)
     PM.add(polly::createDeLICMPass());
   if (EnableSimplify)
@@ -468,6 +468,8 @@ static void buildDefaultPollyPipeline(FunctionPassManager &PM,
   assert(!PollyPrinter && "This option is not implemented");
   assert(!PollyOnlyPrinter && "This option is not implemented");
   assert(!EnablePolyhedralInfo && "This option is not implemented");
+  if (EnableForwardOpTree)
+    SPM.addPass(ForwardOpTreePass());
   assert(!EnableDeLICM && "This option is not implemented");
   assert(!EnableSimplify && "This option is not implemented");
   if (ImportJScop)

diff  --git a/polly/lib/Transform/ForwardOpTree.cpp b/polly/lib/Transform/ForwardOpTree.cpp
index c55dba800eed..6cdcc7145e69 100644
--- a/polly/lib/Transform/ForwardOpTree.cpp
+++ b/polly/lib/Transform/ForwardOpTree.cpp
@@ -1024,8 +1024,75 @@ class ForwardOpTreeImpl : ZoneAlgorithm {
 
     printStatements(OS, Indent);
   }
+
+  bool isModified() const { return Modified; }
 };
 
+static std::unique_ptr<ForwardOpTreeImpl> runForwardOpTree(Scop &S,
+                                                           LoopInfo &LI) {
+  std::unique_ptr<ForwardOpTreeImpl> Impl;
+  {
+    IslMaxOperationsGuard MaxOpGuard(S.getIslCtx().get(), MaxOps, false);
+    Impl = std::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
+
+    if (AnalyzeKnown) {
+      LLVM_DEBUG(dbgs() << "Prepare forwarders...\n");
+      Impl->computeKnownValues();
+    }
+
+    LLVM_DEBUG(dbgs() << "Forwarding operand trees...\n");
+    Impl->forwardOperandTrees();
+
+    if (MaxOpGuard.hasQuotaExceeded()) {
+      LLVM_DEBUG(dbgs() << "Not all operations completed because of "
+                           "max_operations exceeded\n");
+      KnownOutOfQuota++;
+    }
+  }
+
+  LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
+  LLVM_DEBUG(dbgs() << S);
+
+  // Update statistics
+  Scop::ScopStatistics ScopStats = S.getStatistics();
+  NumValueWrites += ScopStats.NumValueWrites;
+  NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
+  NumPHIWrites += ScopStats.NumPHIWrites;
+  NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
+  NumSingletonWrites += ScopStats.NumSingletonWrites;
+  NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
+
+  return Impl;
+}
+
+static PreservedAnalyses
+runForwardOpTreeUsingNPM(Scop &S, ScopAnalysisManager &SAM,
+                         ScopStandardAnalysisResults &SAR, SPMUpdater &U,
+                         raw_ostream *OS) {
+  LoopInfo &LI = SAR.LI;
+
+  std::unique_ptr<ForwardOpTreeImpl> Impl = runForwardOpTree(S, LI);
+  if (OS) {
+    *OS << "Printing analysis 'Polly - Forward operand tree' for region: '"
+        << S.getName() << "' in function '" << S.getFunction().getName()
+        << "':\n";
+    if (Impl) {
+      assert(Impl->getScop() == &S);
+
+      Impl->print(*OS);
+    }
+  }
+
+  if (!Impl->isModified())
+    return PreservedAnalyses::all();
+
+  PreservedAnalyses PA;
+  PA.preserveSet<AllAnalysesOn<Module>>();
+  PA.preserveSet<AllAnalysesOn<Function>>();
+  PA.preserveSet<AllAnalysesOn<Loop>>();
+  return PA;
+}
+
 /// Pass that redirects scalar reads to array elements that are known to contain
 /// the same value.
 ///
@@ -1034,7 +1101,7 @@ class ForwardOpTreeImpl : ZoneAlgorithm {
 /// scalar definition are redirected (We currently do not care about removing
 /// the write in this case).  This is also useful for the main DeLICM pass as
 /// there are less scalars to be mapped.
-class ForwardOpTree : public ScopPass {
+class ForwardOpTreeWrapperPass : public ScopPass {
 private:
   /// The pass implementation, also holding per-scop data.
   std::unique_ptr<ForwardOpTreeImpl> Impl;
@@ -1042,9 +1109,10 @@ class ForwardOpTree : public ScopPass {
 public:
   static char ID;
 
-  explicit ForwardOpTree() : ScopPass(ID) {}
-  ForwardOpTree(const ForwardOpTree &) = delete;
-  ForwardOpTree &operator=(const ForwardOpTree &) = delete;
+  explicit ForwardOpTreeWrapperPass() : ScopPass(ID) {}
+  ForwardOpTreeWrapperPass(const ForwardOpTreeWrapperPass &) = delete;
+  ForwardOpTreeWrapperPass &
+  operator=(const ForwardOpTreeWrapperPass &) = delete;
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.addRequiredTransitive<ScopInfoRegionPass>();
@@ -1058,36 +1126,7 @@ class ForwardOpTree : public ScopPass {
 
     LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
 
-    {
-      IslMaxOperationsGuard MaxOpGuard(S.getIslCtx().get(), MaxOps, false);
-      Impl = std::make_unique<ForwardOpTreeImpl>(&S, &LI, MaxOpGuard);
-
-      if (AnalyzeKnown) {
-        LLVM_DEBUG(dbgs() << "Prepare forwarders...\n");
-        Impl->computeKnownValues();
-      }
-
-      LLVM_DEBUG(dbgs() << "Forwarding operand trees...\n");
-      Impl->forwardOperandTrees();
-
-      if (MaxOpGuard.hasQuotaExceeded()) {
-        LLVM_DEBUG(dbgs() << "Not all operations completed because of "
-                             "max_operations exceeded\n");
-        KnownOutOfQuota++;
-      }
-    }
-
-    LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
-    LLVM_DEBUG(dbgs() << S);
-
-    // Update statistics
-    auto ScopStats = S.getStatistics();
-    NumValueWrites += ScopStats.NumValueWrites;
-    NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
-    NumPHIWrites += ScopStats.NumPHIWrites;
-    NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
-    NumSingletonWrites += ScopStats.NumSingletonWrites;
-    NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
+    Impl = runForwardOpTree(S, LI);
 
     return false;
   }
@@ -1103,13 +1142,28 @@ class ForwardOpTree : public ScopPass {
   void releaseMemory() override { Impl.reset(); }
 }; // class ForwardOpTree
 
-char ForwardOpTree::ID;
+char ForwardOpTreeWrapperPass::ID;
 } // namespace
 
-ScopPass *polly::createForwardOpTreePass() { return new ForwardOpTree(); }
+Pass *polly::createForwardOpTreeWrapperPass() {
+  return new ForwardOpTreeWrapperPass();
+}
 
-INITIALIZE_PASS_BEGIN(ForwardOpTree, "polly-optree",
+INITIALIZE_PASS_BEGIN(ForwardOpTreeWrapperPass, "polly-optree",
                       "Polly - Forward operand tree", false, false)
 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
-INITIALIZE_PASS_END(ForwardOpTree, "polly-optree",
+INITIALIZE_PASS_END(ForwardOpTreeWrapperPass, "polly-optree",
                     "Polly - Forward operand tree", false, false)
+
+llvm::PreservedAnalyses ForwardOpTreePass::run(Scop &S,
+                                               ScopAnalysisManager &SAM,
+                                               ScopStandardAnalysisResults &SAR,
+                                               SPMUpdater &U) {
+  return runForwardOpTreeUsingNPM(S, SAM, SAR, U, nullptr);
+}
+
+llvm::PreservedAnalyses
+ForwardOpTreePrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
+                              ScopStandardAnalysisResults &SAR, SPMUpdater &U) {
+  return runForwardOpTreeUsingNPM(S, SAM, SAR, U, &OS);
+}

diff  --git a/polly/test/ForwardOpTree/forward_load.ll b/polly/test/ForwardOpTree/forward_load.ll
index f3cb6732398c..f3859d31222e 100644
--- a/polly/test/ForwardOpTree/forward_load.ll
+++ b/polly/test/ForwardOpTree/forward_load.ll
@@ -1,4 +1,5 @@
 ; RUN: opt %loadPolly -polly-optree -analyze < %s | FileCheck %s -match-full-lines
+; RUN: opt %loadPolly "-passes=scop(print<polly-optree>)" -disable-output < %s | FileCheck %s -match-full-lines
 ;
 ; Rematerialize a load.
 ;


        


More information about the llvm-commits mailing list