13 #ifndef MLIR_IR_DIALECT_H
14 #define MLIR_IR_DIALECT_H
24 class DialectAsmParser;
25 class DialectAsmPrinter;
26 class DialectInterface;
103 llvm_unreachable(
"dialect has no registered attribute printing hook");
111 llvm_unreachable(
"dialect has no registered type printing hook");
118 virtual std::optional<ParseOpHook>
136 unsigned regionIndex,
145 unsigned regionIndex,
146 unsigned resultIndex,
162 auto it = registeredInterfaces.find(interfaceID);
163 return it != registeredInterfaces.end() ? it->getSecond().get() :
nullptr;
165 template <
typename InterfaceT>
167 return static_cast<InterfaceT *
>(
177 template <
typename InterfaceT>
178 typename InterfaceT::Concept *
180 return static_cast<typename InterfaceT::Concept *
>(
185 void addInterface(std::unique_ptr<DialectInterface> interface);
188 template <
typename... Args>
192 template <
typename InterfaceT,
typename... Args>
194 InterfaceT *
interface = new InterfaceT(this, std::forward<Args>(args)...);
195 addInterface(std::unique_ptr<DialectInterface>(interface));
207 Dialect(StringRef name, MLIRContext *context, TypeID id);
211 template <typename... Args>
217 (void)std::initializer_list<int>{
218 0, (RegisteredOperationName::insert<Args>(*
this), 0)...};
222 template <
typename... Args>
224 (addType<Args>(), ...);
233 template <
typename... Args>
235 (addAttribute<Args>(), ...);
251 void operator=(
Dialect &) =
delete;
254 template <
typename T>
255 void addAttribute() {
257 addAttribute(T::getTypeID(), AbstractAttribute::get<T>(*
this));
258 detail::AttributeUniquer::registerAttribute<T>(context);
262 template <
typename T>
265 addType(T::getTypeID(), AbstractType::get<T>(*
this));
266 detail::TypeUniquer::registerType<T>(context);
282 bool unknownOpsAllowed =
false;
287 bool unknownTypesAllowed =
false;
290 DenseMap<TypeID, std::unique_ptr<DialectInterface>> registeredInterfaces;
301 template <
typename T>
303 std::enable_if_t<std::is_base_of<::mlir::Dialect, T>::value>> {
304 static inline bool doit(const ::mlir::Dialect &dialect) {
305 return mlir::TypeID::get<T>() == dialect.getTypeID();
308 template <
typename T>
311 std::enable_if_t<std::is_base_of<::mlir::DialectInterface, T>::value>> {
312 static inline bool doit(const ::mlir::Dialect &dialect) {
313 return const_cast<::
mlir::Dialect &
>(dialect).getRegisteredInterface<T>();
316 template <
typename T>
320 template <
typename T>
325 template <
typename T>
327 template <
typename To>
328 static std::enable_if_t<std::is_base_of<::mlir::Dialect, To>::value, To &>
330 return static_cast<To &
>(dialect);
332 template <
typename To>
333 static std::enable_if_t<std::is_base_of<::mlir::DialectInterface, To>::value,
344 return &cast_convert_val<T, ::mlir::Dialect, ::mlir::Dialect>::doit(
This class contains all of the static information common to all instances of a registered Attribute.
This class contains all of the static information common to all instances of a registered Type.
Attributes are known-constant values of operations.
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...
This class represents an interface overridden for a single dialect.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
void addAttributes()
Register a set of attribute classes with this dialect.
void addInterfaces()
Register a set of dialect interfaces with this dialect instance.
virtual void * getRegisteredInterfaceForOp(TypeID interfaceID, OperationName opName)
Lookup an op interface for the given ID if one is registered, otherwise nullptr.
virtual Type parseType(DialectAsmParser &parser) const
Parse a type registered to this dialect.
virtual std::optional< ParseOpHook > getParseOperationHook(StringRef opName) const
Return the hook to parse an operation registered to this dialect, if any.
StringRef getNamespace() const
static bool isValidNamespace(StringRef str)
Utility function that returns if the given string is a valid dialect namespace.
virtual LogicalResult verifyRegionResultAttribute(Operation *, unsigned regionIndex, unsigned resultIndex, NamedAttribute)
Verify an attribute from this dialect on the result at 'resultIndex' for the region at 'regionIndex' ...
DialectInterface * getRegisteredInterface(TypeID interfaceID)
Lookup an interface for the given ID if one is registered, otherwise nullptr.
InterfaceT::Concept * getRegisteredInterfaceForOp(OperationName opName)
virtual LogicalResult verifyRegionArgAttribute(Operation *, unsigned regionIndex, unsigned argIndex, NamedAttribute)
Verify an attribute from this dialect on the argument at 'argIndex' for the region at 'regionIndex' o...
void addOperations()
This method is used by derived classes to add their operations to the set.
InterfaceT * getRegisteredInterface()
virtual llvm::unique_function< void(Operation *, OpAsmPrinter &printer)> getOperationPrinter(Operation *op) const
Print an operation registered to this dialect.
bool allowsUnknownTypes() const
Return true if this dialect allows for unregistered types, i.e., types prefixed with the dialect name...
virtual void printAttribute(Attribute, DialectAsmPrinter &) const
Print an attribute registered to this dialect.
void addTypes()
Register a set of type classes with this dialect.
virtual void printType(Type, DialectAsmPrinter &) const
Print a type registered to this dialect.
MLIRContext * getContext() const
void allowUnknownTypes(bool allow=true)
Enable support for unregistered types.
virtual LogicalResult verifyOperationAttribute(Operation *, NamedAttribute)
Verify an attribute from this dialect on the given operation.
void allowUnknownOperations(bool allow=true)
Enable support for unregistered operations.
bool allowsUnknownOperations() const
Returns true if this dialect allows for unregistered operations, i.e.
virtual Attribute parseAttribute(DialectAsmParser &parser, Type type) const
Parse an attribute registered to this dialect.
void addInterface(std::unique_ptr< DialectInterface > interface)
Register a dialect interface with this dialect instance.
TypeID getTypeID() const
Returns the unique identifier that corresponds to this dialect.
friend void registerDialect()
Dialect(StringRef name, MLIRContext *context, TypeID id)
The constructor takes a unique namespace for this dialect as well as the context to bind to.
InterfaceT & addInterface(Args &&...args)
virtual void getCanonicalizationPatterns(RewritePatternSet &results) const
Register dialect-wide canonicalization patterns.
virtual Operation * materializeConstant(OpBuilder &builder, Attribute value, Type type, Location loc)
Registered hook to materialize a single constant operation from a given attribute value with the desi...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttribute represents a combination of a name and an Attribute value.
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...
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
This class represents success/failure for parsing-like operations that find it important to chain tog...
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...
Include the generated interface declarations.
@ Type
An inlay hint that for a type annotation.
This header declares functions that assit transformations in the MemRef dialect.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
static auto & doit(::mlir::Dialect &dialect)
static std::enable_if_t< std::is_base_of<::mlir::Dialect, To >::value, To & > doitImpl(::mlir::Dialect &dialect)
static std::enable_if_t< std::is_base_of<::mlir::DialectInterface, To >::value, To & > doitImpl(::mlir::Dialect &dialect)
static auto doit(::mlir::Dialect *dialect)
static bool doit(const ::mlir::Dialect &dialect)
static bool doit(const ::mlir::Dialect &dialect)
This class represents an efficient way to signal success or failure.
This represents an operation in an abstracted form, suitable for use with the builder APIs.