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 49 bool Attribute::isSymbolRefAttr() const { 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 // None if this is not a TypedAttr. 73 llvm::Optional<Type> Attribute::getValueType() const { 74 if (auto *defInit = dyn_cast<llvm::DefInit>(def->getValueInit("valueType"))) 75 return Type(defInit->getDef()); 76 return llvm::None; 77 } 78 79 StringRef Attribute::getConvertFromStorageCall() const { 80 const auto *init = def->getValueInit("convertFromStorage"); 81 return getValueAsString(init); 82 } 83 84 bool Attribute::isConstBuildable() const { 85 const auto *init = def->getValueInit("constBuilderCall"); 86 return !getValueAsString(init).empty(); 87 } 88 89 StringRef Attribute::getConstBuilderTemplate() const { 90 const auto *init = def->getValueInit("constBuilderCall"); 91 return getValueAsString(init); 92 } 93 94 Attribute Attribute::getBaseAttr() const { 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 102 bool Attribute::hasDefaultValue() const { 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 126 Dialect Attribute::getDialect() const { 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 StringRef Attribute::getDescription() const { 136 return def->getValueAsString("description"); 137 } 138 139 ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) { 140 assert(def->isSubClassOf("ConstantAttr") && 141 "must be subclass of TableGen 'ConstantAttr' class"); 142 } 143 144 Attribute ConstantAttr::getAttribute() const { 145 return Attribute(def->getValueAsDef("attr")); 146 } 147 148 StringRef ConstantAttr::getConstantValue() const { 149 return def->getValueAsString("value"); 150 } 151 152 EnumAttrCase::EnumAttrCase(const llvm::Record *record) : Attribute(record) { 153 assert(isSubClassOf("EnumAttrCaseInfo") && 154 "must be subclass of TableGen 'EnumAttrInfo' class"); 155 } 156 157 EnumAttrCase::EnumAttrCase(const llvm::DefInit *init) 158 : EnumAttrCase(init->getDef()) {} 159 160 StringRef EnumAttrCase::getSymbol() const { 161 return def->getValueAsString("symbol"); 162 } 163 164 StringRef EnumAttrCase::getStr() const { return def->getValueAsString("str"); } 165 166 int64_t EnumAttrCase::getValue() const { return def->getValueAsInt("value"); } 167 168 const llvm::Record &EnumAttrCase::getDef() const { return *def; } 169 170 EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) { 171 assert(isSubClassOf("EnumAttrInfo") && 172 "must be subclass of TableGen 'EnumAttr' class"); 173 } 174 175 EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {} 176 177 EnumAttr::EnumAttr(const llvm::DefInit *init) : EnumAttr(init->getDef()) {} 178 179 bool EnumAttr::classof(const Attribute *attr) { 180 return attr->isSubClassOf("EnumAttrInfo"); 181 } 182 183 bool EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); } 184 185 StringRef EnumAttr::getEnumClassName() const { 186 return def->getValueAsString("className"); 187 } 188 189 StringRef EnumAttr::getCppNamespace() const { 190 return def->getValueAsString("cppNamespace"); 191 } 192 193 StringRef EnumAttr::getUnderlyingType() const { 194 return def->getValueAsString("underlyingType"); 195 } 196 197 StringRef EnumAttr::getUnderlyingToSymbolFnName() const { 198 return def->getValueAsString("underlyingToSymbolFnName"); 199 } 200 201 StringRef EnumAttr::getStringToSymbolFnName() const { 202 return def->getValueAsString("stringToSymbolFnName"); 203 } 204 205 StringRef EnumAttr::getSymbolToStringFnName() const { 206 return def->getValueAsString("symbolToStringFnName"); 207 } 208 209 StringRef EnumAttr::getSymbolToStringFnRetType() const { 210 return def->getValueAsString("symbolToStringFnRetType"); 211 } 212 213 StringRef EnumAttr::getMaxEnumValFnName() const { 214 return def->getValueAsString("maxEnumValFnName"); 215 } 216 217 std::vector<EnumAttrCase> EnumAttr::getAllCases() const { 218 const auto *inits = def->getValueAsListInit("enumerants"); 219 220 std::vector<EnumAttrCase> cases; 221 cases.reserve(inits->size()); 222 223 for (const llvm::Init *init : *inits) { 224 cases.emplace_back(cast<llvm::DefInit>(init)); 225 } 226 227 return cases; 228 } 229 230 bool EnumAttr::genSpecializedAttr() const { 231 return def->getValueAsBit("genSpecializedAttr"); 232 } 233 234 llvm::Record *EnumAttr::getBaseAttrClass() const { 235 return def->getValueAsDef("baseAttrClass"); 236 } 237 238 StringRef EnumAttr::getSpecializedAttrClassName() const { 239 return def->getValueAsString("specializedAttrClassName"); 240 } 241 242 StructFieldAttr::StructFieldAttr(const llvm::Record *record) : def(record) { 243 assert(def->isSubClassOf("StructFieldAttr") && 244 "must be subclass of TableGen 'StructFieldAttr' class"); 245 } 246 247 StructFieldAttr::StructFieldAttr(const llvm::Record &record) 248 : StructFieldAttr(&record) {} 249 250 StructFieldAttr::StructFieldAttr(const llvm::DefInit *init) 251 : StructFieldAttr(init->getDef()) {} 252 253 StringRef StructFieldAttr::getName() const { 254 return def->getValueAsString("name"); 255 } 256 257 Attribute StructFieldAttr::getType() const { 258 auto *init = def->getValueInit("type"); 259 return Attribute(cast<llvm::DefInit>(init)); 260 } 261 262 StructAttr::StructAttr(const llvm::Record *record) : Attribute(record) { 263 assert(isSubClassOf("StructAttr") && 264 "must be subclass of TableGen 'StructAttr' class"); 265 } 266 267 StructAttr::StructAttr(const llvm::DefInit *init) 268 : StructAttr(init->getDef()) {} 269 270 StringRef StructAttr::getStructClassName() const { 271 return def->getValueAsString("className"); 272 } 273 274 StringRef StructAttr::getCppNamespace() const { 275 Dialect dialect(def->getValueAsDef("dialect")); 276 return dialect.getCppNamespace(); 277 } 278 279 std::vector<StructFieldAttr> StructAttr::getAllFields() const { 280 std::vector<StructFieldAttr> attributes; 281 282 const auto *inits = def->getValueAsListInit("fields"); 283 attributes.reserve(inits->size()); 284 285 for (const llvm::Init *init : *inits) { 286 attributes.emplace_back(cast<llvm::DefInit>(init)); 287 } 288 289 return attributes; 290 } 291 292 const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface"; 293