22#include "mlir/Dialect/Bufferization/IR/BufferizationOpsDialect.cpp.inc"
29struct BufferizationInlinerInterface :
public DialectInlinerInterface {
30 using DialectInlinerInterface::DialectInlinerInterface;
33 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
38template <
typename Tensor>
39struct BuiltinTensorExternalModel
40 : TensorLikeType::ExternalModel<BuiltinTensorExternalModel<Tensor>,
42 mlir::LogicalResult verifyCompatibleBufferType(
43 mlir::Type tensor, BufferLikeType bufferType,
44 llvm::function_ref<mlir::InFlightDiagnostic()>
emitError)
const {
45 auto tensorType = cast<ShapedType>(tensor);
46 auto memrefType = dyn_cast<ShapedType>(bufferType);
48 return emitError() <<
"expected a shaped buffer type";
50 if (tensorType.getShape() != memrefType.getShape())
51 return emitError() <<
"shapes do not match";
53 if (tensorType.getElementType() != memrefType.getElementType())
54 return emitError() <<
"element types do not match";
56 return mlir::success();
60template <
typename MemRef>
61struct BuiltinMemRefExternalModel
62 : BufferLikeType::ExternalModel<BuiltinMemRefExternalModel<MemRef>,
70void mlir::bufferization::BufferizationDialect::initialize() {
73#include "mlir/Dialect/Bufferization/IR/BufferizationOps.cpp.inc"
75 addInterfaces<BufferizationInlinerInterface>();
82 RankedTensorType::attachInterface<
83 BuiltinTensorExternalModel<RankedTensorType>>(*
getContext());
84 UnrankedTensorType::attachInterface<
85 BuiltinTensorExternalModel<UnrankedTensorType>>(*
getContext());
86 MemRefType::attachInterface<BuiltinMemRefExternalModel<MemRefType>>(
88 UnrankedMemRefType::attachInterface<
89 BuiltinMemRefExternalModel<UnrankedMemRefType>>(*
getContext());
92LogicalResult BufferizationDialect::verifyRegionArgAttribute(
93 Operation *op,
unsigned ,
unsigned argIndex,
95 if (attr.
getName() == kWritableAttrName) {
96 if (!llvm::isa<BoolAttr>(attr.
getValue())) {
97 return op->
emitError() <<
"'" << kWritableAttrName
98 <<
"' is expected to be a boolean attribute";
100 if (!isa<FunctionOpInterface>(op))
101 return op->
emitError() <<
"expected '" << kWritableAttrName
102 <<
"' to be used on function-like operations";
103 if (cast<FunctionOpInterface>(op).isExternal())
104 return op->
emitError() <<
"'" << kWritableAttrName
105 <<
"' is invalid on external functions";
108 if (attr.
getName() == kBufferAccessAttrName) {
109 if (!llvm::isa<StringAttr>(attr.
getValue())) {
110 return op->
emitError() <<
"'" << kBufferAccessAttrName
111 <<
"' is expected to be a string attribute";
113 StringRef str = llvm::cast<StringAttr>(attr.
getValue()).getValue();
114 if (str !=
"none" && str !=
"read" && str !=
"write" && str !=
"read-write")
116 <<
"invalid value for '" << kBufferAccessAttrName <<
"'";
117 if (!isa<FunctionOpInterface>(op))
118 return op->
emitError() <<
"expected '" << kBufferAccessAttrName
119 <<
"' to be used on function-like operations";
122 if (attr.
getName() == kBufferLayoutAttrName) {
123 if (!llvm::isa<MemRefLayoutAttrInterface>(attr.
getValue())) {
124 return op->
emitError() <<
"'" << kBufferLayoutAttrName
125 <<
"' is expected to be a memref layout attribute";
127 if (!isa<FunctionOpInterface>(op))
128 return op->
emitError() <<
"expected '" << kBufferLayoutAttrName
129 <<
"' to be used on function-like operations";
132 return op->
emitError() <<
"attribute '" << kBufferLayoutAttrName
133 <<
"' not supported as a region arg attribute by the "
134 "bufferization dialect";
138BufferizationDialect::verifyOperationAttribute(
Operation *op,
140 using bufferization::BufferizableOpInterface;
142 if (attr.
getName() == kManualDeallocation) {
146 << kManualDeallocation
147 <<
"' can be used only on ops that have an allocation and/or free "
153 <<
"attribute '" << attr.
getName()
154 <<
"' 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.
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.
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.