[Mlir-commits] [mlir] [mlir][UB] Erase ops that precede `ub.unreachable` (PR #179104)
Jakub Kuderski
llvmlistbot at llvm.org
Mon Feb 2 06:08:13 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))
+ break;
+ reachedBegin = it == block->begin();
+ if (!reachedBegin)
+ --it;
+ rewriter.eraseOp(op);
+ changed = true;
+ if (reachedBegin)
+ break;
----------------
kuhar wrote:
Do we need this?
https://github.com/llvm/llvm-project/pull/179104
More information about the Mlir-commits
mailing list