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