MLIR 24.0.0git
BufferizationDialect.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
18
19using namespace mlir;
20using namespace mlir::bufferization;
21
22#include "mlir/Dialect/Bufferization/IR/BufferizationOpsDialect.cpp.inc"
23
24//===----------------------------------------------------------------------===//
25// Bufferization Dialect Interfaces
26//===----------------------------------------------------------------------===//
27
28namespace {
29struct BufferizationInlinerInterface : public DialectInlinerInterface {
30 using DialectInlinerInterface::DialectInlinerInterface;
31
32 /// Operations in Bufferization dialect are always legal to inline.
33 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
34 return true;
35 }
36};
37
38template <typename Tensor>
39struct BuiltinTensorExternalModel
40 : TensorLikeType::ExternalModel<BuiltinTensorExternalModel<Tensor>,
41 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);
47 if (!memrefType)
48 return emitError() << "expected a shaped buffer type";
49
50 if (tensorType.getShape() != memrefType.getShape())
51 return emitError() << "shapes do not match";
52
53 if (tensorType.getElementType() != memrefType.getElementType())
54 return emitError() << "element types do not match";
55
56 return mlir::success();
57 }
58};
59
60template <typename MemRef>
61struct BuiltinMemRefExternalModel
62 : BufferLikeType::ExternalModel<BuiltinMemRefExternalModel<MemRef>,
63 MemRef> {};
64} // namespace
65
66//===----------------------------------------------------------------------===//
67// Bufferization Dialect
68//===----------------------------------------------------------------------===//
69
70void mlir::bufferization::BufferizationDialect::initialize() {
71 addInterfaces<BufferizationInlinerInterface>();
72
73 // Note: Unlike with other external models, declaring bufferization's
74 // "promised interfaces" in builtins for TensorLike and BufferLike type
75 // interfaces is not possible (due to builtins being independent of
76 // bufferization). Thus, the compromise is to attach these interfaces directly
77 // during dialect initialization.
78 RankedTensorType::attachInterface<
79 BuiltinTensorExternalModel<RankedTensorType>>(*getContext());
80 UnrankedTensorType::attachInterface<
81 BuiltinTensorExternalModel<UnrankedTensorType>>(*getContext());
82 MemRefType::attachInterface<BuiltinMemRefExternalModel<MemRefType>>(
83 *getContext());
84 UnrankedMemRefType::attachInterface<
85 BuiltinMemRefExternalModel<UnrankedMemRefType>>(*getContext());
86}
87
88LogicalResult BufferizationDialect::verifyRegionArgAttribute(
89 Operation *op, unsigned /*regionIndex*/, unsigned argIndex,
90 NamedAttribute attr) {
91 if (attr.getName() == kWritableAttrName) {
92 if (!llvm::isa<BoolAttr>(attr.getValue())) {
93 return op->emitError() << "'" << kWritableAttrName
94 << "' is expected to be a boolean attribute";
95 }
96 if (!isa<FunctionOpInterface>(op))
97 return op->emitError() << "expected '" << kWritableAttrName
98 << "' to be used on function-like operations";
99 if (cast<FunctionOpInterface>(op).isExternal())
100 return op->emitError() << "'" << kWritableAttrName
101 << "' is invalid on external functions";
102 return success();
103 }
104 if (attr.getName() == kBufferAccessAttrName) {
105 if (!llvm::isa<StringAttr>(attr.getValue())) {
106 return op->emitError() << "'" << kBufferAccessAttrName
107 << "' is expected to be a string attribute";
108 }
109 StringRef str = llvm::cast<StringAttr>(attr.getValue()).getValue();
110 if (str != "none" && str != "read" && str != "write" && str != "read-write")
111 return op->emitError()
112 << "invalid value for '" << kBufferAccessAttrName << "'";
113 if (!isa<FunctionOpInterface>(op))
114 return op->emitError() << "expected '" << kBufferAccessAttrName
115 << "' to be used on function-like operations";
116 return success();
117 }
118 if (attr.getName() == kBufferLayoutAttrName) {
119 if (!llvm::isa<MemRefLayoutAttrInterface>(attr.getValue())) {
120 return op->emitError() << "'" << kBufferLayoutAttrName
121 << "' is expected to be a memref layout attribute";
122 }
123 if (!isa<FunctionOpInterface>(op))
124 return op->emitError() << "expected '" << kBufferLayoutAttrName
125 << "' to be used on function-like operations";
126 return success();
127 }
128 return op->emitError() << "attribute '" << kBufferLayoutAttrName
129 << "' not supported as a region arg attribute by the "
130 "bufferization dialect";
131}
132
133LogicalResult
134BufferizationDialect::verifyOperationAttribute(Operation *op,
135 NamedAttribute attr) {
136 using bufferization::BufferizableOpInterface;
137
138 if (attr.getName() == kManualDeallocation) {
141 return op->emitOpError("attribute '")
142 << kManualDeallocation
143 << "' can be used only on ops that have an allocation and/or free "
144 "side effect";
145 return success();
146 }
147
148 return op->emitError()
149 << "attribute '" << attr.getName()
150 << "' not supported as an op attribute by the bufferization dialect";
151}
return success()
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.
b getContext())
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:181
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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.