MLIR
20.0.0git
|
MLIRContext is the top-level object for a collection of MLIR operations. More...
#include "mlir/IR/MLIRContext.h"
Public Types | |
enum class | Threading { DISABLED , ENABLED } |
using | HandlerTy = std::function< void(function_ref< void()>, const tracing::Action &)> |
Signatures for the action handler that can be registered with the context. More... | |
Public Member Functions | |
MLIRContext (Threading multithreading=Threading::ENABLED) | |
Create a new Context. More... | |
MLIRContext (const DialectRegistry ®istry, Threading multithreading=Threading::ENABLED) | |
~MLIRContext () | |
std::vector< Dialect * > | getLoadedDialects () |
Return information about all IR dialects loaded in the context. More... | |
const DialectRegistry & | getDialectRegistry () |
Return the dialect registry associated with this context. More... | |
void | appendDialectRegistry (const DialectRegistry ®istry) |
Append the contents of the given dialect registry to the registry associated with this context. More... | |
std::vector< StringRef > | getAvailableDialects () |
Return information about all available dialects in the registry in this context. More... | |
Dialect * | getLoadedDialect (StringRef name) |
Get a registered IR dialect with the given namespace. More... | |
template<typename T > | |
T * | getLoadedDialect () |
Get a registered IR dialect for the given derived dialect type. More... | |
template<typename T > | |
T * | getOrLoadDialect () |
Get (or create) a dialect for the given derived dialect type. More... | |
template<typename Dialect > | |
void | loadDialect () |
Load a dialect in the context. More... | |
template<typename Dialect , typename OtherDialect , typename... MoreDialects> | |
void | loadDialect () |
Load a list dialects in the context. More... | |
DynamicDialect * | getOrLoadDynamicDialect (StringRef dialectNamespace, function_ref< void(DynamicDialect *)> ctor) |
Get (or create) a dynamic dialect for the given name. More... | |
void | loadAllAvailableDialects () |
Load all dialects available in the registry in this context. More... | |
Dialect * | getOrLoadDialect (StringRef name) |
Get (or create) a dialect for the given derived dialect name. More... | |
bool | allowsUnregisteredDialects () |
Return true if we allow to create operation for unregistered dialects. More... | |
void | allowUnregisteredDialects (bool allow=true) |
Enables creating operations in unregistered dialects. More... | |
bool | isMultithreadingEnabled () |
Return true if multi-threading is enabled by the context. More... | |
void | disableMultithreading (bool disable=true) |
Set the flag specifying if multi-threading is disabled by the context. More... | |
void | enableMultithreading (bool enable=true) |
void | setThreadPool (llvm::ThreadPoolInterface &pool) |
Set a new thread pool to be used in this context. More... | |
unsigned | getNumThreads () |
Return the number of threads used by the thread pool in this context. More... | |
llvm::ThreadPoolInterface & | getThreadPool () |
Return the thread pool used by this context. More... | |
bool | shouldPrintOpOnDiagnostic () |
Return true if we should attach the operation to diagnostics emitted via Operation::emit. More... | |
void | printOpOnDiagnostic (bool enable) |
Set the flag specifying if we should attach the operation to diagnostics emitted via Operation::emit. More... | |
bool | shouldPrintStackTraceOnDiagnostic () |
Return true if we should attach the current stacktrace to diagnostics when emitted. More... | |
void | printStackTraceOnDiagnostic (bool enable) |
Set the flag specifying if we should attach the current stacktrace when emitting diagnostics. More... | |
ArrayRef< RegisteredOperationName > | getRegisteredOperations () |
Return a sorted array containing the information about all registered operations. More... | |
ArrayRef< RegisteredOperationName > | getRegisteredOperationsByDialect (StringRef dialectName) |
Return a sorted array containing the information for registered operations filtered by dialect name. More... | |
bool | isOperationRegistered (StringRef name) |
Return true if this operation name is registered in this context. More... | |
MLIRContextImpl & | getImpl () |
DiagnosticEngine & | getDiagEngine () |
Returns the diagnostic engine for this context. More... | |
StorageUniquer & | getAffineUniquer () |
Returns the storage uniquer used for creating affine constructs. More... | |
StorageUniquer & | getTypeUniquer () |
Returns the storage uniquer used for constructing type storage instances. More... | |
StorageUniquer & | getAttributeUniquer () |
Returns the storage uniquer used for constructing attribute storage instances. More... | |
void | enterMultiThreadedExecution () |
These APIs are tracking whether the context will be used in a multithreading environment: this has no effect other than enabling assertions on misuses of some APIs. More... | |
void | exitMultiThreadedExecution () |
Dialect * | getOrLoadDialect (StringRef dialectNamespace, TypeID dialectID, function_ref< std::unique_ptr< Dialect >()> ctor) |
Get a dialect for the provided namespace and TypeID: abort the program if a dialect exist for this namespace with different TypeID. More... | |
llvm::hash_code | getRegistryHash () |
Returns a hash of the registry of the context that may be used to give a rough indicator of if the state of the context registry has changed. More... | |
void | registerActionHandler (HandlerTy handler) |
Register a handler for handling actions that are dispatched through this context. More... | |
bool | hasActionHandler () |
Return true if a valid ActionHandler is set. More... | |
void | executeAction (function_ref< void()> actionFn, const tracing::Action &action) |
Dispatch the provided action to the handler if any, or just execute it. More... | |
template<typename ActionTy , typename... Args> | |
void | executeAction (function_ref< void()> actionFn, ArrayRef< IRUnit > irUnits, Args &&...args) |
Dispatch the provided action to the handler if any, or just execute it. More... | |
MLIRContext is the top-level object for a collection of MLIR operations.
It holds immortal uniqued objects like types, and the tables used to unique them.
MLIRContext gets a redundant "MLIR" prefix because otherwise it ends up with a very generic name ("Context") and because it is uncommon for clients to interact with it.
The context wrap some multi-threading facilities, and in particular by default it will implicitly create a thread pool. This can be undesirable if multiple context exists at the same time or if a process will be long-lived and create and destroy contexts. To control better thread spawning, an externally owned ThreadPool can be injected in the context. For example:
llvm::DefaultThreadPool myThreadPool; while (auto *request = nextCompilationRequests()) { MLIRContext ctx(registry, MLIRContext::Threading::DISABLED); ctx.setThreadPool(myThreadPool); processRequest(request, cxt); }
Definition at line 60 of file MLIRContext.h.
using mlir::MLIRContext::HandlerTy = std::function<void(function_ref<void()>, const tracing::Action &)> |
Signatures for the action handler that can be registered with the context.
Definition at line 253 of file MLIRContext.h.
|
strong |
Enumerator | |
---|---|
DISABLED | |
ENABLED |
Definition at line 62 of file MLIRContext.h.
|
explicit |
Create a new Context.
Definition at line 297 of file MLIRContext.cpp.
|
explicit |
Types. Floating-point Types.
Index Type.
Integer Types.
None Type.
of the above types internally. Unknown Location Attribute.
Bool Attributes.
Unit Attribute.
The empty dictionary attribute.
The empty string attribute.
Definition at line 300 of file MLIRContext.cpp.
References mlir::DialectRegistry::appendTo(), clOptions, printOpOnDiagnostic(), and printStackTraceOnDiagnostic().
|
default |
bool MLIRContext::allowsUnregisteredDialects | ( | ) |
Return true if we allow to create operation for unregistered dialects.
Definition at line 590 of file MLIRContext.cpp.
Referenced by doVerifyRoundTrip().
void MLIRContext::allowUnregisteredDialects | ( | bool | allow = true | ) |
Enables creating operations in unregistered dialects.
This option is heavily discouraged: it is convenient during testing but it is not a good practice to use it in production code. Some system invariants can be broken (like loading a dialect after creating operations) without being caught by assertions or other means.
Definition at line 594 of file MLIRContext.cpp.
Referenced by mlir::lsp::MLIRServer::convertFromBytecode(), doVerifyRoundTrip(), and mlir::mlirQueryMain().
void MLIRContext::appendDialectRegistry | ( | const DialectRegistry & | registry | ) |
Append the contents of the given dialect registry to the registry associated with this context.
Definition at line 419 of file MLIRContext.cpp.
References mlir::DialectRegistry::appendTo(), mlir::DialectRegistry::applyExtensions(), and mlir::DialectRegistry::isSubsetOf().
Referenced by doVerifyRoundTrip(), loadIRDLDialects(), mlir::registerAllDialects(), mlir::registerAMXDialectTranslation(), mlir::registerArmNeonDialectTranslation(), mlir::registerArmSMEDialectTranslation(), mlir::registerArmSVEDialectTranslation(), mlir::registerBuiltinDialectTranslation(), mlir::registerGPUDialectTranslation(), mlir::registerLLVMDialectImport(), mlir::registerLLVMDialectTranslation(), mlir::linalg::registerMeshShardingInterfaceExternalModels(), mlir::registerNVVMDialectImport(), mlir::registerNVVMDialectTranslation(), mlir::NVVM::registerNVVMTargetInterfaceExternalModels(), mlir::registerOpenACCDialectTranslation(), mlir::registerOpenMPDialectTranslation(), mlir::registerROCDLDialectTranslation(), mlir::ROCDL::registerROCDLTargetInterfaceExternalModels(), mlir::registerSPIRVDialectTranslation(), mlir::spirv::registerSPIRVTargetInterfaceExternalModels(), mlir::registerVCIXDialectTranslation(), mlir::registerX86VectorDialectTranslation(), and mlir::PassManager::run().
void MLIRContext::disableMultithreading | ( | bool | disable = true | ) |
Set the flag specifying if multi-threading is disabled by the context.
The command line debugging flag --mlir-disable-threading
is overriding this call and making it a no-op!
Definition at line 607 of file MLIRContext.cpp.
References isThreadingGloballyDisabled().
Referenced by mlir::PassReproducerOptions::apply(), enableMultithreading(), and performActions().
|
inline |
Definition at line 152 of file MLIRContext.h.
References disableMultithreading().
Referenced by performActions(), and setThreadPool().
void MLIRContext::enterMultiThreadedExecution | ( | ) |
These APIs are tracking whether the context will be used in a multithreading environment: this has no effect other than enabling assertions on misuses of some APIs.
Definition at line 668 of file MLIRContext.cpp.
Referenced by mlir::PassManager::run().
|
inline |
Dispatch the provided action to the handler if any, or just execute it.
Definition at line 274 of file MLIRContext.h.
References hasActionHandler().
|
inline |
Dispatch the provided action to the handler if any, or just execute it.
Definition at line 264 of file MLIRContext.h.
References hasActionHandler().
Referenced by mlir::PatternApplicator::matchAndRewrite(), and mlir::walkAndApplyPatterns().
void MLIRContext::exitMultiThreadedExecution | ( | ) |
Definition at line 673 of file MLIRContext.cpp.
Referenced by mlir::PassManager::run().
StorageUniquer & MLIRContext::getAffineUniquer | ( | ) |
Returns the storage uniquer used for creating affine constructs.
Definition at line 1226 of file MLIRContext.cpp.
References mlir::OperationName::getImpl().
Referenced by mlir::AffineExpr::ceilDiv(), mlir::AffineExpr::floorDiv(), mlir::getAffineConstantExpr(), getAffineDimOrSymbol(), mlir::AffineExpr::operator%(), mlir::AffineExpr::operator*(), and mlir::AffineExpr::operator+().
StorageUniquer & MLIRContext::getAttributeUniquer | ( | ) |
Returns the storage uniquer used for constructing attribute storage instances.
This should not be used directly.
Definition at line 1165 of file MLIRContext.cpp.
References mlir::OperationName::getImpl().
Referenced by mlir::detail::AttributeUniquer::getWithTypeID(), mlir::detail::AttributeUniquer::mutate(), and mlir::detail::AttributeUniquer::registerAttribute().
std::vector< StringRef > MLIRContext::getAvailableDialects | ( | ) |
Return information about all available dialects in the registry in this context.
Definition at line 448 of file MLIRContext.cpp.
Referenced by loadAllAvailableDialects(), and mlir::translateLLVMIRToModule().
DiagnosticEngine & MLIRContext::getDiagEngine | ( | ) |
Returns the diagnostic engine for this context.
Definition at line 413 of file MLIRContext.cpp.
References mlir::MLIRContextImpl::diagEngine, and getImpl().
Referenced by emitDiag(), mlir::detail::ParallelDiagnosticHandlerImpl::ParallelDiagnosticHandlerImpl(), mlir::ScopedDiagnosticHandler::setHandler(), and mlir::detail::ParallelDiagnosticHandlerImpl::~ParallelDiagnosticHandlerImpl().
const DialectRegistry & MLIRContext::getDialectRegistry | ( | ) |
Return the dialect registry associated with this context.
Definition at line 432 of file MLIRContext.cpp.
Referenced by doVerifyRoundTrip(), and roundTripModule().
|
inline |
Definition at line 210 of file MLIRContext.h.
Referenced by mlir::Dialect::addAttribute(), mlir::Dialect::addType(), mlir::BoolAttr::get(), getCachedIntegerType(), getDiagEngine(), getOrLoadDialect(), getOrLoadDynamicDialect(), hasActionHandler(), mlir::detail::StringAttrStorage::initialize(), mlir::AbstractAttribute::lookup(), mlir::AbstractType::lookup(), mlir::RegisteredOperationName::lookup(), mlir::OperationName::OperationName(), and registerActionHandler().
|
inline |
Get a registered IR dialect for the given derived dialect type.
The derived type must provide a static 'getDialectNamespace' method.
Definition at line 90 of file MLIRContext.h.
Referenced by getOrLoadDialect().
Dialect * MLIRContext::getLoadedDialect | ( | StringRef | name | ) |
Get a registered IR dialect with the given namespace.
If an exact match is not found, then return nullptr.
If none is found, then return nullptr.
Definition at line 457 of file MLIRContext.cpp.
Referenced by mlir::NVVM::SerializeGPUModuleBase::appendStandardLibs(), mlir::DialectRegistry::applyExtensions(), combineOneSpec(), mlir::detail::StringAttrStorage::initialize(), mlir::LLVM::isCompatibleType(), maybeReplaceWithConstant(), mergeInto(), and movePaddingToFillOrGenericOp().
std::vector< Dialect * > MLIRContext::getLoadedDialects | ( | ) |
Return information about all IR dialects loaded in the context.
Return information about all registered IR dialects.
Definition at line 437 of file MLIRContext.cpp.
Referenced by mlir::DialectRegistry::applyExtensions(), and mlir::detail::DialectInterfaceCollectionBase::DialectInterfaceCollectionBase().
unsigned MLIRContext::getNumThreads | ( | ) |
Return the number of threads used by the thread pool in this context.
The number of computed hardware threads can change over the lifetime of a process based on affinity changes, so users should use the number of threads actually in the thread pool for dispatching work. Returns 1 if multithreading is disabled.
Definition at line 650 of file MLIRContext.cpp.
References isMultithreadingEnabled().
|
inline |
Get (or create) a dialect for the given derived dialect type.
The derived type must provide a static 'getDialectNamespace' method.
Definition at line 97 of file MLIRContext.h.
Referenced by mlir::DialectResourceBlobHandle< DialectT >::getManagerInterface(), getOrLoadDynamicDialect(), mlir::transform::detail::getPreloadedTransformModule(), mlir::DialectRegistry::insert(), loadAllAvailableDialects(), mlir::FieldParser< AttributeT, std::enable_if_t< std::is_base_of< Attribute, AttributeT >::value, AttributeT > >::parse(), mlir::FieldParser< std::optional< AttributeT >, std::enable_if_t< std::is_base_of< Attribute, AttributeT >::value, std::optional< AttributeT > > >::parse(), mlir::detail::Parser::parseAttributeDict(), mlir::AsmParser::parseResourceHandle(), mlir::func::registerInlinerExtension(), mlir::linalg::registerMeshShardingInterfaceExternalModels(), and mlir::PassManager::run().
Dialect * MLIRContext::getOrLoadDialect | ( | StringRef | dialectNamespace, |
TypeID | dialectID, | ||
function_ref< std::unique_ptr< Dialect >()> | ctor | ||
) |
Get a dialect for the provided namespace and TypeID: abort the program if a dialect exist for this namespace with different TypeID.
If a dialect has not been loaded for this namespace/TypeID yet, use the provided ctor to create one on the fly and load it. Returns a pointer to the dialect owned by the context. The use of this method is in general discouraged in favor of 'getOrLoadDialect<DialectClass>()'.
Returns a pointer to the dialect owned by the context.
Definition at line 476 of file MLIRContext.cpp.
References getImpl(), and mlir::detail::StringAttrStorage::referencedDialect.
Dialect * MLIRContext::getOrLoadDialect | ( | StringRef | name | ) |
Get (or create) a dialect for the given derived dialect name.
The dialect will be loaded from the registry if no dialect is found. If no dialect is loaded for this name and none is available in the registry, returns nullptr.
Definition at line 463 of file MLIRContext.cpp.
References getLoadedDialect().
DynamicDialect * MLIRContext::getOrLoadDynamicDialect | ( | StringRef | dialectNamespace, |
function_ref< void(DynamicDialect *)> | ctor | ||
) |
Get (or create) a dynamic dialect for the given name.
Definition at line 540 of file MLIRContext.cpp.
References mlir::get(), getImpl(), and getOrLoadDialect().
Referenced by loadEmptyDialects().
ArrayRef< RegisteredOperationName > MLIRContext::getRegisteredOperations | ( | ) |
Return a sorted array containing the information about all registered operations.
Return information about all registered operations.
Definition at line 710 of file MLIRContext.cpp.
ArrayRef< RegisteredOperationName > MLIRContext::getRegisteredOperationsByDialect | ( | StringRef | dialectName | ) |
Return a sorted array containing the information for registered operations filtered by dialect name.
Return information for registered operations by dialect.
Definition at line 716 of file MLIRContext.cpp.
llvm::hash_code MLIRContext::getRegistryHash | ( | ) |
Returns a hash of the registry of the context that may be used to give a rough indicator of if the state of the context registry has changed.
The context registry correlates to loaded dialects and their entities (attributes, operations, types, etc.).
Definition at line 580 of file MLIRContext.cpp.
Referenced by mlir::PassManager::run().
llvm::ThreadPoolInterface & MLIRContext::getThreadPool | ( | ) |
Return the thread pool used by this context.
This method requires that multithreading be enabled within the context, and should generally not be used directly. Users should instead prefer the threading utilities within Threading.h.
Definition at line 660 of file MLIRContext.cpp.
References isMultithreadingEnabled().
StorageUniquer & MLIRContext::getTypeUniquer | ( | ) |
Returns the storage uniquer used for constructing type storage instances.
This should not be used directly.
Definition at line 1045 of file MLIRContext.cpp.
References mlir::OperationName::getImpl().
Referenced by mlir::detail::TypeUniquer::getWithTypeID(), mlir::detail::TypeUniquer::mutate(), and mlir::detail::TypeUniquer::registerType().
bool MLIRContext::hasActionHandler | ( | ) |
Return true if a valid ActionHandler is set.
Definition at line 406 of file MLIRContext.cpp.
References mlir::MLIRContextImpl::actionHandler, and getImpl().
Referenced by executeAction().
bool MLIRContext::isMultithreadingEnabled | ( | ) |
Return true if multi-threading is enabled by the context.
Definition at line 602 of file MLIRContext.cpp.
Referenced by mlir::applyPassManagerCLOptions(), mlir::TypeConverter::convertType(), mlir::failableParallelForEach(), getNumThreads(), getThreadPool(), mlir::detail::PassCrashReproducerGenerator::initialize(), mlir::OperationName::OperationName(), performActions(), and setThreadPool().
bool MLIRContext::isOperationRegistered | ( | StringRef | name | ) |
Return true if this operation name is registered in this context.
Definition at line 738 of file MLIRContext.cpp.
References mlir::RegisteredOperationName::lookup().
void MLIRContext::loadAllAvailableDialects | ( | ) |
Load all dialects available in the registry in this context.
Definition at line 575 of file MLIRContext.cpp.
References getAvailableDialects(), and getOrLoadDialect().
Referenced by roundTripModule(), and mlir::translateLLVMIRToModule().
|
inline |
Load a dialect in the context.
Definition at line 107 of file MLIRContext.h.
Referenced by mlir::transform::TransformDialectExtension< DerivedTy, ExtraDialects >::declareDependentDialect(), mlir::transform::TransformDialectExtension< DerivedTy, ExtraDialects >::declareGeneratedDialect(), deserializeModule(), mlir::query::extractFunction(), loadDialect(), mlir::tensor::registerBufferizableOpInterfaceExternalModels(), and mlir::linalg::registerRuntimeVerifiableOpInterfaceExternalModels().
|
inline |
Load a list dialects in the context.
Definition at line 116 of file MLIRContext.h.
References loadDialect().
void MLIRContext::printOpOnDiagnostic | ( | bool | enable | ) |
Set the flag specifying if we should attach the operation to diagnostics emitted via Operation::emit.
Definition at line 687 of file MLIRContext.cpp.
Referenced by MLIRContext().
void MLIRContext::printStackTraceOnDiagnostic | ( | bool | enable | ) |
Set the flag specifying if we should attach the current stacktrace when emitting diagnostics.
Definition at line 702 of file MLIRContext.cpp.
Referenced by MLIRContext().
void MLIRContext::registerActionHandler | ( | HandlerTy | handler | ) |
Register a handler for handling actions that are dispatched through this context.
A nullptr handler can be set to disable a previously set handler.
Definition at line 395 of file MLIRContext.cpp.
References mlir::MLIRContextImpl::actionHandler, and getImpl().
Referenced by mlir::tracing::InstallDebugHandler::Impl::Impl().
void MLIRContext::setThreadPool | ( | llvm::ThreadPoolInterface & | pool | ) |
Set a new thread pool to be used in this context.
This method requires that multithreading is disabled for this context prior to the call. This allows to share a thread pool across multiple contexts, as well as decoupling the lifetime of the threads from the contexts. The thread pool must outlive the context. Multi-threading will be enabled as part of this method. The command line debugging flag --mlir-disable-threading
will still prevent threading from being enabled and threading won't be enabled after this call in this case.
Definition at line 642 of file MLIRContext.cpp.
References enableMultithreading(), and isMultithreadingEnabled().
bool MLIRContext::shouldPrintOpOnDiagnostic | ( | ) |
Return true if we should attach the operation to diagnostics emitted via Operation::emit.
Definition at line 681 of file MLIRContext.cpp.
bool MLIRContext::shouldPrintStackTraceOnDiagnostic | ( | ) |
Return true if we should attach the current stacktrace to diagnostics when emitted.
Definition at line 696 of file MLIRContext.cpp.
Referenced by emitDiag().