My Project
Operator.h
Go to the documentation of this file.
1 //===- Operator.h - Operator 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 // Operator wrapper to simplify using TableGen Record defining a MLIR Op.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TABLEGEN_OPERATOR_H_
14 #define MLIR_TABLEGEN_OPERATOR_H_
15 
16 #include "mlir/Support/LLVM.h"
17 #include "mlir/TableGen/Argument.h"
19 #include "mlir/TableGen/Dialect.h"
20 #include "mlir/TableGen/OpTrait.h"
21 #include "mlir/TableGen/Region.h"
22 #include "mlir/TableGen/Type.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/SMLoc.h"
27 
28 namespace llvm {
29 class CodeInit;
30 class DefInit;
31 class Record;
32 class StringInit;
33 } // end namespace llvm
34 
35 namespace mlir {
36 namespace tblgen {
37 
38 // Wrapper class that contains a MLIR op's information (e.g., operands,
39 // attributes) defined in TableGen and provides helper methods for
40 // accessing them.
41 class Operator {
42 public:
43  explicit Operator(const llvm::Record &def);
44  explicit Operator(const llvm::Record *def) : Operator(*def) {}
45 
46  // Returns this op's dialect name.
47  StringRef getDialectName() const;
48 
49  // Returns the operation name. The name will follow the "<dialect>.<op-name>"
50  // format if its dialect name is not empty.
51  std::string getOperationName() const;
52 
53  // Returns this op's C++ class name.
54  StringRef getCppClassName() const;
55 
56  // Returns this op's C++ class name prefixed with namespaces.
57  std::string getQualCppClassName() const;
58 
61 
62  // Returns true if this op has variadic operands or results.
63  bool isVariadic() const;
64 
65  // Returns true if default builders should not be generated.
66  bool skipDefaultBuilders() const;
67 
68  // Op result iterators.
69  value_iterator result_begin();
70  value_iterator result_end();
71  value_range getResults();
72 
73  // Returns the number of results this op produces.
74  int getNumResults() const;
75 
76  // Returns the op result at the given `index`.
77  NamedTypeConstraint &getResult(int index) { return results[index]; }
78  const NamedTypeConstraint &getResult(int index) const {
79  return results[index];
80  }
81 
82  // Returns the `index`-th result's type constraint.
83  TypeConstraint getResultTypeConstraint(int index) const;
84  // Returns the `index`-th result's name.
85  StringRef getResultName(int index) const;
86 
87  // Returns the number of variadic results in this operation.
88  unsigned getNumVariadicResults() const;
89 
90  // Op attribute iterators.
92  attribute_iterator attribute_begin() const;
93  attribute_iterator attribute_end() const;
94  llvm::iterator_range<attribute_iterator> getAttributes() const;
95 
96  int getNumAttributes() const { return attributes.size(); }
97  int getNumNativeAttributes() const { return numNativeAttributes; }
98 
99  // Op attribute accessors.
100  NamedAttribute &getAttribute(int index) { return attributes[index]; }
101 
102  // Op operand iterators.
103  value_iterator operand_begin();
104  value_iterator operand_end();
105  value_range getOperands();
106 
107  int getNumOperands() const { return operands.size(); }
108  NamedTypeConstraint &getOperand(int index) { return operands[index]; }
109  const NamedTypeConstraint &getOperand(int index) const {
110  return operands[index];
111  }
112 
113  // Returns the number of variadic operands in this operation.
114  unsigned getNumVariadicOperands() const;
115 
116  // Returns the total number of arguments.
117  int getNumArgs() const { return arguments.size(); }
118 
119  using arg_iterator = const Argument *;
121 
122  // Op argument (attribute or operand) iterators.
123  arg_iterator arg_begin() const;
124  arg_iterator arg_end() const;
125  arg_range getArgs() const;
126 
127  // Op argument (attribute or operand) accessors.
128  Argument getArg(int index) const;
129  StringRef getArgName(int index) const;
130 
131  // Returns the trait wrapper for the given MLIR C++ `trait`.
132  // TODO: We should add a C++ wrapper class for TableGen OpTrait instead of
133  // requiring the raw MLIR trait here.
134  const OpTrait *getTrait(llvm::StringRef trait) const;
135 
136  // Returns the number of regions.
137  unsigned getNumRegions() const;
138  // Returns the `index`-th region.
139  const NamedRegion &getRegion(unsigned index) const;
140 
141  // Trait.
142  using const_trait_iterator = const OpTrait *;
143  const_trait_iterator trait_begin() const;
144  const_trait_iterator trait_end() const;
146 
147  ArrayRef<llvm::SMLoc> getLoc() const;
148 
149  // Query functions for the documentation of the operator.
150  bool hasDescription() const;
151  StringRef getDescription() const;
152  bool hasSummary() const;
153  StringRef getSummary() const;
154 
155  // Returns this op's extra class declaration code.
156  StringRef getExtraClassDeclaration() const;
157 
158  // Returns the Tablegen definition this operator was constructed from.
159  // TODO(antiagainst,zinenko): do not expose the TableGen record, this is a
160  // temporary solution to OpEmitter requiring a Record because Operator does
161  // not provide enough methods.
162  const llvm::Record &getDef() const;
163 
164  // Returns the dialect of the op.
165  const Dialect &getDialect() const { return dialect; }
166 
167  // Prints the contents in this operator to the given `os`. This is used for
168  // debugging purposes.
169  void print(llvm::raw_ostream &os) const;
170 
171 private:
172  // Populates the vectors containing operands, attributes, results and traits.
173  void populateOpStructure();
174 
175  // The dialect of this op.
176  Dialect dialect;
177 
178  // The unqualified C++ class name of the op.
179  StringRef cppClassName;
180 
181  // The operands of the op.
183 
184  // The attributes of the op. Contains native attributes (corresponding to the
185  // actual stored attributed of the operation) followed by derived attributes
186  // (corresponding to dynamic properties of the operation that are computed
187  // upon request).
189 
190  // The arguments of the op (operands and native attributes).
191  SmallVector<Argument, 4> arguments;
192 
193  // The results of the op.
195 
196  // The traits of the op.
198 
199  // The regions of this op.
201 
202  // The number of native attributes stored in the leading positions of
203  // `attributes`.
204  int numNativeAttributes;
205 
206  // The TableGen definition of this op.
207  const llvm::Record &def;
208 };
209 
210 } // end namespace tblgen
211 } // end namespace mlir
212 
213 #endif // MLIR_TABLEGEN_OPERATOR_H_
const NamedTypeConstraint & getOperand(int index) const
Definition: Operator.h:109
Definition: InferTypeOpInterface.cpp:20
NamedAttribute & getAttribute(int index)
Definition: Operator.h:100
Definition: PassRegistry.cpp:413
int getNumAttributes() const
Definition: Operator.h:96
Definition: Dialect.h:25
Operator(const llvm::Record *def)
Definition: Operator.h:44
NamedTypeConstraint & getOperand(int index)
Definition: Operator.h:108
Definition: Argument.h:37
Definition: Operator.h:41
Definition: Region.h:28
Definition: OpTrait.h:31
Definition: LLVM.h:37
int getNumNativeAttributes() const
Definition: Operator.h:97
NamedTypeConstraint & getResult(int index)
Definition: Operator.h:77
Definition: Argument.h:43
int getNumOperands() const
Definition: Operator.h:107
const NamedTypeConstraint & getResult(int index) const
Definition: Operator.h:78
Definition: LLVM.h:35
Definition: LLVM.h:50
Definition: Type.h:30
const Dialect & getDialect() const
Definition: Operator.h:165
void print(OpAsmPrinter &p, AffineIfOp op)
Definition: AffineOps.cpp:1671
int getNumArgs() const
Definition: Operator.h:117
Definition: LLVM.h:41