[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:
Alex Zinenko 2020-05-20 16:00:37 +02:00
parent df48026b4c
commit 57cbeaa8b5
7 changed files with 14 additions and 9 deletions

View File

@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
SmallVector<Value, 4> loopIvs; SmallVector<Value, 4> loopIvs;
for (auto dim : tensorType.getShape()) { for (auto dim : tensorType.getShape()) {
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1); 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()); loopIvs.push_back(loop.getInductionVar());
// Terminate the loop body and update the rewriter insertion point to the // Terminate the loop body and update the rewriter insertion point to the

View File

@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
SmallVector<Value, 4> loopIvs; SmallVector<Value, 4> loopIvs;
for (auto dim : tensorType.getShape()) { for (auto dim : tensorType.getShape()) {
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1); 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()); loopIvs.push_back(loop.getInductionVar());
// Terminate the loop body and update the rewriter insertion point to the // Terminate the loop body and update the rewriter insertion point to the

View File

@ -69,7 +69,8 @@ public:
auto step = rewriter.create<ConstantIndexOp>(loc, 1); auto step = rewriter.create<ConstantIndexOp>(loc, 1);
auto loop = auto loop =
rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step); rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
loop.getBody()->clear(); for (Operation &nested : *loop.getBody())
rewriter.eraseOp(&nested);
loopIvs.push_back(loop.getInductionVar()); loopIvs.push_back(loop.getInductionVar());
// Terminate the loop body. // Terminate the loop body.

View File

@ -72,7 +72,8 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
SmallVector<Value, 4> loopIvs; SmallVector<Value, 4> loopIvs;
for (auto dim : tensorType.getShape()) { for (auto dim : tensorType.getShape()) {
auto loop = rewriter.create<AffineForOp>(loc, /*lb=*/0, dim, /*step=*/1); 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()); loopIvs.push_back(loop.getInductionVar());
// Terminate the loop body and update the rewriter insertion point to the // Terminate the loop body and update the rewriter insertion point to the

View File

@ -69,7 +69,8 @@ public:
auto step = rewriter.create<ConstantIndexOp>(loc, 1); auto step = rewriter.create<ConstantIndexOp>(loc, 1);
auto loop = auto loop =
rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step); rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
loop.getBody()->clear(); for (Operation &nested : *loop.getBody())
rewriter.eraseOp(&nested);
loopIvs.push_back(loop.getInductionVar()); loopIvs.push_back(loop.getInductionVar());
// Terminate the loop body. // Terminate the loop body.

View File

@ -350,7 +350,7 @@ public:
Value upperBound = lowerAffineUpperBound(op, rewriter); Value upperBound = lowerAffineUpperBound(op, rewriter);
Value step = rewriter.create<ConstantIndexOp>(loc, op.getStep()); Value step = rewriter.create<ConstantIndexOp>(loc, op.getStep());
auto f = rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step); 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.inlineRegionBefore(op.region(), f.region(), f.region().end());
rewriter.eraseOp(op); rewriter.eraseOp(op);
return success(); return success();
@ -396,10 +396,10 @@ public:
bool hasElseRegion = !op.elseRegion().empty(); bool hasElseRegion = !op.elseRegion().empty();
auto ifOp = rewriter.create<scf::IfOp>(loc, cond, hasElseRegion); auto ifOp = rewriter.create<scf::IfOp>(loc, cond, hasElseRegion);
rewriter.inlineRegionBefore(op.thenRegion(), &ifOp.thenRegion().back()); rewriter.inlineRegionBefore(op.thenRegion(), &ifOp.thenRegion().back());
ifOp.thenRegion().back().erase(); rewriter.eraseBlock(&ifOp.thenRegion().back());
if (hasElseRegion) { if (hasElseRegion) {
rewriter.inlineRegionBefore(op.elseRegion(), &ifOp.elseRegion().back()); rewriter.inlineRegionBefore(op.elseRegion(), &ifOp.elseRegion().back());
ifOp.elseRegion().back().erase(); rewriter.eraseBlock(&ifOp.elseRegion().back());
} }
// Ok, we're done! // Ok, we're done!

View File

@ -419,7 +419,7 @@ LogicalResult GPUModuleConversion::matchAndRewrite(
// The spv.module build method adds a block with a terminator. Remove that // 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 // block. The terminator of the module op in the remaining block will be
// legalized later. // legalized later.
spvModuleRegion.back().erase(); rewriter.eraseBlock(&spvModuleRegion.back());
rewriter.eraseOp(moduleOp); rewriter.eraseOp(moduleOp);
return success(); return success();
} }