MLIR: include/mlir/IR/ExtensibleDialect.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #ifndef MLIR_IR_EXTENSIBLEDIALECT_H
21 #define MLIR_IR_EXTENSIBLEDIALECT_H
22
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include
32
33 namespace mlir {
34 class AsmParser;
35 class AsmPrinter;
36 class DynamicAttr;
37 class DynamicType;
38 class ExtensibleDialect;
39 class MLIRContext;
40 class OptionalParseResult;
41
42 namespace detail {
43 struct DynamicAttrStorage;
44 struct DynamicTypeStorage;
45 }
46
47
48
49
50
51
52
53
54
55
57 public:
58 using VerifierFn = llvm::unique_function<LogicalResult(
60 using ParserFn = llvm::unique_function<ParseResult(
62 const>;
63 using PrinterFn = llvm::unique_function<void(
65
66
67
68 static std::unique_ptr
70 static std::unique_ptr
73
74
75
76
78
79
81
82
84
85
88 return verifier(emitError, params);
89 }
90
91
93
94
95
96 StringRef getName() const { return name; }
97
98
100
101 private:
105
106
107
108
109
111
112
113 void registerInAttrUniquer();
114
115
116 std::string name;
117
118
120
121
122
124
125
126
128
129
130
132
133
135
138 };
139
140
141
142
143
144 namespace AttributeTrait {
145 template
147 }
148
149
150
151
152
153
154
155
156
158 detail::DynamicAttrStorage,
159 AttributeTrait::IsDynamicAttr> {
160 public:
161
163
164
165
166
169
170
171
172
173
177
178
180
181
183
184
187 }
188
189
191
192
193
194
197
198
199
201 };
202
203
204
205
206
207
208
209
210
211
213 public:
214 using VerifierFn = llvm::unique_function<LogicalResult(
216 using ParserFn = llvm::unique_function<ParseResult(
218 const>;
219 using PrinterFn = llvm::unique_function<void(
221
222
223
224 static std::unique_ptr
226 static std::unique_ptr
229
230
231
232
234
235
237
238
240
241
244 return verifier(emitError, params);
245 }
246
247
249
250
251
252 StringRef getName() const { return name; }
253
254
256
257 private:
261
262
263
264
265
267
268
269 void registerInTypeUniquer();
270
271
272 std::string name;
273
274
276
277
278
280
281
282
284
285
286
288
289
291
294 };
295
296
297
298
299
300 namespace TypeTrait {
301 template
303 };
304 }
305
306
307
308
309
310
311
313 : public Type::TypeBase<DynamicType, Type, detail::DynamicTypeStorage,
314 TypeTrait::IsDynamicType> {
315 public:
316
318
319
320
321
324
325
326
327
331
332
334
335
337
338
341 }
342
343
345
346
347
348
351
352
353
355 };
356
357
358
359
360
361
362
363
364
365
366
368 public:
371
372
373
374 static std::unique_ptr
378 static std::unique_ptr
384 static std::unique_ptr
393
394
396
397
398
399
401 verifyFn = std::move(verify);
402 }
403
404
405
406
408 verifyRegionFn = std::move(verify);
409 }
410
411
413 parseFn = std::move(parse);
414 }
415
416
418 printFn = std::move(print);
419 }
420
421
422
424 foldHookFn = std::move(foldHook);
425 }
426
427
428
432 }
433
434
438 }
439
442 return foldHookFn(op, attrs, results);
443 }
446 getCanonicalizationPatternsFn(set, context);
447 }
451 return parseFn(parser, state);
452 };
453 }
456 populateDefaultAttrsFn(name, attrs);
457 }
459 StringRef name) final {
460 printFn(op, printer, name);
461 }
464 return verifyRegionFn(op);
465 }
466
467
469 StringRef name) final {
470 llvm::report_fatal_error("Unsupported getInherentAttr on Dynamic dialects");
471 }
473 llvm::report_fatal_error("Unsupported setInherentAttr on Dynamic dialects");
474 }
476 LogicalResult
479 return success();
480 }
487
488 LogicalResult
492 emitError() << "extensible Dialects don't support properties";
493 return failure();
494 }
499
500 private:
510
511
513
521
523 };
524
525
526
527
528
529
530
532 public:
534
535
537
538
540
541
542 void registerDynamicOp(std::unique_ptr &&type);
543
544
546
547
549 return nameToDynTypes.lookup(name);
550 }
551
552
554 auto it = dynTypes.find(id);
555 if (it == dynTypes.end())
556 return nullptr;
557 return it->second.get();
558 }
559
560
562 return nameToDynAttrs.lookup(name);
563 }
564
565
567 auto it = dynAttrs.find(id);
568 if (it == dynAttrs.end())
569 return nullptr;
570 return it->second.get();
571 }
572
573 protected:
574
575
576
577
578
581 Type &resultType) const;
582
583
584
585
587
588
589
590
591
592
596
597
598
599
601
602 private:
603
605
606
607 llvm::StringMap<DynamicTypeDefinition *> nameToDynTypes;
608
609
611
612
613 llvm::StringMap<DynamicAttrDefinition *> nameToDynAttrs;
614
615
617
618
619 TypeID allocateTypeID() { return typeIDAllocator.allocate(); }
620
621
622 TypeIDAllocator typeIDAllocator;
623 };
624
625
626
627
628
629
630
632 public:
634
636
637
639
642
644 Type type) const override;
647 };
648 }
649
650 namespace llvm {
651
652
653 template <>
655 static inline bool doit(const ::mlir::Dialect &dialect) {
657 }
658 };
659
660
661
662 template <>
664 static inline bool doit(const ::mlir::Dialect &dialect) {
666 }
667 };
668 }
669
670 #endif
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
TypeID getTypeID()
Return a unique identifier for the concrete attribute type.
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
The definition of a dynamic attribute.
ExtensibleDialect * getDialect() const
Return the dialect defining the attribute.
void setVerifyFn(VerifierFn &&verify)
Sets the verifier function for this attribute.
static std::unique_ptr< DynamicAttrDefinition > get(StringRef name, ExtensibleDialect *dialect, VerifierFn &&verifier)
Create a new attribute definition at runtime.
llvm::unique_function< void(AsmPrinter &printer, ArrayRef< Attribute > params) const > PrinterFn
MLIRContext & getContext() const
Return the MLIRContext in which the dynamic attributes are uniqued.
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, ArrayRef< Attribute > params) const
Check that the attribute parameters are valid.
void setParseFn(ParserFn &&parse)
Sets the static hook for parsing this attribute assembly.
StringRef getName() const
Return the name of the attribute, in the format 'attrname' and not 'dialectname.attrname'.
llvm::unique_function< LogicalResult(function_ref< InFlightDiagnostic()>, ArrayRef< Attribute >) const > VerifierFn
void setPrintFn(PrinterFn &&print)
Sets the static hook for printing this attribute assembly.
llvm::unique_function< ParseResult(AsmParser &parser, llvm::SmallVectorImpl< Attribute > &parsedAttributes) const > ParserFn
A dynamic attribute instance.
static DynamicAttr getChecked(function_ref< InFlightDiagnostic()> emitError, DynamicAttrDefinition *attrDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic attribute given a dynamic attribute definition and attribute paramete...
ArrayRef< Attribute > getParams()
Return the attribute parameters.
static DynamicAttr get(DynamicAttrDefinition *attrDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic attribute given a dynamic attribute definition and attribute paramete...
void print(AsmPrinter &printer)
Print the dynamic attribute with the format 'attrname' if there is no parameters, or 'attrname<attr (...
static bool classof(Attribute attr)
Check if an attribute is a dynamic attribute.
static ParseResult parse(AsmParser &parser, DynamicAttrDefinition *attrDef, DynamicAttr &parsedAttr)
Parse the dynamic attribute parameters and construct the attribute.
DynamicAttrDefinition * getAttrDef()
Return the attribute definition of the concrete attribute.
static bool isa(Attribute attr, DynamicAttrDefinition *attrDef)
Check if an attribute is a specific dynamic attribute.
A dialect that can be defined at runtime.
virtual void printAttribute(Attribute attr, DialectAsmPrinter &printer) const override
Print an attribute registered to this dialect.
DynamicDialect(StringRef name, MLIRContext *ctx)
virtual Attribute parseAttribute(DialectAsmParser &parser, Type type) const override
Parse an attribute registered to this dialect.
virtual void printType(Type type, DialectAsmPrinter &printer) const override
Print a type registered to this dialect.
static bool classof(const Dialect *dialect)
Check if the dialect is an extensible dialect.
virtual Type parseType(DialectAsmParser &parser) const override
Parse a type registered to this dialect.
The definition of a dynamic op.
LogicalResult verifyInvariants(Operation *op) final
TypeID getTypeID()
Returns the op typeID.
LogicalResult foldHook(Operation *op, ArrayRef< Attribute > attrs, SmallVectorImpl< OpFoldResult > &results) final
LogicalResult verifyRegionInvariants(Operation *op) final
void setParseFn(OperationName::ParseAssemblyFn &&parse)
Sets the static hook for parsing this op assembly.
bool hasTrait(TypeID id) final
void setVerifyRegionFn(OperationName::VerifyRegionInvariantsFn &&verify)
Sets the region verifier function for this operation.
static std::unique_ptr< DynamicOpDefinition > get(StringRef name, ExtensibleDialect *dialect, OperationName::VerifyInvariantsFn &&verifyFn, OperationName::VerifyRegionInvariantsFn &&verifyRegionFn)
Create a new op at runtime.
void printAssembly(Operation *op, OpAsmPrinter &printer, StringRef name) final
Attribute getPropertiesAsAttr(Operation *op) final
void initProperties(OperationName opName, OpaqueProperties storage, OpaqueProperties init) final
std::optional< Attribute > getInherentAttr(Operation *op, StringRef name) final
Implementation for properties (unsupported right now here).
llvm::unique_function< void(RewritePatternSet &, MLIRContext *) const > GetCanonicalizationPatternsFn
OperationName::ParseAssemblyFn getParseAssemblyFn() final
void setFoldHookFn(OperationName::FoldHookFn &&foldHook)
Sets the hook implementing a generalized folder for the op.
void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final
LogicalResult setPropertiesFromAttr(OperationName opName, OpaqueProperties properties, Attribute attr, function_ref< InFlightDiagnostic()> emitError) final
LogicalResult verifyInherentAttrs(OperationName opName, NamedAttrList &attributes, function_ref< InFlightDiagnostic()> emitError) final
void populateDefaultAttrs(const OperationName &name, NamedAttrList &attrs) final
bool compareProperties(OpaqueProperties, OpaqueProperties) final
void setInherentAttr(Operation *op, StringAttr name, Attribute value) final
void populateInherentAttrs(Operation *op, NamedAttrList &attrs) final
void deleteProperties(OpaqueProperties prop) final
int getOpPropertyByteSize() final
void setVerifyFn(OperationName::VerifyInvariantsFn &&verify)
Sets the verifier function for this operation.
void populateDefaultProperties(OperationName opName, OpaqueProperties properties) final
llvm::hash_code hashProperties(OpaqueProperties prop) final
void setGetCanonicalizationPatternsFn(GetCanonicalizationPatternsFn &&getCanonicalizationPatterns)
Set the hook returning any canonicalization pattern rewrites that the op supports,...
void setPrintFn(OperationName::PrintAssemblyFn &&print)
Sets the static hook for printing this op assembly.
void setPopulateDefaultAttrsFn(OperationName::PopulateDefaultAttrsFn &&populateDefaultAttrs)
Set the hook populating default attributes.
void getCanonicalizationPatterns(RewritePatternSet &set, MLIRContext *context) final
The definition of a dynamic type.
void setPrintFn(PrinterFn &&print)
Sets the static hook for printing this type assembly.
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, ArrayRef< Attribute > params) const
Check that the type parameters are valid.
StringRef getName() const
Return the name of the type, in the format 'typename' and not 'dialectname.typename'.
void setVerifyFn(VerifierFn &&verify)
Sets the verifier function for this type.
void setParseFn(ParserFn &&parse)
Sets the static hook for parsing this type assembly.
MLIRContext & getContext() const
Return the MLIRContext in which the dynamic types is uniqued.
ExtensibleDialect * getDialect() const
Return the dialect defining the type.
llvm::unique_function< void(AsmPrinter &printer, ArrayRef< Attribute > params) const > PrinterFn
llvm::unique_function< ParseResult(AsmParser &parser, llvm::SmallVectorImpl< Attribute > &parsedAttributes) const > ParserFn
llvm::unique_function< LogicalResult(function_ref< InFlightDiagnostic()>, ArrayRef< Attribute >) const > VerifierFn
static std::unique_ptr< DynamicTypeDefinition > get(StringRef name, ExtensibleDialect *dialect, VerifierFn &&verifier)
Create a new dynamic type definition.
static DynamicType getChecked(function_ref< InFlightDiagnostic()> emitError, DynamicTypeDefinition *typeDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic type given a dynamic type definition and type parameters.
ArrayRef< Attribute > getParams()
Return the type parameters.
static bool isa(Type type, DynamicTypeDefinition *typeDef)
Check if a type is a specific dynamic type.
void print(AsmPrinter &printer)
Print the dynamic type with the format 'type' or 'type<>' if there is no parameters,...
DynamicTypeDefinition * getTypeDef()
Return the type definition of the concrete type.
static DynamicType get(DynamicTypeDefinition *typeDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic type given a dynamic type definition and type parameters.
static ParseResult parse(AsmParser &parser, DynamicTypeDefinition *typeDef, DynamicType &parsedType)
Parse the dynamic type parameters and construct the type.
static bool classof(Type type)
Check if a type is a dynamic type.
A dialect that can be extended with new operations/types/attributes at runtime.
static bool classof(const Dialect *dialect)
Check if the dialect is an extensible dialect.
void registerDynamicOp(std::unique_ptr< DynamicOpDefinition > &&type)
Add a new operation defined at runtime to the dialect.
static LogicalResult printIfDynamicType(Type type, AsmPrinter &printer)
If 'type' is a dynamic type, print it.
void registerDynamicType(std::unique_ptr< DynamicTypeDefinition > &&type)
Add a new type defined at runtime to the dialect.
static LogicalResult printIfDynamicAttr(Attribute attr, AsmPrinter &printer)
If 'attr' is a dynamic attribute, print it.
OptionalParseResult parseOptionalDynamicAttr(StringRef attrName, AsmParser &parser, Attribute &resultAttr) const
Parse the dynamic attribute 'attrName' in the dialect 'dialect'.
DynamicAttrDefinition * lookupAttrDefinition(TypeID id) const
Returns nullptr if the definition was not found.
DynamicTypeDefinition * lookupTypeDefinition(TypeID id) const
Returns nullptr if the definition was not found.
ExtensibleDialect(StringRef name, MLIRContext *ctx, TypeID typeID)
DynamicAttrDefinition * lookupAttrDefinition(StringRef name) const
Returns nullptr if the definition was not found.
void registerDynamicAttr(std::unique_ptr< DynamicAttrDefinition > &&attr)
Add a new attribute defined at runtime to the dialect.
OptionalParseResult parseOptionalDynamicType(StringRef typeName, AsmParser &parser, Type &resultType) const
Parse the dynamic type 'typeName' in the dialect 'dialect'.
DynamicTypeDefinition * lookupTypeDefinition(StringRef name) const
Returns nullptr if the definition was not found.
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Simple wrapper around a void* in order to express generically how to pass in op properties through AP...
StringAttr name
The name of the operation.
TypeID typeID
The unique identifier of the derived Op class.
Dialect * dialect
The following fields are only populated when the operation is registered.
llvm::unique_function< LogicalResult(Operation *) const > VerifyInvariantsFn
llvm::unique_function< ParseResult(OpAsmParser &, OperationState &)> ParseAssemblyFn
llvm::unique_function< LogicalResult(Operation *) const > VerifyRegionInvariantsFn
llvm::unique_function< void(Operation *, OpAsmPrinter &, StringRef) const > PrintAssemblyFn
llvm::unique_function< LogicalResult(Operation *, ArrayRef< Attribute >, SmallVectorImpl< OpFoldResult > &) const > FoldHookFn
llvm::unique_function< void(const OperationName &, NamedAttrList &) const > PopulateDefaultAttrsFn
Operation is the basic unit of execution within MLIR.
This class implements Optional functionality for ParseResult.
Defines a TypeID for each instance of this class by using a pointer to the instance.
TypeID getTypeID() const
Return the TypeID owned by this object.
TypeID allocate()
Allocate a new TypeID, that is ensured to be unique for the lifetime of the TypeIDAllocator.
This class provides an efficient unique identifier for a specific C++ type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
TypeID getTypeID()
Return a unique identifier for the concrete type.
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
Helper class for implementing traits for storage classes.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
static bool doit(const ::mlir::Dialect &dialect)
static bool doit(const ::mlir::Dialect &dialect)
This represents an operation in an abstracted form, suitable for use with the builder APIs.