33 parsedParams.push_back(attr);
39 parsedParams.push_back(attr);
52 interleaveComma(params, printer.
getStream());
60 std::unique_ptr<DynamicTypeDefinition>
67 std::unique_ptr<DynamicTypeDefinition>
71 return std::unique_ptr<DynamicTypeDefinition>(
73 std::move(
parser), std::move(printer)));
76 DynamicTypeDefinition::DynamicTypeDefinition(StringRef nameRef,
81 : name(nameRef), dialect(dialect), verifier(std::move(verifier)),
87 : name(nameRef), dialect(dialect), ctx(dialect->
getContext()) {}
89 void DynamicTypeDefinition::registerInTypeUniquer() {
99 using KeyTy = std::pair<DynamicTypeDefinition *, ArrayRef<Attribute>>;
103 : typeDef(typeDef), params(params) {}
106 return typeDef == key.first && params == key.second;
133 return detail::TypeUniquer::getWithTypeID<DynamicType>(
134 &ctx, typeDef->
getTypeID(), typeDef, params);
144 return detail::TypeUniquer::getWithTypeID<DynamicType>(
145 &ctx, typeDef->
getTypeID(), typeDef, params);
160 if (
failed(typeDef->parser(parser, params)))
169 printer << getTypeDef()->getName();
170 getTypeDef()->printer(printer, getParams());
177 std::unique_ptr<DynamicAttrDefinition>
184 std::unique_ptr<DynamicAttrDefinition>
188 return std::unique_ptr<DynamicAttrDefinition>(
190 std::move(
parser), std::move(printer)));
193 DynamicAttrDefinition::DynamicAttrDefinition(StringRef nameRef,
198 : name(nameRef), dialect(dialect), verifier(std::move(verifier)),
204 : name(nameRef), dialect(dialect), ctx(dialect->
getContext()) {}
206 void DynamicAttrDefinition::registerInAttrUniquer() {
207 detail::AttributeUniquer::registerAttribute<DynamicAttr>(&
getContext(),
216 using KeyTy = std::pair<DynamicAttrDefinition *, ArrayRef<Attribute>>;
220 : attrDef(attrDef), params(params) {}
223 return attrDef == key.first && params == key.second;
248 return detail::AttributeUniquer::getWithTypeID<DynamicAttr>(
249 &ctx, attrDef->
getTypeID(), attrDef, params);
258 return get(attrDef, params);
273 if (
failed(attrDef->parser(parser, params)))
282 printer << getAttrDef()->getName();
283 getAttrDef()->printer(printer, getParams());
290 DynamicOpDefinition::DynamicOpDefinition(
298 &&getCanonicalizationPatternsFn,
300 : typeID(dialect->allocateTypeID()),
301 name((dialect->
getNamespace() +
"." + name).str()), dialect(dialect),
302 verifyFn(std::move(verifyFn)), verifyRegionFn(std::move(verifyRegionFn)),
303 parseFn(std::move(parseFn)), printFn(std::move(printFn)),
304 foldHookFn(std::move(foldHookFn)),
305 getCanonicalizationPatternsFn(std::move(getCanonicalizationPatternsFn)),
306 populateDefaultAttrsFn(std::move(populateDefaultAttrsFn)) {}
315 "dynamic operation do not define any parser function");
319 printer.printGenericOp(op);
323 std::move(verifyRegionFn), std::move(parseFn),
345 std::move(verifyRegionFn), std::move(parseFn),
346 std::move(printFn), std::move(foldHookFn),
347 std::move(getCanonicalizationPatternsFn),
348 std::move(populateDefaultAttrsFn));
359 &&getCanonicalizationPatternsFn,
362 name, dialect, std::move(verifyFn), std::move(verifyRegionFn),
363 std::move(parseFn), std::move(printFn), std::move(foldHookFn),
364 std::move(getCanonicalizationPatternsFn),
365 std::move(populateDefaultAttrsFn)));
377 IsExtensibleDialect(
Dialect *dialect) : Base(dialect) {}
386 addInterfaces<IsExtensibleDialect>();
390 std::unique_ptr<DynamicTypeDefinition> &&type) {
392 TypeID typeID = type->getTypeID();
393 StringRef name = type->getName();
396 assert(dialect ==
this &&
397 "trying to register a dynamic type in the wrong dialect");
400 auto registered = dynTypes.try_emplace(typeID, std::move(type)).second;
402 assert(registered &&
"type TypeID was not unique");
404 registered = nameToDynTypes.insert({name, typePtr}).second;
407 "Trying to create a new dynamic type with an existing name");
414 addType(typeID, std::move(abstractType));
415 typePtr->registerInTypeUniquer();
419 std::unique_ptr<DynamicAttrDefinition> &&attr) {
420 auto *attrPtr = attr.get();
421 auto typeID = attr->getTypeID();
422 auto name = attr->getName();
423 auto *dialect = attr->getDialect();
425 assert(dialect ==
this &&
426 "trying to register a dynamic attribute in the wrong dialect");
429 auto registered = dynAttrs.try_emplace(typeID, std::move(attr)).second;
431 assert(registered &&
"attribute TypeID was not unique");
433 registered = nameToDynAttrs.insert({name, attrPtr}).second;
436 "Trying to create a new dynamic attribute with an existing name");
444 attrPtr->registerInAttrUniquer();
448 std::unique_ptr<DynamicOpDefinition> &&op) {
449 assert(op->dialect ==
this &&
450 "trying to register a dynamic op in the wrong dialect");
451 auto hasTraitFn = [](
TypeID traitId) {
return false; };
454 op->name, *op->dialect, op->typeID, std::move(op->parseFn),
455 std::move(op->printFn), std::move(op->verifyFn),
456 std::move(op->verifyRegionFn), std::move(op->foldHookFn),
457 std::move(op->getCanonicalizationPatternsFn),
458 detail::InterfaceMap::get<>(), std::move(hasTraitFn), {},
459 std::move(op->populateDefaultAttrsFn));
463 return const_cast<Dialect *
>(dialect)
464 ->getRegisteredInterface<IsExtensibleDialect>();
476 resultType = dynType;
483 dynType.print(printer);
498 resultAttr = dynAttr;
505 dynAttr.print(printer);
static ParseResult parse(AsmParser &parser, DynamicTypeDefinition *typeDef, DynamicType &parsedType)
Parse the dynamic type parameters and construct the type.
Include the generated interface declarations.
static LogicalResult typeOrAttrParser(AsmParser &parser, SmallVectorImpl< Attribute > &parsedParams)
Default parser for dynamic attribute or type parameters.
virtual raw_ostream & getStream() const
Return the raw output stream used by this printer.
DynamicTypeDefinition * lookupTypeDefinition(StringRef name) const
Returns nullptr if the definition was not found.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
Operation is a basic unit of execution within MLIR.
The base class used for all derived interface types.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
This class represents a diagnostic that is inflight and set to be reported.
static DynamicTypeStorage * construct(TypeStorageAllocator &alloc, const KeyTy &key)
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, ArrayRef< Attribute > params) const
Check that the attribute parameters are valid.
static bool classof(Attribute attr)
Check if an attribute is a dynamic attribute.
Base storage class appearing in a Type.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
This is a utility allocator used to allocate memory for instances of derived types.
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
bool operator==(const KeyTy &key) const
bool operator==(const KeyTy &key) const
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
ArrayRef< Attribute > params
The type parameters.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it...
static ParseResult parse(AsmParser &parser, DynamicAttrDefinition *attrDef, DynamicAttr &parsedAttr)
Parse the dynamic attribute parameters and construct the attribute.
llvm::unique_function< ParseResult(AsmParser &parser, llvm::SmallVectorImpl< Attribute > &parsedAttributes) const > ParserFn
llvm::hash_code hash_value(const MPInt &x)
Redeclarations of friend declaration above to make it discoverable by lookups.
static llvm::hash_code hashKey(const KeyTy &key)
llvm::unique_function< ParseResult(OpAsmParser &, OperationState &) const > ParseAssemblyFn
virtual ParseResult parseComma()=0
Parse a , token.
static AbstractType get(Dialect &dialect)
This method is used by Dialect objects when they register the list of types they contain.
std::pair< DynamicTypeDefinition *, ArrayRef< Attribute > > KeyTy
This class provides an efficient unique identifier for a specific C++ type.
DynamicTypeDefinition * getTypeDef()
Return the type definition of the concrete type.
void registerDynamicOp(std::unique_ptr< DynamicOpDefinition > &&type)
Add a new operation defined at runtime to the dialect.
DynamicTypeStorage(DynamicTypeDefinition *typeDef, ArrayRef< Attribute > params)
llvm::unique_function< LogicalResult(function_ref< InFlightDiagnostic()>, ArrayRef< Attribute >) const > VerifierFn
static DynamicType get(DynamicTypeDefinition *typeDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic type given a dynamic type definition and type parameters.
void addAttribute(TypeID typeID, AbstractAttribute &&attrInfo)
Register an attribute instance with this dialect.
static LogicalResult printIfDynamicAttr(Attribute attr, AsmPrinter &printer)
If 'attr' is a dynamic attribute, print it.
T * allocate()
Allocate an instance of the provided type.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
static std::unique_ptr< DynamicAttrDefinition > get(StringRef name, ExtensibleDialect *dialect, VerifierFn &&verifier)
Create a new attribute definition at runtime.
This class represents an efficient way to signal success or failure.
static DynamicAttrStorage * construct(AttributeStorageAllocator &alloc, const KeyTy &key)
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalLess()=0
Parse a '<' token if present.
llvm::unique_function< LogicalResult(Operation *) const > VerifyInvariantsFn
void print(AsmPrinter &printer)
Print the dynamic attribute with the format 'attrname' if there is no parameters, or 'attrname<attr (...
llvm::unique_function< void(AsmPrinter &printer, ArrayRef< Attribute > params) const > PrinterFn
llvm::unique_function< LogicalResult(function_ref< InFlightDiagnostic()>, ArrayRef< Attribute >) const > VerifierFn
llvm::unique_function< void(Operation *, OpAsmPrinter &, StringRef) const > PrintAssemblyFn
static HasTraitFn getHasTraitFn()
Returns the function that returns true if the given Trait ID matches the IDs of any of the traits def...
DynamicTypeDefinition * typeDef
Definition of the type.
static LogicalResult printIfDynamicType(Type type, AsmPrinter &printer)
If 'type' is a dynamic type, print it.
Attributes are known-constant values of operations.
static std::unique_ptr< DynamicOpDefinition > get(StringRef name, ExtensibleDialect *dialect, OperationName::VerifyInvariantsFn &&verifyFn, OperationName::VerifyRegionInvariantsFn &&verifyRegionFn)
Create a new op at runtime.
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.
The definition of a dynamic type.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
ArrayRef< Attribute > params
The type parameters.
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
This represents an operation in an abstracted form, suitable for use with the builder APIs...
OptionalParseResult parseOptionalDynamicAttr(StringRef attrName, AsmParser &parser, Attribute &resultAttr) const
Parse the dynamic attribute 'attrName' in the dialect 'dialect'.
llvm::unique_function< void(const RegisteredOperationName &, NamedAttrList &) const > PopulateDefaultAttrsFn
static std::unique_ptr< DynamicTypeDefinition > get(StringRef name, ExtensibleDialect *dialect, VerifierFn &&verifier)
Create a new dynamic type definition.
MLIRContext * getContext() const
A dynamic attribute instance.
llvm::unique_function< void(AsmPrinter &printer, ArrayRef< Attribute > params) const > PrinterFn
llvm::unique_function< void(RewritePatternSet &, MLIRContext *) const > GetCanonicalizationPatternsFn
This base class exposes generic asm parser hooks, usable across the various derived parsers...
static AbstractAttribute get(Dialect &dialect)
This method is used by Dialect objects when they register the list of attributes they contain...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
static DynamicAttr get(DynamicAttrDefinition *attrDef, ArrayRef< Attribute > params={})
Return an instance of a dynamic attribute given a dynamic attribute definition and attribute paramete...
The definition of a dynamic op.
static detail::InterfaceMap getInterfaceMap()
Returns an interface map for the interfaces registered to this storage user.
llvm::unique_function< InFlightDiagnostic()> getDefaultDiagnosticEmitFn(MLIRContext *ctx)
Utility method to generate a callback that can be used to generate a diagnostic when checking the con...
This class implements Optional functionality for ParseResult.
static llvm::hash_code hashKey(const KeyTy &key)
A dialect that can be extended with new operations/types/attributes at runtime.
static bool classof(Type type)
Check if a type is a dynamic type.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
virtual ParseResult parseOptionalGreater()=0
Parse a '>' token if present.
llvm::unique_function< LogicalResult(Operation *) const > VerifyRegionInvariantsFn
ArrayRef< Attribute > getParams()
Return the attribute parameters.
std::pair< DynamicAttrDefinition *, ArrayRef< Attribute > > KeyTy
void print(AsmPrinter &printer)
Print the dynamic type with the format 'type' or 'type<>' if there is no parameters, or 'type<attr (,attr)*>'.
The definition of a dynamic attribute.
StringRef getNamespace() const
static void insert(Dialect &dialect)
Register a new operation in a Dialect object.
DynamicAttrDefinition * getAttrDef()
Return the attribute definition of the concrete attribute.
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'.
MLIRContext is the top-level object for a collection of MLIR operations.
DynamicAttrDefinition * attrDef
Definition of the type.
Base storage class appearing in an attribute.
ExtensibleDialect(StringRef name, MLIRContext *ctx, TypeID typeID)
llvm::unique_function< LogicalResult(Operation *, ArrayRef< Attribute >, SmallVectorImpl< OpFoldResult > &) const > FoldHookFn
This base class exposes generic asm printer hooks, usable across the various derived printers...
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, ArrayRef< Attribute > params) const
Check that the type parameters are valid.
static void typeOrAttrPrinter(AsmPrinter &printer, ArrayRef< Attribute > params)
Default printer for dynamic attribute or type parameters.
This is a "type erased" representation of a registered operation.
bool hasTrait()
Returns true if the type was registered with a particular trait.
MLIRContext & getContext() const
Return the MLIRContext in which the dynamic types is uniqued.
MLIRContext & getContext() const
Return the MLIRContext in which the dynamic attributes are uniqued.
void addType(TypeID typeID, AbstractType &&typeInfo)
Register a type instance with this dialect.
ArrayRef< Attribute > getParams()
Return the type parameters.
DynamicAttrDefinition * lookupAttrDefinition(StringRef name) const
Returns nullptr if the definition was not found.
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...
This class represents success/failure for parsing-like operations that find it important to chain tog...
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
TypeID getTypeID() const
Return the TypeID owned by this object.
llvm::unique_function< ParseResult(AsmParser &parser, llvm::SmallVectorImpl< Attribute > &parsedAttributes) const > ParserFn
DynamicAttrStorage(DynamicAttrDefinition *attrDef, ArrayRef< Attribute > params)
auto getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
static bool classof(const Dialect *dialect)
Check if the dialect is an extensible dialect.
bool hasTrait()
Returns true if the type was registered with a particular trait.
void registerDynamicType(std::unique_ptr< DynamicTypeDefinition > &&type)
Add a new type defined at runtime to the dialect.