Skip to content

[mlir][IR] Set insertion point when erasing an operation #146955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mlir/include/mlir/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ class RewriterBase : public OpBuilder {
}

/// This method erases an operation that is known to have no uses.
///
/// If the current insertion point is before the erased operation, it is
/// adjusted to the following operation (or the end of the block). If the
/// current insertion point is within the erased operation, the insertion
/// point is left in an invalid state.
virtual void eraseOp(Operation *op);

/// This method erases all operations in a block.
Expand All @@ -539,6 +544,9 @@ class RewriterBase : public OpBuilder {
/// somewhere in the middle (or beginning) of the dest block, the source block
/// must have no successors. Otherwise, the resulting IR would have
/// unreachable operations.
///
/// If the insertion point is within the source block, it is adjusted to the
/// destination block.
virtual void inlineBlockBefore(Block *source, Block *dest,
Block::iterator before,
ValueRange argValues = {});
Expand All @@ -549,6 +557,9 @@ class RewriterBase : public OpBuilder {
///
/// The source block must have no successors. Otherwise, the resulting IR
/// would have unreachable operations.
///
/// If the insertion point is within the source block, it is adjusted to the
/// destination block.
void inlineBlockBefore(Block *source, Operation *op,
ValueRange argValues = {});

Expand All @@ -558,6 +569,9 @@ class RewriterBase : public OpBuilder {
///
/// The dest block must have no successors. Otherwise, the resulting IR would
/// have unreachable operation.
///
/// If the insertion point is within the source block, it is adjusted to the
/// destination block.
void mergeBlocks(Block *source, Block *dest, ValueRange argValues = {});

/// Split the operations starting at "before" (inclusive) out of the given
Expand Down
10 changes: 10 additions & 0 deletions mlir/lib/IR/PatternMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ void RewriterBase::eraseOp(Operation *op) {
assert(op->use_empty() && "expected 'op' to have no uses");
auto *rewriteListener = dyn_cast_if_present<Listener>(listener);

// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
if (getInsertionPoint() == op->getIterator())
setInsertionPointAfter(op);

// Fast path: If no listener is attached, the op can be dropped in one go.
if (!rewriteListener) {
op->erase();
Expand Down Expand Up @@ -320,6 +325,11 @@ void RewriterBase::inlineBlockBefore(Block *source, Block *dest,
moveOpBefore(&source->front(), dest, before);
}

// If the current insertion point is within the source block, adjust the
// insertion point to the destination block.
if (getInsertionBlock() == source)
setInsertionPoint(dest, getInsertionPoint());

// Erase the source block.
assert(source->empty() && "expected 'source' to be empty");
eraseBlock(source);
Expand Down
23 changes: 23 additions & 0 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,12 @@ void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
impl->logger.startLine()
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
});

// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
if (getInsertionPoint() == op->getIterator())
setInsertionPointAfter(op);

SmallVector<SmallVector<Value>> newVals =
llvm::map_to_vector(newValues, [](Value v) -> SmallVector<Value> {
return v ? SmallVector<Value>{v} : SmallVector<Value>();
Expand All @@ -1773,6 +1779,12 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
impl->logger.startLine()
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
});

// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
if (getInsertionPoint() == op->getIterator())
setInsertionPointAfter(op);

impl->replaceOp(op, std::move(newValues));
}

Expand All @@ -1781,6 +1793,12 @@ void ConversionPatternRewriter::eraseOp(Operation *op) {
impl->logger.startLine()
<< "** Erase : '" << op->getName() << "'(" << op << ")\n";
});

// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
if (getInsertionPoint() == op->getIterator())
setInsertionPointAfter(op);

SmallVector<SmallVector<Value>> nullRepls(op->getNumResults(), {});
impl->replaceOp(op, std::move(nullRepls));
}
Expand Down Expand Up @@ -1887,6 +1905,11 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
moveOpBefore(&source->front(), dest, before);
}

// If the current insertion point is within the source block, adjust the
// insertion point to the destination block.
if (getInsertionBlock() == source)
setInsertionPoint(dest, getInsertionPoint());

// Erase the source block.
eraseBlock(source);
}
Expand Down