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::CodeInit; 22 using llvm::DefInit; 23 using llvm::Init; 24 using llvm::Record; 25 using llvm::StringInit; 26 27 // Returns the initializer's value as string if the given TableGen initializer 28 // is a code or string initializer. Returns the empty StringRef otherwise. 29 static StringRef getValueAsString(const Init *init) { 30 if (const auto *code = dyn_cast<CodeInit>(init)) 31 return code->getValue().trim(); 32 if (const auto *str = dyn_cast<StringInit>(init)) 33 return str->getValue().trim(); 34 return {}; 35 } 36 37 AttrConstraint::AttrConstraint(const Record *record) 38 : Constraint(Constraint::CK_Attr, record) { 39 assert(isSubClassOf("AttrConstraint") && 40 "must be subclass of TableGen 'AttrConstraint' class"); 41 } 42 43 bool AttrConstraint::isSubClassOf(StringRef className) const { 44 return def->isSubClassOf(className); 45 } 46 47 Attribute::Attribute(const Record *record) : AttrConstraint(record) { 48 assert(record->isSubClassOf("Attr") && 49 "must be subclass of TableGen 'Attr' class"); 50 } 51 52 Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {} 53 54 bool Attribute::isDerivedAttr() const { return isSubClassOf("DerivedAttr"); } 55 56 bool Attribute::isTypeAttr() const { return isSubClassOf("TypeAttrBase"); } 57 58 bool Attribute::isEnumAttr() const { return isSubClassOf("EnumAttrInfo"); } 59 60 StringRef Attribute::getStorageType() const { 61 const auto *init = def->getValueInit("storageType"); 62 auto type = getValueAsString(init); 63 if (type.empty()) 64 return "Attribute"; 65 return type; 66 } 67 68 StringRef Attribute::getReturnType() const { 69 const auto *init = def->getValueInit("returnType"); 70 return getValueAsString(init); 71 } 72 73 // Return the type constraint corresponding to the type of this attribute, or 74 // None if this is not a TypedAttr. 75 llvm::Optional<Type> Attribute::getValueType() const { 76 if (auto *defInit = dyn_cast<llvm::DefInit>(def->getValueInit("valueType"))) 77 return Type(defInit->getDef()); 78 return llvm::None; 79 } 80 81 StringRef Attribute::getConvertFromStorageCall() const { 82 const auto *init = def->getValueInit("convertFromStorage"); 83 return getValueAsString(init); 84 } 85 86 bool Attribute::isConstBuildable() const { 87 const auto *init = def->getValueInit("constBuilderCall"); 88 return !getValueAsString(init).empty(); 89 } 90 91 StringRef Attribute::getConstBuilderTemplate() const { 92 const auto *init = def->getValueInit("constBuilderCall"); 93 return getValueAsString(init); 94 } 95 96 Attribute Attribute::getBaseAttr() const { 97 if (const auto *defInit = 98 llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseAttr"))) { 99 return Attribute(defInit).getBaseAttr(); 100 } 101 return *this; 102 } 103 104 bool Attribute::hasDefaultValue() const { 105 const auto *init = def->getValueInit("defaultValue"); 106 return !getValueAsString(init).empty(); 107 } 108 109 StringRef Attribute::getDefaultValue() const { 110 const auto *init = def->getValueInit("defaultValue"); 111 return getValueAsString(init); 112 } 113 114 bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); } 115 116 StringRef Attribute::getAttrDefName() const { 117 if (def->isAnonymous()) { 118 return getBaseAttr().def->getName(); 119 } 120 return def->getName(); 121 } 122 123 StringRef Attribute::getDerivedCodeBody() const { 124 assert(isDerivedAttr() && "only derived attribute has 'body' field"); 125 return def->getValueAsString("body"); 126 } 127 128 Dialect Attribute::getDialect() const { 129 return Dialect(def->getValueAsDef("dialect")); 130 } 131 132 ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) { 133 assert(def->isSubClassOf("ConstantAttr") && 134 "must be subclass of TableGen 'ConstantAttr' class"); 135 } 136 137 Attribute ConstantAttr::getAttribute() const { 138 return Attribute(def->getValueAsDef("attr")); 139 } 140 141 StringRef ConstantAttr::getConstantValue() const { 142 return def->getValueAsString("value"); 143 } 144 145 EnumAttrCase::EnumAttrCase(const llvm::Record *record) : Attribute(record) { 146 assert(isSubClassOf("EnumAttrCaseInfo") && 147 "must be subclass of TableGen 'EnumAttrInfo' class"); 148 } 149 150 EnumAttrCase::EnumAttrCase(const llvm::DefInit *init) 151 : EnumAttrCase(init->getDef()) {} 152 153 bool EnumAttrCase::isStrCase() const { return isSubClassOf("StrEnumAttrCase"); } 154 155 StringRef EnumAttrCase::getSymbol() const { 156 return def->getValueAsString("symbol"); 157 } 158 159 StringRef EnumAttrCase::getStr() const { return def->getValueAsString("str"); } 160 161 int64_t EnumAttrCase::getValue() const { return def->getValueAsInt("value"); } 162 163 const llvm::Record &EnumAttrCase::getDef() const { return *def; } 164 165 EnumAttr::EnumAttr(const llvm::Record *record) : Attribute(record) { 166 assert(isSubClassOf("EnumAttrInfo") && 167 "must be subclass of TableGen 'EnumAttr' class"); 168 } 169 170 EnumAttr::EnumAttr(const llvm::Record &record) : Attribute(&record) {} 171 172 EnumAttr::EnumAttr(const llvm::DefInit *init) : EnumAttr(init->getDef()) {} 173 174 bool EnumAttr::classof(const Attribute *attr) { 175 return attr->isSubClassOf("EnumAttrInfo"); 176 } 177 178 bool EnumAttr::isBitEnum() const { return isSubClassOf("BitEnumAttr"); } 179 180 StringRef EnumAttr::getEnumClassName() const { 181 return def->getValueAsString("className"); 182 } 183 184 StringRef EnumAttr::getCppNamespace() const { 185 return def->getValueAsString("cppNamespace"); 186 } 187 188 StringRef EnumAttr::getUnderlyingType() const { 189 return def->getValueAsString("underlyingType"); 190 } 191 192 StringRef EnumAttr::getUnderlyingToSymbolFnName() const { 193 return def->getValueAsString("underlyingToSymbolFnName"); 194 } 195 196 StringRef EnumAttr::getStringToSymbolFnName() const { 197 return def->getValueAsString("stringToSymbolFnName"); 198 } 199 200 StringRef EnumAttr::getSymbolToStringFnName() const { 201 return def->getValueAsString("symbolToStringFnName"); 202 } 203 204 StringRef EnumAttr::getSymbolToStringFnRetType() const { 205 return def->getValueAsString("symbolToStringFnRetType"); 206 } 207 208 StringRef EnumAttr::getMaxEnumValFnName() const { 209 return def->getValueAsString("maxEnumValFnName"); 210 } 211 212 std::vector<EnumAttrCase> EnumAttr::getAllCases() const { 213 const auto *inits = def->getValueAsListInit("enumerants"); 214 215 std::vector<EnumAttrCase> cases; 216 cases.reserve(inits->size()); 217 218 for (const llvm::Init *init : *inits) { 219 cases.push_back(EnumAttrCase(cast<llvm::DefInit>(init))); 220 } 221 222 return cases; 223 } 224 225 StructFieldAttr::StructFieldAttr(const llvm::Record *record) : def(record) { 226 assert(def->isSubClassOf("StructFieldAttr") && 227 "must be subclass of TableGen 'StructFieldAttr' class"); 228 } 229 230 StructFieldAttr::StructFieldAttr(const llvm::Record &record) 231 : StructFieldAttr(&record) {} 232 233 StructFieldAttr::StructFieldAttr(const llvm::DefInit *init) 234 : StructFieldAttr(init->getDef()) {} 235 236 StringRef StructFieldAttr::getName() const { 237 return def->getValueAsString("name"); 238 } 239 240 Attribute StructFieldAttr::getType() const { 241 auto init = def->getValueInit("type"); 242 return Attribute(cast<llvm::DefInit>(init)); 243 } 244 245 StructAttr::StructAttr(const llvm::Record *record) : Attribute(record) { 246 assert(isSubClassOf("StructAttr") && 247 "must be subclass of TableGen 'StructAttr' class"); 248 } 249 250 StructAttr::StructAttr(const llvm::DefInit *init) 251 : StructAttr(init->getDef()) {} 252 253 StringRef StructAttr::getStructClassName() const { 254 return def->getValueAsString("className"); 255 } 256 257 StringRef StructAttr::getCppNamespace() const { 258 Dialect dialect(def->getValueAsDef("structDialect")); 259 return dialect.getCppNamespace(); 260 } 261 262 std::vector<StructFieldAttr> StructAttr::getAllFields() const { 263 std::vector<StructFieldAttr> attributes; 264 265 const auto *inits = def->getValueAsListInit("fields"); 266 attributes.reserve(inits->size()); 267 268 for (const llvm::Init *init : *inits) { 269 attributes.emplace_back(cast<llvm::DefInit>(init)); 270 } 271 272 return attributes; 273 } 274 275 const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface"; 276