[Mlir-commits] [mlir] [mlir] Fix crash in dialect conversion for detached root ops (PR #185068)

Matthias Springer llvmlistbot at llvm.org
Mon Mar 9 02:18:52 PDT 2026


================
@@ -3459,6 +3463,16 @@ LogicalResult ConversionPatternRewriter::legalize(Region *r) {
 LogicalResult OperationConverter::applyConversion(ArrayRef<Operation *> ops) {
   // Convert each operation and discard rewrites on failure.
   ConversionPatternRewriterImpl &rewriterImpl = rewriter.getImpl();
+
+  // Track which root ops are detached (not in any block). If a detached root
+  // op is replaced or erased during conversion, we must report an error because
+  // the newly inserted replacement op would have no parent context, and the
+  // original op cannot be properly unlinked.
+  SmallPtrSet<Operation *, 4> detachedRootOps;
----------------
matthias-springer wrote:

This is an edge case that points to invalid dialect conversion API usage. I would do the following:

* Guard this check (and all extra data structures) by `#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS`.
* Store `detachedRootOps` in `ConversionPatternRewriterImpl`.
* Move check to `void ConversionPatternRewriterImpl::replaceOp(Operation *op, SmallVector<SmallVector<Value>> &&newValues)` and use `report_fatal_error` to signal API violation. (No need to `emitError` or rollback.)


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


More information about the Mlir-commits mailing list