Skip to content

Commit 4ff9375

Browse files
address comments
1 parent 68af841 commit 4ff9375

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
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: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,44 +150,16 @@ void RewriterBase::replaceOp(Operation *op, Operation *newOp) {
150150
eraseOp(op);
151151
}
152152

153-
/// Returns the given block iterator if it lies within the block `b`.
154-
/// Otherwise, otherwise finds the ancestor of the given block iterator that
155-
/// lies within `b`. Returns and "empty" iterator if the latter fails.
156-
///
157-
/// Note: This is a variant of Block::findAncestorOpInBlock that operates on
158-
/// block iterators instead of ops.
159-
static std::pair<Block *, Block::iterator>
160-
findAncestorIteratorInBlock(Block *b, Block *itBlock, Block::iterator it) {
161-
// Case 1: The iterator lies within the block.
162-
if (itBlock == b)
163-
return std::make_pair(itBlock, it);
164-
165-
// Otherwise: Find ancestor iterator. Bail if we run out of parent ops.
166-
Operation *parentOp = itBlock->getParentOp();
167-
if (!parentOp)
168-
return std::make_pair(static_cast<Block *>(nullptr), Block::iterator());
169-
Operation *op = b->findAncestorOpInBlock(*parentOp);
170-
if (!op)
171-
return std::make_pair(static_cast<Block *>(nullptr), Block::iterator());
172-
return std::make_pair(op->getBlock(), op->getIterator());
173-
}
174-
175153
/// This method erases an operation that is known to have no uses. The uses of
176154
/// the given operation *must* be known to be dead.
177155
void RewriterBase::eraseOp(Operation *op) {
178156
assert(op->use_empty() && "expected 'op' to have no uses");
179157
auto *rewriteListener = dyn_cast_if_present<Listener>(listener);
180158

181-
// If the current insertion point is before/within the erased operation, we
182-
// need to adjust the insertion point to be after the operation.
183-
if (getInsertionBlock()) {
184-
Block *insertionBlock;
185-
Block::iterator insertionPoint;
186-
std::tie(insertionBlock, insertionPoint) = findAncestorIteratorInBlock(
187-
op->getBlock(), getInsertionBlock(), getInsertionPoint());
188-
if (insertionBlock && insertionPoint == op->getIterator())
189-
setInsertionPointAfter(op);
190-
}
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);
191163

192164
// Fast path: If no listener is attached, the op can be dropped in one go.
193165
if (!rewriteListener) {

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,11 @@ void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
18871887
moveOpBefore(&source->front(), dest, before);
18881888
}
18891889

1890+
// If the current insertion point is within the source block, adjust the
1891+
// insertion point to the destination block.
1892+
if (getInsertionBlock() == source)
1893+
setInsertionPoint(dest, getInsertionPoint());
1894+
18901895
// Erase the source block.
18911896
eraseBlock(source);
18921897
}

0 commit comments

Comments
 (0)