[Mlir-commits] [mlir] [mlir][reducer] Change mlir-reducer apply pattern logic (PR #195997)

lonely eagle llvmlistbot at llvm.org
Wed May 6 00:38:06 PDT 2026


https://github.com/linuxlonelyeagle updated https://github.com/llvm/llvm-project/pull/195997

>From 18222aa2f838fa0a4749e5db6d540de60ff1aa57 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 04:20:41 +0000
Subject: [PATCH 1/5] fix reduce apapply pattern log.

---
 mlir/lib/Reducer/ReductionTreePass.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/mlir/lib/Reducer/ReductionTreePass.cpp b/mlir/lib/Reducer/ReductionTreePass.cpp
index 13cad4e7e9d47..92060123df5fc 100644
--- a/mlir/lib/Reducer/ReductionTreePass.cpp
+++ b/mlir/lib/Reducer/ReductionTreePass.cpp
@@ -57,14 +57,15 @@ static void applyPatterns(Region &region,
   // pattern matching in above iteration. Besides, erase op not-in-range may end
   // up in invalid module, so `applyOpPatternsGreedily` with folding should come
   // before that transform.
-  for (Operation *op : opsInRange) {
-    // `applyOpPatternsGreedily` with folding returns whether the op is
-    // converted. Omit it because we don't have expectation this reduction will
-    // be success or not.
-    (void)applyOpPatternsGreedily(op, patterns,
-                                  GreedyRewriteConfig().setStrictness(
-                                      GreedyRewriteStrictness::ExistingOps));
-  }
+  if (!eraseOpNotInRange)
+    for (Operation *op : opsNotInRange) {
+      // `applyOpPatternsGreedily` with folding returns whether the op is
+      // converted. Omit it because we don't have expectation this reduction
+      // will be success or not.
+      (void)applyOpPatternsGreedily(op, patterns,
+                                    GreedyRewriteConfig().setStrictness(
+                                        GreedyRewriteStrictness::ExistingOps));
+    }
 
   if (eraseOpNotInRange)
     for (Operation *op : opsNotInRange) {

>From 8d849fb6e4e84186ae86ca6055c1fe64b4f84804 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 06:33:06 +0000
Subject: [PATCH 2/5] Separate scripts and tests into distinct directories.

---
 mlir/test/mlir-reduce/opt-reduction/cse-test.mlir             | 2 +-
 mlir/test/mlir-reduce/opt-reduction/dce-test.mlir             | 4 ++--
 mlir/test/mlir-reduce/{ => reduction-tree}/invalid.mlir       | 2 +-
 .../test/mlir-reduce/{ => reduction-tree}/reduction-tree.mlir | 2 +-
 mlir/test/mlir-reduce/{ => reduction-tree}/simple-test.mlir   | 2 +-
 .../mlir-reduce/{ => reduction-tree}/single-function.mlir     | 0
 mlir/test/mlir-reduce/{ => script}/failure-test.sh            | 0
 mlir/test/mlir-reduce/{ => script}/false.sh                   | 0
 mlir/test/mlir-reduce/{ => script}/true.sh                    | 0
 9 files changed, 6 insertions(+), 6 deletions(-)
 rename mlir/test/mlir-reduce/{ => reduction-tree}/invalid.mlir (84%)
 rename mlir/test/mlir-reduce/{ => reduction-tree}/reduction-tree.mlir (95%)
 rename mlir/test/mlir-reduce/{ => reduction-tree}/simple-test.mlir (93%)
 rename mlir/test/mlir-reduce/{ => reduction-tree}/single-function.mlir (100%)
 rename mlir/test/mlir-reduce/{ => script}/failure-test.sh (100%)
 rename mlir/test/mlir-reduce/{ => script}/false.sh (100%)
 rename mlir/test/mlir-reduce/{ => script}/true.sh (100%)

diff --git a/mlir/test/mlir-reduce/opt-reduction/cse-test.mlir b/mlir/test/mlir-reduce/opt-reduction/cse-test.mlir
index 33a3bd10637e0..e212f7e1022a5 100644
--- a/mlir/test/mlir-reduce/opt-reduction/cse-test.mlir
+++ b/mlir/test/mlir-reduce/opt-reduction/cse-test.mlir
@@ -1,5 +1,5 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s --no-implicit-module -opt-reduction-pass='opt-pass=cse test=%S/../failure-test.sh' | FileCheck %s
+// RUN: mlir-reduce %s --no-implicit-module -opt-reduction-pass='opt-pass=cse test=%S/../script/failure-test.sh' | FileCheck %s
 
 // CHECK-LABEL: func @cse_on_func
 //  CHECK-SAME:   %[[ARG0:.*]]: i32,
diff --git a/mlir/test/mlir-reduce/opt-reduction/dce-test.mlir b/mlir/test/mlir-reduce/opt-reduction/dce-test.mlir
index c9f81fe8b1f20..93ce10ba651ad 100644
--- a/mlir/test/mlir-reduce/opt-reduction/dce-test.mlir
+++ b/mlir/test/mlir-reduce/opt-reduction/dce-test.mlir
@@ -1,6 +1,6 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -opt-reduction-pass='opt-pass=symbol-dce test=%S/../failure-test.sh' | FileCheck %s
-// RUN: mlir-reduce %s -opt-reduction-pass='opt-pass-file=%S/dce-pipeline test=%S/../failure-test.sh' | FileCheck %s  --check-prefix=CHECK-OPT-FILE
+// RUN: mlir-reduce %s -opt-reduction-pass='opt-pass=symbol-dce test=%S/../script/failure-test.sh' | FileCheck %s
+// RUN: mlir-reduce %s -opt-reduction-pass='opt-pass-file=%S/dce-pipeline test=%S/../script/failure-test.sh' | FileCheck %s  --check-prefix=CHECK-OPT-FILE
 // This input should be reduced by the pass pipeline so that only
 // the @simple1 function remains as the other functions should be
 // removed by the dead code elimination pass.
diff --git a/mlir/test/mlir-reduce/invalid.mlir b/mlir/test/mlir-reduce/reduction-tree/invalid.mlir
similarity index 84%
rename from mlir/test/mlir-reduce/invalid.mlir
rename to mlir/test/mlir-reduce/reduction-tree/invalid.mlir
index 43bcb68a32112..7e973520a657a 100644
--- a/mlir/test/mlir-reduce/invalid.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree/invalid.mlir
@@ -1,6 +1,6 @@
 // UNSUPPORTED: system-windows
 // RUN: not mlir-reduce -reduction-tree --no-implicit-module %s 2>&1 | FileCheck %s --check-prefix=CHECK-TREE
-// RUN: not mlir-reduce -reduction-tree='traversal-mode=0 test=%S/false.sh' %s 2>&1 | FileCheck %s --check-prefix=CHECK-INTERESTING
+// RUN: not mlir-reduce -reduction-tree='traversal-mode=0 test=%S/../script/false.sh' %s 2>&1 | FileCheck %s --check-prefix=CHECK-INTERESTING
 
 // The reduction passes are currently restricted to 'builtin.module'.
 //        CHECK-TREE: error: top-level op must be 'builtin.module'
diff --git a/mlir/test/mlir-reduce/reduction-tree.mlir b/mlir/test/mlir-reduce/reduction-tree/reduction-tree.mlir
similarity index 95%
rename from mlir/test/mlir-reduce/reduction-tree.mlir
rename to mlir/test/mlir-reduce/reduction-tree/reduction-tree.mlir
index 2aee89741b42b..b235ca14d693a 100644
--- a/mlir/test/mlir-reduce/reduction-tree.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree/reduction-tree.mlir
@@ -1,5 +1,5 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -split-input-file -reduction-tree='traversal-mode=0 test=%S/failure-test.sh' | FileCheck %s
+// RUN: mlir-reduce %s -split-input-file -reduction-tree='traversal-mode=0 test=%S/../script/failure-test.sh' | FileCheck %s
 // "test.op_crash_long" should be replaced with a shorter form "test.op_crash_short".
 
 // CHECK-NOT: func @simple1() {
diff --git a/mlir/test/mlir-reduce/simple-test.mlir b/mlir/test/mlir-reduce/reduction-tree/simple-test.mlir
similarity index 93%
rename from mlir/test/mlir-reduce/simple-test.mlir
rename to mlir/test/mlir-reduce/reduction-tree/simple-test.mlir
index 7d189db6a206a..c9c62fec28234 100644
--- a/mlir/test/mlir-reduce/simple-test.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree/simple-test.mlir
@@ -1,5 +1,5 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/true.sh' | FileCheck %s
+// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/../script/true.sh' | FileCheck %s
 
 // Since the test.sh always returns 1 (interesting), 
 // all operations within the ModuleOp should be erased.
diff --git a/mlir/test/mlir-reduce/single-function.mlir b/mlir/test/mlir-reduce/reduction-tree/single-function.mlir
similarity index 100%
rename from mlir/test/mlir-reduce/single-function.mlir
rename to mlir/test/mlir-reduce/reduction-tree/single-function.mlir
diff --git a/mlir/test/mlir-reduce/failure-test.sh b/mlir/test/mlir-reduce/script/failure-test.sh
similarity index 100%
rename from mlir/test/mlir-reduce/failure-test.sh
rename to mlir/test/mlir-reduce/script/failure-test.sh
diff --git a/mlir/test/mlir-reduce/false.sh b/mlir/test/mlir-reduce/script/false.sh
similarity index 100%
rename from mlir/test/mlir-reduce/false.sh
rename to mlir/test/mlir-reduce/script/false.sh
diff --git a/mlir/test/mlir-reduce/true.sh b/mlir/test/mlir-reduce/script/true.sh
similarity index 100%
rename from mlir/test/mlir-reduce/true.sh
rename to mlir/test/mlir-reduce/script/true.sh

>From 3e303648f3e2d72fa57cb510ba2d6c280e9b28f3 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:02:21 +0000
Subject: [PATCH 3/5] update test.

---
 .../reduction-tree/query-test.mlir            | 22 +++++++++++++++++++
 mlir/test/mlir-reduce/script/query-test.sh    | 19 ++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 mlir/test/mlir-reduce/reduction-tree/query-test.mlir
 create mode 100755 mlir/test/mlir-reduce/script/query-test.sh

diff --git a/mlir/test/mlir-reduce/reduction-tree/query-test.mlir b/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
new file mode 100644
index 0000000000000..e9224c3a72886
--- /dev/null
+++ b/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
@@ -0,0 +1,22 @@
+// RUN: mlir-reduce %s -split-input-file -reduction-tree='traversal-mode=0 test=%S/../script/query-test.sh' | FileCheck %s
+func.func @query_test(%arg0: i1) -> f32 {
+  %0 = arith.constant 1 : i32
+  %1 = arith.constant 2 : i32
+  %2 = arith.constant 2.2 : f32
+  %3 = arith.constant 5.3 : f32
+  %4 = arith.addi %0, %1 : i32
+  %5 = arith.addf %2, %3 : f32
+  %6 = arith.muli %4, %4 : i32
+  %7 = arith.subi %6, %4 : i32
+  %8 = arith.select %arg0, %5, %2 : f32
+  %9 = arith.addf %2, %8 : f32
+  return %9 : f32
+}
+
+// CHECK-LABEL: func @query_test
+//  CHECK-SAME:   %[[ARG0:.*]]: i1) -> f32
+//       CHECK:   %[[CONSTANT_0:.*]] = arith.constant 2.200000e+00 : f32
+//       CHECK:   %[[CONSTANT_1:.*]] = arith.constant 7.500000e+00 : f32
+//       CHECK:   %[[SELECT_0:.*]] = arith.select %[[ARG0]], %[[CONSTANT_1]], %[[CONSTANT_0]] : f32
+//       CHECK:   %[[ADDF_0:.*]] = arith.addf %[[SELECT_0]], %[[CONSTANT_0]] : f32
+//       CHECK:   return %[[ADDF_0]] : f32
diff --git a/mlir/test/mlir-reduce/script/query-test.sh b/mlir/test/mlir-reduce/script/query-test.sh
new file mode 100755
index 0000000000000..276b0ec029400
--- /dev/null
+++ b/mlir/test/mlir-reduce/script/query-test.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+#
+# query-test.sh
+# `2>&1` redirects stderr (where errors and diagnostics are printed) to stdout
+# so it can be piped to grep.
+#
+# Note mlir-opt needs to be on your path, or else replace mlir-opt with the
+# absolute path to the binary. If mlir-opt cannot be found, this interestingness
+# test will report "uninteresting on the first run, and mlir-reduce will
+# report that the input is not marked as interesting.
+#
+mlir-opt $1 2>&1 | grep "arith.select"
+
+# grep's exit code is 0 if the queried string is found
+if [[ $? -eq 0 ]]; then
+  exit 1
+else
+  exit 0
+fi

>From 3e5da86caf56f2384169d0f4834640936adb4b3a Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:10:00 +0000
Subject: [PATCH 4/5] add trivially dead example.

---
 .../reduction-tree/trivially-dead.mlir            | 15 +++++++++++++++
 mlir/test/mlir-reduce/script/trivially-dead.sh    |  4 ++++
 2 files changed, 19 insertions(+)
 create mode 100644 mlir/test/mlir-reduce/reduction-tree/trivially-dead.mlir
 create mode 100755 mlir/test/mlir-reduce/script/trivially-dead.sh

diff --git a/mlir/test/mlir-reduce/reduction-tree/trivially-dead.mlir b/mlir/test/mlir-reduce/reduction-tree/trivially-dead.mlir
new file mode 100644
index 0000000000000..b75021226afd0
--- /dev/null
+++ b/mlir/test/mlir-reduce/reduction-tree/trivially-dead.mlir
@@ -0,0 +1,15 @@
+// UNSUPPORTED: system-windows
+// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/../script/trivially-dead.sh' | FileCheck %s
+// We are testing the ability of keeping trivially-dead yet interesting code
+
+func.func @trivially_dead() {
+  %0 = arith.constant 1 : i32
+  %1 = arith.constant 2 : i32
+  %2 = arith.constant 3 : i32
+  %3 = arith.constant 4 : i32
+  return
+}
+
+// CHECK-LABEL: func @trivially_dead
+//  CHECK-NEXT:   arith.constant 2 : i32
+//  CHECK-NEXT: return
diff --git a/mlir/test/mlir-reduce/script/trivially-dead.sh b/mlir/test/mlir-reduce/script/trivially-dead.sh
new file mode 100755
index 0000000000000..3a973c12f0486
--- /dev/null
+++ b/mlir/test/mlir-reduce/script/trivially-dead.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# break only on `arith.constat 2 : i32`
+! cat $1 | grep "arith.constant 2 : i32" 2>&1 1>/dev/null

>From fca214bf00e821d4f6ee0251451dffd182fc34f6 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:37:49 +0000
Subject: [PATCH 5/5] add UNSUPPORTED: system-windows.

---
 mlir/lib/Reducer/ReductionTreePass.cpp               | 5 +----
 mlir/test/mlir-reduce/reduction-tree/query-test.mlir | 1 +
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/mlir/lib/Reducer/ReductionTreePass.cpp b/mlir/lib/Reducer/ReductionTreePass.cpp
index 92060123df5fc..c53bd76fe04c8 100644
--- a/mlir/lib/Reducer/ReductionTreePass.cpp
+++ b/mlir/lib/Reducer/ReductionTreePass.cpp
@@ -40,7 +40,6 @@ static void applyPatterns(Region &region,
                           ArrayRef<ReductionNode::Range> rangeToKeep,
                           bool eraseOpNotInRange) {
   std::vector<Operation *> opsNotInRange;
-  std::vector<Operation *> opsInRange;
   size_t keepIndex = 0;
   for (const auto &op : enumerate(region.getOps())) {
     int index = op.index();
@@ -48,9 +47,7 @@ static void applyPatterns(Region &region,
         index == rangeToKeep[keepIndex].second)
       ++keepIndex;
     if (keepIndex == rangeToKeep.size() || index < rangeToKeep[keepIndex].first)
-      opsNotInRange.push_back(&op.value());
-    else
-      opsInRange.push_back(&op.value());
+      opsNotInRange.push_back(&op.value()); 
   }
 
   // `applyOpPatternsGreedily` with folding may erase the ops so we can't do the
diff --git a/mlir/test/mlir-reduce/reduction-tree/query-test.mlir b/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
index e9224c3a72886..403aef0b8ea95 100644
--- a/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
@@ -1,3 +1,4 @@
+// UNSUPPORTED: system-windows
 // RUN: mlir-reduce %s -split-input-file -reduction-tree='traversal-mode=0 test=%S/../script/query-test.sh' | FileCheck %s
 func.func @query_test(%arg0: i1) -> f32 {
   %0 = arith.constant 1 : i32



More information about the Mlir-commits mailing list