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