[llvm] 9879c55 - Expose ScalarizerPass options to C++ (not just commandline)

Bjorn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 14 04:00:55 PDT 2022


Author: Benoit Jacob
Date: 2022-03-14T12:00:35+01:00
New Revision: 9879c555f21097aee15e73dd25bd89f652dba8ea

URL: https://github.com/llvm/llvm-project/commit/9879c555f21097aee15e73dd25bd89f652dba8ea
DIFF: https://github.com/llvm/llvm-project/commit/9879c555f21097aee15e73dd25bd89f652dba8ea.diff

LOG: Expose ScalarizerPass options to C++ (not just commandline)

Context: I needed this for https://github.com/google/iree/pull/8474 .
I found that TSan instrumentation expects vector sizes to be <= 16,
and in my project (IREE) we have tests with higher vector sizes.
That left some test functions uninstrumented, resulting in crashes as
instrumented code called into them.

Differential Revision: https://reviews.llvm.org/D121182

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Scalar/Scalarizer.h
    llvm/lib/Transforms/Scalar/Scalarizer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Scalar/Scalarizer.h b/llvm/include/llvm/Transforms/Scalar/Scalarizer.h
index 32db96e84e965..5cc67f78e5a20 100644
--- a/llvm/include/llvm/Transforms/Scalar/Scalarizer.h
+++ b/llvm/include/llvm/Transforms/Scalar/Scalarizer.h
@@ -17,6 +17,7 @@
 #ifndef LLVM_TRANSFORMS_SCALAR_SCALARIZER_H
 #define LLVM_TRANSFORMS_SCALAR_SCALARIZER_H
 
+#include "llvm/ADT/Optional.h"
 #include "llvm/IR/PassManager.h"
 
 namespace llvm {
@@ -24,9 +25,25 @@ namespace llvm {
 class Function;
 class FunctionPass;
 
+struct ScalarizerPassOptions {
+  // These optional booleans correspond 1:1 to cl::opt<bool> options defined in
+  // Scalarizer.cpp. When the cl::opt are specified, they take precedence.
+  // When the cl::opt are not specified, the present optional booleans allow to
+  // override the cl::opt's default values.
+  llvm::Optional<bool> ScalarizeVariableInsertExtract;
+  llvm::Optional<bool> ScalarizeLoadStore;
+};
+
 class ScalarizerPass : public PassInfoMixin<ScalarizerPass> {
+  ScalarizerPassOptions Options;
+
 public:
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+
+  void setScalarizeVariableInsertExtract(bool Value) {
+    Options.ScalarizeVariableInsertExtract = Value;
+  }
+  void setScalarizeLoadStore(bool Value) { Options.ScalarizeLoadStore = Value; }
 };
 
 /// Create a legacy pass manager instance of the Scalarizer pass

diff  --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
index 4a77ce826ca3a..28135ffb331ed 100644
--- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -50,7 +50,7 @@ using namespace llvm;
 
 #define DEBUG_TYPE "scalarizer"
 
-static cl::opt<bool> ScalarizeVariableInsertExtract(
+static cl::opt<bool> ClScalarizeVariableInsertExtract(
     "scalarize-variable-insert-extract", cl::init(true), cl::Hidden,
     cl::desc("Allow the scalarizer pass to scalarize "
              "insertelement/extractelement with variable index"));
@@ -58,9 +58,9 @@ static cl::opt<bool> ScalarizeVariableInsertExtract(
 // This is disabled by default because having separate loads and stores
 // makes it more likely that the -combiner-alias-analysis limits will be
 // reached.
-static cl::opt<bool>
-    ScalarizeLoadStore("scalarize-load-store", cl::init(false), cl::Hidden,
-                       cl::desc("Allow the scalarizer pass to scalarize loads and store"));
+static cl::opt<bool> ClScalarizeLoadStore(
+    "scalarize-load-store", cl::init(false), cl::Hidden,
+    cl::desc("Allow the scalarizer pass to scalarize loads and store"));
 
 namespace {
 
@@ -186,10 +186,23 @@ struct VectorLayout {
   uint64_t ElemSize = 0;
 };
 
+template <typename T>
+T getWithDefaultOverride(const cl::opt<T> &ClOption,
+                         const llvm::Optional<T> &DefaultOverride) {
+  return ClOption.getNumOccurrences() ? ClOption
+                                      : DefaultOverride.getValueOr(ClOption);
+}
+
 class ScalarizerVisitor : public InstVisitor<ScalarizerVisitor, bool> {
 public:
-  ScalarizerVisitor(unsigned ParallelLoopAccessMDKind, DominatorTree *DT)
-    : ParallelLoopAccessMDKind(ParallelLoopAccessMDKind), DT(DT) {
+  ScalarizerVisitor(unsigned ParallelLoopAccessMDKind, DominatorTree *DT,
+                    ScalarizerPassOptions Options)
+      : ParallelLoopAccessMDKind(ParallelLoopAccessMDKind), DT(DT),
+        ScalarizeVariableInsertExtract(
+            getWithDefaultOverride(ClScalarizeVariableInsertExtract,
+                                   Options.ScalarizeVariableInsertExtract)),
+        ScalarizeLoadStore(getWithDefaultOverride(ClScalarizeLoadStore,
+                                                  Options.ScalarizeLoadStore)) {
   }
 
   bool visit(Function &F);
@@ -235,6 +248,9 @@ class ScalarizerVisitor : public InstVisitor<ScalarizerVisitor, bool> {
   unsigned ParallelLoopAccessMDKind;
 
   DominatorTree *DT;
+
+  const bool ScalarizeVariableInsertExtract;
+  const bool ScalarizeLoadStore;
 };
 
 class ScalarizerLegacyPass : public FunctionPass {
@@ -334,7 +350,7 @@ bool ScalarizerLegacyPass::runOnFunction(Function &F) {
   unsigned ParallelLoopAccessMDKind =
       M.getContext().getMDKindID("llvm.mem.parallel_loop_access");
   DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
-  ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT);
+  ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT, ScalarizerPassOptions());
   return Impl.visit(F);
 }
 
@@ -983,7 +999,7 @@ PreservedAnalyses ScalarizerPass::run(Function &F, FunctionAnalysisManager &AM)
   unsigned ParallelLoopAccessMDKind =
       M.getContext().getMDKindID("llvm.mem.parallel_loop_access");
   DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
-  ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT);
+  ScalarizerVisitor Impl(ParallelLoopAccessMDKind, DT, Options);
   bool Changed = Impl.visit(F);
   PreservedAnalyses PA;
   PA.preserve<DominatorTreeAnalysis>();


        


More information about the llvm-commits mailing list