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
21namespace llvm {
22class DefInit;
23class Record;
24} // namespace llvm
25
26namespace mlir {
27namespace tblgen {
28class Dialect;
29class Type;
30
31// Wrapper class with helper methods for accessing attribute constraints defined
32// in TableGen.
33class AttrConstraint : public Constraint {
34public:
35 using Constraint::Constraint;
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.
47class Attribute : public AttrConstraint {
48public:
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.
115 Dialect getDialect() const;
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.
124class ConstantAttr {
125public:
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
134private:
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.
142class EnumAttrCase : public Attribute {
143public:
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.
163class EnumAttr : public Attribute {
164public:
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 llvm::Record *getBaseAttrClass() const;
208 StringRef getSpecializedAttrClassName() const;
209 bool printBitEnumPrimaryGroups() const;
210};
211
212// Name of infer type op interface.
213extern const char *inferTypeOpInterface;
214
215} // namespace tblgen
216} // namespace mlir
217
218#endif // MLIR_TABLEGEN_ATTRIBUTE_H_
219

source code of mlir/include/mlir/TableGen/Attribute.h