MLIR 24.0.0git
Operator.h
Go to the documentation of this file.
1//===- Operator.h - Operator 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// 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"
24#include "mlir/TableGen/Trait.h"
25#include "mlir/TableGen/Type.h"
26#include "llvm/ADT/PointerUnion.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/Support/SMLoc.h"
31
32namespace llvm {
33class DefInit;
34class Record;
35class StringInit;
36} // namespace llvm
37
38namespace mlir {
39namespace tblgen {
40
41/// The canonical and legacy names of the implicit segment-size properties.
42inline constexpr StringLiteral operandSegmentAttrName = "operandSegmentSizes";
43inline constexpr StringLiteral resultSegmentAttrName = "resultSegmentSizes";
44inline constexpr StringLiteral legacyOperandSegmentAttrName =
45 "operand_segment_sizes";
46inline constexpr StringLiteral legacyResultSegmentAttrName =
47 "result_segment_sizes";
48
49/// This class represents an inferred result type. The result type can be
50/// inferred from an argument or result type. If it is inferred from another
51/// result type, that type must be buildable or inferred from yet another type.
53public:
54 InferredResultType(int index, std::string transformer)
55 : index(index), transformer(std::move(transformer)) {}
56
57 /// Returns true if result type is inferred from an argument type.
58 bool isArg() const { return isArgIndex(index); }
59 /// Return the mapped argument or result index.
60 int getIndex() const { return index; }
61 /// If the type is inferred from a result, return the result index.
62 int getResultIndex() const { return unmapResultIndex(index); }
63
64 // Mapping from result index to combined argument and result index.
65 // Arguments are indexed to match getArg index, while the result indexes are
66 // mapped to avoid overlap.
67 static int mapResultIndex(int i) { return -1 - i; }
68 static int unmapResultIndex(int i) { return -i - 1; }
69 static bool isResultIndex(int i) { return i < 0; }
70 static bool isArgIndex(int i) { return i >= 0; }
71
72 StringRef getTransformer() const { return transformer; }
73
74private:
75 /// The index of the source argument or result.
76 int index;
77
78 /// The transfer to apply to the type to obtain the inferred type.
79 std::string transformer;
80};
81
82/// Wrapper class that contains a MLIR op's information (e.g., operands,
83/// attributes) defined in TableGen and provides helper methods for
84/// accessing them.
85class Operator {
86public:
87 explicit Operator(const llvm::Record &def);
88 explicit Operator(const llvm::Record *def) : Operator(*def) {}
89
90 /// Returns this op's dialect name.
91 StringRef getDialectName() const;
92
93 /// Returns the operation name. The name will follow the "<dialect>.<op-name>"
94 /// format if its dialect name is not empty.
95 std::string getOperationName() const;
96
97 /// Returns this op's C++ class name.
98 StringRef getCppClassName() const;
99
100 /// Returns this op's C++ class name prefixed with namespaces.
101 std::string getQualCppClassName() const;
102
103 /// Returns this op's C++ namespace.
104 StringRef getCppNamespace() const;
105
106 /// Returns the name of op's adaptor C++ class.
107 std::string getAdaptorName() const;
108
109 /// Returns the name of op's generic adaptor C++ class.
110 std::string getGenericAdaptorName() const;
111
112 /// Check invariants (like no duplicated or conflicted names) and abort the
113 /// process if any invariant is broken.
114 void assertInvariants() const;
115
116 /// A class used to represent the decorators of an operator variable, i.e.
117 /// argument or result.
119 public:
120 explicit VariableDecorator(const llvm::Record *def) : def(def) {}
121 const llvm::Record &getDef() const { return *def; }
122
123 protected:
124 /// The TableGen definition of this decorator.
125 const llvm::Record *def;
126 };
127
128 /// A utility iterator over a list of variable decorators.
130 : public llvm::mapped_iterator<const llvm::Init *const *,
131 VariableDecorator (*)(
132 const llvm::Init *)> {
133 /// Initializes the iterator to the specified iterator.
134 VariableDecoratorIterator(const llvm::Init *const *it)
135 : llvm::mapped_iterator<const llvm::Init *const *,
136 VariableDecorator (*)(const llvm::Init *)>(
137 it, &unwrap) {}
138 static VariableDecorator unwrap(const llvm::Init *init);
139 };
142
147
148 /// Returns true if this op has variable length operands or results.
149 bool isVariadic() const;
150
151 /// Returns true if default builders should not be generated.
152 bool skipDefaultBuilders() const;
153
154 /// Returns true if the operation provides a custom properties printer.
155 bool hasCustomPropertiesPrinter() const;
156
157 /// Op result iterators.
161
162 /// Returns the number of results this op produces.
163 int getNumResults() const;
164
165 /// Returns the op result at the given `index`.
166 NamedTypeConstraint &getResult(int index) { return results[index]; }
168 return results[index];
169 }
170
171 /// Returns the `index`-th result's type constraint.
173 /// Returns the `index`-th result's name.
174 StringRef getResultName(int index) const;
175 /// Returns the `index`-th result's decorators.
177
178 /// Returns the number of variable length results in this operation.
179 unsigned getNumVariableLengthResults() const;
180
181 /// Op attribute iterators.
190
191 int getNumAttributes() const { return attributes.size(); }
192 int getNumNativeAttributes() const { return numNativeAttributes; }
193
194 /// Op attribute accessors.
195 NamedAttribute &getAttribute(int index) { return attributes[index]; }
197 return attributes[index];
198 }
199
200 /// Op operand iterators.
204
205 // Op properties iterators.
208 return properties.begin();
209 }
210 const_property_iterator properties_end() const { return properties.end(); }
212 return properties;
213 }
215 property_iterator properties_begin() { return properties.begin(); }
216 property_iterator properties_end() { return properties.end(); }
218 int getNumCoreAttributes() const { return properties.size(); }
219
220 /// Returns whether this operation has any non-empty properties.
221 bool hasNonEmptyProperties() const;
222
223 /// Returns all accepted attribute spellings for this operation's inherent
224 /// attributes and properties, including legacy segment-size aliases.
226
227 // Op properties accessors.
228 NamedProperty &getProperty(int index) { return properties[index]; }
229 const NamedProperty &getProperty(int index) const {
230 return properties[index];
231 }
232
233 int getNumOperands() const { return operands.size(); }
234 NamedTypeConstraint &getOperand(int index) { return operands[index]; }
236 return operands[index];
237 }
238
239 /// Returns the number of variadic operands in this operation.
240 unsigned getNumVariableLengthOperands() const;
241
242 /// Returns the total number of arguments.
243 int getNumArgs() const { return arguments.size(); }
244
245 /// Returns true of the operation has a single variadic arg.
246 bool hasSingleVariadicArg() const;
247
248 /// Returns true if the operation has a single variadic result.
250 return getNumResults() == 1 && getResult(0).isVariadic();
251 }
252
253 /// Returns true of the operation has no variadic regions.
254 bool hasNoVariadicRegions() const { return getNumVariadicRegions() == 0; }
255
256 using arg_iterator = const Argument *;
258
259 /// Op argument (attribute or operand) iterators.
260 arg_iterator arg_begin() const;
261 arg_iterator arg_end() const;
262 arg_range getArgs() const;
263
264 /// Op argument (attribute or operand) accessors.
265 Argument getArg(int index) const;
266 StringRef getArgName(int index) const;
268
269 /// Returns the trait wrapper for the given MLIR C++ `trait`.
270 const Trait *getTrait(llvm::StringRef trait) const;
271
272 /// Regions.
277
278 /// Returns the number of regions.
279 unsigned getNumRegions() const;
280 /// Returns the `index`-th region.
281 const NamedRegion &getRegion(unsigned index) const;
282
283 /// Returns the number of variadic regions in this operation.
284 unsigned getNumVariadicRegions() const;
285
286 /// Successors.
291
292 /// Returns the number of successors.
293 unsigned getNumSuccessors() const;
294 /// Returns the `index`-th successor.
295 const NamedSuccessor &getSuccessor(unsigned index) const;
296
297 /// Returns the number of variadic successors in this operation.
298 unsigned getNumVariadicSuccessors() const;
299
300 /// Trait.
305
306 ArrayRef<SMLoc> getLoc() const;
307
308 /// Query functions for the documentation of the operator.
309 bool hasDescription() const;
310 StringRef getDescription() const;
311 bool hasSummary() const;
312 StringRef getSummary() const;
313
314 /// Query functions for the assembly format of the operator.
315 bool hasAssemblyFormat() const;
316 StringRef getAssemblyFormat() const;
317
318 /// Returns this op's extra class declaration code.
319 StringRef getExtraClassDeclaration() const;
320
321 /// Returns this op's extra class definition code.
322 StringRef getExtraClassDefinition() const;
323
324 /// Returns the Tablegen definition this operator was constructed from.
325 /// TODO: do not expose the TableGen record, this is a temporary solution to
326 /// OpEmitter requiring a Record because Operator does not provide enough
327 /// methods.
328 const llvm::Record &getDef() const;
329
330 /// Returns the dialect of the op.
331 const Dialect &getDialect() const { return dialect; }
332
333 /// Prints the contents in this operator to the given `os`. This is used for
334 /// debugging purposes.
335 void print(llvm::raw_ostream &os) const;
336
337 /// Return whether all the result types are known.
338 bool allResultTypesKnown() const { return allResultsHaveKnownTypes; };
339
340 /// Return all arguments or type constraints with same type as result[index].
341 /// Requires: all result types are known.
343
344 /// Pair consisting kind of argument and index into operands, attributes, or
345 /// properties.
347 enum class Kind { Operand = 0x0, Attribute = 0x1, Property = 0x2 };
349 packed = (index << 2) | static_cast<int>(kind);
350 }
351 int operandOrAttributeIndex() const { return (packed >> 2); }
352 Kind kind() const { return static_cast<Kind>(packed & 0x3); }
353
354 private:
355 int packed;
356 };
357
358 /// Returns the OperandAttrOrProp corresponding to the index.
359 OperandAttrOrProp getArgToOperandAttrOrProp(int index) const;
360
361 /// Returns the builders of this operation.
362 ArrayRef<Builder> getBuilders() const { return builders; }
363
364 /// Returns the getter name for the accessor of `name`.
365 std::string getGetterName(StringRef name) const;
366
367 /// Returns the setter name for the accessor of `name`.
368 std::string getSetterName(StringRef name) const;
369
370 /// Returns the remove name for the accessor of `name`.
371 std::string getRemoverName(StringRef name) const;
372
373 bool hasFolder() const;
374
375 /// Whether to generate the `readProperty`/`writeProperty` methods for
376 /// bytecode emission.
377 bool useCustomPropertiesEncoding() const;
378
379private:
380 /// Populates the vectors containing operands, attributes, results and traits.
381 void populateOpStructure();
382
383 /// Populates type inference info (mostly equality) with input a mapping from
384 /// names to indices for arguments and results.
385 void populateTypeInferenceInfo(
386 const llvm::StringMap<int> &argumentsAndResultsIndex);
387
388 /// The dialect of this op.
389 Dialect dialect;
390
391 /// The unqualified C++ class name of the op.
392 StringRef cppClassName;
393
394 /// The C++ namespace for this op.
395 StringRef cppNamespace;
396
397 /// The operands of the op.
399
400 /// The attributes of the op. Contains native attributes (corresponding to
401 /// the actual stored attributed of the operation) followed by derived
402 /// attributes (corresponding to dynamic properties of the operation that are
403 /// computed upon request).
405
406 /// The properties of the op.
408
409 /// The arguments of the op (operands and native attributes).
410 SmallVector<Argument, 4> arguments;
411
412 /// The results of the op.
414
415 /// The successors of this op.
417
418 /// The traits of the op.
420
421 /// The regions of this op.
423
424 /// The argument with the same type as the result.
425 SmallVector<InferredResultType> resultTypeMapping;
426
427 /// Map from argument to attribute, property, or operand number.
428 SmallVector<OperandAttrOrProp, 4> attrPropOrOperandMapping;
429
430 /// The builders of this operator.
431 SmallVector<Builder> builders;
432
433 /// The number of native attributes stored in the leading positions of
434 /// `attributes`.
435 int numNativeAttributes;
436
437 /// The TableGen definition of this op.
438 const llvm::Record &def;
439
440 /// Whether the type of all results are known.
441 bool allResultsHaveKnownTypes;
442};
443
444} // namespace tblgen
445} // namespace mlir
446
447#endif // MLIR_TABLEGEN_OPERATOR_H_
This class represents an inferred result type.
Definition Operator.h:52
StringRef getTransformer() const
Definition Operator.h:72
static int mapResultIndex(int i)
Definition Operator.h:67
static int unmapResultIndex(int i)
Definition Operator.h:68
int getResultIndex() const
If the type is inferred from a result, return the result index.
Definition Operator.h:62
int getIndex() const
Return the mapped argument or result index.
Definition Operator.h:60
InferredResultType(int index, std::string transformer)
Definition Operator.h:54
static bool isResultIndex(int i)
Definition Operator.h:69
static bool isArgIndex(int i)
Definition Operator.h:70
bool isArg() const
Returns true if result type is inferred from an argument type.
Definition Operator.h:58
std::string getQualCppClassName() const
Returns this op's C++ class name prefixed with namespaces.
Definition Operator.cpp:156
unsigned getNumSuccessors() const
Returns the number of successors.
Definition Operator.cpp:339
bool hasSingleVariadicResult() const
Returns true if the operation has a single variadic result.
Definition Operator.h:249
const NamedRegion & getRegion(unsigned index) const
Returns the index-th region.
Definition Operator.cpp:319
const NamedTypeConstraint & getResult(int index) const
Definition Operator.h:167
TypeConstraint getResultTypeConstraint(int index) const
Returns the index-th result's type constraint.
Definition Operator.cpp:205
ArrayRef< SMLoc > getLoc() const
Definition Operator.cpp:865
ArrayRef< Builder > getBuilders() const
Returns the builders of this operation.
Definition Operator.h:362
NamedAttribute & getAttribute(int index)
Op attribute accessors.
Definition Operator.h:195
Operator(const llvm::Record &def)
const NamedTypeConstraint * const_value_iterator
Definition Operator.h:144
llvm::iterator_range< const_region_iterator > getRegions() const
Definition Operator.cpp:312
const Dialect & getDialect() const
Returns the dialect of the op.
Definition Operator.h:331
StringRef getCppNamespace() const
Returns this op's C++ namespace.
Definition Operator.cpp:162
const_attribute_iterator attribute_begin() const
Definition Operator.cpp:360
std::string getGetterName(StringRef name) const
Returns the getter name for the accessor of name.
Definition Operator.cpp:910
const_successor_iterator successor_end() const
Definition Operator.cpp:331
int getNumOperands() const
Definition Operator.h:233
StringRef getDescription() const
Definition Operator.cpp:871
const_value_range getResults() const
Definition Operator.cpp:201
arg_range getArgs() const
Definition Operator.cpp:244
const NamedAttribute * const_attribute_iterator
Op attribute iterators.
Definition Operator.h:182
const_value_range getOperands() const
Definition Operator.cpp:386
const_region_iterator region_begin() const
Definition Operator.cpp:306
bool useCustomPropertiesEncoding() const
Whether to generate the readProperty/writeProperty methods for bytecode emission.
Definition Operator.cpp:924
const NamedRegion * const_region_iterator
Regions.
Definition Operator.h:273
property_iterator properties_begin()
Definition Operator.h:215
NamedTypeConstraint & getOperand(int index)
Definition Operator.h:234
StringRef getResultName(int index) const
Returns the index-th result's name.
Definition Operator.cpp:210
var_decorator_range getArgDecorators(int index) const
Definition Operator.cpp:253
const Argument * arg_iterator
Definition Operator.h:256
unsigned getNumVariableLengthOperands() const
Returns the number of variadic operands in this operation.
Definition Operator.cpp:229
OperandAttrOrProp getArgToOperandAttrOrProp(int index) const
Returns the OperandAttrOrProp corresponding to the index.
Definition Operator.cpp:906
var_decorator_range getResultDecorators(int index) const
Returns the index-th result's decorators.
Definition Operator.cpp:215
std::string getGenericAdaptorName() const
Returns the name of op's generic adaptor C++ class.
Definition Operator.cpp:75
NamedProperty * property_iterator
Definition Operator.h:214
StringRef getExtraClassDefinition() const
Returns this op's extra class definition code.
Definition Operator.cpp:176
llvm::iterator_range< const_property_iterator > getProperties() const
Definition Operator.h:211
const_value_iterator result_begin() const
Op result iterators.
Definition Operator.cpp:193
const Trait * const_trait_iterator
Trait.
Definition Operator.h:301
const_attribute_iterator attribute_end() const
Definition Operator.cpp:363
const_trait_iterator trait_end() const
Definition Operator.cpp:353
Operator(const llvm::Record *def)
Definition Operator.h:88
llvm::iterator_range< VariableDecoratorIterator > var_decorator_range
Definition Operator.h:141
std::string getAdaptorName() const
Returns the name of op's adaptor C++ class.
Definition Operator.cpp:71
bool hasNonEmptyProperties() const
Returns whether this operation has any non-empty properties.
Definition Operator.cpp:277
int getNumResults() const
Returns the number of results this op produces.
Definition Operator.cpp:164
llvm::iterator_range< const_attribute_iterator > getAttributes() const
Definition Operator.cpp:366
llvm::iterator_range< const_value_iterator > const_value_range
Definition Operator.h:146
bool hasFolder() const
Definition Operator.cpp:922
const_value_iterator operand_end() const
Definition Operator.cpp:383
arg_iterator arg_end() const
Definition Operator.cpp:242
int getNumArgs() const
Returns the total number of arguments.
Definition Operator.h:243
const NamedProperty & getProperty(int index) const
Definition Operator.h:229
NamedTypeConstraint & getResult(int index)
Returns the op result at the given index.
Definition Operator.h:166
llvm::iterator_range< arg_iterator > arg_range
Definition Operator.h:257
int getNumAttributes() const
Definition Operator.h:191
const NamedProperty * const_property_iterator
Definition Operator.h:206
const_value_iterator operand_begin() const
Op operand iterators.
Definition Operator.cpp:380
void assertInvariants() const
Check invariants (like no duplicated or conflicted names) and abort the process if any invariant is b...
Definition Operator.cpp:114
StringRef getArgName(int index) const
Definition Operator.cpp:248
StringRef getDialectName() const
Returns this op's dialect name.
Definition Operator.cpp:152
NamedTypeConstraint * value_iterator
Definition Operator.h:143
const_region_iterator region_end() const
Definition Operator.cpp:309
unsigned getNumVariableLengthResults() const
Returns the number of variable length results in this operation.
Definition Operator.cpp:223
bool hasSingleVariadicArg() const
Returns true of the operation has a single variadic arg.
Definition Operator.cpp:235
const NamedSuccessor * const_successor_iterator
Successors.
Definition Operator.h:287
unsigned getNumVariadicSuccessors() const
Returns the number of variadic successors in this operation.
Definition Operator.cpp:345
StringRef getSummary() const
Definition Operator.cpp:877
bool isVariadic() const
Returns true if this op has variable length operands or results.
Definition Operator.cpp:392
llvm::iterator_range< const_trait_iterator > getTraits() const
Definition Operator.cpp:356
int getNumCoreAttributes() const
Definition Operator.h:218
bool hasCustomPropertiesPrinter() const
Returns true if the operation provides a custom properties printer.
Definition Operator.cpp:189
const Trait * getTrait(llvm::StringRef trait) const
Returns the trait wrapper for the given MLIR C++ trait.
Definition Operator.cpp:261
SmallVector< StringRef > getInherentAttrNames() const
Returns all accepted attribute spellings for this operation's inherent attributes and properties,...
Definition Operator.cpp:288
llvm::iterator_range< const_successor_iterator > getSuccessors() const
Definition Operator.cpp:334
bool hasSummary() const
Definition Operator.cpp:875
const NamedTypeConstraint & getOperand(int index) const
Definition Operator.h:235
property_iterator properties_end()
Definition Operator.h:216
const_successor_iterator successor_begin() const
Definition Operator.cpp:328
void print(llvm::raw_ostream &os) const
Prints the contents in this operator to the given os.
Definition Operator.cpp:891
unsigned getNumRegions() const
Returns the number of regions.
Definition Operator.cpp:317
const_trait_iterator trait_begin() const
Definition Operator.cpp:350
bool hasNoVariadicRegions() const
Returns true of the operation has no variadic regions.
Definition Operator.h:254
const_property_iterator properties_end() const
Definition Operator.h:210
int getNumNativeAttributes() const
Definition Operator.h:192
llvm::iterator_range< property_iterator > getProperties()
Definition Operator.h:217
NamedAttribute * attribute_iterator
Definition Operator.h:186
StringRef getExtraClassDeclaration() const
Returns this op's extra class declaration code.
Definition Operator.cpp:169
llvm::iterator_range< value_iterator > value_range
Definition Operator.h:145
StringRef getAssemblyFormat() const
Definition Operator.cpp:886
std::string getSetterName(StringRef name) const
Returns the setter name for the accessor of name.
Definition Operator.cpp:914
const_property_iterator properties_begin() const
Definition Operator.h:207
std::string getOperationName() const
Returns the operation name.
Definition Operator.cpp:63
const NamedAttribute & getAttribute(int index) const
Definition Operator.h:196
const NamedSuccessor & getSuccessor(unsigned index) const
Returns the index-th successor.
Definition Operator.cpp:341
StringRef getCppClassName() const
Returns this op's C++ class name.
Definition Operator.cpp:154
bool allResultTypesKnown() const
Return whether all the result types are known.
Definition Operator.h:338
bool hasAssemblyFormat() const
Query functions for the assembly format of the operator.
Definition Operator.cpp:881
unsigned getNumVariadicRegions() const
Returns the number of variadic regions in this operation.
Definition Operator.cpp:323
bool skipDefaultBuilders() const
Returns true if default builders should not be generated.
Definition Operator.cpp:185
arg_iterator arg_begin() const
Op argument (attribute or operand) iterators.
Definition Operator.cpp:240
const InferredResultType & getInferredResultType(int index) const
Return all arguments or type constraints with same type as result[index].
Definition Operator.cpp:860
const llvm::Record & getDef() const
Returns the Tablegen definition this operator was constructed from.
Definition Operator.cpp:183
VariableDecoratorIterator var_decorator_iterator
Definition Operator.h:140
const_value_iterator result_end() const
Definition Operator.cpp:197
NamedProperty & getProperty(int index)
Definition Operator.h:228
std::string getRemoverName(StringRef name) const
Returns the remove name for the accessor of name.
Definition Operator.cpp:918
Argument getArg(int index) const
Op argument (attribute or operand) accessors.
Definition Operator.cpp:390
bool hasDescription() const
Query functions for the documentation of the operator.
Definition Operator.cpp:867
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:227
constexpr StringLiteral legacyResultSegmentAttrName
Definition Operator.h:46
constexpr StringLiteral legacyOperandSegmentAttrName
Definition Operator.h:44
constexpr StringLiteral operandSegmentAttrName
The canonical and legacy names of the implicit segment-size properties.
Definition Operator.h:42
llvm::PointerUnion< NamedAttribute *, NamedProperty *, NamedTypeConstraint * > Argument
Definition Argument.h:63
constexpr StringLiteral resultSegmentAttrName
Definition Operator.h:43
Include the generated interface declarations.
OperandAttrOrProp(Kind kind, int index)
Definition Operator.h:348
A utility iterator over a list of variable decorators.
Definition Operator.h:132
VariableDecoratorIterator(const llvm::Init *const *it)
Initializes the iterator to the specified iterator.
Definition Operator.h:134
static VariableDecorator unwrap(const llvm::Init *init)
Definition Operator.cpp:901
A class used to represent the decorators of an operator variable, i.e.
Definition Operator.h:118
const llvm::Record & getDef() const
Definition Operator.h:121
const llvm::Record * def
The TableGen definition of this decorator.
Definition Operator.h:125
VariableDecorator(const llvm::Record *def)
Definition Operator.h:120