[Mlir-commits] [mlir] [mlir][UB] Erase ops that precede `ub.unreachable` (PR #179104)

Mehdi Amini llvmlistbot at llvm.org
Mon Feb 2 06:05:17 PST 2026


================
@@ -59,8 +59,45 @@ Operation *UBDialect::materializeConstant(OpBuilder &builder, Attribute value,
   return nullptr;
 }
 
+//===----------------------------------------------------------------------===//
+// PoisonOp
+//===----------------------------------------------------------------------===//
+
 OpFoldResult PoisonOp::fold(FoldAdaptor /*adaptor*/) { return getValue(); }
 
+//===----------------------------------------------------------------------===//
+// UnreachableOp
+//===----------------------------------------------------------------------===//
+
+LogicalResult UnreachableOp::canonicalize(UnreachableOp unreachableOp,
+                                          PatternRewriter &rewriter) {
+  // Erase operations that precede ub.unreachable (if they must progress).
+  Block *block = unreachableOp->getBlock();
+  if (llvm::hasSingleElement(*block))
+    return rewriter.notifyMatchFailure(
+        unreachableOp, "unreachable op is the only operation in the block");
+
+  // Erase all other operations in the block. They must be dead.
+  auto it = unreachableOp->getIterator();
+  --it;
+  bool reachedBegin = false;
+  bool changed = false;
+  do {
+    Operation *op = &*it;
+    // Do not erase ops that may not progress (and ops that precede them).
+    if (!mustProgress(op))
----------------
joker-eph wrote:

There is one more category of ops that we can't remove: these are operations with volatile memory effect (which we may just not be modeling right now in the memory effects unfortunately), which at minima deserves a TODO.

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


More information about the Mlir-commits mailing list