149e37000SMatthias Springer //===- BufferizableOpInterfaceImpl.cpp - Impl. of BufferizableOpInterface -===//
249e37000SMatthias Springer //
349e37000SMatthias Springer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
449e37000SMatthias Springer // See https://llvm.org/LICENSE.txt for license information.
549e37000SMatthias Springer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
649e37000SMatthias Springer //
749e37000SMatthias Springer //===----------------------------------------------------------------------===//
849e37000SMatthias Springer 
949e37000SMatthias Springer #include "mlir/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.h"
10eda6f907SRiver Riddle #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
1149e37000SMatthias Springer #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
12b3ebe3beSMatthias Springer #include "mlir/Dialect/Bufferization/IR/Bufferization.h"
1349e37000SMatthias Springer #include "mlir/Dialect/MemRef/IR/MemRef.h"
148b68da2cSAlex Zinenko #include "mlir/Dialect/SCF/IR/SCF.h"
1549e37000SMatthias Springer #include "mlir/Dialect/Tensor/IR/Tensor.h"
1649e37000SMatthias Springer #include "mlir/IR/Dialect.h"
1749e37000SMatthias Springer #include "mlir/IR/Operation.h"
1849e37000SMatthias Springer 
1949e37000SMatthias Springer using namespace mlir;
2049e37000SMatthias Springer using namespace mlir::bufferization;
2149e37000SMatthias Springer using namespace mlir::tensor;
2249e37000SMatthias Springer 
2349e37000SMatthias Springer namespace mlir {
2449e37000SMatthias Springer namespace tensor {
2549e37000SMatthias Springer namespace {
2649e37000SMatthias Springer 
2749e37000SMatthias Springer struct CastOpInterface
2849e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<CastOpInterface,
2949e37000SMatthias Springer                                                     tensor::CastOp> {
3049e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
319597b16aSMatthias Springer                               const AnalysisState &state) const {
3249e37000SMatthias Springer     return false;
3349e37000SMatthias Springer   }
3449e37000SMatthias Springer 
3549e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
369597b16aSMatthias Springer                                const AnalysisState &state) const {
3749e37000SMatthias Springer     return false;
3849e37000SMatthias Springer   }
3949e37000SMatthias Springer 
409597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
419597b16aSMatthias Springer                                             const AnalysisState &state) const {
42585a8a32SMatthias Springer     return {op->getResult(0)};
4349e37000SMatthias Springer   }
4449e37000SMatthias Springer 
4549e37000SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
469597b16aSMatthias Springer                                 const AnalysisState &state) const {
4749e37000SMatthias Springer     return BufferRelation::Equivalent;
4849e37000SMatthias Springer   }
4949e37000SMatthias Springer 
5049e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
51b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
5249e37000SMatthias Springer     auto castOp = cast<tensor::CastOp>(op);
5349e37000SMatthias Springer 
5449e37000SMatthias Springer     // The result buffer still has the old (pre-cast) type.
555d50f51cSMatthias Springer     FailureOr<Value> resultBuffer =
565d50f51cSMatthias Springer         getBuffer(rewriter, castOp.getSource(), options);
575d50f51cSMatthias Springer     if (failed(resultBuffer))
585d50f51cSMatthias Springer       return failure();
595d50f51cSMatthias Springer     auto sourceMemRefType = resultBuffer->getType().cast<BaseMemRefType>();
6049e37000SMatthias Springer     TensorType resultTensorType =
6149e37000SMatthias Springer         castOp.getResult().getType().cast<TensorType>();
6249e37000SMatthias Springer     MemRefLayoutAttrInterface layout;
6349e37000SMatthias Springer 
6449e37000SMatthias Springer     if (auto rankedMemRefType = sourceMemRefType.dyn_cast<MemRefType>())
6549e37000SMatthias Springer       if (resultTensorType.isa<RankedTensorType>())
6649e37000SMatthias Springer         layout = rankedMemRefType.getLayout();
6749e37000SMatthias Springer 
6849e37000SMatthias Springer     // Compute the new memref type.
69b55d55ecSMatthias Springer     Type resultMemRefType =
70606f7c8fSMatthias Springer         getMemRefType(castOp.getResult(), options, layout,
71b06614e2SMatthias Springer                       sourceMemRefType.getMemorySpaceAsInt());
7249e37000SMatthias Springer 
7349e37000SMatthias Springer     // Replace the op with a memref.cast.
745d50f51cSMatthias Springer     assert(memref::CastOp::areCastCompatible(resultBuffer->getType(),
7549e37000SMatthias Springer                                              resultMemRefType) &&
7649e37000SMatthias Springer            "CallOp::bufferize: cast incompatible");
7749e37000SMatthias Springer     replaceOpWithNewBufferizedOp<memref::CastOp>(rewriter, op, resultMemRefType,
785d50f51cSMatthias Springer                                                  *resultBuffer);
7949e37000SMatthias Springer 
8049e37000SMatthias Springer     return success();
8149e37000SMatthias Springer   }
8249e37000SMatthias Springer };
8349e37000SMatthias Springer 
84e6f69161SMatthias Springer /// Bufferization of tensor.collapse_shape. Replace with memref.collapse_shape.
85e6f69161SMatthias Springer struct CollapseShapeOpInterface
86e6f69161SMatthias Springer     : public BufferizableOpInterface::ExternalModel<CollapseShapeOpInterface,
87e6f69161SMatthias Springer                                                     tensor::CollapseShapeOp> {
88e6f69161SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
899597b16aSMatthias Springer                               const AnalysisState &state) const {
90e6f69161SMatthias Springer     return false;
91e6f69161SMatthias Springer   }
92e6f69161SMatthias Springer 
93e6f69161SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
949597b16aSMatthias Springer                                const AnalysisState &state) const {
95e6f69161SMatthias Springer     return false;
96e6f69161SMatthias Springer   }
97e6f69161SMatthias Springer 
989597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
999597b16aSMatthias Springer                                             const AnalysisState &state) const {
100e6f69161SMatthias Springer     if (&opOperand == &op->getOpOperand(0) /*src*/)
101e6f69161SMatthias Springer       return {op->getOpResult(0)};
102e6f69161SMatthias Springer     return {};
103e6f69161SMatthias Springer   }
104e6f69161SMatthias Springer 
105e6f69161SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
1069597b16aSMatthias Springer                                 const AnalysisState &state) const {
107e6f69161SMatthias Springer     return BufferRelation::Equivalent;
108e6f69161SMatthias Springer   }
109e6f69161SMatthias Springer 
110e6f69161SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
111b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
112e6f69161SMatthias Springer     auto collapseShapeOp = cast<tensor::CollapseShapeOp>(op);
11351df6238SMatthias Springer     RankedTensorType tensorResultType = collapseShapeOp.getResultType();
1145d50f51cSMatthias Springer     FailureOr<Value> maybeBuffer =
1155d50f51cSMatthias Springer         getBuffer(rewriter, collapseShapeOp.getSrc(), options);
1165d50f51cSMatthias Springer     if (failed(maybeBuffer))
1175d50f51cSMatthias Springer       return failure();
1185d50f51cSMatthias Springer     Value buffer = *maybeBuffer;
119b3ebe3beSMatthias Springer     auto bufferType = buffer.getType().cast<MemRefType>();
12051df6238SMatthias Springer 
12151df6238SMatthias Springer     if (tensorResultType.getRank() == 0) {
12251df6238SMatthias Springer       // 0-d collapses must go through a different op builder.
12373c0333dSMatthias Springer       MemRefType resultType;
12473c0333dSMatthias Springer 
12573c0333dSMatthias Springer       if (bufferType.getLayout().isIdentity()) {
12673c0333dSMatthias Springer         // Standard layout: result type has no offset.
12751df6238SMatthias Springer         MemRefLayoutAttrInterface layout;
12873c0333dSMatthias Springer         resultType = MemRefType::get({}, tensorResultType.getElementType(),
12951df6238SMatthias Springer                                      layout, bufferType.getMemorySpace());
13073c0333dSMatthias Springer       } else {
13173c0333dSMatthias Springer         // Source memref has a layout map: result type has the same offset as
13273c0333dSMatthias Springer         // the source type.
13373c0333dSMatthias Springer         SmallVector<int64_t> strides;
13473c0333dSMatthias Springer         int64_t offset;
13573c0333dSMatthias Springer         if (failed(getStridesAndOffset(bufferType, strides, offset)))
13673c0333dSMatthias Springer           return failure();
13773c0333dSMatthias Springer         AffineMap resultLayout =
13873c0333dSMatthias Springer             makeStridedLinearLayoutMap({}, offset, op->getContext());
13973c0333dSMatthias Springer         resultType =
14073c0333dSMatthias Springer             MemRefType::get({}, tensorResultType.getElementType(), resultLayout,
14173c0333dSMatthias Springer                             bufferType.getMemorySpaceAsInt());
14273c0333dSMatthias Springer       }
14373c0333dSMatthias Springer 
144e6f69161SMatthias Springer       replaceOpWithNewBufferizedOp<memref::CollapseShapeOp>(
1458df54a6aSJacques Pienaar           rewriter, op, resultType, buffer, collapseShapeOp.getReassociation());
146e6f69161SMatthias Springer       return success();
147e6f69161SMatthias Springer     }
14851df6238SMatthias Springer 
149d7a9bf91SMatthias Springer     // If the dims are not collapsible (due to an incompatible source layout
150d7a9bf91SMatthias Springer     // map), force an out-of-place bufferization, i.e., a buffer copy. This
151d7a9bf91SMatthias Springer     // newly allocated buffer will have no layout map and thus be collapsible.
152a74e5a89SAdrian Kuegel     bool canBeCollapsed = memref::CollapseShapeOp::isGuaranteedCollapsible(
153d7a9bf91SMatthias Springer         bufferType, collapseShapeOp.getReassociationIndices());
154b3ebe3beSMatthias Springer     if (!canBeCollapsed) {
155b3ebe3beSMatthias Springer       // TODO: Create alloc_tensor ops during TensorCopyInsertion.
156b55d55ecSMatthias Springer       AnalysisState analysisState(options);
15745b995cdSMatthias Springer       FailureOr<Value> tensorAlloc = allocateTensorForShapedValue(
1588df54a6aSJacques Pienaar           rewriter, op->getLoc(), collapseShapeOp.getSrc(),
15945b995cdSMatthias Springer           analysisState.isTensorYielded(collapseShapeOp.getResult()), options);
16045b995cdSMatthias Springer       if (failed(tensorAlloc))
16145b995cdSMatthias Springer         return failure();
162b3ebe3beSMatthias Springer       auto memrefType =
163b3ebe3beSMatthias Springer           MemRefType::get(collapseShapeOp.getSrcType().getShape(),
164b3ebe3beSMatthias Springer                           collapseShapeOp.getSrcType().getElementType(),
165b3ebe3beSMatthias Springer                           AffineMap(), bufferType.getMemorySpaceAsInt());
166b3ebe3beSMatthias Springer       buffer = rewriter.create<bufferization::ToMemrefOp>(
16745b995cdSMatthias Springer           op->getLoc(), memrefType, *tensorAlloc);
168b3ebe3beSMatthias Springer     }
169d7a9bf91SMatthias Springer 
17051df6238SMatthias Springer     // Result type is inferred by the builder.
17151df6238SMatthias Springer     replaceOpWithNewBufferizedOp<memref::CollapseShapeOp>(
172b3ebe3beSMatthias Springer         rewriter, op, buffer, collapseShapeOp.getReassociationIndices());
17351df6238SMatthias Springer     return success();
17451df6238SMatthias Springer   }
175e6f69161SMatthias Springer };
176e6f69161SMatthias Springer 
17749e37000SMatthias Springer /// Bufferization of tensor.dim. Replace with memref.dim.
17849e37000SMatthias Springer struct DimOpInterface
17949e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<DimOpInterface,
18049e37000SMatthias Springer                                                     tensor::DimOp> {
18149e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
1829597b16aSMatthias Springer                               const AnalysisState &state) const {
18349e37000SMatthias Springer     return true;
18449e37000SMatthias Springer   }
18549e37000SMatthias Springer 
18649e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
1879597b16aSMatthias Springer                                const AnalysisState &state) const {
18849e37000SMatthias Springer     return false;
18949e37000SMatthias Springer   }
19049e37000SMatthias Springer 
1919597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
1929597b16aSMatthias Springer                                             const AnalysisState &state) const {
193585a8a32SMatthias Springer     return {};
19449e37000SMatthias Springer   }
19549e37000SMatthias Springer 
19649e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
197b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
19849e37000SMatthias Springer     auto dimOp = cast<tensor::DimOp>(op);
1995d50f51cSMatthias Springer     FailureOr<Value> v = getBuffer(rewriter, dimOp.getSource(), options);
2005d50f51cSMatthias Springer     if (failed(v))
2015d50f51cSMatthias Springer       return failure();
2025d50f51cSMatthias Springer     replaceOpWithNewBufferizedOp<memref::DimOp>(rewriter, op, *v,
203*136d746eSJacques Pienaar                                                 dimOp.getIndex());
20449e37000SMatthias Springer     return success();
20549e37000SMatthias Springer   }
20649e37000SMatthias Springer };
20749e37000SMatthias Springer 
208e6f69161SMatthias Springer /// Bufferization of tensor.expand_shape. Replace with memref.expand_shape.
209e6f69161SMatthias Springer struct ExpandShapeOpInterface
210e6f69161SMatthias Springer     : public BufferizableOpInterface::ExternalModel<ExpandShapeOpInterface,
211e6f69161SMatthias Springer                                                     tensor::ExpandShapeOp> {
212e6f69161SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
2139597b16aSMatthias Springer                               const AnalysisState &state) const {
214e6f69161SMatthias Springer     return false;
215e6f69161SMatthias Springer   }
216e6f69161SMatthias Springer 
217e6f69161SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
2189597b16aSMatthias Springer                                const AnalysisState &state) const {
219e6f69161SMatthias Springer     return false;
220e6f69161SMatthias Springer   }
221e6f69161SMatthias Springer 
2229597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
2239597b16aSMatthias Springer                                             const AnalysisState &state) const {
224e6f69161SMatthias Springer     if (&opOperand == &op->getOpOperand(0) /*src*/)
225e6f69161SMatthias Springer       return {op->getOpResult(0)};
226e6f69161SMatthias Springer     return {};
227e6f69161SMatthias Springer   }
228e6f69161SMatthias Springer 
229e6f69161SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
2309597b16aSMatthias Springer                                 const AnalysisState &state) const {
231e6f69161SMatthias Springer     return BufferRelation::Equivalent;
232e6f69161SMatthias Springer   }
233e6f69161SMatthias Springer 
234e6f69161SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
235b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
236e6f69161SMatthias Springer     auto expandShapeOp = cast<tensor::ExpandShapeOp>(op);
23751df6238SMatthias Springer     auto tensorResultType = expandShapeOp.getResultType();
2385d50f51cSMatthias Springer     FailureOr<Value> buffer =
2395d50f51cSMatthias Springer         getBuffer(rewriter, expandShapeOp.getSrc(), options);
2405d50f51cSMatthias Springer     if (failed(buffer))
2415d50f51cSMatthias Springer       return failure();
24251df6238SMatthias Springer 
24351df6238SMatthias Springer     // Memref result type is inferred by the builder based on reassociation
24451df6238SMatthias Springer     // indices and result shape.
245e6f69161SMatthias Springer     replaceOpWithNewBufferizedOp<memref::ExpandShapeOp>(
2465d50f51cSMatthias Springer         rewriter, op, tensorResultType.getShape(), *buffer,
24751df6238SMatthias Springer         expandShapeOp.getReassociationIndices());
248e6f69161SMatthias Springer     return success();
249e6f69161SMatthias Springer   }
250e6f69161SMatthias Springer };
251e6f69161SMatthias Springer 
25249e37000SMatthias Springer /// Bufferization of tensor.extract_slice. Replace with memref.subview.
25349e37000SMatthias Springer struct ExtractSliceOpInterface
25449e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<ExtractSliceOpInterface,
25549e37000SMatthias Springer                                                     tensor::ExtractSliceOp> {
25649e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
2579597b16aSMatthias Springer                               const AnalysisState &state) const {
25849e37000SMatthias Springer     return false;
25949e37000SMatthias Springer   }
26049e37000SMatthias Springer 
26149e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
2629597b16aSMatthias Springer                                const AnalysisState &state) const {
26349e37000SMatthias Springer     return false;
26449e37000SMatthias Springer   }
26549e37000SMatthias Springer 
2669597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
2679597b16aSMatthias Springer                                             const AnalysisState &state) const {
268585a8a32SMatthias Springer     if (&opOperand == &op->getOpOperand(0) /*source*/)
269585a8a32SMatthias Springer       return {op->getOpResult(0)};
270585a8a32SMatthias Springer     return {};
27149e37000SMatthias Springer   }
27249e37000SMatthias Springer 
27349e37000SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
2749597b16aSMatthias Springer                                 const AnalysisState &state) const {
27549e37000SMatthias Springer     return BufferRelation::None;
27649e37000SMatthias Springer   }
27749e37000SMatthias Springer 
27849e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
279b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
28049e37000SMatthias Springer     auto extractSliceOp = cast<tensor::ExtractSliceOp>(op);
2816c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedOffsets = extractSliceOp.getMixedOffsets();
2826c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedSizes = extractSliceOp.getMixedSizes();
2836c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedStrides = extractSliceOp.getMixedStrides();
28449e37000SMatthias Springer     Location loc = extractSliceOp.getLoc();
285d7a9bf91SMatthias Springer 
2866c3c5f80SMatthias Springer     // Get source buffer.
2875d50f51cSMatthias Springer     FailureOr<Value> srcMemref =
2885d50f51cSMatthias Springer         getBuffer(rewriter, extractSliceOp.getSource(), options);
2895d50f51cSMatthias Springer     if (failed(srcMemref))
2905d50f51cSMatthias Springer       return failure();
2915d50f51cSMatthias Springer     auto srcMemrefType = srcMemref->getType().cast<MemRefType>();
29249e37000SMatthias Springer 
2936c3c5f80SMatthias Springer     // Take a subview of the source buffer.
2946c3c5f80SMatthias Springer     auto subviewMemRefType =
2956c3c5f80SMatthias Springer         memref::SubViewOp::inferRankReducedResultType(
2966c3c5f80SMatthias Springer             extractSliceOp.getType().getShape(), srcMemrefType, mixedOffsets,
2976c3c5f80SMatthias Springer             mixedSizes, mixedStrides)
29849e37000SMatthias Springer             .cast<MemRefType>();
29949e37000SMatthias Springer     Value subView = rewriter.create<memref::SubViewOp>(
3005d50f51cSMatthias Springer         loc, subviewMemRefType, *srcMemref, mixedOffsets, mixedSizes,
30149e37000SMatthias Springer         mixedStrides);
30249e37000SMatthias Springer 
30349e37000SMatthias Springer     replaceOpWithBufferizedValues(rewriter, op, subView);
30449e37000SMatthias Springer     return success();
30549e37000SMatthias Springer   }
30649e37000SMatthias Springer };
30749e37000SMatthias Springer 
30849e37000SMatthias Springer /// Bufferization of tensor.extract. Replace with memref.load.
30949e37000SMatthias Springer struct ExtractOpInterface
31049e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<ExtractOpInterface,
31149e37000SMatthias Springer                                                     tensor::ExtractOp> {
31249e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
3139597b16aSMatthias Springer                               const AnalysisState &state) const {
31449e37000SMatthias Springer     return true;
31549e37000SMatthias Springer   }
31649e37000SMatthias Springer 
31749e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
3189597b16aSMatthias Springer                                const AnalysisState &state) const {
31949e37000SMatthias Springer     return false;
32049e37000SMatthias Springer   }
32149e37000SMatthias Springer 
3229597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
3239597b16aSMatthias Springer                                             const AnalysisState &state) const {
324585a8a32SMatthias Springer     return {};
32549e37000SMatthias Springer   }
32649e37000SMatthias Springer 
32749e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
328b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
32949e37000SMatthias Springer     auto extractOp = cast<tensor::ExtractOp>(op);
3305d50f51cSMatthias Springer     FailureOr<Value> srcMemref =
3315d50f51cSMatthias Springer         getBuffer(rewriter, extractOp.getTensor(), options);
3325d50f51cSMatthias Springer     if (failed(srcMemref))
3335d50f51cSMatthias Springer       return failure();
3345d50f51cSMatthias Springer     replaceOpWithNewBufferizedOp<memref::LoadOp>(rewriter, op, *srcMemref,
335*136d746eSJacques Pienaar                                                  extractOp.getIndices());
33649e37000SMatthias Springer     return success();
33749e37000SMatthias Springer   }
33849e37000SMatthias Springer };
33949e37000SMatthias Springer 
340d581c94dSMatthias Springer // Implements backtracking to traverse indices of the output buffer while
341d581c94dSMatthias Springer // iterating over op.elements().
342d581c94dSMatthias Springer static void createStores(RewriterBase &rewriter, Location loc, int dim,
343d581c94dSMatthias Springer                          Value buffer, ArrayRef<int64_t> shape,
344d581c94dSMatthias Springer                          ArrayRef<Value> constants,
345d581c94dSMatthias Springer                          OperandRange::iterator &elementIt,
346d581c94dSMatthias Springer                          SmallVectorImpl<Value> &indices) {
347d581c94dSMatthias Springer   if (dim == static_cast<int>(shape.size()) - 1) {
348d581c94dSMatthias Springer     for (int i = 0; i < shape.back(); ++i) {
349d581c94dSMatthias Springer       indices.back() = constants[i];
350d581c94dSMatthias Springer       rewriter.create<memref::StoreOp>(loc, *elementIt, buffer, indices);
351d581c94dSMatthias Springer       ++elementIt;
352d581c94dSMatthias Springer     }
353d581c94dSMatthias Springer     return;
354d581c94dSMatthias Springer   }
355d581c94dSMatthias Springer   for (int i = 0; i < shape[dim]; ++i) {
356d581c94dSMatthias Springer     indices[dim] = constants[i];
357d581c94dSMatthias Springer     createStores(rewriter, loc, dim + 1, buffer, shape, constants, elementIt,
358d581c94dSMatthias Springer                  indices);
359d581c94dSMatthias Springer   }
360d581c94dSMatthias Springer }
361d581c94dSMatthias Springer 
362d581c94dSMatthias Springer /// Bufferization of tensor.from_elements.
363d581c94dSMatthias Springer struct FromElementsOpInterface
364d581c94dSMatthias Springer     : public BufferizableOpInterface::ExternalModel<FromElementsOpInterface,
365d581c94dSMatthias Springer                                                     tensor::FromElementsOp> {
366d581c94dSMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
367b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
368d581c94dSMatthias Springer     auto fromElementsOp = cast<tensor::FromElementsOp>(op);
369d581c94dSMatthias Springer 
370c0b0b6a0SMatthias Springer     // TODO: Implement memory space for this op.
371c0b0b6a0SMatthias Springer     if (options.defaultMemorySpace != static_cast<unsigned>(0))
372c0b0b6a0SMatthias Springer       return op->emitError("memory space not implemented yet");
373c0b0b6a0SMatthias Springer 
374d581c94dSMatthias Springer     // Allocate a buffer for the result.
375d581c94dSMatthias Springer     Location loc = op->getLoc();
376d581c94dSMatthias Springer     auto tensorType = fromElementsOp.getType().cast<RankedTensorType>();
377d581c94dSMatthias Springer     auto shape = tensorType.getShape();
378b3ebe3beSMatthias Springer     // TODO: Create alloc_tensor ops during TensorCopyInsertion.
379b55d55ecSMatthias Springer     AnalysisState analysisState(options);
38045b995cdSMatthias Springer     FailureOr<Value> tensorAlloc = allocateTensorForShapedValue(
3818df54a6aSJacques Pienaar         rewriter, loc, fromElementsOp.getResult(),
38245b995cdSMatthias Springer         analysisState.isTensorYielded(fromElementsOp.getResult()), options,
383b3ebe3beSMatthias Springer         /*copy=*/false);
38445b995cdSMatthias Springer     if (failed(tensorAlloc))
38545b995cdSMatthias Springer       return failure();
386b3ebe3beSMatthias Springer     auto memrefType =
387b3ebe3beSMatthias Springer         MemRefType::get(tensorType.getShape(), tensorType.getElementType());
388b3ebe3beSMatthias Springer     Value buffer = rewriter.create<bufferization::ToMemrefOp>(
38945b995cdSMatthias Springer         op->getLoc(), memrefType, *tensorAlloc);
390d581c94dSMatthias Springer 
391d581c94dSMatthias Springer     // Case: tensor<0xelem_type>.
3928df54a6aSJacques Pienaar     if (fromElementsOp.getElements().empty()) {
393d581c94dSMatthias Springer       replaceOpWithBufferizedValues(rewriter, op, buffer);
394d581c94dSMatthias Springer       return success();
395d581c94dSMatthias Springer     }
396d581c94dSMatthias Springer 
397d581c94dSMatthias Springer     // Case: tensor<elem_type>.
398d581c94dSMatthias Springer     if (shape.empty()) {
3998df54a6aSJacques Pienaar       rewriter.create<memref::StoreOp>(
4008df54a6aSJacques Pienaar           loc, fromElementsOp.getElements().front(), buffer);
401d581c94dSMatthias Springer       replaceOpWithBufferizedValues(rewriter, op, buffer);
402d581c94dSMatthias Springer       return success();
403d581c94dSMatthias Springer     }
404d581c94dSMatthias Springer 
405d581c94dSMatthias Springer     // Create constants for the range of possible indices [0, max{shape_i}).
406d581c94dSMatthias Springer     auto maxDim = *std::max_element(shape.begin(), shape.end());
407d581c94dSMatthias Springer     SmallVector<Value, 2> constants;
408d581c94dSMatthias Springer     constants.reserve(maxDim);
409d581c94dSMatthias Springer     for (int i = 0; i < maxDim; ++i)
410d581c94dSMatthias Springer       constants.push_back(rewriter.create<arith::ConstantIndexOp>(loc, i));
411d581c94dSMatthias Springer 
412d581c94dSMatthias Springer     // Traverse all `elements` and create `memref.store` ops.
4138df54a6aSJacques Pienaar     auto elementIt = fromElementsOp.getElements().begin();
414d581c94dSMatthias Springer     SmallVector<Value, 2> indices(tensorType.getRank(), constants[0]);
415d581c94dSMatthias Springer     createStores(rewriter, loc, /*dim=*/0, buffer, shape, constants, elementIt,
416d581c94dSMatthias Springer                  indices);
417d581c94dSMatthias Springer 
418d581c94dSMatthias Springer     replaceOpWithBufferizedValues(rewriter, op, buffer);
419d581c94dSMatthias Springer     return success();
420d581c94dSMatthias Springer   }
421d581c94dSMatthias Springer };
422d581c94dSMatthias Springer 
42371bbb78bSMatthias Springer /// Bufferization of tensor.generate.
42471bbb78bSMatthias Springer struct GenerateOpInterface
42571bbb78bSMatthias Springer     : public BufferizableOpInterface::ExternalModel<GenerateOpInterface,
42671bbb78bSMatthias Springer                                                     tensor::GenerateOp> {
42771bbb78bSMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
428b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
42971bbb78bSMatthias Springer     auto generateOp = cast<tensor::GenerateOp>(op);
430c0b0b6a0SMatthias Springer 
431c0b0b6a0SMatthias Springer     // TODO: Implement memory space for this op.
432c0b0b6a0SMatthias Springer     if (options.defaultMemorySpace != static_cast<unsigned>(0))
433c0b0b6a0SMatthias Springer       return op->emitError("memory space not implemented yet");
434c0b0b6a0SMatthias Springer 
435b3ebe3beSMatthias Springer     auto tensorType = generateOp.getType().cast<RankedTensorType>();
43671bbb78bSMatthias Springer     // Allocate memory.
43771bbb78bSMatthias Springer     Location loc = op->getLoc();
438b3ebe3beSMatthias Springer     // TODO: Create alloc_tensor ops during TensorCopyInsertion.
439b55d55ecSMatthias Springer     AnalysisState analysisState(options);
44045b995cdSMatthias Springer     FailureOr<Value> tensorAlloc = allocateTensorForShapedValue(
4418df54a6aSJacques Pienaar         rewriter, loc, generateOp.getResult(),
44245b995cdSMatthias Springer         analysisState.isTensorYielded(generateOp.getResult()), options,
443b3ebe3beSMatthias Springer         /*copy=*/false);
44445b995cdSMatthias Springer     if (failed(tensorAlloc))
44545b995cdSMatthias Springer       return failure();
446b3ebe3beSMatthias Springer     auto memrefType =
447b3ebe3beSMatthias Springer         MemRefType::get(tensorType.getShape(), tensorType.getElementType());
448b3ebe3beSMatthias Springer     Value buffer = rewriter.create<bufferization::ToMemrefOp>(
44945b995cdSMatthias Springer         op->getLoc(), memrefType, *tensorAlloc);
45071bbb78bSMatthias Springer 
45171bbb78bSMatthias Springer     // Collect loop bounds.
45271bbb78bSMatthias Springer     int64_t rank = memrefType.getRank();
45371bbb78bSMatthias Springer     Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
45471bbb78bSMatthias Springer     Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
45571bbb78bSMatthias Springer     SmallVector<Value, 4> lowerBounds(rank, zero);
45671bbb78bSMatthias Springer     SmallVector<Value, 4> steps(rank, one);
45771bbb78bSMatthias Springer     SmallVector<Value, 4> upperBounds;
45871bbb78bSMatthias Springer     int nextDynamicIndex = 0;
45971bbb78bSMatthias Springer     for (int i = 0; i < rank; i++) {
4608df54a6aSJacques Pienaar       Value upperBound =
4618df54a6aSJacques Pienaar           memrefType.isDynamicDim(i)
4628df54a6aSJacques Pienaar               ? generateOp.getDynamicExtents()[nextDynamicIndex++]
46371bbb78bSMatthias Springer               : rewriter.create<arith::ConstantIndexOp>(
46471bbb78bSMatthias Springer                     loc, memrefType.getDimSize(i));
46571bbb78bSMatthias Springer       upperBounds.push_back(upperBound);
46671bbb78bSMatthias Springer     }
46771bbb78bSMatthias Springer 
46871bbb78bSMatthias Springer     // Generate tensor elements with a parallel loop that stores into
46971bbb78bSMatthias Springer     // each element of the resulting memref. We use mergeBlockBefore to "move"
47071bbb78bSMatthias Springer     // this op's body into the scf.parallel's body.
47171bbb78bSMatthias Springer     auto parallel =
47271bbb78bSMatthias Springer         rewriter.create<scf::ParallelOp>(loc, lowerBounds, upperBounds, steps);
47371bbb78bSMatthias Springer     Block *parallelBody = parallel.getBody();
474eca86cb2SJacques Pienaar     rewriter.mergeBlockBefore(&generateOp.getBody().front(),
47571bbb78bSMatthias Springer                               parallelBody->getTerminator(),
47671bbb78bSMatthias Springer                               parallelBody->getArguments());
47771bbb78bSMatthias Springer     // Replace the inlined yield op with a store op. The scf.parallel's builder
47871bbb78bSMatthias Springer     // already populated an scf.yield at the end, so we don't need to worry
47971bbb78bSMatthias Springer     // about creating that.
48071bbb78bSMatthias Springer     Operation *elementYield = parallelBody->getTerminator()->getPrevNode();
48171bbb78bSMatthias Springer     rewriter.setInsertionPointAfter(elementYield);
48271bbb78bSMatthias Springer     rewriter.replaceOpWithNewOp<memref::StoreOp>(
483b3ebe3beSMatthias Springer         elementYield, elementYield->getOperands()[0], buffer,
48471bbb78bSMatthias Springer         parallelBody->getArguments());
48571bbb78bSMatthias Springer 
486b3ebe3beSMatthias Springer     replaceOpWithBufferizedValues(rewriter, op, buffer);
48771bbb78bSMatthias Springer     return success();
48871bbb78bSMatthias Springer   }
48971bbb78bSMatthias Springer };
49071bbb78bSMatthias Springer 
49149e37000SMatthias Springer /// Bufferization of tensor.insert. Replace with memref.store.
49249e37000SMatthias Springer struct InsertOpInterface
49349e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<InsertOpInterface,
49449e37000SMatthias Springer                                                     tensor::InsertOp> {
49549e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
4969597b16aSMatthias Springer                               const AnalysisState &state) const {
49749e37000SMatthias Springer     return true;
49849e37000SMatthias Springer   }
49949e37000SMatthias Springer 
50049e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
5019597b16aSMatthias Springer                                const AnalysisState &state) const {
50249e37000SMatthias Springer     return true;
50349e37000SMatthias Springer   }
50449e37000SMatthias Springer 
5059597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
5069597b16aSMatthias Springer                                             const AnalysisState &state) const {
50749e37000SMatthias Springer     assert(&opOperand == &op->getOpOperand(1) /*dest*/ &&
50849e37000SMatthias Springer            "expected dest OpOperand");
509585a8a32SMatthias Springer     return {op->getOpResult(0)};
51049e37000SMatthias Springer   }
51149e37000SMatthias Springer 
51249e37000SMatthias Springer   SmallVector<OpOperand *>
51349e37000SMatthias Springer   getAliasingOpOperand(Operation *op, OpResult opResult,
5149597b16aSMatthias Springer                        const AnalysisState &state) const {
51549e37000SMatthias Springer     return {&op->getOpOperand(1) /*dest*/};
51649e37000SMatthias Springer   }
51749e37000SMatthias Springer 
51849e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
519b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
52049e37000SMatthias Springer     auto insertOp = cast<tensor::InsertOp>(op);
5215d50f51cSMatthias Springer     FailureOr<Value> destMemref =
5225d50f51cSMatthias Springer         getBuffer(rewriter, insertOp.getDest(), options);
5235d50f51cSMatthias Springer     if (failed(destMemref))
5245d50f51cSMatthias Springer       return failure();
5258df54a6aSJacques Pienaar     rewriter.create<memref::StoreOp>(insertOp.getLoc(), insertOp.getScalar(),
5265d50f51cSMatthias Springer                                      *destMemref, insertOp.getIndices());
5275d50f51cSMatthias Springer     replaceOpWithBufferizedValues(rewriter, op, *destMemref);
52849e37000SMatthias Springer     return success();
52949e37000SMatthias Springer   }
53049e37000SMatthias Springer 
53149e37000SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
5329597b16aSMatthias Springer                                 const AnalysisState &state) const {
53349e37000SMatthias Springer     return BufferRelation::Equivalent;
53449e37000SMatthias Springer   }
53549e37000SMatthias Springer };
53649e37000SMatthias Springer 
53749e37000SMatthias Springer /// Return true if the (ExtractSliceOp, InsertSliceOp) pair match (i.e.
53849e37000SMatthias Springer /// equivalent operand / result and same offset/sizes/strides specification).
53949e37000SMatthias Springer ///
54049e37000SMatthias Springer /// This is one particular type of relationship between ops on tensors that
54149e37000SMatthias Springer /// reduce to an equivalence on buffers. This should be generalized and
54249e37000SMatthias Springer /// exposed as interfaces on the proper types.
5439597b16aSMatthias Springer static bool areEquivalentExtractSliceOps(const AnalysisState &state,
54449e37000SMatthias Springer                                          ExtractSliceOp st, InsertSliceOp sti) {
54549e37000SMatthias Springer   if (!st || !sti)
54649e37000SMatthias Springer     return false;
54749e37000SMatthias Springer   if (sti != sti &&
5488df54a6aSJacques Pienaar       !state.areEquivalentBufferizedValues(st.getSource(), sti.getDest()))
54949e37000SMatthias Springer     return false;
55049e37000SMatthias Springer   if (!sameOffsetsSizesAndStrides(st, sti, isEqualConstantIntOrValue))
55149e37000SMatthias Springer     return false;
55249e37000SMatthias Springer   return true;
55349e37000SMatthias Springer }
55449e37000SMatthias Springer 
55549e37000SMatthias Springer /// Return true if `value` is originating from an ExtractSliceOp that matches
55649e37000SMatthias Springer /// the given InsertSliceOp.
5579597b16aSMatthias Springer static bool hasMatchingExtractSliceOp(const AnalysisState &state, Value value,
5589597b16aSMatthias Springer                                       InsertSliceOp insertOp) {
55949e37000SMatthias Springer   auto condition = [&](Value val) {
56049e37000SMatthias Springer     if (auto extractOp = val.getDefiningOp<ExtractSliceOp>())
56149e37000SMatthias Springer       if (areEquivalentExtractSliceOps(state, extractOp, insertOp))
56249e37000SMatthias Springer         return true;
56349e37000SMatthias Springer     return false;
56449e37000SMatthias Springer   };
56549e37000SMatthias Springer 
56649e37000SMatthias Springer   return llvm::all_of(state.findValueInReverseUseDefChain(value, condition),
56749e37000SMatthias Springer                       condition);
56849e37000SMatthias Springer }
56949e37000SMatthias Springer 
57049e37000SMatthias Springer /// Bufferization of tensor.insert_slice. Replace with a memory copy. Under
57149e37000SMatthias Springer /// certain circumstances, this op can also be a no-op.
57249e37000SMatthias Springer struct InsertSliceOpInterface
57349e37000SMatthias Springer     : public BufferizableOpInterface::ExternalModel<InsertSliceOpInterface,
57449e37000SMatthias Springer                                                     tensor::InsertSliceOp> {
57549e37000SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
5769597b16aSMatthias Springer                               const AnalysisState &state) const {
57749e37000SMatthias Springer     return true;
57849e37000SMatthias Springer   }
57949e37000SMatthias Springer 
58049e37000SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
5819597b16aSMatthias Springer                                const AnalysisState &state) const {
58249e37000SMatthias Springer     return &opOperand == &op->getOpOperand(1) /*dest*/;
58349e37000SMatthias Springer   }
58449e37000SMatthias Springer 
5859597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
5869597b16aSMatthias Springer                                             const AnalysisState &state) const {
587585a8a32SMatthias Springer     if (&opOperand == &op->getOpOperand(1) /*dest*/)
588585a8a32SMatthias Springer       return {op->getResult(0)};
589585a8a32SMatthias Springer     return {};
59049e37000SMatthias Springer   }
59149e37000SMatthias Springer 
59249e37000SMatthias Springer   BufferRelation bufferRelation(Operation *op, OpResult opResult,
5939597b16aSMatthias Springer                                 const AnalysisState &state) const {
59449e37000SMatthias Springer     return BufferRelation::Equivalent;
59549e37000SMatthias Springer   }
59649e37000SMatthias Springer 
59749e37000SMatthias Springer   bool isNotConflicting(Operation *op, OpOperand *uRead,
59849e37000SMatthias Springer                         OpOperand *uConflictingWrite,
5999597b16aSMatthias Springer                         const AnalysisState &state) const {
60049e37000SMatthias Springer     Operation *readingOp = uRead->getOwner();
60149e37000SMatthias Springer     Operation *conflictingWritingOp = uConflictingWrite->getOwner();
60249e37000SMatthias Springer 
60349e37000SMatthias Springer     // Special rules for matching ExtractSliceOp/InsertSliceOp pairs. If
60449e37000SMatthias Springer     // uRead is an InsertSliceOp...
60549e37000SMatthias Springer     if (auto insertSliceOp = dyn_cast<InsertSliceOp>(readingOp)) {
60649e37000SMatthias Springer       // As an example, consider the following IR.
60749e37000SMatthias Springer       //
60849e37000SMatthias Springer       // %0 = tensor.extract_slice %t[%a, %b][%c, %d][1, 1] {inplace = [true] }
60949e37000SMatthias Springer       // %1 = linalg.fill %cst, %0 {inplace= [true] }
61049e37000SMatthias Springer       // %2 = tensor.insert_slice %1 into %t[%a, %b][%c, %d][1, 1]
61149e37000SMatthias Springer       //     {inplace= [true] }
61249e37000SMatthias Springer 
61349e37000SMatthias Springer       // TODO: Use insertSliceOp.getDestOpOperand etc. when available.
61449e37000SMatthias Springer       if (uRead == &insertSliceOp->getOpOperand(1) /*dest*/ &&
61549e37000SMatthias Springer           hasMatchingExtractSliceOp(state, uConflictingWrite->get(),
61649e37000SMatthias Springer                                     insertSliceOp))
61749e37000SMatthias Springer         // Case 1: The main insight is that InsertSliceOp reads only part of
61849e37000SMatthias Springer         // the destination tensor. The overwritten area is not read. If
61949e37000SMatthias Springer         // uConflictingWrite writes into exactly the memory location that is
62049e37000SMatthias Springer         // being read by uRead, this is not a conflict.
62149e37000SMatthias Springer         //
62249e37000SMatthias Springer         // In the above example:
62349e37000SMatthias Springer         // uRead             = OpOperand 1 (%t) of tensor.insert_slice
62449e37000SMatthias Springer         // uConflictingWrite = OpOperand 1 (%0) of linalg.fill
62549e37000SMatthias Springer         //
62649e37000SMatthias Springer         // The read of %t does not conflict with the write of the FillOp
62749e37000SMatthias Springer         // (same aliases!) because the area that the FillOp operates on is
62849e37000SMatthias Springer         // exactly the one that is *not* read via %t.
62949e37000SMatthias Springer         return true;
63049e37000SMatthias Springer 
63149e37000SMatthias Springer       if (uRead == &insertSliceOp->getOpOperand(0) /*source*/ &&
63249e37000SMatthias Springer           uConflictingWrite == &insertSliceOp->getOpOperand(1) /*dest*/ &&
63349e37000SMatthias Springer           hasMatchingExtractSliceOp(state, uRead->get(), insertSliceOp))
63449e37000SMatthias Springer         // Case 2: The read of the source tensor and the write to the dest
63549e37000SMatthias Springer         // tensor via an InsertSliceOp is not a conflict if the read is
63649e37000SMatthias Springer         // reading exactly that part of an equivalent tensor that the
63749e37000SMatthias Springer         // InsertSliceOp is writing.
63849e37000SMatthias Springer         //
63949e37000SMatthias Springer         // In the above example:
64049e37000SMatthias Springer         // uRead             = OpOperand 0 (%1) of tensor.insert_slice
64149e37000SMatthias Springer         // uConflictingWrite = OpOperand 1 (%t) of tensor.insert_slice
64249e37000SMatthias Springer         return true;
64349e37000SMatthias Springer     }
64449e37000SMatthias Springer 
64549e37000SMatthias Springer     // If uConflictingWrite is an InsertSliceOp...
64649e37000SMatthias Springer     if (auto insertSliceOp = dyn_cast<InsertSliceOp>(conflictingWritingOp))
64749e37000SMatthias Springer       // As an example, consider the following IR.
64849e37000SMatthias Springer       //
64949e37000SMatthias Springer       // %0 = tensor.extract_slice %t[%a, %b][%c, %d][1, 1] {inplace = [true] }
65049e37000SMatthias Springer       // %1 = linalg.fill %cst, %0 {inplace= [true] }
65149e37000SMatthias Springer       // %2 = tensor.insert_slice %1 into %t[%a, %b][%c, %d][1, 1]
65249e37000SMatthias Springer       //     {inplace= [true] }
65349e37000SMatthias Springer       // %3 = vector.transfer_read %1, %cst
65449e37000SMatthias Springer       //
65549e37000SMatthias Springer       // In the above example:
65649e37000SMatthias Springer       // uRead             = OpOperand 0 (%1) of vector.transfer_read
65749e37000SMatthias Springer       // uConflictingWrite = OpOperand 1 (%t) of tensor.insert_slice
65849e37000SMatthias Springer       // lastWrite         = %1
65949e37000SMatthias Springer       //
66049e37000SMatthias Springer       // This is not a conflict because the InsertSliceOp overwrites the
66149e37000SMatthias Springer       // memory segment of %1 with the exact same data. (Effectively, there
66249e37000SMatthias Springer       // is no memory write here.)
66349e37000SMatthias Springer       if (uConflictingWrite == &insertSliceOp->getOpOperand(1) /*dest*/ &&
66449e37000SMatthias Springer           state.areEquivalentBufferizedValues(uRead->get(),
6658df54a6aSJacques Pienaar                                               insertSliceOp.getSource()) &&
6668df54a6aSJacques Pienaar           hasMatchingExtractSliceOp(state, insertSliceOp.getSource(),
66749e37000SMatthias Springer                                     insertSliceOp))
66849e37000SMatthias Springer         return true;
66949e37000SMatthias Springer 
67049e37000SMatthias Springer     return false;
67149e37000SMatthias Springer   }
67249e37000SMatthias Springer 
67349e37000SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
674b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
67549e37000SMatthias Springer     // insert_slice ops arise from tiling and bufferizing them out-of-place is
67649e37000SMatthias Springer     // generally a deal breaker. When used with loops, this ends up cloning the
67749e37000SMatthias Springer     // whole tensor on every single iteration and is a symptom of a
67849e37000SMatthias Springer     // catastrophically bad scheduling decision.
67949e37000SMatthias Springer     // TODO: be very loud about it or even consider failing the pass.
68049e37000SMatthias Springer     auto insertSliceOp = cast<tensor::InsertSliceOp>(op);
6816c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedOffsets = insertSliceOp.getMixedOffsets();
6826c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedSizes = insertSliceOp.getMixedSizes();
6836c3c5f80SMatthias Springer     SmallVector<OpFoldResult> mixedStrides = insertSliceOp.getMixedStrides();
68449e37000SMatthias Springer     Location loc = insertSliceOp.getLoc();
6856c3c5f80SMatthias Springer 
6866c3c5f80SMatthias Springer     // Get destination buffer.
6875d50f51cSMatthias Springer     FailureOr<Value> dstMemref =
6885d50f51cSMatthias Springer         getBuffer(rewriter, insertSliceOp.getDest(), options);
6895d50f51cSMatthias Springer     if (failed(dstMemref))
6905d50f51cSMatthias Springer       return failure();
69149e37000SMatthias Springer 
6926c3c5f80SMatthias Springer     // Take a subview of the destination buffer.
6935d50f51cSMatthias Springer     auto dstMemrefType = dstMemref->getType().cast<MemRefType>();
69449e37000SMatthias Springer     auto subviewMemRefType =
69549e37000SMatthias Springer         memref::SubViewOp::inferRankReducedResultType(
6966c3c5f80SMatthias Springer             insertSliceOp.getSourceType().getShape(), dstMemrefType,
69749e37000SMatthias Springer             mixedOffsets, mixedSizes, mixedStrides)
69849e37000SMatthias Springer             .cast<MemRefType>();
69949e37000SMatthias Springer     Value subView = rewriter.create<memref::SubViewOp>(
7005d50f51cSMatthias Springer         loc, subviewMemRefType, *dstMemref, mixedOffsets, mixedSizes,
70149e37000SMatthias Springer         mixedStrides);
70249e37000SMatthias Springer 
70349e37000SMatthias Springer     // Copy tensor. If this tensor.insert_slice has a matching
70449e37000SMatthias Springer     // tensor.extract_slice, the copy operation will eventually fold away.
7055d50f51cSMatthias Springer     FailureOr<Value> srcMemref =
7065d50f51cSMatthias Springer         getBuffer(rewriter, insertSliceOp.getSource(), options);
7075d50f51cSMatthias Springer     if (failed(srcMemref))
7085d50f51cSMatthias Springer       return failure();
7095d50f51cSMatthias Springer     if (failed(options.createMemCpy(rewriter, loc, *srcMemref, subView)))
71049e37000SMatthias Springer       return failure();
71149e37000SMatthias Springer 
7125d50f51cSMatthias Springer     replaceOpWithBufferizedValues(rewriter, op, *dstMemref);
71349e37000SMatthias Springer     return success();
71449e37000SMatthias Springer   }
71549e37000SMatthias Springer };
71649e37000SMatthias Springer 
717fc08d1c2SMatthias Springer /// Bufferization of tensor.rank. Replace with memref.rank.
718fc08d1c2SMatthias Springer struct RankOpInterface
719fc08d1c2SMatthias Springer     : public BufferizableOpInterface::ExternalModel<RankOpInterface,
720fc08d1c2SMatthias Springer                                                     tensor::RankOp> {
721fc08d1c2SMatthias Springer   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
7229597b16aSMatthias Springer                               const AnalysisState &state) const {
723fc08d1c2SMatthias Springer     return true;
724fc08d1c2SMatthias Springer   }
725fc08d1c2SMatthias Springer 
726fc08d1c2SMatthias Springer   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
7279597b16aSMatthias Springer                                const AnalysisState &state) const {
728fc08d1c2SMatthias Springer     return false;
729fc08d1c2SMatthias Springer   }
730fc08d1c2SMatthias Springer 
7319597b16aSMatthias Springer   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
7329597b16aSMatthias Springer                                             const AnalysisState &state) const {
733585a8a32SMatthias Springer     return {};
734fc08d1c2SMatthias Springer   }
735fc08d1c2SMatthias Springer 
736fc08d1c2SMatthias Springer   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
737b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
738fc08d1c2SMatthias Springer     auto rankOp = cast<tensor::RankOp>(op);
7395d50f51cSMatthias Springer     FailureOr<Value> v = getBuffer(rewriter, rankOp.getTensor(), options);
7405d50f51cSMatthias Springer     if (failed(v))
7415d50f51cSMatthias Springer       return failure();
742fc08d1c2SMatthias Springer     replaceOpWithNewBufferizedOp<memref::RankOp>(rewriter, op, rankOp.getType(),
7435d50f51cSMatthias Springer                                                  *v);
744fc08d1c2SMatthias Springer     return success();
745fc08d1c2SMatthias Springer   }
746fc08d1c2SMatthias Springer };
747fc08d1c2SMatthias Springer 
748e287d647SAshay Rane /// Bufferization of tensor.reshape. Replace with memref.reshape.
749e287d647SAshay Rane struct ReshapeOpInterface
750e287d647SAshay Rane     : public BufferizableOpInterface::ExternalModel<ReshapeOpInterface,
751e287d647SAshay Rane                                                     tensor::ReshapeOp> {
752e287d647SAshay Rane   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
753e287d647SAshay Rane                               const AnalysisState &state) const {
754e287d647SAshay Rane     if (&opOperand == &op->getOpOperand(1) /* shape */)
755e287d647SAshay Rane       return true;
756e287d647SAshay Rane     return false;
757e287d647SAshay Rane   }
758e287d647SAshay Rane 
759e287d647SAshay Rane   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
760e287d647SAshay Rane                                const AnalysisState &state) const {
761e287d647SAshay Rane     return false;
762e287d647SAshay Rane   }
763e287d647SAshay Rane 
764e287d647SAshay Rane   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
765e287d647SAshay Rane                                             const AnalysisState &state) const {
766e287d647SAshay Rane     return {op->getOpResult(0)};
767e287d647SAshay Rane   }
768e287d647SAshay Rane 
769e287d647SAshay Rane   BufferRelation bufferRelation(Operation *op, OpResult opResult,
770e287d647SAshay Rane                                 const AnalysisState &state) const {
771e287d647SAshay Rane     return BufferRelation::Equivalent;
772e287d647SAshay Rane   }
773e287d647SAshay Rane 
774e287d647SAshay Rane   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
775b55d55ecSMatthias Springer                           const BufferizationOptions &options) const {
776e287d647SAshay Rane     auto reshapeOp = cast<tensor::ReshapeOp>(op);
7775d50f51cSMatthias Springer     FailureOr<Value> srcBuffer =
7785d50f51cSMatthias Springer         getBuffer(rewriter, reshapeOp.getSource(), options);
7795d50f51cSMatthias Springer     FailureOr<Value> shapeBuffer =
7805d50f51cSMatthias Springer         getBuffer(rewriter, reshapeOp.getShape(), options);
7815d50f51cSMatthias Springer     if (failed(srcBuffer) || failed(shapeBuffer))
7825d50f51cSMatthias Springer       return failure();
783c0b0b6a0SMatthias Springer     auto resultMemRefType = getMemRefType(
784606f7c8fSMatthias Springer         reshapeOp.getResult(), options, /*layout=*/{},
785c0b0b6a0SMatthias Springer         srcBuffer->getType().cast<BaseMemRefType>().getMemorySpaceAsInt());
786e287d647SAshay Rane     replaceOpWithNewBufferizedOp<memref::ReshapeOp>(
7875d50f51cSMatthias Springer         rewriter, op, resultMemRefType, *srcBuffer, *shapeBuffer);
788e287d647SAshay Rane     return success();
789e287d647SAshay Rane   }
790e287d647SAshay Rane };
791e287d647SAshay Rane 
7927fbf55c9SNicolas Vasilache /// Return true if the (ExtractSliceOp, ParallelInsertSliceOp) pair match (i.e.
7937fbf55c9SNicolas Vasilache /// equivalent operand / result and same offset/sizes/strides specification).
7947fbf55c9SNicolas Vasilache static bool areEquivalentExtractSliceOps(const AnalysisState &state,
7957fbf55c9SNicolas Vasilache                                          ExtractSliceOp st,
7967fbf55c9SNicolas Vasilache                                          ParallelInsertSliceOp sti) {
7977fbf55c9SNicolas Vasilache   if (!st || !sti)
7987fbf55c9SNicolas Vasilache     return false;
7997fbf55c9SNicolas Vasilache   if (st != sti &&
8007fbf55c9SNicolas Vasilache       !state.areEquivalentBufferizedValues(st.getSource(), sti.getDest()))
8017fbf55c9SNicolas Vasilache     return false;
8027fbf55c9SNicolas Vasilache   if (!sameOffsetsSizesAndStrides(st, sti, isEqualConstantIntOrValue))
8037fbf55c9SNicolas Vasilache     return false;
8047fbf55c9SNicolas Vasilache   return true;
8057fbf55c9SNicolas Vasilache }
8067fbf55c9SNicolas Vasilache 
8077fbf55c9SNicolas Vasilache /// Return true if `value` is originating from an ExtractSliceOp that matches
8087fbf55c9SNicolas Vasilache /// the given InsertSliceOp.
8097fbf55c9SNicolas Vasilache static bool hasMatchingExtractSliceOp(const AnalysisState &state, Value value,
8107fbf55c9SNicolas Vasilache                                       ParallelInsertSliceOp insertOp) {
8117fbf55c9SNicolas Vasilache   auto condition = [&](Value val) {
8127fbf55c9SNicolas Vasilache     if (auto extractOp = val.getDefiningOp<ExtractSliceOp>())
8137fbf55c9SNicolas Vasilache       if (areEquivalentExtractSliceOps(state, extractOp, insertOp))
8147fbf55c9SNicolas Vasilache         return true;
8157fbf55c9SNicolas Vasilache     return false;
8167fbf55c9SNicolas Vasilache   };
8177fbf55c9SNicolas Vasilache 
8187fbf55c9SNicolas Vasilache   return llvm::all_of(state.findValueInReverseUseDefChain(value, condition),
8197fbf55c9SNicolas Vasilache                       condition);
8207fbf55c9SNicolas Vasilache }
8217fbf55c9SNicolas Vasilache 
8227fbf55c9SNicolas Vasilache /// Analysis of ParallelInsertSliceOp.
8237fbf55c9SNicolas Vasilache struct ParallelInsertSliceOpInterface
8247fbf55c9SNicolas Vasilache     : public BufferizableOpInterface::ExternalModel<
8257fbf55c9SNicolas Vasilache           ParallelInsertSliceOpInterface, ParallelInsertSliceOp> {
8267fbf55c9SNicolas Vasilache   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
8277fbf55c9SNicolas Vasilache                                             const AnalysisState &state) const {
8287fbf55c9SNicolas Vasilache     if (&opOperand != &op->getOpOperand(1) /*dest*/)
8297fbf55c9SNicolas Vasilache       return {};
8307fbf55c9SNicolas Vasilache 
8317fbf55c9SNicolas Vasilache     // ParallelInsertSliceOp itself has no results, query its tied op results.
8327fbf55c9SNicolas Vasilache     auto insertOp = cast<ParallelInsertSliceOp>(op);
8337fbf55c9SNicolas Vasilache     return {insertOp.getTiedOpResult()};
8347fbf55c9SNicolas Vasilache   }
8357fbf55c9SNicolas Vasilache 
8367fbf55c9SNicolas Vasilache   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
8377fbf55c9SNicolas Vasilache                               const AnalysisState &state) const {
8387fbf55c9SNicolas Vasilache     return true;
8397fbf55c9SNicolas Vasilache   }
8407fbf55c9SNicolas Vasilache 
8417fbf55c9SNicolas Vasilache   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
8427fbf55c9SNicolas Vasilache                                const AnalysisState &state) const {
8437fbf55c9SNicolas Vasilache     return &opOperand == &op->getOpOperand(1) /*dest*/;
8447fbf55c9SNicolas Vasilache   }
8457fbf55c9SNicolas Vasilache 
8467fbf55c9SNicolas Vasilache   BufferRelation bufferRelation(Operation *op, OpResult opResult,
8477fbf55c9SNicolas Vasilache                                 const AnalysisState &state) const {
8487fbf55c9SNicolas Vasilache     return BufferRelation::Equivalent;
8497fbf55c9SNicolas Vasilache   }
8507fbf55c9SNicolas Vasilache 
8517fbf55c9SNicolas Vasilache   LogicalResult resolveConflicts(Operation *op, RewriterBase &rewriter,
8527fbf55c9SNicolas Vasilache                                  const AnalysisState &state) const {
8537fbf55c9SNicolas Vasilache     // This interface method is overridden because we want to set a custom
8547fbf55c9SNicolas Vasilache     // insertion point for tensor copies. They should be inserted right before
8557fbf55c9SNicolas Vasilache     // the ForeachThreadOp. E.g.:
8567fbf55c9SNicolas Vasilache     //
8577fbf55c9SNicolas Vasilache     // %r0, %r1 = foreach_thead ... {
8587fbf55c9SNicolas Vasilache     //   ...
8597fbf55c9SNicolas Vasilache     //   perform_concurrently {
8607fbf55c9SNicolas Vasilache     //     parallel_insert_slice %a into %b ... {inplace = ["true", "true"]}
8617fbf55c9SNicolas Vasilache     //     parallel_insert_slice %c into %d ... {inplace = ["true", "false"]}
8627fbf55c9SNicolas Vasilache     //   }
8637fbf55c9SNicolas Vasilache     // }
8647fbf55c9SNicolas Vasilache     //
8657fbf55c9SNicolas Vasilache     // After TensorCopyInsertion:
8667fbf55c9SNicolas Vasilache     //
8677fbf55c9SNicolas Vasilache     // %copy = bufferization.alloc_tensor() copy(%d)
8687fbf55c9SNicolas Vasilache     // %r0, %r1 = foreach_thead ... {
8697fbf55c9SNicolas Vasilache     //   ...
8707fbf55c9SNicolas Vasilache     //   perform_concurrently {
8717fbf55c9SNicolas Vasilache     //     parallel_insert_slice %a into %b ...
8727fbf55c9SNicolas Vasilache     //     parallel_insert_slice %c into %copy ...
8737fbf55c9SNicolas Vasilache     //   }
8747fbf55c9SNicolas Vasilache     // }
8757fbf55c9SNicolas Vasilache 
8767fbf55c9SNicolas Vasilache     OpBuilder::InsertionGuard g(rewriter);
8777fbf55c9SNicolas Vasilache     auto parallelInsertSliceOp = cast<ParallelInsertSliceOp>(op);
8787fbf55c9SNicolas Vasilache     ParallelCombiningOpInterface parallelCombiningParent =
8797fbf55c9SNicolas Vasilache         parallelInsertSliceOp.getParallelCombiningParent();
8807fbf55c9SNicolas Vasilache     Operation *parallelIteratingOp = parallelCombiningParent->getParentOp();
8817fbf55c9SNicolas Vasilache 
8827fbf55c9SNicolas Vasilache     // Nothing to do if the destination tensor is inplace.
8837fbf55c9SNicolas Vasilache     assert(state.isInPlace(op->getOpOperand(0) /*src*/) &&
8847fbf55c9SNicolas Vasilache            "source is always in-place");
8857fbf55c9SNicolas Vasilache     if (state.isInPlace(op->getOpOperand(1) /*dest*/))
8867fbf55c9SNicolas Vasilache       return success();
8877fbf55c9SNicolas Vasilache 
8887fbf55c9SNicolas Vasilache     // Find corresponding OpResult.
8897fbf55c9SNicolas Vasilache     OpResult opResult = parallelInsertSliceOp.getTiedOpResult();
8907fbf55c9SNicolas Vasilache 
8917fbf55c9SNicolas Vasilache     // Insert tensor allocation right before the ForeachThreadOp.
8927fbf55c9SNicolas Vasilache     rewriter.setInsertionPoint(parallelIteratingOp);
8937fbf55c9SNicolas Vasilache     bool isYielded = state.isTensorYielded(opResult);
8947fbf55c9SNicolas Vasilache     FailureOr<Value> alloc = allocateTensorForShapedValue(
8957fbf55c9SNicolas Vasilache         rewriter, op->getLoc(), parallelInsertSliceOp.getDest(),
8967fbf55c9SNicolas Vasilache         /*escape=*/isYielded, state.getOptions());
8977fbf55c9SNicolas Vasilache     if (failed(alloc))
8987fbf55c9SNicolas Vasilache       return failure();
8997fbf55c9SNicolas Vasilache 
9007fbf55c9SNicolas Vasilache     // Update destination operand.
9017fbf55c9SNicolas Vasilache     rewriter.updateRootInPlace(parallelInsertSliceOp, [&]() {
9027fbf55c9SNicolas Vasilache       parallelInsertSliceOp.getDestMutable().assign(*alloc);
9037fbf55c9SNicolas Vasilache     });
9047fbf55c9SNicolas Vasilache 
9057fbf55c9SNicolas Vasilache     return success();
9067fbf55c9SNicolas Vasilache   }
9077fbf55c9SNicolas Vasilache 
9087fbf55c9SNicolas Vasilache   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
9097fbf55c9SNicolas Vasilache                           const BufferizationOptions &options) const {
9107fbf55c9SNicolas Vasilache     OpBuilder::InsertionGuard g(rewriter);
9117fbf55c9SNicolas Vasilache     auto parallelInsertSliceOp = cast<ParallelInsertSliceOp>(op);
9127fbf55c9SNicolas Vasilache     ParallelCombiningOpInterface parallelCombiningParent =
9137fbf55c9SNicolas Vasilache         parallelInsertSliceOp.getParallelCombiningParent();
9147fbf55c9SNicolas Vasilache     Operation *parallelIteratingOp = parallelCombiningParent->getParentOp();
9157fbf55c9SNicolas Vasilache 
9167fbf55c9SNicolas Vasilache     // Get destination buffer.
9177fbf55c9SNicolas Vasilache     FailureOr<Value> destBuffer =
9187fbf55c9SNicolas Vasilache         getBuffer(rewriter, parallelInsertSliceOp.getDest(), options);
9197fbf55c9SNicolas Vasilache     if (failed(destBuffer))
9207fbf55c9SNicolas Vasilache       return failure();
9217fbf55c9SNicolas Vasilache 
9227fbf55c9SNicolas Vasilache     // Bufferize the ParallelInsertSliceOp outside of `parallelCombiningParent`.
9237fbf55c9SNicolas Vasilache     rewriter.setInsertionPoint(parallelCombiningParent);
9247fbf55c9SNicolas Vasilache     FailureOr<Value> srcBuffer =
9257fbf55c9SNicolas Vasilache         getBuffer(rewriter, parallelInsertSliceOp.getSource(), options);
9267fbf55c9SNicolas Vasilache     if (failed(srcBuffer))
9277fbf55c9SNicolas Vasilache       return failure();
9286c3c5f80SMatthias Springer 
9296c3c5f80SMatthias Springer     // Take a subview of the destination buffer.
9306c3c5f80SMatthias Springer     auto destBufferType = destBuffer->getType().cast<MemRefType>();
9316c3c5f80SMatthias Springer     auto subviewMemRefType =
9326c3c5f80SMatthias Springer         memref::SubViewOp::inferRankReducedResultType(
9336c3c5f80SMatthias Springer             parallelInsertSliceOp.getSourceType().getShape(), destBufferType,
9346c3c5f80SMatthias Springer             parallelInsertSliceOp.getMixedOffsets(),
9356c3c5f80SMatthias Springer             parallelInsertSliceOp.getMixedSizes(),
9366c3c5f80SMatthias Springer             parallelInsertSliceOp.getMixedStrides())
9376c3c5f80SMatthias Springer             .cast<MemRefType>();
9387fbf55c9SNicolas Vasilache     Value subview = rewriter.create<memref::SubViewOp>(
9396c3c5f80SMatthias Springer         parallelInsertSliceOp.getLoc(), subviewMemRefType, *destBuffer,
9407fbf55c9SNicolas Vasilache         parallelInsertSliceOp.getMixedOffsets(),
9417fbf55c9SNicolas Vasilache         parallelInsertSliceOp.getMixedSizes(),
9427fbf55c9SNicolas Vasilache         parallelInsertSliceOp.getMixedStrides());
9436c3c5f80SMatthias Springer 
9447fbf55c9SNicolas Vasilache     // This memcpy will fold away if everything bufferizes in-place.
9457fbf55c9SNicolas Vasilache     if (failed(options.createMemCpy(rewriter, parallelInsertSliceOp.getLoc(),
9467fbf55c9SNicolas Vasilache                                     *srcBuffer, subview)))
9477fbf55c9SNicolas Vasilache       return failure();
9487fbf55c9SNicolas Vasilache 
9497fbf55c9SNicolas Vasilache     // Replace all uses of parallelIteratingOp (just the corresponding result).
9507fbf55c9SNicolas Vasilache     rewriter.setInsertionPointAfter(parallelIteratingOp);
9517fbf55c9SNicolas Vasilache     Value toTensorOp =
9527fbf55c9SNicolas Vasilache         rewriter.create<ToTensorOp>(parallelIteratingOp->getLoc(), *destBuffer);
9537fbf55c9SNicolas Vasilache     // PerformConcurrentlyOp can have multiple ParallelInsertSliceOps.
9547fbf55c9SNicolas Vasilache     SmallVector<OpOperand *> resultUses = llvm::to_vector(
9557fbf55c9SNicolas Vasilache         llvm::map_range(parallelInsertSliceOp.getTiedOpResult().getUses(),
9567fbf55c9SNicolas Vasilache                         [](OpOperand &use) { return &use; }));
9577fbf55c9SNicolas Vasilache     for (OpOperand *use : resultUses) {
9587fbf55c9SNicolas Vasilache       rewriter.updateRootInPlace(use->getOwner(),
9597fbf55c9SNicolas Vasilache                                  [&]() { use->set(toTensorOp); });
9607fbf55c9SNicolas Vasilache     }
9617fbf55c9SNicolas Vasilache     rewriter.eraseOp(op);
9627fbf55c9SNicolas Vasilache     return success();
9637fbf55c9SNicolas Vasilache   }
9647fbf55c9SNicolas Vasilache 
9657fbf55c9SNicolas Vasilache   // TODO: This is copied from TensorInterfaceImpl.cpp. Find a way to share
9667fbf55c9SNicolas Vasilache   // the code.
9677fbf55c9SNicolas Vasilache   bool isNotConflicting(Operation *op, OpOperand *uRead,
9687fbf55c9SNicolas Vasilache                         OpOperand *uConflictingWrite,
9697fbf55c9SNicolas Vasilache                         const AnalysisState &state) const {
9707fbf55c9SNicolas Vasilache     Operation *readingOp = uRead->getOwner();
9717fbf55c9SNicolas Vasilache     Operation *conflictingWritingOp = uConflictingWrite->getOwner();
9727fbf55c9SNicolas Vasilache 
9737fbf55c9SNicolas Vasilache     // Special rules for matching ExtractSliceOp/InsertSliceOp pairs. If
9747fbf55c9SNicolas Vasilache     // uRead is an InsertSliceOp...
9757fbf55c9SNicolas Vasilache     if (auto insertSliceOp = dyn_cast<ParallelInsertSliceOp>(readingOp)) {
9767fbf55c9SNicolas Vasilache       // As an example, consider the following IR.
9777fbf55c9SNicolas Vasilache       //
9787fbf55c9SNicolas Vasilache       // %0 = tensor.extract_slice %t[%a, %b][%c, %d][1, 1] {inplace = [true] }
9797fbf55c9SNicolas Vasilache       // %1 = linalg.fill %cst, %0 {inplace= [true] }
9807fbf55c9SNicolas Vasilache       // %2 = tensor.insert_slice %1 into %t[%a, %b][%c, %d][1, 1]
9817fbf55c9SNicolas Vasilache       //     {inplace= [true] }
9827fbf55c9SNicolas Vasilache 
9837fbf55c9SNicolas Vasilache       // TODO: Use insertSliceOp.getDestOpOperand etc. when available.
9847fbf55c9SNicolas Vasilache       if (uRead == &insertSliceOp->getOpOperand(1) /*dest*/ &&
9857fbf55c9SNicolas Vasilache           hasMatchingExtractSliceOp(state, uConflictingWrite->get(),
9867fbf55c9SNicolas Vasilache                                     insertSliceOp))
9877fbf55c9SNicolas Vasilache         // Case 1: The main insight is that InsertSliceOp reads only part of
9887fbf55c9SNicolas Vasilache         // the destination tensor. The overwritten area is not read. If
9897fbf55c9SNicolas Vasilache         // uConflictingWrite writes into exactly the memory location that is
9907fbf55c9SNicolas Vasilache         // being read by uRead, this is not a conflict.
9917fbf55c9SNicolas Vasilache         //
9927fbf55c9SNicolas Vasilache         // In the above example:
9937fbf55c9SNicolas Vasilache         // uRead             = OpOperand 1 (%t) of tensor.insert_slice
9947fbf55c9SNicolas Vasilache         // uConflictingWrite = OpOperand 1 (%0) of linalg.fill
9957fbf55c9SNicolas Vasilache         //
9967fbf55c9SNicolas Vasilache         // The read of %t does not conflict with the write of the FillOp
9977fbf55c9SNicolas Vasilache         // (same aliases!) because the area that the FillOp operates on is
9987fbf55c9SNicolas Vasilache         // exactly the one that is *not* read via %t.
9997fbf55c9SNicolas Vasilache         return true;
10007fbf55c9SNicolas Vasilache 
10017fbf55c9SNicolas Vasilache       if (uRead == &insertSliceOp->getOpOperand(0) /*source*/ &&
10027fbf55c9SNicolas Vasilache           uConflictingWrite == &insertSliceOp->getOpOperand(1) /*dest*/ &&
10037fbf55c9SNicolas Vasilache           hasMatchingExtractSliceOp(state, uRead->get(), insertSliceOp))
10047fbf55c9SNicolas Vasilache         // Case 2: The read of the source tensor and the write to the dest
10057fbf55c9SNicolas Vasilache         // tensor via an InsertSliceOp is not a conflict if the read is
10067fbf55c9SNicolas Vasilache         // reading exactly that part of an equivalent tensor that the
10077fbf55c9SNicolas Vasilache         // InsertSliceOp is writing.
10087fbf55c9SNicolas Vasilache         //
10097fbf55c9SNicolas Vasilache         // In the above example:
10107fbf55c9SNicolas Vasilache         // uRead             = OpOperand 0 (%1) of tensor.insert_slice
10117fbf55c9SNicolas Vasilache         // uConflictingWrite = OpOperand 1 (%t) of tensor.insert_slice
10127fbf55c9SNicolas Vasilache         return true;
10137fbf55c9SNicolas Vasilache     }
10147fbf55c9SNicolas Vasilache 
10157fbf55c9SNicolas Vasilache     // If uConflictingWrite is an InsertSliceOp...
10167fbf55c9SNicolas Vasilache     if (auto insertSliceOp =
10177fbf55c9SNicolas Vasilache             dyn_cast<ParallelInsertSliceOp>(conflictingWritingOp))
10187fbf55c9SNicolas Vasilache       // As an example, consider the following IR.
10197fbf55c9SNicolas Vasilache       //
10207fbf55c9SNicolas Vasilache       // %0 = tensor.extract_slice %t[%a, %b][%c, %d][1, 1] {inplace = [true] }
10217fbf55c9SNicolas Vasilache       // %1 = linalg.fill %cst, %0 {inplace= [true] }
10227fbf55c9SNicolas Vasilache       // %2 = tensor.insert_slice %1 into %t[%a, %b][%c, %d][1, 1]
10237fbf55c9SNicolas Vasilache       //     {inplace= [true] }
10247fbf55c9SNicolas Vasilache       // %3 = vector.transfer_read %1, %cst
10257fbf55c9SNicolas Vasilache       //
10267fbf55c9SNicolas Vasilache       // In the above example:
10277fbf55c9SNicolas Vasilache       // uRead             = OpOperand 0 (%1) of vector.transfer_read
10287fbf55c9SNicolas Vasilache       // uConflictingWrite = OpOperand 1 (%t) of tensor.insert_slice
10297fbf55c9SNicolas Vasilache       // lastWrite         = %1
10307fbf55c9SNicolas Vasilache       //
10317fbf55c9SNicolas Vasilache       // This is not a conflict because the InsertSliceOp overwrites the
10327fbf55c9SNicolas Vasilache       // memory segment of %1 with the exact same data. (Effectively, there
10337fbf55c9SNicolas Vasilache       // is no memory write here.)
10347fbf55c9SNicolas Vasilache       if (uConflictingWrite == &insertSliceOp->getOpOperand(1) /*dest*/ &&
10357fbf55c9SNicolas Vasilache           state.areEquivalentBufferizedValues(uRead->get(),
10367fbf55c9SNicolas Vasilache                                               insertSliceOp.getSource()) &&
10377fbf55c9SNicolas Vasilache           hasMatchingExtractSliceOp(state, insertSliceOp.getSource(),
10387fbf55c9SNicolas Vasilache                                     insertSliceOp))
10397fbf55c9SNicolas Vasilache         return true;
10407fbf55c9SNicolas Vasilache 
10417fbf55c9SNicolas Vasilache     return false;
10427fbf55c9SNicolas Vasilache   }
10437fbf55c9SNicolas Vasilache };
10447fbf55c9SNicolas Vasilache 
104549e37000SMatthias Springer } // namespace
104649e37000SMatthias Springer } // namespace tensor
104749e37000SMatthias Springer } // namespace mlir
104849e37000SMatthias Springer 
104949e37000SMatthias Springer void mlir::tensor::registerBufferizableOpInterfaceExternalModels(
105049e37000SMatthias Springer     DialectRegistry &registry) {
105177eee579SRiver Riddle   registry.addExtension(+[](MLIRContext *ctx, tensor::TensorDialect *dialect) {
105277eee579SRiver Riddle     CastOp::attachInterface<CastOpInterface>(*ctx);
105377eee579SRiver Riddle     CollapseShapeOp::attachInterface<CollapseShapeOpInterface>(*ctx);
105477eee579SRiver Riddle     DimOp::attachInterface<DimOpInterface>(*ctx);
105577eee579SRiver Riddle     ExpandShapeOp::attachInterface<ExpandShapeOpInterface>(*ctx);
105677eee579SRiver Riddle     ExtractSliceOp::attachInterface<ExtractSliceOpInterface>(*ctx);
105777eee579SRiver Riddle     ExtractOp::attachInterface<ExtractOpInterface>(*ctx);
105877eee579SRiver Riddle     FromElementsOp::attachInterface<FromElementsOpInterface>(*ctx);
105977eee579SRiver Riddle     GenerateOp::attachInterface<GenerateOpInterface>(*ctx);
106077eee579SRiver Riddle     InsertOp::attachInterface<InsertOpInterface>(*ctx);
106177eee579SRiver Riddle     InsertSliceOp::attachInterface<InsertSliceOpInterface>(*ctx);
10627fbf55c9SNicolas Vasilache     ParallelInsertSliceOp::attachInterface<ParallelInsertSliceOpInterface>(
10637fbf55c9SNicolas Vasilache         *ctx);
106477eee579SRiver Riddle     RankOp::attachInterface<RankOpInterface>(*ctx);
1065e287d647SAshay Rane     ReshapeOp::attachInterface<ReshapeOpInterface>(*ctx);
106677eee579SRiver Riddle   });
106749e37000SMatthias Springer }
1068