MLIR  15.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:
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.
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  // 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.
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 the symbol of this enum attribute case.
145  StringRef getSymbol() const;
146 
147  // Returns the textual representation of this enum attribute case.
148  StringRef getStr() const;
149 
150  // Returns the value of this enum attribute case.
151  int64_t getValue() const;
152 
153  // Returns the TableGen definition this EnumAttrCase was constructed from.
154  const llvm::Record &getDef() const;
155 };
156 
157 // Wrapper class providing helper methods for accessing enum attributes defined
158 // in TableGen.This is used for enum attribute case backed by both StringAttr
159 // and IntegerAttr.
160 class EnumAttr : public Attribute {
161 public:
162  explicit EnumAttr(const llvm::Record *record);
163  explicit EnumAttr(const llvm::Record &record);
164  explicit EnumAttr(const llvm::DefInit *init);
165 
166  static bool classof(const Attribute *attr);
167 
168  // Returns true if this is a bit enum attribute.
169  bool isBitEnum() const;
170 
171  // Returns the enum class name.
172  StringRef getEnumClassName() const;
173 
174  // Returns the C++ namespaces this enum class should be placed in.
175  StringRef getCppNamespace() const;
176 
177  // Returns the underlying type.
178  StringRef getUnderlyingType() const;
179 
180  // Returns the name of the utility function that converts a value of the
181  // underlying type to the corresponding symbol.
182  StringRef getUnderlyingToSymbolFnName() const;
183 
184  // Returns the name of the utility function that converts a string to the
185  // corresponding symbol.
186  StringRef getStringToSymbolFnName() const;
187 
188  // Returns the name of the utility function that converts a symbol to the
189  // corresponding string.
190  StringRef getSymbolToStringFnName() const;
191 
192  // Returns the return type of the utility function that converts a symbol to
193  // the corresponding string.
194  StringRef getSymbolToStringFnRetType() const;
195 
196  // Returns the name of the utilit function that returns the max enum value
197  // used within the enum class.
198  StringRef getMaxEnumValFnName() const;
199 
200  // Returns all allowed cases for this enum attribute.
201  std::vector<EnumAttrCase> getAllCases() const;
202 
203  bool genSpecializedAttr() const;
204  llvm::Record *getBaseAttrClass() const;
205  StringRef getSpecializedAttrClassName() const;
206  bool printBitEnumPrimaryGroups() const;
207 };
208 
209 // Name of infer type op interface.
210 extern const char *inferTypeOpInterface;
211 
212 } // namespace tblgen
213 } // namespace mlir
214 
215 #endif // MLIR_TABLEGEN_ATTRIBUTE_H_
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:221
An inlay hint that for a type annotation.
const char * inferTypeOpInterface
Definition: Attribute.cpp:242
static Type getUnderlyingType(Type type, type_list< ShapedTypes... >, type_list< ElementTypes... >)
Returns a non-null type only if the provided type is one of the allowed types or one of the allowed s...
Kind getKind() const
Definition: Constraint.h:72
static bool classof(const Constraint *c)
Definition: Attribute.h:37