Skip to content

Commit a7c9563

Browse files
[mlir][IR] Set insertion point when erasing an operation (#146955)
Erasing the operation to which the current insertion point is set, leaves the insertion point in an invalid state. This commit resets the insertion point to the following operation. Also adjust the insertion point when inlining a block.
1 parent d1f2a66 commit a7c9563

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ class RewriterBase : public OpBuilder {
525525
}
526526

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

530535
/// This method erases all operations in a block.
@@ -539,6 +544,9 @@ class RewriterBase : public OpBuilder {
539544
/// somewhere in the middle (or beginning) of the dest block, the source block
540545
/// must have no successors. Otherwise, the resulting IR would have
541546
/// unreachable operations.
547+
///
548+
/// If the insertion point is within the source block, it is adjusted to the
549+
/// destination block.
542550
virtual void inlineBlockBefore(Block *source, Block *dest,
543551
Block::iterator before,
544552
ValueRange argValues = {});
@@ -549,6 +557,9 @@ class RewriterBase : public OpBuilder {
549557
///
550558
/// The source block must have no successors. Otherwise, the resulting IR
551559
/// would have unreachable operations.
560+
///
561+
/// If the insertion point is within the source block, it is adjusted to the
562+
/// destination block.
552563
void inlineBlockBefore(Block *source, Operation *op,
553564
ValueRange argValues = {});
554565

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

563577
/// Split the operations starting at "before" (inclusive) out of the given

mlir/lib/IR/PatternMatch.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ void RewriterBase::eraseOp(Operation *op) {
156156
assert(op->use_empty() && "expected 'op' to have no uses");
157157
auto *rewriteListener = dyn_cast_if_present<Listener>(listener);
158158

159+
// If the current insertion point is before the erased operation, we adjust
160+
// the insertion point to be after the operation.
161+
if (getInsertionPoint() == op->getIterator())
162+
setInsertionPointAfter(op);
163+
159164
// Fast path: If no listener is attached, the op can be dropped in one go.
160165
if (!rewriteListener) {
161166
op->erase();
@@ -320,6 +325,11 @@ void RewriterBase::inlineBlockBefore(Block *source, Block *dest,
320325
moveOpBefore(&source->front(), dest, before);
321326
}
322327

328+
// If the current insertion point is within the source block, adjust the
329+
// insertion point to the destination block.
330+
if (getInsertionBlock() == source)
331+
setInsertionPoint(dest, getInsertionPoint());
332+
323333
// Erase the source block.
324334
assert(source->empty() && "expected 'source' to be empty");
325335
eraseBlock(source);

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,12 @@ void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
17591759
impl->logger.startLine()
17601760
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
17611761
});
1762+
1763+
// If the current insertion point is before the erased operation, we adjust
1764+
// the insertion point to be after the operation.
1765+
if (getInsertionPoint() == op->getIterator())
1766+
setInsertionPointAfter(op);
1767+
17621768
SmallVector<SmallVector<Value>> newVals =
17631769
llvm::map_to_vector(newValues, [](Value v) -> SmallVector<Value> {
17641770
return v ? SmallVector<Value>{v} : SmallVector<Value>();
@@ -1774,6 +1780,12 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
17741780
impl->logger.startLine()
17751781
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
17761782
});
1783+
1784+
// If the current insertion point is before the erased operation, we adjust
1785+
// the insertion point to be after the operation.
1786+
if (getInsertionPoint() == op->getIterator())
1787+
setInsertionPointAfter(op);
1788+
17771789
impl->replaceOp(op, std::move(newValues));
17781790
}
17791791

@@ -1782,6 +1794,12 @@ void ConversionPatternRewriter::eraseOp(Operation *op) {
17821794
impl->logger.startLine()
17831795
<< "** Erase : '" << op->getName() << "'(" << op << ")\n";
17841796
});
1797+
1798+
// If the current insertion point is before the erased operation, we adjust
1799+
// the insertion point to be after the operation.
1800+
if (getInsertionPoint() == op->getIterator())
1801+
setInsertionPointAfter(op);
1802+
17851803
SmallVector<SmallVector<Value>> nullRepls(op->getNumResults(), {});
17861804
impl->replaceOp(op, std::move(nullRepls));
17871805
}
@@ -1888,6 +1906,11 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
18881906
moveOpBefore(&source->front(), dest, before);
18891907
}
18901908

1909+
// If the current insertion point is within the source block, adjust the
1910+
// insertion point to the destination block.
1911+
if (getInsertionBlock() == source)
1912+
setInsertionPoint(dest, getInsertionPoint());
1913+
18911914
// Erase the source block.
18921915
eraseBlock(source);
18931916
}

0 commit comments

Comments
 (0)