1 | //===- Attribute.cpp - Attribute wrapper class ----------------------------===// |
---|---|
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 | // Attribute wrapper to simplify using TableGen Record defining a MLIR |
10 | // Attribute. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "mlir/TableGen/Format.h" |
15 | #include "mlir/TableGen/Operator.h" |
16 | #include "llvm/TableGen/Record.h" |
17 | |
18 | using namespace mlir; |
19 | using namespace mlir::tblgen; |
20 | |
21 | using llvm::DefInit; |
22 | using llvm::Init; |
23 | using llvm::Record; |
24 | using llvm::StringInit; |
25 | |
26 | // Returns the initializer's value as string if the given TableGen initializer |
27 | // is a code or string initializer. Returns the empty StringRef otherwise. |
28 | static StringRef getValueAsString(const Init *init) { |
29 | if (const auto *str = dyn_cast<StringInit>(Val: init)) |
30 | return str->getValue().trim(); |
31 | return {}; |
32 | } |
33 | |
34 | bool AttrConstraint::isSubClassOf(StringRef className) const { |
35 | return def->isSubClassOf(Name: className); |
36 | } |
37 | |
38 | Attribute::Attribute(const Record *record) : AttrConstraint(record) { |
39 | assert(record->isSubClassOf("Attr") && |
40 | "must be subclass of TableGen 'Attr' class"); |
41 | } |
42 | |
43 | Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {} |
44 | |
45 | bool Attribute::isDerivedAttr() const { return isSubClassOf(className: "DerivedAttr"); } |
46 | |
47 | bool Attribute::isTypeAttr() const { return isSubClassOf(className: "TypeAttrBase"); } |
48 | |
49 | bool Attribute::isSymbolRefAttr() const { |
50 | StringRef defName = def->getName(); |
51 | if (defName == "SymbolRefAttr"|| defName == "FlatSymbolRefAttr") |
52 | return true; |
53 | return isSubClassOf(className: "SymbolRefAttr") || isSubClassOf(className: "FlatSymbolRefAttr"); |
54 | } |
55 | |
56 | bool Attribute::isEnumAttr() const { return isSubClassOf(className: "EnumAttrInfo"); } |
57 | |
58 | StringRef Attribute::getStorageType() const { |
59 | const auto *init = def->getValueInit(FieldName: "storageType"); |
60 | auto type = getValueAsString(init); |
61 | if (type.empty()) |
62 | return "::mlir::Attribute"; |
63 | return type; |
64 | } |
65 | |
66 | StringRef Attribute::getReturnType() const { |
67 | const auto *init = def->getValueInit(FieldName: "returnType"); |
68 | return getValueAsString(init); |
69 | } |
70 | |
71 | // Return the type constraint corresponding to the type of this attribute, or |
72 | // std::nullopt if this is not a TypedAttr. |
73 | std::optional<Type> Attribute::getValueType() const { |
74 | if (const auto *defInit = dyn_cast<DefInit>(Val: def->getValueInit(FieldName: "valueType"))) |
75 | return Type(defInit->getDef()); |
76 | return std::nullopt; |
77 | } |
78 | |
79 | StringRef Attribute::getConvertFromStorageCall() const { |
80 | const auto *init = def->getValueInit(FieldName: "convertFromStorage"); |
81 | return getValueAsString(init); |
82 | } |
83 | |
84 | bool Attribute::isConstBuildable() const { |
85 | const auto *init = def->getValueInit(FieldName: "constBuilderCall"); |
86 | return !getValueAsString(init).empty(); |
87 | } |
88 | |
89 | StringRef Attribute::getConstBuilderTemplate() const { |
90 | const auto *init = def->getValueInit(FieldName: "constBuilderCall"); |
91 | return getValueAsString(init); |
92 | } |
93 | |
94 | Attribute Attribute::getBaseAttr() const { |
95 | if (const auto *defInit = dyn_cast<DefInit>(Val: def->getValueInit(FieldName: "baseAttr"))) { |
96 | return Attribute(defInit).getBaseAttr(); |
97 | } |
98 | return *this; |
99 | } |
100 | |
101 | bool Attribute::hasDefaultValue() const { |
102 | const auto *init = def->getValueInit(FieldName: "defaultValue"); |
103 | return !getValueAsString(init).empty(); |
104 | } |
105 | |
106 | StringRef Attribute::getDefaultValue() const { |
107 | const auto *init = def->getValueInit(FieldName: "defaultValue"); |
108 | return getValueAsString(init); |
109 | } |
110 | |
111 | bool Attribute::isOptional() const { return def->getValueAsBit(FieldName: "isOptional"); } |
112 | |
113 | StringRef Attribute::getAttrDefName() const { |
114 | if (def->isAnonymous()) { |
115 | return getBaseAttr().def->getName(); |
116 | } |
117 | return def->getName(); |
118 | } |
119 | |
120 | StringRef Attribute::getDerivedCodeBody() const { |
121 | assert(isDerivedAttr() && "only derived attribute has 'body' field"); |
122 | return def->getValueAsString(FieldName: "body"); |
123 | } |
124 | |
125 | Dialect Attribute::getDialect() const { |
126 | const llvm::RecordVal *record = def->getValue(Name: "dialect"); |
127 | if (record && record->getValue()) { |
128 | if (const DefInit *init = dyn_cast<DefInit>(Val: record->getValue())) |
129 | return Dialect(init->getDef()); |
130 | } |
131 | return Dialect(nullptr); |
132 | } |
133 | |
134 | const Record &Attribute::getDef() const { return *def; } |
135 | |
136 | ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) { |
137 | assert(def->isSubClassOf("ConstantAttr") && |
138 | "must be subclass of TableGen 'ConstantAttr' class"); |
139 | } |
140 | |
141 | Attribute ConstantAttr::getAttribute() const { |
142 | return Attribute(def->getValueAsDef(FieldName: "attr")); |
143 | } |
144 | |
145 | StringRef ConstantAttr::getConstantValue() const { |
146 | return def->getValueAsString(FieldName: "value"); |
147 | } |
148 | |
149 | const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface"; |
150 |
Definitions
- getValueAsString
- isSubClassOf
- Attribute
- Attribute
- isDerivedAttr
- isTypeAttr
- isSymbolRefAttr
- isEnumAttr
- getStorageType
- getReturnType
- getValueType
- getConvertFromStorageCall
- isConstBuildable
- getConstBuilderTemplate
- getBaseAttr
- hasDefaultValue
- getDefaultValue
- isOptional
- getAttrDefName
- getDerivedCodeBody
- getDialect
- getDef
- ConstantAttr
- getAttribute
- getConstantValue
Learn to use CMake with our Intro Training
Find out more