Skip to content

Commit

Permalink
[mlir][linalg][bufferize] LinalgOp: Move existing region to new op
Browse files Browse the repository at this point in the history
This has two advantages.

1. It is more efficient. No need to clone the entire region.
2. Recreating ops (via cloning) invalidates analysis results. Previously, an OpResult could have bufferized out-of-place, even though the analysis requested an in-place bufferization. That is because BufferizationState keeps track of OpResults for storing bufferization analysis results (and cloned ops have new OpResults).

Differential Revision: https://reviews.llvm.org/D116453
  • Loading branch information
matthias-springer committed Jan 6, 2022
1 parent a5af260 commit 42fd68b
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ static LogicalResult bufferizeLinalgOp(RewriterBase &rewriter, LinalgOp op,

// Set insertion point now that potential alloc/dealloc are introduced.
rewriter.setInsertionPoint(op);
// Clone the op, but use the new operands. Since the new op does not have any
// tensor results, it does not return anything.
op.clone(rewriter, op.getLoc(), /*resultTypes=*/TypeRange{}, newOperands);
// Clone the op, but use the new operands. Move the existing block into the
// new op. Since the new op does not have any tensor results, it does not
// return anything.
assert(op->getNumRegions() == 1 && "expected that op has 1 region");
auto newOp = cast<LinalgOp>(op.cloneWithoutRegions(
rewriter, op.getLoc(), /*resultTypes=*/TypeRange{}, newOperands));
rewriter.inlineRegionBefore(op->getRegion(0), newOp->getRegion(0),
newOp->getRegion(0).begin());

// Replace the results of the old op with the new output buffers.
replaceOpWithBufferizedValues(rewriter, op, newOutputBuffers);
Expand Down

0 comments on commit 42fd68b

Please sign in to comment.