22#include "mlir/Dialect/Bufferization/IR/BufferizationOpsDialect.cpp.inc"
33 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
38template <
typename Tensor>
39struct BuiltinTensorExternalModel
40 : TensorLikeType::ExternalModel<BuiltinTensorExternalModel<Tensor>,
43 mlir::Type tensor,
const BufferizationOptions &
options,
44 llvm::function_ref<mlir::InFlightDiagnostic()>
emitError)
const {
45 auto tensorType = cast<TensorType>(tensor);
46 auto memSpace =
options.defaultMemorySpaceFn(tensorType);
47 if (!memSpace.has_value())
48 return emitError() <<
"could not infer memory space";
50 return cast<BufferLikeType>(
54 mlir::LogicalResult verifyCompatibleBufferType(
55 mlir::Type tensor, BufferLikeType bufferType,
56 llvm::function_ref<mlir::InFlightDiagnostic()>
emitError)
const {
57 auto tensorType = cast<ShapedType>(tensor);
58 auto memrefType = cast<ShapedType>(bufferType);
60 if (tensorType.getShape() != memrefType.getShape())
61 return emitError() <<
"shapes do not match";
63 if (tensorType.getElementType() != memrefType.getElementType())
64 return emitError() <<
"element types do not match";
66 return mlir::success();
70template <
typename MemRef>
71struct BuiltinMemRefExternalModel
72 : BufferLikeType::ExternalModel<BuiltinMemRefExternalModel<MemRef>,
80void mlir::bufferization::BufferizationDialect::initialize() {
83#include "mlir/Dialect/Bufferization/IR/BufferizationOps.cpp.inc"
85 addInterfaces<BufferizationInlinerInterface>();
92 RankedTensorType::attachInterface<
93 BuiltinTensorExternalModel<RankedTensorType>>(*
getContext());
94 UnrankedTensorType::attachInterface<
95 BuiltinTensorExternalModel<UnrankedTensorType>>(*
getContext());
96 MemRefType::attachInterface<BuiltinMemRefExternalModel<MemRefType>>(
98 UnrankedMemRefType::attachInterface<
99 BuiltinMemRefExternalModel<UnrankedMemRefType>>(*
getContext());
102LogicalResult BufferizationDialect::verifyRegionArgAttribute(
103 Operation *op,
unsigned ,
unsigned argIndex,
105 if (attr.
getName() == kWritableAttrName) {
106 if (!llvm::isa<BoolAttr>(attr.
getValue())) {
107 return op->
emitError() <<
"'" << kWritableAttrName
108 <<
"' is expected to be a boolean attribute";
110 if (!isa<FunctionOpInterface>(op))
111 return op->
emitError() <<
"expected '" << kWritableAttrName
112 <<
"' to be used on function-like operations";
113 if (cast<FunctionOpInterface>(op).isExternal())
114 return op->
emitError() <<
"'" << kWritableAttrName
115 <<
"' is invalid on external functions";
118 if (attr.
getName() == kBufferAccessAttrName) {
119 if (!llvm::isa<StringAttr>(attr.
getValue())) {
120 return op->
emitError() <<
"'" << kBufferAccessAttrName
121 <<
"' is expected to be a string attribute";
123 StringRef str = llvm::cast<StringAttr>(attr.
getValue()).getValue();
124 if (str !=
"none" && str !=
"read" && str !=
"write" && str !=
"read-write")
126 <<
"invalid value for '" << kBufferAccessAttrName <<
"'";
127 if (!isa<FunctionOpInterface>(op))
128 return op->
emitError() <<
"expected '" << kBufferAccessAttrName
129 <<
"' to be used on function-like operations";
132 if (attr.
getName() == kBufferLayoutAttrName) {
133 if (!llvm::isa<MemRefLayoutAttrInterface>(attr.
getValue())) {
134 return op->
emitError() <<
"'" << kBufferLayoutAttrName
135 <<
"' is expected to be a memref layout attribute";
137 if (!isa<FunctionOpInterface>(op))
138 return op->
emitError() <<
"expected '" << kBufferLayoutAttrName
139 <<
"' to be used on function-like operations";
142 return op->
emitError() <<
"attribute '" << kBufferLayoutAttrName
143 <<
"' not supported as a region arg attribute by the "
144 "bufferization dialect";
148BufferizationDialect::verifyOperationAttribute(
Operation *op,
150 using bufferization::BufferizableOpInterface;
152 if (attr.
getName() == kManualDeallocation) {
156 << kManualDeallocation
157 <<
"' can be used only on ops that have an allocation and/or free "
163 <<
"attribute '" << attr.
getName()
164 <<
"' not supported as an op attribute by the bufferization dialect";
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
static RankedTensorType getBufferType(const SparseTensorType &stt, bool needTmpCOO)
This is the interface that must be implemented by the dialects of operations to be inlined.
DialectInlinerInterface(Dialect *dialect)
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.
MemRefType getMemRefType(T &&t)
Convenience method to abbreviate casting getType().
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
bool hasEffect(Operation *op)
Returns "true" if op has an effect of type EffectTy.