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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions mlir/include/mlir/IR/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,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 Down
11 changes: 11 additions & 0 deletions mlir/lib/IR/PatternMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ 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 (getInsertionBlock() == op->getBlock() &&
getInsertionPoint() == op->getIterator())
Comment on lines +163 to +164
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (getInsertionBlock() == op->getBlock() &&
getInsertionPoint() == op->getIterator())
if (getInsertionPoint() == op->getIterator())

nit: Just comparing iterators ought to be enough

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 @@ -322,6 +328,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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this deserve an API documentation update as well?


// Erase the source block.
assert(source->empty() && "expected 'source' to be empty");
eraseBlock(source);
Expand Down