22 #include "mlir/Dialect/Bufferization/IR/BufferizationOpsDialect.cpp.inc"
26 constexpr const ::llvm::StringLiteral BufferizationDialect::kWritableAttrName;
30 constexpr const ::llvm::StringLiteral
31 BufferizationDialect::kBufferLayoutAttrName;
41 constexpr const ::llvm::StringLiteral BufferizationDialect::kManualDeallocation;
57 template <
typename Tensor>
58 struct BuiltinTensorExternalModel
59 : TensorLikeType::ExternalModel<BuiltinTensorExternalModel<Tensor>,
64 auto tensorType = cast<TensorType>(tensor);
65 auto memSpace =
options.defaultMemorySpaceFn(tensorType);
66 if (!memSpace.has_value())
67 return emitError() <<
"could not infer memory space";
69 return cast<BufferLikeType>(
73 mlir::LogicalResult verifyCompatibleBufferType(
76 assert(isa<TensorType>(tensor) &&
"expected tensor type");
77 assert(isa<BaseMemRefType>(bufferType) &&
"expected memref type");
79 auto tensorType = cast<ShapedType>(tensor);
80 auto memrefType = cast<ShapedType>(bufferType);
82 if (tensorType.getShape() != memrefType.getShape())
83 return emitError() <<
"shapes do not match";
85 if (tensorType.getElementType() != memrefType.getElementType())
86 return emitError() <<
"element types do not match";
88 return mlir::success();
92 template <
typename MemRef>
93 struct BuiltinMemRefExternalModel
94 : BufferLikeType::ExternalModel<BuiltinMemRefExternalModel<MemRef>,
102 void mlir::bufferization::BufferizationDialect::initialize() {
105 #include "mlir/Dialect/Bufferization/IR/BufferizationOps.cpp.inc"
107 addInterfaces<BufferizationInlinerInterface>();
114 RankedTensorType::attachInterface<
115 BuiltinTensorExternalModel<RankedTensorType>>(*
getContext());
116 UnrankedTensorType::attachInterface<
117 BuiltinTensorExternalModel<UnrankedTensorType>>(*
getContext());
118 MemRefType::attachInterface<BuiltinMemRefExternalModel<MemRefType>>(
120 UnrankedMemRefType::attachInterface<
121 BuiltinMemRefExternalModel<UnrankedMemRefType>>(*
getContext());
124 LogicalResult BufferizationDialect::verifyRegionArgAttribute(
125 Operation *op,
unsigned ,
unsigned argIndex,
127 if (attr.
getName() == kWritableAttrName) {
128 if (!llvm::isa<BoolAttr>(attr.
getValue())) {
129 return op->
emitError() <<
"'" << kWritableAttrName
130 <<
"' is expected to be a boolean attribute";
132 if (!isa<FunctionOpInterface>(op))
133 return op->
emitError() <<
"expected '" << kWritableAttrName
134 <<
"' to be used on function-like operations";
135 if (cast<FunctionOpInterface>(op).isExternal())
136 return op->
emitError() <<
"'" << kWritableAttrName
137 <<
"' is invalid on external functions";
140 if (attr.
getName() == kBufferAccessAttrName) {
141 if (!llvm::isa<StringAttr>(attr.
getValue())) {
142 return op->
emitError() <<
"'" << kBufferAccessAttrName
143 <<
"' is expected to be a string attribute";
145 StringRef str = llvm::cast<StringAttr>(attr.
getValue()).getValue();
146 if (str !=
"none" && str !=
"read" && str !=
"write" && str !=
"read-write")
148 <<
"invalid value for '" << kBufferAccessAttrName <<
"'";
149 if (!isa<FunctionOpInterface>(op))
150 return op->
emitError() <<
"expected '" << kBufferAccessAttrName
151 <<
"' to be used on function-like operations";
154 if (attr.
getName() == kBufferLayoutAttrName) {
155 if (!llvm::isa<MemRefLayoutAttrInterface>(attr.
getValue())) {
156 return op->
emitError() <<
"'" << kBufferLayoutAttrName
157 <<
"' is expected to be a memref layout attribute";
159 if (!isa<FunctionOpInterface>(op))
160 return op->
emitError() <<
"expected '" << kBufferLayoutAttrName
161 <<
"' to be used on function-like operations";
164 return op->
emitError() <<
"attribute '" << kBufferLayoutAttrName
165 <<
"' not supported as a region arg attribute by the "
166 "bufferization dialect";
170 BufferizationDialect::verifyOperationAttribute(
Operation *op,
172 using bufferization::BufferizableOpInterface;
174 if (attr.
getName() == kManualDeallocation) {
175 if (!mlir::hasEffect<MemoryEffects::Allocate>(op) &&
176 !mlir::hasEffect<MemoryEffects::Free>(op))
178 << kManualDeallocation
179 <<
"' can be used only on ops that have an allocation and/or free "
185 <<
"attribute '" << attr.
getName()
186 <<
"' not supported as an op attribute by the bufferization dialect";
static MLIRContext * getContext(OpFoldResult val)
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
static llvm::ManagedStatic< PassManagerOptions > options
This is the interface that must be implemented by the dialects of operations to be inlined.
DialectInlinerInterface(Dialect *dialect)
This is a utility class for mapping one set of IR entities to another.
This class represents a diagnostic that is inflight and set to be reported.
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Operation is the basic unit of execution within MLIR.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
BaseMemRefType getMemRefType(TensorType tensorType, const BufferizationOptions &options, MemRefLayoutAttrInterface layout={}, Attribute memorySpace=nullptr)
Return a MemRefType to which the TensorType can be bufferized.
FailureOr< BufferLikeType > getBufferType(Value value, const BufferizationOptions &options, const BufferizationState &state)
Return the buffer type for a given Value (tensor) after bufferization without bufferizing any IR.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Options for BufferizableOpInterface-based bufferization.