1 //===- Attribute.h - Attribute wrapper class --------------------*- C++ -*-===// 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 #ifndef MLIR_TABLEGEN_ATTRIBUTE_H_ 15 #define MLIR_TABLEGEN_ATTRIBUTE_H_ 16 17 #include "mlir/Support/LLVM.h" 18 #include "mlir/TableGen/Constraint.h" 19 #include "llvm/ADT/StringRef.h" 20 21 namespace llvm { 22 class DefInit; 23 class Record; 24 } // end namespace llvm 25 26 namespace mlir { 27 namespace tblgen { 28 class Dialect; 29 class Type; 30 31 // Wrapper class with helper methods for accessing attribute constraints defined 32 // in TableGen. 33 class AttrConstraint : public Constraint { 34 public: 35 explicit AttrConstraint(const llvm::Record *record); 36 classof(const Constraint * c)37 static bool classof(const Constraint *c) { return c->getKind() == CK_Attr; } 38 39 // Returns true if this constraint is a subclass of the given `className` 40 // class defined in TableGen. 41 bool isSubClassOf(StringRef className) const; 42 }; 43 44 // Wrapper class providing helper methods for accessing MLIR Attribute defined 45 // in TableGen. This class should closely reflect what is defined as class 46 // `Attr` in TableGen. 47 class Attribute : public AttrConstraint { 48 public: 49 explicit Attribute(const llvm::Record *record); 50 explicit Attribute(const llvm::DefInit *init); 51 52 // Returns the storage type if set. Returns the default storage type 53 // ("Attribute") otherwise. 54 StringRef getStorageType() const; 55 56 // Returns the return type for this attribute. 57 StringRef getReturnType() const; 58 59 // Return the type constraint corresponding to the type of this attribute, or 60 // None if this is not a TypedAttr. 61 llvm::Optional<Type> getValueType() const; 62 63 // Returns the template getter method call which reads this attribute's 64 // storage and returns the value as of the desired return type. 65 // The call will contain a `{0}` which will be expanded to this attribute. 66 StringRef getConvertFromStorageCall() const; 67 68 // Returns true if this attribute can be built from a constant value. 69 bool isConstBuildable() const; 70 71 // Returns the template that can be used to produce an instance of the 72 // attribute. 73 // Syntax: `$builder` should be replaced with a builder, `$0` should be 74 // replaced with the constant value. 75 StringRef getConstBuilderTemplate() const; 76 77 // Returns the base-level attribute that this attribute constraint is 78 // built upon. 79 Attribute getBaseAttr() const; 80 81 // Returns whether this attribute has a default value. 82 bool hasDefaultValue() const; 83 // Returns the default value for this attribute. 84 StringRef getDefaultValue() const; 85 86 // Returns whether this attribute is optional. 87 bool isOptional() const; 88 89 // Returns true if this attribute is a derived attribute (i.e., a subclass 90 // of `DerivedAttr`). 91 bool isDerivedAttr() const; 92 93 // Returns true if this attribute is a type attribute (i.e., a subclass 94 // of `TypeAttrBase`). 95 bool isTypeAttr() const; 96 97 // Returns true if this attribute is a symbol reference attribute (i.e., a 98 // subclass of `SymbolRefAttr` or `FlatSymbolRefAttr`). 99 bool isSymbolRefAttr() const; 100 101 // Returns true if this attribute is an enum attribute (i.e., a subclass of 102 // `EnumAttrInfo`) 103 bool isEnumAttr() const; 104 105 // Returns this attribute's TableGen def name. If this is an `OptionalAttr` 106 // or `DefaultValuedAttr` without explicit name, returns the base attribute's 107 // name. 108 StringRef getAttrDefName() const; 109 110 // Returns the code body for derived attribute. Aborts if this is not a 111 // derived attribute. 112 StringRef getDerivedCodeBody() const; 113 114 // Returns the dialect for the attribute if defined. 115 Dialect getDialect() const; 116 }; 117 118 // Wrapper class providing helper methods for accessing MLIR constant attribute 119 // defined in TableGen. This class should closely reflect what is defined as 120 // class `ConstantAttr` in TableGen. 121 class ConstantAttr { 122 public: 123 explicit ConstantAttr(const llvm::DefInit *init); 124 125 // Returns the attribute kind. 126 Attribute getAttribute() const; 127 128 // Returns the constant value. 129 StringRef getConstantValue() const; 130 131 private: 132 // The TableGen definition of this constant attribute. 133 const llvm::Record *def; 134 }; 135 136 // Wrapper class providing helper methods for accessing enum attribute cases 137 // defined in TableGen. This is used for enum attribute case backed by both 138 // StringAttr and IntegerAttr. 139 class EnumAttrCase : public Attribute { 140 public: 141 explicit EnumAttrCase(const llvm::Record *record); 142 explicit EnumAttrCase(const llvm::DefInit *init); 143 144 // Returns true if this EnumAttrCase is backed by a StringAttr. 145 bool isStrCase() const; 146 147 // Returns the symbol of this enum attribute case. 148 StringRef getSymbol() const; 149 150 // Returns the textual representation of this enum attribute case. 151 StringRef getStr() const; 152 153 // Returns the value of this enum attribute case. 154 int64_t getValue() const; 155 156 // Returns the TableGen definition this EnumAttrCase was constructed from. 157 const llvm::Record &getDef() const; 158 }; 159 160 // Wrapper class providing helper methods for accessing enum attributes defined 161 // in TableGen.This is used for enum attribute case backed by both StringAttr 162 // and IntegerAttr. 163 class EnumAttr : public Attribute { 164 public: 165 explicit EnumAttr(const llvm::Record *record); 166 explicit EnumAttr(const llvm::Record &record); 167 explicit EnumAttr(const llvm::DefInit *init); 168 169 static bool classof(const Attribute *attr); 170 171 // Returns true if this is a bit enum attribute. 172 bool isBitEnum() const; 173 174 // Returns the enum class name. 175 StringRef getEnumClassName() const; 176 177 // Returns the C++ namespaces this enum class should be placed in. 178 StringRef getCppNamespace() const; 179 180 // Returns the underlying type. 181 StringRef getUnderlyingType() const; 182 183 // Returns the name of the utility function that converts a value of the 184 // underlying type to the corresponding symbol. 185 StringRef getUnderlyingToSymbolFnName() const; 186 187 // Returns the name of the utility function that converts a string to the 188 // corresponding symbol. 189 StringRef getStringToSymbolFnName() const; 190 191 // Returns the name of the utility function that converts a symbol to the 192 // corresponding string. 193 StringRef getSymbolToStringFnName() const; 194 195 // Returns the return type of the utility function that converts a symbol to 196 // the corresponding string. 197 StringRef getSymbolToStringFnRetType() const; 198 199 // Returns the name of the utilit function that returns the max enum value 200 // used within the enum class. 201 StringRef getMaxEnumValFnName() const; 202 203 // Returns all allowed cases for this enum attribute. 204 std::vector<EnumAttrCase> getAllCases() const; 205 }; 206 207 class StructFieldAttr { 208 public: 209 explicit StructFieldAttr(const llvm::Record *record); 210 explicit StructFieldAttr(const llvm::Record &record); 211 explicit StructFieldAttr(const llvm::DefInit *init); 212 213 StringRef getName() const; 214 Attribute getType() const; 215 216 private: 217 const llvm::Record *def; 218 }; 219 220 // Wrapper class providing helper methods for accessing struct attributes 221 // defined in TableGen. 222 class StructAttr : public Attribute { 223 public: 224 explicit StructAttr(const llvm::Record *record); StructAttr(const llvm::Record & record)225 explicit StructAttr(const llvm::Record &record) : StructAttr(&record){}; 226 explicit StructAttr(const llvm::DefInit *init); 227 228 // Returns the struct class name. 229 StringRef getStructClassName() const; 230 231 // Returns the C++ namespaces this struct class should be placed in. 232 StringRef getCppNamespace() const; 233 234 std::vector<StructFieldAttr> getAllFields() const; 235 }; 236 237 // Name of infer type op interface. 238 extern const char *inferTypeOpInterface; 239 240 } // end namespace tblgen 241 } // end namespace mlir 242 243 #endif // MLIR_TABLEGEN_ATTRIBUTE_H_ 244