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
9#include "mlir/Dialect/Affine/IR/AffineOps.h"
10#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
11#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
12#include "mlir/Dialect/Bufferization/IR/BufferizationTypeInterfaces.h"
13#include "mlir/Dialect/MemRef/IR/MemRef.h"
14#include "mlir/Dialect/Tensor/IR/Tensor.h"
15#include "mlir/IR/BuiltinTypes.h"
16#include "mlir/Interfaces/FunctionInterfaces.h"
17#include "mlir/Transforms/InliningUtils.h"
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>(Val&: tensor);
46 auto memrefType = dyn_cast<ShapedType>(Val&: 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 addOperations<
72#define GET_OP_LIST
73#include "mlir/Dialect/Bufferization/IR/BufferizationOps.cpp.inc"
74 >();
75 addInterfaces<BufferizationInlinerInterface>();
76
77 // Note: Unlike with other external models, declaring bufferization's
78 // "promised interfaces" in builtins for TensorLike and BufferLike type
79 // interfaces is not possible (due to builtins being independent of
80 // bufferization). Thus, the compromise is to attach these interfaces directly
81 // during dialect initialization.
82 RankedTensorType::attachInterface<
83 BuiltinTensorExternalModel<RankedTensorType>>(context&: *getContext());
84 UnrankedTensorType::attachInterface<
85 BuiltinTensorExternalModel<UnrankedTensorType>>(context&: *getContext());
86 MemRefType::attachInterface<BuiltinMemRefExternalModel<MemRefType>>(
87 context&: *getContext());
88 UnrankedMemRefType::attachInterface<
89 BuiltinMemRefExternalModel<UnrankedMemRefType>>(context&: *getContext());
90}
91
92LogicalResult BufferizationDialect::verifyRegionArgAttribute(
93 Operation *op, unsigned /*regionIndex*/, unsigned argIndex,
94 NamedAttribute attr) {
95 if (attr.getName() == kWritableAttrName) {
96 if (!llvm::isa<BoolAttr>(Val: attr.getValue())) {
97 return op->emitError() << "'" << kWritableAttrName
98 << "' is expected to be a boolean attribute";
99 }
100 if (!isa<FunctionOpInterface>(Val: op))
101 return op->emitError() << "expected '" << kWritableAttrName
102 << "' to be used on function-like operations";
103 if (cast<FunctionOpInterface>(Val: op).isExternal())
104 return op->emitError() << "'" << kWritableAttrName
105 << "' is invalid on external functions";
106 return success();
107 }
108 if (attr.getName() == kBufferAccessAttrName) {
109 if (!llvm::isa<StringAttr>(Val: attr.getValue())) {
110 return op->emitError() << "'" << kBufferAccessAttrName
111 << "' is expected to be a string attribute";
112 }
113 StringRef str = llvm::cast<StringAttr>(Val: attr.getValue()).getValue();
114 if (str != "none" && str != "read" && str != "write" && str != "read-write")
115 return op->emitError()
116 << "invalid value for '" << kBufferAccessAttrName << "'";
117 if (!isa<FunctionOpInterface>(Val: op))
118 return op->emitError() << "expected '" << kBufferAccessAttrName
119 << "' to be used on function-like operations";
120 return success();
121 }
122 if (attr.getName() == kBufferLayoutAttrName) {
123 if (!llvm::isa<MemRefLayoutAttrInterface>(Val: attr.getValue())) {
124 return op->emitError() << "'" << kBufferLayoutAttrName
125 << "' is expected to be a memref layout attribute";
126 }
127 if (!isa<FunctionOpInterface>(Val: op))
128 return op->emitError() << "expected '" << kBufferLayoutAttrName
129 << "' to be used on function-like operations";
130 return success();
131 }
132 return op->emitError() << "attribute '" << kBufferLayoutAttrName
133 << "' not supported as a region arg attribute by the "
134 "bufferization dialect";
135}
136
137LogicalResult
138BufferizationDialect::verifyOperationAttribute(Operation *op,
139 NamedAttribute attr) {
140 using bufferization::BufferizableOpInterface;
141
142 if (attr.getName() == kManualDeallocation) {
143 if (!mlir::hasEffect<MemoryEffects::Allocate>(op) &&
144 !mlir::hasEffect<MemoryEffects::Free>(op))
145 return op->emitOpError(message: "attribute '")
146 << kManualDeallocation
147 << "' can be used only on ops that have an allocation and/or free "
148 "side effect";
149 return success();
150 }
151
152 return op->emitError()
153 << "attribute '" << attr.getName()
154 << "' not supported as an op attribute by the bufferization dialect";
155}
156