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

lonely eagle llvmlistbot at llvm.org
Thu May 7 01:59:46 PDT 2026


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

>From e1c01459f93337243ec148826bced3a2356962f9 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/6] 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 80e6aaa83f0c38e868e11d79f9654cbad1a98666 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:02:21 +0000
Subject: [PATCH 2/6] 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 1178c467bd6cd5de3d2ffaac9632c5b051b84b27 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:10:00 +0000
Subject: [PATCH 3/6] 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 a0fd1c0b5041fb82ced25ebf1654021c30aa79b9 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:37:49 +0000
Subject: [PATCH 4/6] 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

>From 340a278568c467c8ba42e2ae147c26deeb22fc68 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 6 May 2026 07:45:46 +0000
Subject: [PATCH 5/6] use clang-format.

---
 mlir/lib/Reducer/ReductionTreePass.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Reducer/ReductionTreePass.cpp b/mlir/lib/Reducer/ReductionTreePass.cpp
index c53bd76fe04c8..2244475e268fe 100644
--- a/mlir/lib/Reducer/ReductionTreePass.cpp
+++ b/mlir/lib/Reducer/ReductionTreePass.cpp
@@ -47,7 +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()); 
+      opsNotInRange.push_back(&op.value());
   }
 
   // `applyOpPatternsGreedily` with folding may erase the ops so we can't do the

>From 294ec5838f58ea31d866f6151a6fd3c5a062f1d9 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Thu, 7 May 2026 08:59:29 +0000
Subject: [PATCH 6/6] remove query-test example.

---
 .../reduction-tree/query-test.mlir            | 23 -------------------
 mlir/test/mlir-reduce/script/query-test.sh    | 19 ---------------
 2 files changed, 42 deletions(-)
 delete mode 100644 mlir/test/mlir-reduce/reduction-tree/query-test.mlir
 delete 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
deleted file mode 100644
index 403aef0b8ea95..0000000000000
--- a/mlir/test/mlir-reduce/reduction-tree/query-test.mlir
+++ /dev/null
@@ -1,23 +0,0 @@
-// 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
-  %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
deleted file mode 100755
index 276b0ec029400..0000000000000
--- a/mlir/test/mlir-reduce/script/query-test.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/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



More information about the Mlir-commits mailing list