[Mlir-commits] [mlir] [mlir][reducer] Make mlir-reduce don't delete terminator and use ub.poison to repalce op's results (PR #185445)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Mar 9 08:41:14 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: lonely eagle (linuxlonelyeagle)

<details>
<summary>Changes</summary>

To ensure the IR remains valid after deleting ops, some ops can be deleted, but doing so might make the IR invalid, which prevents further reduction, make mlir-reduce don't delete terminator and use ub.poison to repalce op's results.

---
Full diff: https://github.com/llvm/llvm-project/pull/185445.diff


2 Files Affected:

- (modified) mlir/lib/Reducer/ReductionTreePass.cpp (+9) 
- (modified) mlir/test/mlir-reduce/simple-test.mlir (+13-1) 


``````````diff
diff --git a/mlir/lib/Reducer/ReductionTreePass.cpp b/mlir/lib/Reducer/ReductionTreePass.cpp
index 1e00ed645f71e..bede177e9e58e 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 &region,
 
   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();
     }
diff --git a/mlir/test/mlir-reduce/simple-test.mlir b/mlir/test/mlir-reduce/simple-test.mlir
index 1cc414946a592..7efa29c227166 100644
--- a/mlir/test/mlir-reduce/simple-test.mlir
+++ b/mlir/test/mlir-reduce/simple-test.mlir
@@ -1,6 +1,10 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/test.sh'
+// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/test.sh' | FileCheck %s
 
+// CHECK-LABEL: func @simple1(
+//  CHECK-SAME:   %[[ARG0:.*]]: i1,
+//  CHECK-SAME:   %[[ARG1:.*]]: memref<2xf32>,
+//  CHECK-SAME:   %[[ARG2:.*]]: memref<2xf32>)
 func.func @simple1(%arg0: i1, %arg1: memref<2xf32>, %arg2: memref<2xf32>) {
   cf.cond_br %arg0, ^bb1, ^bb2
 ^bb1:
@@ -11,3 +15,11 @@ func.func @simple1(%arg0: i1, %arg1: memref<2xf32>, %arg2: memref<2xf32>) {
 ^bb3(%1: memref<2xf32>):
   return
 }
+// CHECK:   cf.cond_br %[[ARG0]], ^bb1, ^bb2
+// CHECK: ^bb1:
+// CHECK:   cf.br ^bb3(%[[ARG1]] : memref<2xf32>)
+// CHECK: ^bb2:
+// CHECK:   %[[POISON_0:.*]] = ub.poison : memref<2xf32>
+// CHECK:   cf.br ^bb3(%[[POISON_0]] : memref<2xf32>)
+// CHECK: ^bb3(%[[VAL_0:.*]]: memref<2xf32>):
+// CHECK:   return

``````````

</details>


https://github.com/llvm/llvm-project/pull/185445


More information about the Mlir-commits mailing list