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/Arith/Transforms/BufferizableOpInterfaceImpl.h"
10#include "mlir/Dialect/Arith/IR/Arith.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/Attributes.h"
15#include "mlir/IR/Dialect.h"
16#include "mlir/IR/Operation.h"
17
18using namespace mlir;
19using namespace mlir::bufferization;
20
21namespace {
22/// Bufferization of arith.constant. Replace with memref.get_global.
23struct ConstantOpInterface
24 : public BufferizableOpInterface::ExternalModel<ConstantOpInterface,
25 arith::ConstantOp> {
26 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
27 const BufferizationOptions &options,
28 BufferizationState &state) const {
29 auto constantOp = cast<arith::ConstantOp>(Val: op);
30 auto type = dyn_cast<RankedTensorType>(Val: constantOp.getType());
31
32 // Only ranked tensors are supported.
33 if (!type)
34 return failure();
35
36 Attribute memorySpace;
37 if (auto memSpace =
38 options.defaultMemorySpaceFn(cast<TensorLikeType>(Val&: type)))
39 memorySpace = *memSpace;
40 else
41 return constantOp->emitError(message: "could not infer memory space");
42
43 // Only constants inside a module are supported.
44 auto moduleOp = constantOp->getParentOfType<ModuleOp>();
45 if (!moduleOp)
46 return failure();
47
48 // Create global memory segment and replace tensor with memref pointing to
49 // that memory segment.
50 FailureOr<memref::GlobalOp> globalOp =
51 getGlobalFor(constantOp, symbolTables&: state.getSymbolTables(),
52 alignment: options.bufferAlignment, memorySpace);
53 if (failed(Result: globalOp))
54 return failure();
55 memref::GlobalOp globalMemref = *globalOp;
56 replaceOpWithNewBufferizedOp<memref::GetGlobalOp>(
57 rewriter, op, args: globalMemref.getType(), args: globalMemref.getName());
58
59 return success();
60 }
61
62 bool isWritable(Operation *op, Value value,
63 const AnalysisState &state) const {
64 // Memory locations returned by memref::GetGlobalOp may not be written to.
65 assert(isa<OpResult>(value));
66 return false;
67 }
68};
69
70struct IndexCastOpInterface
71 : public BufferizableOpInterface::ExternalModel<IndexCastOpInterface,
72 arith::IndexCastOp> {
73 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
74 const AnalysisState &state) const {
75 return false;
76 }
77
78 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
79 const AnalysisState &state) const {
80 return false;
81 }
82
83 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
84 const AnalysisState &state) const {
85 return {{op->getResult(idx: 0), BufferRelation::Equivalent}};
86 }
87
88 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
89 const BufferizationOptions &options,
90 BufferizationState &state) const {
91 auto castOp = cast<arith::IndexCastOp>(Val: op);
92 auto resultTensorType = cast<TensorType>(Val: castOp.getType());
93
94 FailureOr<Value> source =
95 getBuffer(rewriter, value: castOp.getIn(), options, state);
96 if (failed(Result: source))
97 return failure();
98 auto sourceType = cast<BaseMemRefType>(Val: source->getType());
99
100 // Result type should have same layout and address space as the source type.
101 BaseMemRefType resultType;
102 if (auto rankedMemRefType = dyn_cast<MemRefType>(Val&: sourceType)) {
103 resultType = MemRefType::get(
104 shape: rankedMemRefType.getShape(), elementType: resultTensorType.getElementType(),
105 layout: rankedMemRefType.getLayout(), memorySpace: rankedMemRefType.getMemorySpace());
106 } else {
107 auto unrankedMemrefType = cast<UnrankedMemRefType>(Val&: sourceType);
108 resultType = UnrankedMemRefType::get(elementType: resultTensorType.getElementType(),
109 memorySpace: unrankedMemrefType.getMemorySpace());
110 }
111
112 replaceOpWithNewBufferizedOp<arith::IndexCastOp>(rewriter, op, args&: resultType,
113 args&: *source);
114 return success();
115 }
116};
117
118/// Bufferization of arith.select. Just replace the operands.
119struct SelectOpInterface
120 : public BufferizableOpInterface::ExternalModel<SelectOpInterface,
121 arith::SelectOp> {
122 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
123 const AnalysisState &state) const {
124 return false;
125 }
126
127 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
128 const AnalysisState &state) const {
129 return false;
130 }
131
132 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
133 const AnalysisState &state) const {
134 return {{op->getOpResult(idx: 0) /*result*/, BufferRelation::Equivalent,
135 /*isDefinite=*/false}};
136 }
137
138 LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
139 const BufferizationOptions &options,
140 BufferizationState &state) const {
141 auto selectOp = cast<arith::SelectOp>(Val: op);
142 Location loc = selectOp.getLoc();
143
144 // Elementwise conditions are not supported yet. To bufferize such an op,
145 // it could be lowered to an elementwise "linalg.generic" with a new
146 // "tensor.empty" out tensor, followed by "empty tensor elimination". Such
147 // IR will bufferize.
148 if (!selectOp.getCondition().getType().isInteger(width: 1))
149 return op->emitOpError(message: "only i1 condition values are supported");
150
151 // TODO: It would be more efficient to copy the result of the `select` op
152 // instead of its OpOperands. In the worst case, 2 copies are inserted at
153 // the moment (one for each tensor). When copying the op result, only one
154 // copy would be needed.
155 FailureOr<Value> maybeTrueBuffer =
156 getBuffer(rewriter, value: selectOp.getTrueValue(), options, state);
157 FailureOr<Value> maybeFalseBuffer =
158 getBuffer(rewriter, value: selectOp.getFalseValue(), options, state);
159 if (failed(Result: maybeTrueBuffer) || failed(Result: maybeFalseBuffer))
160 return failure();
161 Value trueBuffer = *maybeTrueBuffer;
162 Value falseBuffer = *maybeFalseBuffer;
163
164 // The "true" and the "false" operands must have the same type. If the
165 // buffers have different types, they differ only in their layout map. Cast
166 // both of them to the reconciled type.
167 if (trueBuffer.getType() != falseBuffer.getType()) {
168 auto targetType = options.reconcileBufferTypeMismatchFn(
169 cast<BufferLikeType>(Val: trueBuffer.getType()),
170 cast<BufferLikeType>(Val: falseBuffer.getType()), options);
171 if (failed(Result: targetType)) {
172 return selectOp->emitError(
173 message: "incompatible buffer types on true/false operands");
174 }
175 if (trueBuffer.getType() != *targetType) {
176 trueBuffer = *options.castFn(rewriter, loc, *targetType, trueBuffer);
177 }
178 if (falseBuffer.getType() != *targetType) {
179 falseBuffer = *options.castFn(rewriter, loc, *targetType, falseBuffer);
180 }
181 }
182
183 replaceOpWithNewBufferizedOp<arith::SelectOp>(
184 rewriter, op, args: selectOp.getCondition(), args&: trueBuffer, args&: falseBuffer);
185 return success();
186 }
187
188 FailureOr<BufferLikeType>
189 getBufferType(Operation *op, Value value, const BufferizationOptions &options,
190 const BufferizationState &state,
191 SmallVector<Value> &invocationStack) const {
192 auto selectOp = cast<arith::SelectOp>(Val: op);
193 assert(value == selectOp.getResult() && "invalid value");
194 auto trueType = bufferization::getBufferType(
195 value: selectOp.getTrueValue(), options, state, invocationStack);
196 auto falseType = bufferization::getBufferType(
197 value: selectOp.getFalseValue(), options, state, invocationStack);
198 if (failed(Result: trueType) || failed(Result: falseType))
199 return failure();
200 if (*trueType == *falseType)
201 return cast<BufferLikeType>(Val&: *trueType);
202
203 return options.reconcileBufferTypeMismatchFn(*trueType, *falseType,
204 options);
205 }
206};
207
208} // namespace
209
210void mlir::arith::registerBufferizableOpInterfaceExternalModels(
211 DialectRegistry &registry) {
212 registry.addExtension(extensionFn: +[](MLIRContext *ctx, ArithDialect *dialect) {
213 ConstantOp::attachInterface<ConstantOpInterface>(context&: *ctx);
214 IndexCastOp::attachInterface<IndexCastOpInterface>(context&: *ctx);
215 SelectOp::attachInterface<SelectOpInterface>(context&: *ctx);
216 });
217}
218