MLIR 23.0.0git
BufferizableOpInterfaceImpl.cpp
Go to the documentation of this file.
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
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>(op);
30 auto type = dyn_cast<RankedTensorType>(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>(type)))
39 memorySpace = *memSpace;
40 else
41 return constantOp->emitError("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, state.getSymbolTables(),
52 options.bufferAlignment, memorySpace);
53 if (failed(globalOp))
54 return failure();
55 memref::GlobalOp globalMemref = *globalOp;
56 replaceOpWithNewBufferizedOp<memref::GetGlobalOp>(
57 rewriter, op, globalMemref.getType(), 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(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>(op);
92 auto resultTensorType = cast<TensorType>(castOp.getType());
93
94 FailureOr<Value> source =
95 getBuffer(rewriter, castOp.getIn(), options, state);
96 if (failed(source))
97 return failure();
98 auto sourceType = cast<BaseMemRefType>(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>(sourceType)) {
103 resultType = MemRefType::get(
104 rankedMemRefType.getShape(), resultTensorType.getElementType(),
105 rankedMemRefType.getLayout(), rankedMemRefType.getMemorySpace());
106 } else {
107 auto unrankedMemrefType = cast<UnrankedMemRefType>(sourceType);
108 resultType = UnrankedMemRefType::get(resultTensorType.getElementType(),
109 unrankedMemrefType.getMemorySpace());
110 }
111
112 replaceOpWithNewBufferizedOp<arith::IndexCastOp>(rewriter, op, resultType,
113 *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(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>(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(1))
149 return op->emitOpError("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, selectOp.getTrueValue(), options, state);
157 FailureOr<Value> maybeFalseBuffer =
158 getBuffer(rewriter, selectOp.getFalseValue(), options, state);
159 if (failed(maybeTrueBuffer) || failed(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>(trueBuffer.getType()),
170 cast<BufferLikeType>(falseBuffer.getType()), options);
171 if (failed(targetType)) {
172 return selectOp->emitError(
173 "incompatible buffer types on true/false operands");
174 }
175 if (trueBuffer.getType() != *targetType)
176 trueBuffer =
177 memref::CastOp::create(rewriter, loc, *targetType, trueBuffer);
178 if (falseBuffer.getType() != *targetType)
179 falseBuffer =
180 memref::CastOp::create(rewriter, loc, *targetType, falseBuffer);
181 }
182
183 replaceOpWithNewBufferizedOp<arith::SelectOp>(
184 rewriter, op, selectOp.getCondition(), trueBuffer, 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>(op);
193 assert(value == selectOp.getResult() && "invalid value");
194 auto trueType = bufferization::getBufferType(
195 selectOp.getTrueValue(), options, state, invocationStack);
196 auto falseType = bufferization::getBufferType(
197 selectOp.getFalseValue(), options, state, invocationStack);
198 if (failed(trueType) || failed(falseType))
199 return failure();
200 if (*trueType == *falseType)
201 return cast<BufferLikeType>(*trueType);
202
203 return options.reconcileBufferTypeMismatchFn(*trueType, *falseType,
204 options);
205 }
206};
207
208} // namespace
209
211 DialectRegistry &registry) {
212 registry.addExtension(+[](MLIRContext *ctx, ArithDialect *dialect) {
213 ConstantOp::attachInterface<ConstantOpInterface>(*ctx);
214 IndexCastOp::attachInterface<IndexCastOpInterface>(*ctx);
215 SelectOp::attachInterface<SelectOpInterface>(*ctx);
216 });
217}
return success()
static llvm::ManagedStatic< PassManagerOptions > options
static RankedTensorType getBufferType(const SparseTensorType &stt, bool needTmpCOO)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
OpResult getOpResult(unsigned idx)
Definition Operation.h:446
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:432
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Type getType() const
Return the type of this value.
Definition Value.h:105
void registerBufferizableOpInterfaceExternalModels(DialectRegistry &registry)
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, SymbolTableCollection &symbolTables, uint64_t alignment, Attribute memorySpace={})
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:717
Include the generated interface declarations.