MLIR  20.0.0git
Public Types | Public Member Functions | List of all members
mlir::MLIRContext Class Reference

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 &registry, Threading multithreading=Threading::ENABLED)
 
 ~MLIRContext ()
 
std::vector< Dialect * > getLoadedDialects ()
 Return information about all IR dialects loaded in the context. More...
 
const DialectRegistrygetDialectRegistry ()
 Return the dialect registry associated with this context. More...
 
void appendDialectRegistry (const DialectRegistry &registry)
 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...
 
DialectgetLoadedDialect (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...
 
DynamicDialectgetOrLoadDynamicDialect (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...
 
DialectgetOrLoadDialect (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< RegisteredOperationNamegetRegisteredOperations ()
 Return a sorted array containing the information about all registered operations. More...
 
ArrayRef< RegisteredOperationNamegetRegisteredOperationsByDialect (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...
 
MLIRContextImplgetImpl ()
 
DiagnosticEnginegetDiagEngine ()
 Returns the diagnostic engine for this context. More...
 
StorageUniquergetAffineUniquer ()
 Returns the storage uniquer used for creating affine constructs. More...
 
StorageUniquergetTypeUniquer ()
 Returns the storage uniquer used for constructing type storage instances. More...
 
StorageUniquergetAttributeUniquer ()
 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 ()
 
DialectgetOrLoadDialect (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...
 

Detailed Description

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.

Member Typedef Documentation

◆ HandlerTy

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.

Member Enumeration Documentation

◆ Threading

Enumerator
DISABLED 
ENABLED 

Definition at line 62 of file MLIRContext.h.

Constructor & Destructor Documentation

◆ MLIRContext() [1/2]

MLIRContext::MLIRContext ( Threading  multithreading = Threading::ENABLED)
explicit

Create a new Context.

Definition at line 297 of file MLIRContext.cpp.

◆ MLIRContext() [2/2]

MLIRContext::MLIRContext ( const DialectRegistry registry,
Threading  multithreading = Threading::ENABLED 
)
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().

◆ ~MLIRContext()

MLIRContext::~MLIRContext ( )
default

Member Function Documentation

◆ allowsUnregisteredDialects()

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().

◆ allowUnregisteredDialects()

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().

◆ appendDialectRegistry()

void MLIRContext::appendDialectRegistry ( const DialectRegistry registry)

◆ disableMultithreading()

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().

◆ enableMultithreading()

void mlir::MLIRContext::enableMultithreading ( bool  enable = true)
inline

Definition at line 152 of file MLIRContext.h.

References disableMultithreading().

Referenced by performActions(), and setThreadPool().

◆ enterMultiThreadedExecution()

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().

◆ executeAction() [1/2]

template<typename ActionTy , typename... Args>
void mlir::MLIRContext::executeAction ( function_ref< void()>  actionFn,
ArrayRef< IRUnit irUnits,
Args &&...  args 
)
inline

Dispatch the provided action to the handler if any, or just execute it.

Definition at line 274 of file MLIRContext.h.

References hasActionHandler().

◆ executeAction() [2/2]

void mlir::MLIRContext::executeAction ( function_ref< void()>  actionFn,
const tracing::Action action 
)
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().

◆ exitMultiThreadedExecution()

void MLIRContext::exitMultiThreadedExecution ( )

Definition at line 673 of file MLIRContext.cpp.

Referenced by mlir::PassManager::run().

◆ getAffineUniquer()

StorageUniquer & MLIRContext::getAffineUniquer ( )

◆ getAttributeUniquer()

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().

◆ getAvailableDialects()

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().

◆ getDiagEngine()

DiagnosticEngine & MLIRContext::getDiagEngine ( )

◆ getDialectRegistry()

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().

◆ getImpl()

MLIRContextImpl& mlir::MLIRContext::getImpl ( )
inline

◆ getLoadedDialect() [1/2]

template<typename T >
T* mlir::MLIRContext::getLoadedDialect ( )
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().

◆ getLoadedDialect() [2/2]

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().

◆ getLoadedDialects()

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().

◆ getNumThreads()

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().

◆ getOrLoadDialect() [1/3]

template<typename T >
T* mlir::MLIRContext::getOrLoadDialect ( )
inline

◆ getOrLoadDialect() [2/3]

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.

◆ getOrLoadDialect() [3/3]

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().

◆ getOrLoadDynamicDialect()

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().

◆ getRegisteredOperations()

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.

◆ getRegisteredOperationsByDialect()

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.

◆ getRegistryHash()

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().

◆ getThreadPool()

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().

◆ getTypeUniquer()

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().

◆ hasActionHandler()

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().

◆ isMultithreadingEnabled()

bool MLIRContext::isMultithreadingEnabled ( )

◆ isOperationRegistered()

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().

◆ loadAllAvailableDialects()

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().

◆ loadDialect() [1/2]

template<typename Dialect >
void mlir::MLIRContext::loadDialect ( )
inline

◆ loadDialect() [2/2]

template<typename Dialect , typename OtherDialect , typename... MoreDialects>
void mlir::MLIRContext::loadDialect ( )
inline

Load a list dialects in the context.

Definition at line 116 of file MLIRContext.h.

References loadDialect().

◆ printOpOnDiagnostic()

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().

◆ printStackTraceOnDiagnostic()

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().

◆ registerActionHandler()

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().

◆ setThreadPool()

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().

◆ shouldPrintOpOnDiagnostic()

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.

◆ shouldPrintStackTraceOnDiagnostic()

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().


The documentation for this class was generated from the following files: