[llvm] [Passes] Generalize ShouldRunExtraVectorPasses to allow re-use (NFCI). (PR #118323)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 2 09:04:33 PST 2024


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/118323

Generalize ShouldRunExtraVectorPasses toj ShouldRunExtraPasses, to allow re-use for other transformations. I intend to use this to let LowerMatrixIntrinsicsPass request EarlyCSE if there are any matrix constructs to lower. This should allow enabling LowerMatrixIntrinsics without a noticable compile-time hit.

>From cb6f7b99e53d894a7761776191b0671400e73f90 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 1 Dec 2024 12:56:39 +0000
Subject: [PATCH] [Passes] Generalize ShouldRunExtraVectorPasses to allow
 re-use (NFCI).

Generalize ShouldRunExtraVectorPasses toj ShouldRunExtraPasses, to allow
re-use for other transformations. I intend to use this to let
LowerMatrixIntrinsicsPass request EarlyCSE if there are any matrix
constructs to lower. This should allow enabling LowerMatrixIntrinsics
without a noticable compile-time hit.
---
 llvm/include/llvm/IR/PassManager.h            | 32 +++++++++++++++++++
 .../llvm/Transforms/Vectorize/LoopVectorize.h | 32 -------------------
 llvm/lib/IR/PassManager.cpp                   |  2 ++
 llvm/lib/Passes/PassBuilderPipelines.cpp      |  4 +--
 llvm/lib/Passes/PassRegistry.def              |  4 +--
 .../Transforms/Vectorize/LoopVectorize.cpp    |  6 ++--
 6 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index 5dab9d0d0a7979..91730301bbd415 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -935,6 +935,38 @@ struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
   }
 };
 
+/// A marker analyss to determine if extra passes should be run on demand.
+/// Passes requesting extra transformations to run need to request and preserve
+/// this analysis.
+struct ShouldRunExtraPasses : public AnalysisInfoMixin<ShouldRunExtraPasses> {
+  static AnalysisKey Key;
+  struct Result {
+    bool invalidate(Function &F, const PreservedAnalyses &PA,
+                    FunctionAnalysisManager::Invalidator &) {
+      // Check whether the analysis has been explicitly invalidated. Otherwise,
+      // it remains preserved.
+      auto PAC = PA.getChecker<ShouldRunExtraPasses>();
+      return !PAC.preservedWhenStateless();
+    }
+  };
+
+  Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
+};
+
+/// A pass manager to run a set of extra function passes if the
+/// ShouldRunExtraPasses marker analysis is present. This allows passes to
+/// request additional transformations on demand. An example is extra
+/// simplifications after loop-vectorization, if runtime checks have been added.
+struct ExtraPassManager : public FunctionPassManager {
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
+    auto PA = PreservedAnalyses::all();
+    if (AM.getCachedResult<ShouldRunExtraPasses>(F))
+      PA.intersect(FunctionPassManager::run(F, AM));
+    PA.abandon<ShouldRunExtraPasses>();
+    return PA;
+  }
+};
+
 } // end namespace llvm
 
 #endif // LLVM_IR_PASSMANAGER_H
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
index 24b6354662955e..9256cf16c0157e 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorize.h
@@ -80,38 +80,6 @@ class TargetTransformInfo;
 extern cl::opt<bool> EnableLoopInterleaving;
 extern cl::opt<bool> EnableLoopVectorization;
 
-/// A marker to determine if extra passes after loop vectorization should be
-/// run.
-struct ShouldRunExtraVectorPasses
-    : public AnalysisInfoMixin<ShouldRunExtraVectorPasses> {
-  static AnalysisKey Key;
-  struct Result {
-    bool invalidate(Function &F, const PreservedAnalyses &PA,
-                    FunctionAnalysisManager::Invalidator &) {
-      // Check whether the analysis has been explicitly invalidated. Otherwise,
-      // it remains preserved.
-      auto PAC = PA.getChecker<ShouldRunExtraVectorPasses>();
-      return !PAC.preservedWhenStateless();
-    }
-  };
-
-  Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
-};
-
-/// A pass manager to run a set of extra function simplification passes after
-/// vectorization, if requested. LoopVectorize caches the
-/// ShouldRunExtraVectorPasses analysis to request extra simplifications, if
-/// they could be beneficial.
-struct ExtraVectorPassManager : public FunctionPassManager {
-  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
-    auto PA = PreservedAnalyses::all();
-    if (AM.getCachedResult<ShouldRunExtraVectorPasses>(F))
-      PA.intersect(FunctionPassManager::run(F, AM));
-    PA.abandon<ShouldRunExtraVectorPasses>();
-    return PA;
-  }
-};
-
 struct LoopVectorizeOptions {
   /// If false, consider all loops for interleaving.
   /// If true, only loops that explicitly request interleaving are considered.
diff --git a/llvm/lib/IR/PassManager.cpp b/llvm/lib/IR/PassManager.cpp
index bf8f7906d3368d..4990695108fc70 100644
--- a/llvm/lib/IR/PassManager.cpp
+++ b/llvm/lib/IR/PassManager.cpp
@@ -160,3 +160,5 @@ void llvm::printIRUnitNameForStackTrace<Function>(raw_ostream &OS,
 AnalysisSetKey CFGAnalyses::SetKey;
 
 AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
+
+AnalysisKey ShouldRunExtraPasses::Key;
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 5a7c327de95870..5a8c60adccee34 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -1306,8 +1306,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
   // Cleanup after the loop optimization passes.
   FPM.addPass(InstCombinePass());
 
+  ExtraPassManager ExtraPasses;
   if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) {
-    ExtraVectorPassManager ExtraPasses;
     // At higher optimization levels, try to clean up any runtime overlap and
     // alignment checks inserted by the vectorizer. We want to track correlated
     // runtime checks for two inner loops in the same outer loop, fold any
@@ -1328,8 +1328,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
     ExtraPasses.addPass(
         SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
     ExtraPasses.addPass(InstCombinePass());
-    FPM.addPass(std::move(ExtraPasses));
   }
+  FPM.addPass(std::move(ExtraPasses));
 
   // Now that we've formed fast to execute loop structures, we do further
   // optimizations. These are run afterward as they might block doing complex
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 7c3798f6462a46..260fbae1d2f87d 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -303,8 +303,8 @@ FUNCTION_ANALYSIS("regions", RegionInfoAnalysis())
 FUNCTION_ANALYSIS("scalar-evolution", ScalarEvolutionAnalysis())
 FUNCTION_ANALYSIS("should-not-run-function-passes",
                   ShouldNotRunFunctionPassesAnalysis())
-FUNCTION_ANALYSIS("should-run-extra-vector-passes",
-                  ShouldRunExtraVectorPasses())
+FUNCTION_ANALYSIS("should-run-extra-passes",
+                  ShouldRunExtraPasses())
 FUNCTION_ANALYSIS("ssp-layout", SSPLayoutAnalysis())
 FUNCTION_ANALYSIS("stack-safety-local", StackSafetyAnalysis())
 FUNCTION_ANALYSIS("target-ir",
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 90312c1a28df3c..2ce293889e88e4 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -442,8 +442,6 @@ using SCEV2ValueTy = DenseMap<const SCEV *, Value *>;
 
 namespace llvm {
 
-AnalysisKey ShouldRunExtraVectorPasses::Key;
-
 /// InnerLoopVectorizer vectorizes loops which contain only one basic
 /// block to a specified vectorization factor (VF).
 /// This class performs the widening of scalars into vectors, or multiple
@@ -10476,8 +10474,8 @@ PreservedAnalyses LoopVectorizePass::run(Function &F,
     // extra simplification passes should be run.
     // TODO: MadeCFGChanges is not a prefect proxy. Extra passes should only
     // be run if runtime checks have been added.
-    AM.getResult<ShouldRunExtraVectorPasses>(F);
-    PA.preserve<ShouldRunExtraVectorPasses>();
+    AM.getResult<ShouldRunExtraPasses>(F);
+    PA.preserve<ShouldRunExtraPasses>();
   } else {
     PA.preserveSet<CFGAnalyses>();
   }



More information about the llvm-commits mailing list