1 //===- BufferizableOpInterfaceImpl.cpp - Impl. of BufferizableOpInterface -===//
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 #include "mlir/Dialect/Arithmetic/Transforms/BufferizableOpInterfaceImpl.h"
10 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
11 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
12 #include "mlir/Dialect/Bufferization/Transforms/BufferUtils.h"
13 #include "mlir/Dialect/MemRef/IR/MemRef.h"
14 #include "mlir/IR/Dialect.h"
15 #include "mlir/IR/Operation.h"
16 
17 using namespace mlir;
18 using namespace mlir::bufferization;
19 
20 namespace {
21 /// Bufferization of arith.constant. Replace with memref.get_global.
22 struct ConstantOpInterface
23     : public BufferizableOpInterface::ExternalModel<ConstantOpInterface,
24                                                     arith::ConstantOp> {
25   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
26                           BufferizationState &state) const {
27     auto constantOp = cast<arith::ConstantOp>(op);
28 
29     // Only ranked tensors are supported.
30     if (!constantOp.getType().isa<RankedTensorType>())
31       return failure();
32 
33     // Only constants inside a module are supported.
34     auto moduleOp = constantOp->getParentOfType<ModuleOp>();
35     if (!moduleOp)
36       return failure();
37 
38     // Create global memory segment and replace tensor with memref pointing to
39     // that memory segment.
40     FailureOr<memref::GlobalOp> globalOp =
41         getGlobalFor(constantOp, state.getOptions().bufferAlignment);
42     if (failed(globalOp))
43       return failure();
44     memref::GlobalOp globalMemref = globalOp.getValue();
45     replaceOpWithNewBufferizedOp<memref::GetGlobalOp>(
46         rewriter, op, globalMemref.type(), globalMemref.getName());
47 
48     return success();
49   }
50 
51   bool isWritable(Operation *op, Value value,
52                   const AnalysisState &state) const {
53     // Memory locations returned by memref::GetGlobalOp may not be written to.
54     assert(value.isa<OpResult>());
55     return false;
56   }
57 };
58 
59 struct IndexCastOpInterface
60     : public BufferizableOpInterface::ExternalModel<IndexCastOpInterface,
61                                                     arith::IndexCastOp> {
62   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
63                               const AnalysisState &state) const {
64     return false;
65   }
66 
67   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
68                                const AnalysisState &state) const {
69     return false;
70   }
71 
72   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
73                                             const AnalysisState &state) const {
74     return {op->getResult(0)};
75   }
76 
77   BufferRelation bufferRelation(Operation *op, OpResult opResult,
78                                 const AnalysisState &state) const {
79     return BufferRelation::Equivalent;
80   }
81 
82   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
83                           BufferizationState &state) const {
84     auto castOp = cast<arith::IndexCastOp>(op);
85 
86     Value source = *state.getBuffer(rewriter, op->getOpOperand(0) /*in*/);
87     auto sourceType = source.getType().cast<BaseMemRefType>();
88 
89     // Result type should have same layout and address space as the source type.
90     MemRefLayoutAttrInterface layout = {};
91     if (auto rankedMemRefType = sourceType.dyn_cast<MemRefType>())
92       layout = rankedMemRefType.getLayout();
93     Type resultType =
94         getMemRefType(castOp.getType().cast<TensorType>(), state.getOptions(),
95                       layout, sourceType.getMemorySpace());
96 
97     replaceOpWithNewBufferizedOp<arith::IndexCastOp>(rewriter, op, resultType,
98                                                      source);
99     return success();
100   }
101 };
102 
103 /// Bufferization of arith.select. Just replace the operands.
104 struct SelectOpInterface
105     : public BufferizableOpInterface::ExternalModel<SelectOpInterface,
106                                                     arith::SelectOp> {
107   bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
108                               const AnalysisState &state) const {
109     return false;
110   }
111 
112   bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
113                                const AnalysisState &state) const {
114     return false;
115   }
116 
117   SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
118                                             const AnalysisState &state) const {
119     return {op->getOpResult(0) /*result*/};
120   }
121 
122   SmallVector<OpOperand *>
123   getAliasingOpOperand(Operation *op, OpResult opResult,
124                        const AnalysisState &state) const {
125     return {&op->getOpOperand(1) /*true_value*/,
126             &op->getOpOperand(2) /*false_value*/};
127   }
128 
129   LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
130                           BufferizationState &state) const {
131     auto selectOp = cast<arith::SelectOp>(op);
132     Location loc = selectOp.getLoc();
133 
134     // `getBuffer` introduces copies if an OpOperand bufferizes out-of-place.
135     // TODO: It would be more efficient to copy the result of the `select` op
136     // instead of its OpOperands. In the worst case, 2 copies are inserted at
137     // the moment (one for each tensor). When copying the op result, only one
138     // copy would be needed.
139     Value trueBuffer =
140         *state.getBuffer(rewriter, selectOp->getOpOperand(1) /*true_value*/);
141     Value falseBuffer =
142         *state.getBuffer(rewriter, selectOp->getOpOperand(2) /*false_value*/);
143 
144     // The "true" and the "false" operands must have the same type. If the
145     // buffers have different types, they differ only in their layout map. Cast
146     // both of them to the most dynamic MemRef type.
147     if (trueBuffer.getType() != falseBuffer.getType()) {
148       auto trueType = trueBuffer.getType().cast<MemRefType>();
149       auto tensorType = selectOp.getTrueValue().getType().cast<TensorType>();
150       int64_t dynamicOffset = ShapedType::kDynamicStrideOrOffset;
151       SmallVector<int64_t> dynamicStrides(tensorType.getRank(),
152                                           ShapedType::kDynamicStrideOrOffset);
153       AffineMap stridedLayout = makeStridedLinearLayoutMap(
154           dynamicStrides, dynamicOffset, op->getContext());
155       BaseMemRefType castedType = bufferization::getMemRefType(
156           tensorType, state.getOptions(), AffineMapAttr::get(stridedLayout),
157           trueType.getMemorySpace());
158       trueBuffer = rewriter.create<memref::CastOp>(loc, castedType, trueBuffer);
159       falseBuffer =
160           rewriter.create<memref::CastOp>(loc, castedType, falseBuffer);
161     }
162 
163     replaceOpWithNewBufferizedOp<arith::SelectOp>(
164         rewriter, op, selectOp.getCondition(), trueBuffer, falseBuffer);
165     return success();
166   }
167 
168   BufferRelation bufferRelation(Operation *op, OpResult opResult,
169                                 const AnalysisState &state) const {
170     return BufferRelation::None;
171   }
172 };
173 
174 } // namespace
175 
176 void mlir::arith::registerBufferizableOpInterfaceExternalModels(
177     DialectRegistry &registry) {
178   registry.addExtension(+[](MLIRContext *ctx, ArithmeticDialect *dialect) {
179     ConstantOp::attachInterface<ConstantOpInterface>(*ctx);
180     IndexCastOp::attachInterface<IndexCastOpInterface>(*ctx);
181     SelectOp::attachInterface<SelectOpInterface>(*ctx);
182   });
183 }
184