forked from OSchip/llvm-project
[mlir] Erase or clear blocks through ConversionPatternRewriter when applicable
Multiple places in the code base were erasing Blocks or operations in them using in-place modifications (`Block::erase` or `Block::clear`) unknown to ConversionPatternRewriter. These operations could not be undone if the pattern failed and could lead to inconsistent in-memory state of the IR with dangling pointers. Use `ConversionPatternRewriter::eraseOp` and `::eraseBlock` instead. Differential Revision: https://reviews.llvm.org/D80136
This commit is contained in:
parent
df48026b4c
commit
57cbeaa8b5
|
@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
|
|||
SmallVector<Value, 4> loopIvs;
|
||||
for (auto dim : tensorType.getShape()) {
|
||||
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1);
|
||||
loop.getBody()->clear();
|
||||
for (Operation &nested : *loop.getBody())
|
||||
rewriter.eraseOp(&nested);
|
||||
loopIvs.push_back(loop.getInductionVar());
|
||||
|
||||
// Terminate the loop body and update the rewriter insertion point to the
|
||||
|
|
|
@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
|
|||
SmallVector<Value, 4> loopIvs;
|
||||
for (auto dim : tensorType.getShape()) {
|
||||
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1);
|
||||
loop.getBody()->clear();
|
||||
for (Operation &nested : *loop.getBody())
|
||||
rewriter.eraseOp(&nested);
|
||||
loopIvs.push_back(loop.getInductionVar());
|
||||
|
||||
// Terminate the loop body and update the rewriter insertion point to the
|
||||
|
|
|
@ -69,7 +69,8 @@ public:
|
|||
auto step = rewriter.create<ConstantIndexOp>(loc, 1);
|
||||
auto loop =
|
||||
rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
|
||||
loop.getBody()->clear();
|
||||
for (Operation &nested : *loop.getBody())
|
||||
rewriter.eraseOp(&nested);
|
||||
loopIvs.push_back(loop.getInductionVar());
|
||||
|
||||
// Terminate the loop body.
|
||||
|
|
|
@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
|
|||
SmallVector<Value, 4> loopIvs;
|
||||
for (auto dim : tensorType.getShape()) {
|
||||
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1);
|
||||
loop.getBody()->clear();
|
||||
for (Operation &nested : *loop.getBody())
|
||||
rewriter.eraseOp(&nested);
|
||||
loopIvs.push_back(loop.getInductionVar());
|
||||
|
||||
// Terminate the loop body and update the rewriter insertion point to the
|
||||
|
|
|
@ -69,7 +69,8 @@ public:
|
|||
auto step = rewriter.create<ConstantIndexOp>(loc, 1);
|
||||
auto loop =
|
||||
rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
|
||||
loop.getBody()->clear();
|
||||
for (Operation &nested : *loop.getBody())
|
||||
rewriter.eraseOp(&nested);
|
||||
loopIvs.push_back(loop.getInductionVar());
|
||||
|
||||
// Terminate the loop body.
|
||||
|
|
|
@ -350,7 +350,7 @@ public:
|
|||
Value upperBound = lowerAffineUpperBound(op, rewriter);
|
||||
Value step = rewriter.create<ConstantIndexOp>(loc, op.getStep());
|
||||
auto f = rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
|
||||
f.region().getBlocks().clear();
|
||||
rewriter.eraseBlock(f.getBody());
|
||||
rewriter.inlineRegionBefore(op.region(), f.region(), f.region().end());
|
||||
rewriter.eraseOp(op);
|
||||
return success();
|
||||
|
@ -396,10 +396,10 @@ public:
|
|||
bool hasElseRegion = !op.elseRegion().empty();
|
||||
auto ifOp = rewriter.create<scf::IfOp>(loc, cond, hasElseRegion);
|
||||
rewriter.inlineRegionBefore(op.thenRegion(), &ifOp.thenRegion().back());
|
||||
ifOp.thenRegion().back().erase();
|
||||
rewriter.eraseBlock(&ifOp.thenRegion().back());
|
||||
if (hasElseRegion) {
|
||||
rewriter.inlineRegionBefore(op.elseRegion(), &ifOp.elseRegion().back());
|
||||
ifOp.elseRegion().back().erase();
|
||||
rewriter.eraseBlock(&ifOp.elseRegion().back());
|
||||
}
|
||||
|
||||
// Ok, we're done!
|
||||
|
|
|
@ -419,7 +419,7 @@ LogicalResult GPUModuleConversion::matchAndRewrite(
|
|||
// The spv.module build method adds a block with a terminator. Remove that
|
||||
// block. The terminator of the module op in the remaining block will be
|
||||
// legalized later.
|
||||
spvModuleRegion.back().erase();
|
||||
rewriter.eraseBlock(&spvModuleRegion.back());
|
||||
rewriter.eraseOp(moduleOp);
|
||||
return success();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue