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/Bufferization.h"
11 #include "mlir/Dialect/MemRef/IR/MemRef.h"
12 #include "mlir/Dialect/Tensor/IR/Tensor.h"
13 #include "mlir/IR/FunctionInterfaces.h"
14 #include "mlir/Transforms/InliningUtils.h"
15 
16 using namespace mlir;
17 using namespace mlir::bufferization;
18 
19 #include "mlir/Dialect/Bufferization/IR/BufferizationOpsDialect.cpp.inc"
20 
21 /// Attribute name used to mark function arguments who's buffers can be written
22 /// to during One-Shot Module Bufferize.
23 constexpr const ::llvm::StringLiteral BufferizationDialect::kWritableAttrName;
24 
25 /// Attribute name used to mark the bufferization layout for region arguments
26 /// during One-Shot Module Bufferize.
27 constexpr const ::llvm::StringLiteral
28     BufferizationDialect::kBufferLayoutAttrName;
29 
30 //===----------------------------------------------------------------------===//
31 // Bufferization Dialect Interfaces
32 //===----------------------------------------------------------------------===//
33 
34 namespace {
35 struct BufferizationInlinerInterface : public DialectInlinerInterface {
36   using DialectInlinerInterface::DialectInlinerInterface;
37 
38   /// Operations in Bufferization dialect are always legal to inline.
39   bool isLegalToInline(Operation *, Region *, bool,
40                        BlockAndValueMapping &) const final {
41     return true;
42   }
43 };
44 } // namespace
45 
46 //===----------------------------------------------------------------------===//
47 // Bufferization Dialect
48 //===----------------------------------------------------------------------===//
49 
50 void mlir::bufferization::BufferizationDialect::initialize() {
51   addOperations<
52 #define GET_OP_LIST
53 #include "mlir/Dialect/Bufferization/IR/BufferizationOps.cpp.inc"
54       >();
55   addInterfaces<BufferizationInlinerInterface>();
56 }
57 
58 LogicalResult
59 BufferizationDialect::verifyOperationAttribute(Operation *op,
60                                                NamedAttribute attr) {
61   using bufferization::BufferizableOpInterface;
62 
63   if (attr.getName() == kWritableAttrName) {
64     if (!attr.getValue().isa<BoolAttr>()) {
65       return op->emitError() << "'" << kWritableAttrName
66                              << "' is expected to be a boolean attribute";
67     }
68     if (!isa<FunctionOpInterface>(op))
69       return op->emitError() << "expected " << attr.getName()
70                              << " to be used on function-like operations";
71     return success();
72   }
73   if (attr.getName() == kBufferLayoutAttrName) {
74     if (!attr.getValue().isa<AffineMapAttr>()) {
75       return op->emitError() << "'" << kBufferLayoutAttrName
76                              << "' is expected to be a affine map attribute";
77     }
78     if (!isa<FunctionOpInterface>(op))
79       return op->emitError() << "expected " << attr.getName()
80                              << " to be used on function-like operations";
81     return success();
82   }
83 
84   return op->emitError() << "attribute '" << attr.getName()
85                          << "' not supported by the bufferization dialect";
86 }
87