23struct ConstantOpInterface
24 :
public BufferizableOpInterface::ExternalModel<ConstantOpInterface,
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());
36 Attribute memorySpace;
38 options.defaultMemorySpaceFn(cast<TensorLikeType>(type)))
39 memorySpace = *memSpace;
41 return constantOp->emitError(
"could not infer memory space");
44 auto moduleOp = constantOp->getParentOfType<ModuleOp>();
50 FailureOr<memref::GlobalOp> globalOp =
52 options.bufferAlignment, memorySpace);
55 memref::GlobalOp globalMemref = *globalOp;
56 replaceOpWithNewBufferizedOp<memref::GetGlobalOp>(
57 rewriter, op, globalMemref.getType(), globalMemref.getName());
62 bool isWritable(Operation *op, Value value,
63 const AnalysisState &state)
const {
65 assert(isa<OpResult>(value));
70struct IndexCastOpInterface
71 :
public BufferizableOpInterface::ExternalModel<IndexCastOpInterface,
73 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
74 const AnalysisState &state)
const {
78 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
79 const AnalysisState &state)
const {
83 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
84 const AnalysisState &state)
const {
85 return {{op->
getResult(0), BufferRelation::Equivalent}};
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());
94 FailureOr<Value> source =
95 getBuffer(rewriter, castOp.getIn(),
options, state);
98 auto sourceType = cast<BaseMemRefType>(source->getType());
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());
107 auto unrankedMemrefType = cast<UnrankedMemRefType>(sourceType);
108 resultType = UnrankedMemRefType::get(resultTensorType.getElementType(),
109 unrankedMemrefType.getMemorySpace());
112 replaceOpWithNewBufferizedOp<arith::IndexCastOp>(rewriter, op, resultType,
119struct SelectOpInterface
120 :
public BufferizableOpInterface::ExternalModel<SelectOpInterface,
122 bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
123 const AnalysisState &state)
const {
127 bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
128 const AnalysisState &state)
const {
132 AliasingValueList getAliasingValues(Operation *op, OpOperand &opOperand,
133 const AnalysisState &state)
const {
134 return {{op->
getOpResult(0) , BufferRelation::Equivalent,
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();
148 if (!selectOp.getCondition().getType().isInteger(1))
149 return op->
emitOpError(
"only i1 condition values are supported");
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))
161 Value trueBuffer = *maybeTrueBuffer;
162 Value falseBuffer = *maybeFalseBuffer;
168 auto targetType =
options.reconcileBufferTypeMismatchFn(
169 cast<BufferLikeType>(trueBuffer.
getType()),
172 return selectOp->emitError(
173 "incompatible buffer types on true/false operands");
175 if (trueBuffer.
getType() != *targetType)
177 memref::CastOp::create(rewriter, loc, *targetType, trueBuffer);
178 if (falseBuffer.
getType() != *targetType)
180 memref::CastOp::create(rewriter, loc, *targetType, falseBuffer);
183 replaceOpWithNewBufferizedOp<arith::SelectOp>(
184 rewriter, op, selectOp.getCondition(), trueBuffer, falseBuffer);
188 FailureOr<BufferLikeType>
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);
200 if (*trueType == *falseType)
201 return cast<BufferLikeType>(*trueType);
203 return options.reconcileBufferTypeMismatchFn(*trueType, *falseType,
213 ConstantOp::attachInterface<ConstantOpInterface>(*ctx);
214 IndexCastOp::attachInterface<IndexCastOpInterface>(*ctx);
215 SelectOp::attachInterface<SelectOpInterface>(*ctx);
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.
OpResult getOpResult(unsigned idx)
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
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.
void registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry)
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, SymbolTableCollection &symbolTables, uint64_t alignment, Attribute memorySpace={})
Include the generated interface declarations.