MLIR  17.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 (auto *defInit = dyn_cast<llvm::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 =
96  llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseAttr"))) {
97  return Attribute(defInit).getBaseAttr();
98  }
99  return *this;
100 }
101 
103  const auto *init = def->getValueInit("defaultValue");
104  return !getValueAsString(init).empty();
105 }
106 
107 StringRef Attribute::getDefaultValue() const {
108  const auto *init = def->getValueInit("defaultValue");
109  return getValueAsString(init);
110 }
111 
112 bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); }
113 
114 StringRef Attribute::getAttrDefName() const {
115  if (def->isAnonymous()) {
116  return getBaseAttr().def->getName();
117  }
118  return def->getName();
119 }
120 
121 StringRef Attribute::getDerivedCodeBody() const {
122  assert(isDerivedAttr() && "only derived attribute has 'body' field");
123  return def->getValueAsString("body");
124 }
125 
127  const llvm::RecordVal *record = def->getValue("dialect");
128  if (record && record->getValue()) {
129  if (DefInit *init = dyn_cast<DefInit>(record->getValue()))
130  return Dialect(init->getDef());
131  }
132  return Dialect(nullptr);
133 }
134 
135 const llvm::Record &Attribute::getDef() const { return *def; }
136 
137 ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
138  assert(def->isSubClassOf("ConstantAttr") &&
139  "must be subclass of TableGen 'ConstantAttr' class");
140 }
141 
143  return Attribute(def->getValueAsDef("attr"));
144 }
145 
147  return def->getValueAsString("value");
148 }
149 
150 EnumAttrCase::EnumAttrCase(const llvm::Record *record) : Attribute(record) {
151  assert(isSubClassOf("EnumAttrCaseInfo") &&
152  "must be subclass of TableGen 'EnumAttrInfo' class");
153 }
154 
155 EnumAttrCase::EnumAttrCase(const llvm::DefInit *init)
156  : EnumAttrCase(init->getDef()) {}
157 
158 StringRef EnumAttrCase::getSymbol() const {
159  return def->getValueAsString("symbol");
160 }
161 
162 StringRef EnumAttrCase::getStr() const { return def->getValueAsString("str"); }
163 
164 int64_t EnumAttrCase::getValue() const { return def->getValueAsInt("value"); }
165 
166 const llvm::Record &EnumAttrCase::getDef() const { return *def; }
167 
168 EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) {
169  assert(isSubClassOf("EnumAttrInfo") &&
170  "must be subclass of TableGen 'EnumAttr' class");
171 }
172 
173 EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {}
174 
175 EnumAttr::EnumAttr(const llvm::DefInit *init) : EnumAttr(init->getDef()) {}
176 
177 bool EnumAttr::classof(const Attribute *attr) {
178  return attr->isSubClassOf("EnumAttrInfo");
179 }
180 
181 bool EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); }
182 
183 StringRef EnumAttr::getEnumClassName() const {
184  return def->getValueAsString("className");
185 }
186 
187 StringRef EnumAttr::getCppNamespace() const {
188  return def->getValueAsString("cppNamespace");
189 }
190 
191 StringRef EnumAttr::getUnderlyingType() const {
192  return def->getValueAsString("underlyingType");
193 }
194 
196  return def->getValueAsString("underlyingToSymbolFnName");
197 }
198 
200  return def->getValueAsString("stringToSymbolFnName");
201 }
202 
204  return def->getValueAsString("symbolToStringFnName");
205 }
206 
208  return def->getValueAsString("symbolToStringFnRetType");
209 }
210 
211 StringRef EnumAttr::getMaxEnumValFnName() const {
212  return def->getValueAsString("maxEnumValFnName");
213 }
214 
215 std::vector<EnumAttrCase> EnumAttr::getAllCases() const {
216  const auto *inits = def->getValueAsListInit("enumerants");
217 
218  std::vector<EnumAttrCase> cases;
219  cases.reserve(inits->size());
220 
221  for (const llvm::Init *init : *inits) {
222  cases.emplace_back(cast<llvm::DefInit>(init));
223  }
224 
225  return cases;
226 }
227 
229  return def->getValueAsBit("genSpecializedAttr");
230 }
231 
232 llvm::Record *EnumAttr::getBaseAttrClass() const {
233  return def->getValueAsDef("baseAttrClass");
234 }
235 
237  return def->getValueAsString("specializedAttrClassName");
238 }
239 
241  return def->getValueAsBit("printBitEnumPrimaryGroups");
242 }
243 
244 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:41
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:102
StringRef getDefaultValue() const
Definition: Attribute.cpp:107
Attribute(const llvm::Record *record)
StringRef getAttrDefName() const
Definition: Attribute.cpp:114
StringRef getDerivedCodeBody() const
Definition: Attribute.cpp:121
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
const llvm::Record & getDef() const
Definition: Attribute.cpp:135
ConstantAttr(const llvm::DefInit *init)
Definition: Attribute.cpp:137
StringRef getConstantValue() const
Definition: Attribute.cpp:146
Attribute getAttribute() const
Definition: Attribute.cpp:142
const llvm::Record * def
Definition: Constraint.h:79
StringRef getSymbol() const
Definition: Attribute.cpp:158
StringRef getStr() const
Definition: Attribute.cpp:162
int64_t getValue() const
Definition: Attribute.cpp:164
EnumAttrCase(const llvm::Record *record)
Definition: Attribute.cpp:150
const llvm::Record & getDef() const
Definition: Attribute.cpp:166
llvm::Record * getBaseAttrClass() const
Definition: Attribute.cpp:232
StringRef getStringToSymbolFnName() const
Definition: Attribute.cpp:199
StringRef getSpecializedAttrClassName() const
Definition: Attribute.cpp:236
bool isBitEnum() const
Definition: Attribute.cpp:181
StringRef getEnumClassName() const
Definition: Attribute.cpp:183
StringRef getSymbolToStringFnRetType() const
Definition: Attribute.cpp:207
bool printBitEnumPrimaryGroups() const
Definition: Attribute.cpp:240
StringRef getCppNamespace() const
Definition: Attribute.cpp:187
StringRef getSymbolToStringFnName() const
Definition: Attribute.cpp:203
static bool classof(const Attribute *attr)
Definition: Attribute.cpp:177
bool genSpecializedAttr() const
Definition: Attribute.cpp:228
EnumAttr(const llvm::Record *record)
Definition: Attribute.cpp:168
StringRef getUnderlyingToSymbolFnName() const
Definition: Attribute.cpp:195
StringRef getUnderlyingType() const
Definition: Attribute.cpp:191
StringRef getMaxEnumValFnName() const
Definition: Attribute.cpp:211
std::vector< EnumAttrCase > getAllCases() const
Definition: Attribute.cpp:215
const char * inferTypeOpInterface
Definition: Attribute.cpp:244
Include the generated interface declarations.