-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[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
matthias-springer
wants to merge
2
commits into
main
Choose a base branch
from
users/matthias-springer/ip_after_erase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+16
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,12 +152,45 @@ void RewriterBase::replaceOp(Operation *op, Operation *newOp) { | |
eraseOp(op); | ||
} | ||
|
||
/// Returns the given block iterator if it lies within the block `b`. | ||
/// Otherwise, otherwise finds the ancestor of the given block iterator that | ||
/// lies within `b`. Returns and "empty" iterator if the latter fails. | ||
/// | ||
/// Note: This is a variant of Block::findAncestorOpInBlock that operates on | ||
/// block iterators instead of ops. | ||
static std::pair<Block *, Block::iterator> | ||
findAncestorIteratorInBlock(Block *b, Block *itBlock, Block::iterator it) { | ||
// Case 1: The iterator lies within the block. | ||
if (itBlock == b) | ||
return std::make_pair(itBlock, it); | ||
|
||
// Otherwise: Find ancestor iterator. Bail if we run out of parent ops. | ||
Operation *parentOp = itBlock->getParentOp(); | ||
if (!parentOp) | ||
return std::make_pair(static_cast<Block *>(nullptr), Block::iterator()); | ||
Operation *op = b->findAncestorOpInBlock(*parentOp); | ||
if (!op) | ||
return std::make_pair(static_cast<Block *>(nullptr), Block::iterator()); | ||
return std::make_pair(op->getBlock(), op->getIterator()); | ||
} | ||
|
||
/// This method erases an operation that is known to have no uses. The uses of | ||
/// the given operation *must* be known to be dead. | ||
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/within the erased operation, we | ||
// need to adjust the insertion point to be after the operation. | ||
if (getInsertionBlock()) { | ||
Block *insertionBlock; | ||
Block::iterator insertionPoint; | ||
std::tie(insertionBlock, insertionPoint) = findAncestorIteratorInBlock( | ||
op->getBlock(), getInsertionBlock(), getInsertionPoint()); | ||
if (insertionBlock && insertionPoint == op->getIterator()) | ||
setInsertionPointAfter(op); | ||
} | ||
|
||
// Fast path: If no listener is attached, the op can be dropped in one go. | ||
if (!rewriteListener) { | ||
op->erase(); | ||
|
@@ -322,6 +355,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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about simplifying this to just:
I agree with you that we don't want to make this too expensive. I think the above should cover the cases discussed in #146908 and is a rather very small cost.
I personally do not have expectations of the insertion point still working if the block is being deleted that one is inserting, but I think few users are thinking "My insertion point is before this op and the op is used as an anchor inside the builder to insert new ops, therefore I now need to set the insertion point again after I deleted the op". Even less so in patterns where the framework sets the insertion point for you!