MLIR  21.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 
20 namespace llvm {
21 class DefInit;
22 class Record;
23 } // namespace llvm
24 
25 namespace mlir {
26 namespace tblgen {
27 class Dialect;
28 class Type;
29 
30 // Wrapper class with helper methods for accessing attribute constraints defined
31 // in TableGen.
32 class AttrConstraint : public Constraint {
33 public:
35 
36  static bool classof(const Constraint *c) { return c->getKind() == CK_Attr; }
37 
38  // Returns true if this constraint is a subclass of the given `className`
39  // class defined in TableGen.
40  bool isSubClassOf(StringRef className) const;
41 };
42 
43 // Wrapper class providing helper methods for accessing MLIR Attribute defined
44 // in TableGen. This class should closely reflect what is defined as class
45 // `Attr` in TableGen.
46 class Attribute : public AttrConstraint {
47 public:
48  explicit Attribute(const llvm::Record *record);
49  explicit Attribute(const llvm::DefInit *init);
50 
51  // Returns the storage type if set. Returns the default storage type
52  // ("::mlir::Attribute") otherwise.
53  StringRef getStorageType() const;
54 
55  // Returns the return type for this attribute.
56  StringRef getReturnType() const;
57 
58  // Return the type constraint corresponding to the type of this attribute, or
59  // std::nullopt if this is not a TypedAttr.
60  std::optional<Type> getValueType() const;
61 
62  // Returns the template getter method call which reads this attribute's
63  // storage and returns the value as of the desired return type.
64  // The call will contain a `{0}` which will be expanded to this attribute.
65  StringRef getConvertFromStorageCall() const;
66 
67  // Returns true if this attribute can be built from a constant value.
68  bool isConstBuildable() const;
69 
70  // Returns the template that can be used to produce an instance of the
71  // attribute.
72  // Syntax: `$builder` should be replaced with a builder, `$0` should be
73  // replaced with the constant value.
74  StringRef getConstBuilderTemplate() const;
75 
76  // Returns the base-level attribute that this attribute constraint is
77  // built upon.
78  Attribute getBaseAttr() const;
79 
80  // Returns whether this attribute has a default value.
81  bool hasDefaultValue() const;
82  // Returns the default value for this attribute.
83  StringRef getDefaultValue() const;
84 
85  // Returns whether this attribute is optional.
86  bool isOptional() const;
87 
88  // Returns true if this attribute is a derived attribute (i.e., a subclass
89  // of `DerivedAttr`).
90  bool isDerivedAttr() const;
91 
92  // Returns true if this attribute is a type attribute (i.e., a subclass
93  // of `TypeAttrBase`).
94  bool isTypeAttr() const;
95 
96  // Returns true if this attribute is a symbol reference attribute (i.e., a
97  // subclass of `SymbolRefAttr` or `FlatSymbolRefAttr`).
98  bool isSymbolRefAttr() const;
99 
100  // Returns true if this attribute is an enum attribute (i.e., a subclass of
101  // `EnumAttrInfo`)
102  bool isEnumAttr() const;
103 
104  // Returns this attribute's TableGen def name. If this is an `OptionalAttr`
105  // or `DefaultValuedAttr` without explicit name, returns the base attribute's
106  // name.
107  StringRef getAttrDefName() const;
108 
109  // Returns the code body for derived attribute. Aborts if this is not a
110  // derived attribute.
111  StringRef getDerivedCodeBody() const;
112 
113  // Returns the dialect for the attribute if defined.
115 
116  // Returns the TableGen definition this Attribute was constructed from.
117  const llvm::Record &getDef() const;
118 };
119 
120 // Wrapper class providing helper methods for accessing MLIR constant attribute
121 // defined in TableGen. This class should closely reflect what is defined as
122 // class `ConstantAttr` in TableGen.
124 public:
125  explicit ConstantAttr(const llvm::DefInit *init);
126 
127  // Returns the attribute kind.
128  Attribute getAttribute() const;
129 
130  // Returns the constant value.
131  StringRef getConstantValue() const;
132 
133 private:
134  // The TableGen definition of this constant attribute.
135  const llvm::Record *def;
136 };
137 
138 // Name of infer type op interface.
139 extern const char *inferTypeOpInterface;
140 
141 } // namespace tblgen
142 } // namespace mlir
143 
144 #endif // MLIR_TABLEGEN_ATTRIBUTE_H_
static bool classof(const Constraint *c)
Definition: Attribute.h:36
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
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:149
Include the generated interface declarations.