13 #ifndef MLIR_IR_DIALECTREGISTRY_H
14 #define MLIR_IR_DIALECTREGISTRY_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/MapVector.h"
56 virtual std::unique_ptr<DialectExtensionBase>
clone()
const = 0;
63 : dialectNames(dialectNames) {}
73 template <
typename DerivedT,
typename... DialectsT>
80 std::unique_ptr<DialectExtensionBase>
clone() const final {
81 return std::make_unique<DerivedT>(
static_cast<const DerivedT &
>(*
this));
87 ArrayRef<StringRef>({DialectsT::getDialectNamespace()...})) {}
92 unsigned dialectIdx = 0;
93 auto derivedDialects = std::tuple<DialectsT *...>{
94 static_cast<DialectsT *
>(dialects[dialectIdx++])...};
95 std::apply([&](DialectsT *...dialect) {
apply(context, dialect...); },
100 namespace dialect_extension_detail {
106 TypeID interfaceRequestorID,
108 StringRef interfaceName);
114 TypeID interfaceRequestorID,
122 template <
typename ConcreteT,
typename InterfaceT>
125 InterfaceT::getInterfaceID());
141 std::map<std::string, std::pair<TypeID, DialectAllocatorFunction>,
147 template <
typename ConcreteDialect>
149 insert(TypeID::get<ConcreteDialect>(),
150 ConcreteDialect::getDialectNamespace(),
158 template <
typename ConcreteDialect,
typename OtherDialect,
159 typename... MoreDialects>
161 insert<ConcreteDialect>();
162 insert<OtherDialect, MoreDialects...>();
185 for (
const auto &nameAndRegistrationIt : registry)
186 destination.
insert(nameAndRegistrationIt.second.first,
187 nameAndRegistrationIt.first,
188 nameAndRegistrationIt.second.second);
190 for (
const auto &extension : extensions)
191 destination.extensions.try_emplace(extension.first,
192 extension.second->clone());
197 return llvm::map_range(
199 [](
const MapTy::value_type &item) -> StringRef {
return item.first; });
212 std::unique_ptr<DialectExtensionBase> extension) {
213 return extensions.try_emplace(extensionID, std::move(extension)).second;
217 template <
typename... ExtensionsT>
219 (
addExtension(TypeID::get<ExtensionsT>(), std::make_unique<ExtensionsT>()),
233 template <
typename... DialectsT>
235 using ExtensionFnT = void (*)(
MLIRContext *, DialectsT *...);
238 Extension(
const Extension &) =
default;
239 Extension(ExtensionFnT extensionFn)
241 extensionFn(extensionFn) {}
242 ~Extension()
override =
default;
244 void apply(
MLIRContext *context, DialectsT *...dialects)
const final {
245 extensionFn(context, dialects...);
247 ExtensionFnT extensionFn;
250 reinterpret_cast<const void *
>(extensionFn)),
251 std::make_unique<Extension>(extensionFn));
260 llvm::MapVector<TypeID, std::unique_ptr<DialectExtensionBase>> extensions;
This class represents an opaque dialect extension.
ArrayRef< StringRef > getRequiredDialects() const
Return the dialects that our required by this extension to be loaded before applying.
virtual ~DialectExtensionBase()
DialectExtensionBase(ArrayRef< StringRef > dialectNames)
Initialize the extension with a set of required dialects.
virtual void apply(MLIRContext *context, MutableArrayRef< Dialect * > dialects) const =0
Apply this extension to the given context and the required dialects.
virtual std::unique_ptr< DialectExtensionBase > clone() const =0
Return a copy of this extension.
This class represents a dialect extension anchored on the given set of dialects.
virtual void apply(MLIRContext *context, DialectsT *...dialects) const =0
Applies this extension to the given context and set of required dialects.
std::unique_ptr< DialectExtensionBase > clone() const final
Return a copy of this extension.
void apply(MLIRContext *context, MutableArrayRef< Dialect * > dialects) const final
Override the base apply method to allow providing the exact dialect types.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(void(*extensionFn)(MLIRContext *, DialectsT *...))
Add an extension function that requires the given dialects.
bool isSubsetOf(const DialectRegistry &rhs) const
Returns true if the current registry is a subset of 'rhs', i.e.
DialectAllocatorFunctionRef getDialectAllocator(StringRef name) const
Return an allocation function for constructing the dialect identified by its namespace,...
void appendTo(DialectRegistry &destination) const
void insertDynamic(StringRef name, const DynamicDialectPopulationFunction &ctor)
Add a new dynamic dialect constructor in the registry.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
auto getDialectNames() const
Return the names of dialects known to this registry.
void addExtensions()
Add the given extensions to the registry.
void applyExtensions(Dialect *dialect) const
Apply any held extensions that require the given dialect.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
A dialect that can be defined at runtime.
MLIRContext is the top-level object for a collection of MLIR operations.
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
This class provides an efficient unique identifier for a specific C++ type.
static TypeID getFromOpaquePointer(const void *pointer)
bool hasPromisedInterface(Dialect &dialect, TypeID interfaceRequestorID, TypeID interfaceID)
Checks if a promise has been made for the interface/requestor pair.
void handleAdditionOfUndefinedPromisedInterface(Dialect &dialect, TypeID interfaceRequestorID, TypeID interfaceID)
Checks if the given interface, which is attempting to be attached, is a promised interface of this di...
void handleUseOfUndefinedPromisedInterface(Dialect &dialect, TypeID interfaceRequestorID, TypeID interfaceID, StringRef interfaceName)
Checks if the given interface, which is attempting to be used, is a promised interface of this dialec...
Include the generated interface declarations.
std::function< void(MLIRContext *, DynamicDialect *)> DynamicDialectPopulationFunction
std::function< Dialect *(MLIRContext *)> DialectAllocatorFunction