1 //===- Bufferize.cpp - Bufferization for `tensor` dialect ops -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements bufferization of `tensor` dialect ops
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Transforms/Bufferize.h"
14 #include "PassDetail.h"
15 #include "mlir/Dialect/StandardOps/IR/Ops.h"
16 #include "mlir/Dialect/Tensor/IR/Tensor.h"
17 #include "mlir/Dialect/Tensor/Transforms/Passes.h"
18 #include "mlir/Transforms/DialectConversion.h"
19 
20 using namespace mlir;
21 
22 namespace {
23 class BufferizeCastOp : public OpConversionPattern<tensor::CastOp> {
24 public:
25   using OpConversionPattern::OpConversionPattern;
26   LogicalResult
27   matchAndRewrite(tensor::CastOp op, ArrayRef<Value> operands,
28                   ConversionPatternRewriter &rewriter) const override {
29     auto resultType = getTypeConverter()->convertType(op.getType());
30     rewriter.replaceOpWithNewOp<MemRefCastOp>(op, resultType, operands[0]);
31     return success();
32   }
33 };
34 } // namespace
35 
36 namespace {
37 class BufferizeExtractOp : public OpConversionPattern<tensor::ExtractOp> {
38 public:
39   using OpConversionPattern::OpConversionPattern;
40   LogicalResult
41   matchAndRewrite(tensor::ExtractOp op, ArrayRef<Value> operands,
42                   ConversionPatternRewriter &rewriter) const override {
43     tensor::ExtractOp::Adaptor adaptor(operands);
44     rewriter.replaceOpWithNewOp<LoadOp>(op, adaptor.tensor(),
45                                         adaptor.indices());
46     return success();
47   }
48 };
49 } // namespace
50 
51 void mlir::populateTensorBufferizePatterns(
52     MLIRContext *context, BufferizeTypeConverter &typeConverter,
53     OwningRewritePatternList &patterns) {
54   patterns.insert<BufferizeCastOp, BufferizeExtractOp>(typeConverter, context);
55 }
56 
57 namespace {
58 struct TensorBufferizePass : public TensorBufferizeBase<TensorBufferizePass> {
59   void runOnFunction() override {
60     auto *context = &getContext();
61     BufferizeTypeConverter typeConverter;
62     OwningRewritePatternList patterns;
63     ConversionTarget target(*context);
64 
65     populateTensorBufferizePatterns(context, typeConverter, patterns);
66     target.addIllegalOp<tensor::CastOp, tensor::ExtractOp>();
67     target.addLegalDialect<StandardOpsDialect>();
68 
69     if (failed(
70             applyPartialConversion(getFunction(), target, std::move(patterns))))
71       signalPassFailure();
72   }
73 };
74 } // namespace
75 
76 std::unique_ptr<Pass> mlir::createTensorBufferizePass() {
77   return std::make_unique<TensorBufferizePass>();
78 }
79