[Mlir-commits] [mlir] [mlir][reducer] Make mlir-reduce don't delete terminator and use ub.poison to repalce op's results (PR #185445)
lonely eagle
llvmlistbot at llvm.org
Wed Mar 18 09:09:13 PDT 2026
https://github.com/linuxlonelyeagle updated https://github.com/llvm/llvm-project/pull/185445
>From 410d62da7fa28fc55d523d27203cc84c90c04577 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Mon, 9 Mar 2026 15:35:59 +0000
Subject: [PATCH 1/2] make mlir-reduce don't delete terminator and use
ub.poison to repalce op's results.
---
mlir/lib/Reducer/ReductionTreePass.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/mlir/lib/Reducer/ReductionTreePass.cpp b/mlir/lib/Reducer/ReductionTreePass.cpp
index 83497143d9669..912318540e991 100644
--- a/mlir/lib/Reducer/ReductionTreePass.cpp
+++ b/mlir/lib/Reducer/ReductionTreePass.cpp
@@ -14,6 +14,7 @@
//
//===----------------------------------------------------------------------===//
+#include "mlir/Dialect/UB/IR/UBOps.h"
#include "mlir/IR/DialectInterface.h"
#include "mlir/Reducer/Passes.h"
#include "mlir/Reducer/ReductionNode.h"
@@ -68,6 +69,14 @@ static void applyPatterns(Region ®ion,
if (eraseOpNotInRange)
for (Operation *op : opsNotInRange) {
+ if (op->hasTrait<mlir::OpTrait::IsTerminator>())
+ continue;
+ OpBuilder b(op);
+ for (auto result : op->getResults()) {
+ OpBuilder b(op);
+ auto p = ub::PoisonOp::create(b, op->getLoc(), result.getType());
+ result.replaceAllUsesWith(p.getResult());
+ }
op->dropAllUses();
op->erase();
}
>From 03dc05a7aaa7f4590f522c1e263e8b025c2909d6 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Mon, 9 Mar 2026 15:49:22 +0000
Subject: [PATCH 2/2] add test.
---
mlir/test/mlir-reduce/reduction-tree.mlir | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/mlir/test/mlir-reduce/reduction-tree.mlir b/mlir/test/mlir-reduce/reduction-tree.mlir
index 2aee89741b42b..88becc45f5626 100644
--- a/mlir/test/mlir-reduce/reduction-tree.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree.mlir
@@ -58,3 +58,28 @@ func.func @simple4(%arg0: i1, %arg1: memref<2xf32>, %arg2: memref<2xf32>) {
func.func @simple5() {
return
}
+
+// -----
+
+// This test checks the ability to remove useless ops interspersed between
+// an 'interesting' op and the return op.
+
+// CHECK: func @simple1() {
+func.func @simple1() {
+ return
+}
+
+// CHECK-LABEL: func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
+func.func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
+ call @simple1() : () -> ()
+ %0 = "test.op_crash_long" (%arg0, %arg1, %arg2) : (i32, i32, i32) -> i32
+ call @simple5() : ()-> ()
+ // CHECK: %0 = "test.op_crash_short"() : () -> i32
+ // CHECK-NEXT: return
+ return
+}
+
+// CHECK: func @simple5() {
+func.func @simple5() {
+ return
+}
More information about the Mlir-commits
mailing list