[llvm] [Passes] Always run LowerMatrixIntrinsics, request EarlyCSE on demand. (PR #118507)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 13:14:17 PST 2024


https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/118507

>From 938b38f9ab6ec3e67641fe829a21d520855a3047 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 1 Dec 2024 12:44:39 +0000
Subject: [PATCH 1/3] [Passes] Always run LowerMatrixIntrinsics, request
 EarlyCSE on demand.

---
 .../Transforms/Scalar/LowerMatrixIntrinsics.h   | 10 ++++++++++
 llvm/lib/Passes/PassBuilderPipelines.cpp        | 17 ++++++-----------
 llvm/lib/Passes/PassRegistry.def                |  2 ++
 .../Transforms/Scalar/LowerMatrixIntrinsics.cpp |  4 ++++
 llvm/test/Other/cse-after-matrix-lowering.ll    | 17 +++++++++++++++++
 llvm/test/Other/new-pm-O0-defaults.ll           |  9 ++++-----
 llvm/test/Other/new-pm-defaults.ll              |  7 +------
 .../Other/new-pm-thinlto-postlink-defaults.ll   |  1 +
 .../new-pm-thinlto-postlink-pgo-defaults.ll     |  1 +
 ...ew-pm-thinlto-postlink-samplepgo-defaults.ll |  1 +
 10 files changed, 47 insertions(+), 22 deletions(-)
 create mode 100644 llvm/test/Other/cse-after-matrix-lowering.ll

diff --git a/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h b/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
index d44d297dd4ffc7..e95fbc1ffcdef2 100644
--- a/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
+++ b/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
@@ -14,6 +14,7 @@
 #define LLVM_TRANSFORMS_SCALAR_LOWERMATRIXINTRINSICS_H
 
 #include "llvm/IR/PassManager.h"
+#include "llvm/Transforms/Utils/ExtraPassManager.h"
 
 namespace llvm {
 class LowerMatrixIntrinsicsPass
@@ -27,6 +28,15 @@ class LowerMatrixIntrinsicsPass
                      function_ref<StringRef(StringRef)> MapClassName2PassName);
   static bool isRequired() { return true; }
 };
+
+/// A marker analyss to determine if extra passes should be run after lowering
+/// matrix intrinsics.
+struct ShouldRunExtraMatrixPasses
+    : public ShouldRunExtraPasses<ShouldRunExtraMatrixPasses>,
+      public AnalysisInfoMixin<ShouldRunExtraMatrixPasses> {
+  static AnalysisKey Key;
+};
+
 } // namespace llvm
 
 #endif
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index d737ea5ab070a9..5eb516843d55c3 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -270,10 +270,6 @@ static cl::opt<bool> EnableOrderFileInstrumentation(
     "enable-order-file-instrumentation", cl::init(false), cl::Hidden,
     cl::desc("Enable order file instrumentation (default = off)"));
 
-static cl::opt<bool>
-    EnableMatrix("enable-matrix", cl::init(false), cl::Hidden,
-                 cl::desc("Enable lowering of the matrix intrinsics"));
-
 static cl::opt<bool> EnableConstraintElimination(
     "enable-constraint-elimination", cl::init(true), cl::Hidden,
     cl::desc(
@@ -1489,10 +1485,10 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
   OptimizePM.addPass(Float2IntPass());
   OptimizePM.addPass(LowerConstantIntrinsicsPass());
 
-  if (EnableMatrix) {
-    OptimizePM.addPass(LowerMatrixIntrinsicsPass());
-    OptimizePM.addPass(EarlyCSEPass());
-  }
+  OptimizePM.addPass(LowerMatrixIntrinsicsPass());
+  ExtraPassManager<ShouldRunExtraMatrixPasses> ExtraPasses;
+  ExtraPasses.addPass(EarlyCSEPass());
+  OptimizePM.addPass(std::move(ExtraPasses));
 
   // CHR pass should only be applied with the profile information.
   // The check is to check the profile summary information in CHR.
@@ -2189,9 +2185,8 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
   if (PTO.MergeFunctions)
     MPM.addPass(MergeFunctionsPass());
 
-  if (EnableMatrix)
-    MPM.addPass(
-        createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true)));
+  MPM.addPass(
+      createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true)));
 
   if (!CGSCCOptimizerLateEPCallbacks.empty()) {
     CGSCCPassManager CGPM;
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 2ddebb07017c2a..3ce028278ff8f2 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -305,6 +305,8 @@ FUNCTION_ANALYSIS("scalar-evolution", ScalarEvolutionAnalysis())
 FUNCTION_ANALYSIS("should-not-run-function-passes",
                   ShouldNotRunFunctionPassesAnalysis())
 FUNCTION_ANALYSIS("should-run-extra-vector-passes",
+                  ShouldRunExtraMatrixPasses())
+FUNCTION_ANALYSIS("should-run-extra-matrix-passes",
                   ShouldRunExtraVectorPasses())
 FUNCTION_ANALYSIS("ssp-layout", SSPLayoutAnalysis())
 FUNCTION_ANALYSIS("stack-safety-local", StackSafetyAnalysis())
diff --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index 796fba67ee2576..168c7016d5b0ce 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -117,6 +117,8 @@ auto m_AnyAdd(const LTy &L, const RTy &R) {
   return m_CombineOr(m_Add(L, R), m_FAdd(L, R));
 }
 
+AnalysisKey ShouldRunExtraMatrixPasses::Key;
+
 namespace {
 
 // Given an element pointer \p BasePtr to the start of a (sub) matrix, compute
@@ -2651,6 +2653,8 @@ PreservedAnalyses LowerMatrixIntrinsicsPass::run(Function &F,
     if (!Minimal) {
       PA.preserve<LoopAnalysis>();
       PA.preserve<DominatorTreeAnalysis>();
+      AM.getResult<ShouldRunExtraMatrixPasses>(F);
+      PA.preserve<ShouldRunExtraMatrixPasses>();
     }
     return PA;
   }
diff --git a/llvm/test/Other/cse-after-matrix-lowering.ll b/llvm/test/Other/cse-after-matrix-lowering.ll
new file mode 100644
index 00000000000000..66ea3837d7c119
--- /dev/null
+++ b/llvm/test/Other/cse-after-matrix-lowering.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -disable-verify -verify-analysis-invalidation=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
+; RUN:     -passes='default<O3>' -S %s 2>&1 | FileCheck %s
+
+define <4 x float> @multiply_2x2(<4 x float> %a, <4 x float> %b) {
+; CHECK:      Running pass: LowerMatrixIntrinsicsPass
+; CHECK-NEXT: Running analysis: ShouldRunExtraPasses
+; CHECK-NEXT: Invalidating analysis: PostDominatorTreeAnalysis
+; CHECK-NEXT: Running pass: EarlyCSEPass
+; CHECK-NEXT: Invalidating analysis: ShouldRunExtraPasses
+;
+entry:
+  %c = call <4 x float> @llvm.matrix.multiply.v4f32.v4f32.v4f32(<4 x float> %a, <4 x float> %b, i32 2, i32 2, i32 2)
+  ret <4 x float> %c
+}
+
+declare <4 x float> @llvm.matrix.multiply.v4f32.v4f32.v4f32(<4 x float>, <4 x float>, i32, i32, i32)
diff --git a/llvm/test/Other/new-pm-O0-defaults.ll b/llvm/test/Other/new-pm-O0-defaults.ll
index e8131ac7fab45a..59b91dfdc308b4 100644
--- a/llvm/test/Other/new-pm-O0-defaults.ll
+++ b/llvm/test/Other/new-pm-O0-defaults.ll
@@ -10,9 +10,6 @@
 ; RUN: opt -disable-verify -verify-analysis-invalidation=0 -debug-pass-manager \
 ; RUN:     -passes='default<O0>' -S %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefixes=CHECK,CHECK-DEFAULT,CHECK-CORO
-; RUN: opt -disable-verify -verify-analysis-invalidation=0 -debug-pass-manager -enable-matrix \
-; RUN:     -passes='default<O0>' -S %s 2>&1 \
-; RUN:     | FileCheck %s --check-prefixes=CHECK,CHECK-DEFAULT,CHECK-MATRIX,CHECK-CORO
 ; RUN: opt -disable-verify -verify-analysis-invalidation=0 -debug-pass-manager -debug-info-for-profiling \
 ; RUN:     -passes='default<O0>' -S %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefixes=CHECK,CHECK-DIS,CHECK-CORO
@@ -34,12 +31,14 @@
 ; CHECK-DIS-NEXT: Running pass: AddDiscriminatorsPass
 ; CHECK-DIS-NEXT: Running pass: AlwaysInlinerPass
 ; CHECK-DIS-NEXT: Running analysis: ProfileSummaryAnalysis
+; CHECK-DIS: Running pass: LowerMatrixIntrinsicsPass
+; CHECK-DIS-NEXT: Running analysis: TargetIRAnalysis
 ; CHECK-DEFAULT: Running analysis: InnerAnalysisManagerProxy
 ; CHECK-DEFAULT-NEXT: Running pass: EntryExitInstrumenterPass
 ; CHECK-DEFAULT-NEXT: Running pass: AlwaysInlinerPass
 ; CHECK-DEFAULT-NEXT: Running analysis: ProfileSummaryAnalysis
-; CHECK-MATRIX: Running pass: LowerMatrixIntrinsicsPass
-; CHECK-MATRIX-NEXT: Running analysis: TargetIRAnalysis
+; CHECK-DEFAULT: Running pass: LowerMatrixIntrinsicsPass
+; CHECK-DEFAULT-NEXT: Running analysis: TargetIRAnalysis
 ; CHECK-CORO-NEXT: Running pass: CoroConditionalWrapper
 ; CHECK-PRE-LINK: Running pass: CanonicalizeAliasesPass
 ; CHECK-PRE-LINK-NEXT: Running pass: NameAnonGlobalPass
diff --git a/llvm/test/Other/new-pm-defaults.ll b/llvm/test/Other/new-pm-defaults.ll
index 7cf035b0c6f376..8e6390dbbcab7a 100644
--- a/llvm/test/Other/new-pm-defaults.ll
+++ b/llvm/test/Other/new-pm-defaults.ll
@@ -75,10 +75,6 @@
 ; RUN:     -passes='default<O3>' -enable-jump-table-to-switch -S  %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,CHECK-JUMP-TABLE-TO-SWITCH,CHECK-O23SZ,%llvmcheckext
 
-; RUN: opt -disable-verify -verify-analysis-invalidation=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
-; RUN:     -passes='default<O3>' -enable-matrix -S  %s 2>&1 \
-; RUN:     | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,CHECK-O23SZ,%llvmcheckext,CHECK-MATRIX
-
 ; RUN: opt -disable-verify -verify-analysis-invalidation=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
 ; RUN:     -passes='default<O3>' -enable-merge-functions -S  %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefixes=CHECK-O,CHECK-DEFAULT,CHECK-O3,CHECK-O23SZ,%llvmcheckext,CHECK-MERGE-FUNCS
@@ -242,8 +238,7 @@
 ; CHECK-EP-OPTIMIZER-EARLY: Running pass: NoOpModulePass
 ; CHECK-O-NEXT: Running pass: Float2IntPass
 ; CHECK-O-NEXT: Running pass: LowerConstantIntrinsicsPass on foo
-; CHECK-MATRIX: Running pass: LowerMatrixIntrinsicsPass on f
-; CHECK-MATRIX-NEXT: Running pass: EarlyCSEPass on f
+; CHECK-O-NEXT: Running pass: LowerMatrixIntrinsicsPass
 ; CHECK-O3-NEXT: Running pass: ControlHeightReductionPass
 ; CHECK-EP-VECTORIZER-START-NEXT: Running pass: NoOpFunctionPass
 ; CHECK-EXT: Running pass: {{.*}}::Bye on foo
diff --git a/llvm/test/Other/new-pm-thinlto-postlink-defaults.ll b/llvm/test/Other/new-pm-thinlto-postlink-defaults.ll
index ed13402e1c4b15..01e5e73c9b1fe5 100644
--- a/llvm/test/Other/new-pm-thinlto-postlink-defaults.ll
+++ b/llvm/test/Other/new-pm-thinlto-postlink-defaults.ll
@@ -168,6 +168,7 @@
 ; CHECK-POST-EP-OPT-EARLY-NEXT: Running pass: NoOpModulePass
 ; CHECK-POSTLINK-O-NEXT: Running pass: Float2IntPass
 ; CHECK-POSTLINK-O-NEXT: Running pass: LowerConstantIntrinsicsPass
+; CHECK-POSTLINK-O-NEXT: Running pass: LowerMatrixIntrinsicsPass
 ; CHECK-POSTLINK-O3-NEXT: Running pass: ControlHeightReductionPass
 ; CHECK-EXT: Running pass: {{.*}}::Bye
 ; CHECK-POSTLINK-O-NEXT: Running pass: LoopSimplifyPass
diff --git a/llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll b/llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll
index c82c34f7ff01e7..b2ad9901bffaa7 100644
--- a/llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll
+++ b/llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll
@@ -151,6 +151,7 @@
 ; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass
 ; CHECK-O-NEXT: Running pass: Float2IntPass
 ; CHECK-O-NEXT: Running pass: LowerConstantIntrinsicsPass
+; CHECK-O-NEXT: Running pass: LowerMatrixIntrinsicsPass
 ; CHECK-O3-NEXT: Running pass: ControlHeightReductionPass
 ; CHECK-O3-NEXT: Running analysis: RegionInfoAnalysis
 ; CHECK-O3-NEXT: Running analysis: DominanceFrontierAnalysis
diff --git a/llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll b/llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll
index d375747547d61f..44423e342c9f3b 100644
--- a/llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll
+++ b/llvm/test/Other/new-pm-thinlto-postlink-samplepgo-defaults.ll
@@ -160,6 +160,7 @@
 ; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass
 ; CHECK-O-NEXT: Running pass: Float2IntPass
 ; CHECK-O-NEXT: Running pass: LowerConstantIntrinsicsPass
+; CHECK-O-NEXT: Running pass: LowerMatrixIntrinsicsPass
 ; CHECK-O3-NEXT: Running pass: ControlHeightReductionPass
 ; CHECK-O3-NEXT: Running analysis: RegionInfoAnalysis
 ; CHECK-O3-NEXT: Running analysis: DominanceFrontierAnalysis

>From 2e8ea462d84aacff131eb194ff99992913b3bcce Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 5 Dec 2024 15:48:32 +0000
Subject: [PATCH 2/3] !fixup update after rebase.

---
 llvm/lib/Passes/PassBuilderPipelines.cpp     | 2 +-
 llvm/test/Other/cse-after-matrix-lowering.ll | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 5eb516843d55c3..0a30a91be73abf 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -1486,7 +1486,7 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
   OptimizePM.addPass(LowerConstantIntrinsicsPass());
 
   OptimizePM.addPass(LowerMatrixIntrinsicsPass());
-  ExtraPassManager<ShouldRunExtraMatrixPasses> ExtraPasses;
+  ExtraFunctionPassManager<ShouldRunExtraMatrixPasses> ExtraPasses;
   ExtraPasses.addPass(EarlyCSEPass());
   OptimizePM.addPass(std::move(ExtraPasses));
 
diff --git a/llvm/test/Other/cse-after-matrix-lowering.ll b/llvm/test/Other/cse-after-matrix-lowering.ll
index 66ea3837d7c119..1963aa8d0b4354 100644
--- a/llvm/test/Other/cse-after-matrix-lowering.ll
+++ b/llvm/test/Other/cse-after-matrix-lowering.ll
@@ -4,10 +4,10 @@
 
 define <4 x float> @multiply_2x2(<4 x float> %a, <4 x float> %b) {
 ; CHECK:      Running pass: LowerMatrixIntrinsicsPass
-; CHECK-NEXT: Running analysis: ShouldRunExtraPasses
+; CHECK-NEXT: Running analysis: ShouldRunExtraMatrixPasses
 ; CHECK-NEXT: Invalidating analysis: PostDominatorTreeAnalysis
 ; CHECK-NEXT: Running pass: EarlyCSEPass
-; CHECK-NEXT: Invalidating analysis: ShouldRunExtraPasses
+; CHECK-NEXT: Invalidating analysis: ShouldRunExtraMatrixPasses
 ;
 entry:
   %c = call <4 x float> @llvm.matrix.multiply.v4f32.v4f32.v4f32(<4 x float> %a, <4 x float> %b, i32 2, i32 2, i32 2)

>From 0786c1cddf4bd2f83066183542166ff794cc26dc Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 11 Dec 2024 21:12:57 +0000
Subject: [PATCH 3/3] !fixup fix comment, thanks!

---
 llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h b/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
index e95fbc1ffcdef2..d095298f00c0f9 100644
--- a/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
+++ b/llvm/include/llvm/Transforms/Scalar/LowerMatrixIntrinsics.h
@@ -29,7 +29,7 @@ class LowerMatrixIntrinsicsPass
   static bool isRequired() { return true; }
 };
 
-/// A marker analyss to determine if extra passes should be run after lowering
+/// A marker analysis to signal if extra passes should be run after lowering
 /// matrix intrinsics.
 struct ShouldRunExtraMatrixPasses
     : public ShouldRunExtraPasses<ShouldRunExtraMatrixPasses>,



More information about the llvm-commits mailing list