MLIR  20.0.0git
Attribute.cpp
Go to the documentation of this file.
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>(init))
30  return str->getValue().trim();
31  return {};
32 }
33 
34 bool AttrConstraint::isSubClassOf(StringRef className) const {
35  return def->isSubClassOf(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("DerivedAttr"); }
46 
47 bool Attribute::isTypeAttr() const { return isSubClassOf("TypeAttrBase"); }
48 
50  StringRef defName = def->getName();
51  if (defName == "SymbolRefAttr" || defName == "FlatSymbolRefAttr")
52  return true;
53  return isSubClassOf("SymbolRefAttr") || isSubClassOf("FlatSymbolRefAttr");
54 }
55 
56 bool Attribute::isEnumAttr() const { return isSubClassOf("EnumAttrInfo"); }
57 
58 StringRef Attribute::getStorageType() const {
59  const auto *init = def->getValueInit("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("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>(def->getValueInit("valueType")))
75  return Type(defInit->getDef());
76  return std::nullopt;
77 }
78 
80  const auto *init = def->getValueInit("convertFromStorage");
81  return getValueAsString(init);
82 }
83 
85  const auto *init = def->getValueInit("constBuilderCall");
86  return !getValueAsString(init).empty();
87 }
88 
90  const auto *init = def->getValueInit("constBuilderCall");
91  return getValueAsString(init);
92 }
93 
95  if (const auto *defInit = dyn_cast<DefInit>(def->getValueInit("baseAttr"))) {
96  return Attribute(defInit).getBaseAttr();
97  }
98  return *this;
99 }
100 
102  const auto *init = def->getValueInit("defaultValue");
103  return !getValueAsString(init).empty();
104 }
105 
106 StringRef Attribute::getDefaultValue() const {
107  const auto *init = def->getValueInit("defaultValue");
108  return getValueAsString(init);
109 }
110 
111 bool Attribute::isOptional() const { return def->getValueAsBit("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("body");
123 }
124 
126  const llvm::RecordVal *record = def->getValue("dialect");
127  if (record && record->getValue()) {
128  if (const DefInit *init = dyn_cast<DefInit>(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 
142  return Attribute(def->getValueAsDef("attr"));
143 }
144 
146  return def->getValueAsString("value");
147 }
148 
149 EnumAttrCase::EnumAttrCase(const Record *record) : Attribute(record) {
150  assert(isSubClassOf("EnumAttrCaseInfo") &&
151  "must be subclass of TableGen 'EnumAttrInfo' class");
152 }
153 
154 EnumAttrCase::EnumAttrCase(const DefInit *init)
155  : EnumAttrCase(init->getDef()) {}
156 
157 StringRef EnumAttrCase::getSymbol() const {
158  return def->getValueAsString("symbol");
159 }
160 
161 StringRef EnumAttrCase::getStr() const { return def->getValueAsString("str"); }
162 
163 int64_t EnumAttrCase::getValue() const { return def->getValueAsInt("value"); }
164 
165 const Record &EnumAttrCase::getDef() const { return *def; }
166 
167 EnumAttr::EnumAttr(const Record *record) : Attribute(record) {
168  assert(isSubClassOf("EnumAttrInfo") &&
169  "must be subclass of TableGen 'EnumAttr' class");
170 }
171 
172 EnumAttr::EnumAttr(const Record &record) : Attribute(&record) {}
173 
174 EnumAttr::EnumAttr(const DefInit *init) : EnumAttr(init->getDef()) {}
175 
176 bool EnumAttr::classof(const Attribute *attr) {
177  return attr->isSubClassOf("EnumAttrInfo");
178 }
179 
180 bool EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); }
181 
182 StringRef EnumAttr::getEnumClassName() const {
183  return def->getValueAsString("className");
184 }
185 
186 StringRef EnumAttr::getCppNamespace() const {
187  return def->getValueAsString("cppNamespace");
188 }
189 
190 StringRef EnumAttr::getUnderlyingType() const {
191  return def->getValueAsString("underlyingType");
192 }
193 
195  return def->getValueAsString("underlyingToSymbolFnName");
196 }
197 
199  return def->getValueAsString("stringToSymbolFnName");
200 }
201 
203  return def->getValueAsString("symbolToStringFnName");
204 }
205 
207  return def->getValueAsString("symbolToStringFnRetType");
208 }
209 
210 StringRef EnumAttr::getMaxEnumValFnName() const {
211  return def->getValueAsString("maxEnumValFnName");
212 }
213 
214 std::vector<EnumAttrCase> EnumAttr::getAllCases() const {
215  const auto *inits = def->getValueAsListInit("enumerants");
216 
217  std::vector<EnumAttrCase> cases;
218  cases.reserve(inits->size());
219 
220  for (const Init *init : *inits) {
221  cases.emplace_back(cast<DefInit>(init));
222  }
223 
224  return cases;
225 }
226 
228  return def->getValueAsBit("genSpecializedAttr");
229 }
230 
231 const Record *EnumAttr::getBaseAttrClass() const {
232  return def->getValueAsDef("baseAttrClass");
233 }
234 
236  return def->getValueAsString("specializedAttrClassName");
237 }
238 
240  return def->getValueAsBit("printBitEnumPrimaryGroups");
241 }
242 
243 const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface";
static StringRef getValueAsString(const Init *init)
Definition: Attribute.cpp:28
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
bool isSubClassOf(StringRef className) const
Definition: Attribute.cpp:34
StringRef getConstBuilderTemplate() const
Definition: Attribute.cpp:89
bool isConstBuildable() const
Definition: Attribute.cpp:84
StringRef getConvertFromStorageCall() const
Definition: Attribute.cpp:79
StringRef getStorageType() const
Definition: Attribute.cpp:58
bool hasDefaultValue() const
Definition: Attribute.cpp:101
StringRef getDefaultValue() const
Definition: Attribute.cpp:106
Attribute(const llvm::Record *record)
const llvm::Record & getDef() const
Definition: Attribute.cpp:134
StringRef getAttrDefName() const
Definition: Attribute.cpp:113
StringRef getDerivedCodeBody() const
Definition: Attribute.cpp:120
StringRef getReturnType() const
Definition: Attribute.cpp:66
bool isDerivedAttr() const
Definition: Attribute.cpp:45
std::optional< Type > getValueType() const
Definition: Attribute.cpp:73
bool isSymbolRefAttr() const
Definition: Attribute.cpp:49
Dialect getDialect() const
Attribute getBaseAttr() const
Definition: Attribute.cpp:94
bool isEnumAttr() const
Definition: Attribute.cpp:56
bool isTypeAttr() const
Definition: Attribute.cpp:47
ConstantAttr(const llvm::DefInit *init)
Definition: Attribute.cpp:136
StringRef getConstantValue() const
Definition: Attribute.cpp:145
Attribute getAttribute() const
Definition: Attribute.cpp:141
const llvm::Record * def
Definition: Constraint.h:83
StringRef getSymbol() const
Definition: Attribute.cpp:157
const llvm::Record & getDef() const
Definition: Attribute.cpp:165
StringRef getStr() const
Definition: Attribute.cpp:161
int64_t getValue() const
Definition: Attribute.cpp:163
EnumAttrCase(const llvm::Record *record)
StringRef getStringToSymbolFnName() const
Definition: Attribute.cpp:198
StringRef getSpecializedAttrClassName() const
Definition: Attribute.cpp:235
bool isBitEnum() const
Definition: Attribute.cpp:180
StringRef getEnumClassName() const
Definition: Attribute.cpp:182
StringRef getSymbolToStringFnRetType() const
Definition: Attribute.cpp:206
bool printBitEnumPrimaryGroups() const
Definition: Attribute.cpp:239
EnumAttr(const llvm::Record *record)
StringRef getCppNamespace() const
Definition: Attribute.cpp:186
StringRef getSymbolToStringFnName() const
Definition: Attribute.cpp:202
static bool classof(const Attribute *attr)
Definition: Attribute.cpp:176
bool genSpecializedAttr() const
Definition: Attribute.cpp:227
StringRef getUnderlyingToSymbolFnName() const
Definition: Attribute.cpp:194
StringRef getUnderlyingType() const
Definition: Attribute.cpp:190
StringRef getMaxEnumValFnName() const
Definition: Attribute.cpp:210
std::vector< EnumAttrCase > getAllCases() const
Definition: Attribute.cpp:214
const llvm::Record * getBaseAttrClass() const
Definition: Attribute.cpp:231
const char * inferTypeOpInterface
Definition: Attribute.cpp:243
Include the generated interface declarations.