MLIR  20.0.0git
Attribute.h
Go to the documentation of this file.
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"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace llvm {
22 class DefInit;
23 class Record;
24 } // 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:
36 
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  // ("::mlir::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  // std::nullopt if this is not a TypedAttr.
61  std::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.
116 
117  // Returns the TableGen definition this Attribute was constructed from.
118  const llvm::Record &getDef() const;
119 };
120 
121 // Wrapper class providing helper methods for accessing MLIR constant attribute
122 // defined in TableGen. This class should closely reflect what is defined as
123 // class `ConstantAttr` in TableGen.
125 public:
126  explicit ConstantAttr(const llvm::DefInit *init);
127 
128  // Returns the attribute kind.
129  Attribute getAttribute() const;
130 
131  // Returns the constant value.
132  StringRef getConstantValue() const;
133 
134 private:
135  // The TableGen definition of this constant attribute.
136  const llvm::Record *def;
137 };
138 
139 // Wrapper class providing helper methods for accessing enum attribute cases
140 // defined in TableGen. This is used for enum attribute case backed by both
141 // StringAttr and IntegerAttr.
142 class EnumAttrCase : public Attribute {
143 public:
144  explicit EnumAttrCase(const llvm::Record *record);
145  explicit EnumAttrCase(const llvm::DefInit *init);
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  bool genSpecializedAttr() const;
207  const llvm::Record *getBaseAttrClass() const;
208  StringRef getSpecializedAttrClassName() const;
209  bool printBitEnumPrimaryGroups() const;
210 };
211 
212 // Name of infer type op interface.
213 extern const char *inferTypeOpInterface;
214 
215 } // namespace tblgen
216 } // namespace mlir
217 
218 #endif // MLIR_TABLEGEN_ATTRIBUTE_H_
static bool classof(const Constraint *c)
Definition: Attribute.h:37
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
Attribute(const llvm::DefInit *init)
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
Kind getKind() const
Definition: Constraint.h:76
Constraint(const llvm::Record *record, Kind kind)
Definition: Constraint.h:36
StringRef getSymbol() const
Definition: Attribute.cpp:157
const llvm::Record & getDef() const
Definition: Attribute.cpp:165
EnumAttrCase(const llvm::DefInit *init)
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
EnumAttr(const llvm::DefInit *init)
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)
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
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
@ Type
An inlay hint that for a type annotation.
const char * inferTypeOpInterface
Definition: Attribute.cpp:243
Include the generated interface declarations.