MLIR  16.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  Threading { Threading::DISABLED, Threading::ENABLED }
 

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::ThreadPool &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::ThreadPool & 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...
 
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...
 
DebugActionManagergetDebugActionManager ()
 Returns the manager of debug actions within the context. 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...
 

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::ThreadPool myThreadPool; while (auto *request = nextCompilationRequests()) { MLIRContext ctx(registry, MLIRContext::Threading::DISABLED); ctx.setThreadPool(myThreadPool); processRequest(request, cxt); }

Definition at line 56 of file MLIRContext.h.

Member Enumeration Documentation

◆ Threading

Enumerator
DISABLED 
ENABLED 

Definition at line 58 of file MLIRContext.h.

Constructor & Destructor Documentation

◆ MLIRContext() [1/2]

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

Create a new Context.

Definition at line 256 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 259 of file MLIRContext.cpp.

References mlir::DialectRegistry::appendTo(), clOptions, printOpOnDiagnostic(), printStackTraceOnDiagnostic(), and ~MLIRContext().

◆ ~MLIRContext()

MLIRContext::~MLIRContext ( )
default

Referenced by MLIRContext().

Member Function Documentation

◆ allowsUnregisteredDialects()

bool MLIRContext::allowsUnregisteredDialects ( )

Return true if we allow to create operation for unregistered dialects.

Definition at line 509 of file MLIRContext.cpp.

Referenced by mlir::BoolAttr::classof(), mlir::FloatType::getFPMantissaWidth(), and parseEntry().

◆ allowUnregisteredDialects()

void MLIRContext::allowUnregisteredDialects ( bool  allow = true)

Enables creating operations in unregistered dialects.

Definition at line 513 of file MLIRContext.cpp.

Referenced by mlir::lsp::MLIRServer::convertFromBytecode(), and processBuffer().

◆ 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 526 of file MLIRContext.cpp.

References isThreadingGloballyDisabled().

Referenced by performActions().

◆ enableMultithreading()

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

Definition at line 141 of file MLIRContext.h.

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 587 of file MLIRContext.cpp.

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

◆ exitMultiThreadedExecution()

void MLIRContext::exitMultiThreadedExecution ( )

Definition at line 592 of file MLIRContext.cpp.

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

◆ getAffineUniquer()

StorageUniquer & MLIRContext::getAffineUniquer ( )

◆ getAttributeUniquer()

StorageUniquer & MLIRContext::getAttributeUniquer ( )

◆ getAvailableDialects()

std::vector< StringRef > MLIRContext::getAvailableDialects ( )

Return information about all available dialects in the registry in this context.

Definition at line 386 of file MLIRContext.cpp.

Referenced by getLspDiagnoticFromDiag(), and loadAllAvailableDialects().

◆ getDebugActionManager()

DebugActionManager & MLIRContext::getDebugActionManager ( )

Returns the manager of debug actions within the context.

Definition at line 342 of file MLIRContext.cpp.

References mlir::MLIRContextImpl::debugActionManager, and getImpl().

Referenced by processBuffer().

◆ getDiagEngine()

DiagnosticEngine & MLIRContext::getDiagEngine ( )

Returns the diagnostic engine for this context.

Definition at line 351 of file MLIRContext.cpp.

References mlir::MLIRContextImpl::diagEngine, and getImpl().

Referenced by emitDiag(), and mlir::detail::ParallelDiagnosticHandlerImpl::ParallelDiagnosticHandlerImpl().

◆ getDialectRegistry()

const DialectRegistry & MLIRContext::getDialectRegistry ( )

Return the dialect registry associated with this context.

Definition at line 370 of file MLIRContext.cpp.

Referenced by roundTripModule().

◆ getImpl()

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

◆ getLoadedDialect() [1/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 395 of file MLIRContext.cpp.

Referenced by mlir::DialectRegistry::applyExtensions(), mlir::BoolAttr::classof(), combineOneSpec(), mlir::FloatType::getFPMantissaWidth(), mlir::detail::StringAttrStorage::initialize(), mlir::LLVM::isCompatibleType(), materializeConstants(), and mlir::linalg::populateElementwiseOpsFusionPatterns().

◆ getLoadedDialect() [2/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 86 of file MLIRContext.h.

Referenced by getOrLoadDialect().

◆ 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 375 of file MLIRContext.cpp.

Referenced by 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 569 of file MLIRContext.cpp.

References isMultithreadingEnabled().

Referenced by inlineCallsInSCC().

◆ getOrLoadDialect() [1/3]

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

◆ getOrLoadDialect() [2/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 401 of file MLIRContext.cpp.

References getLoadedDialect().

◆ getOrLoadDialect() [3/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 414 of file MLIRContext.cpp.

References getImpl(), and mlir::detail::StringAttrStorage::referencedDialect.

◆ getOrLoadDynamicDialect()

DynamicDialect * MLIRContext::getOrLoadDynamicDialect ( StringRef  dialectNamespace,
function_ref< void(DynamicDialect *)>  ctor 
)

Get (or create) a dynamic dialect for the given name.

Definition at line 459 of file MLIRContext.cpp.

References getImpl(), and getOrLoadDialect().

◆ 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 629 of file MLIRContext.cpp.

Referenced by getLspDiagnoticFromDiag().

◆ 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 499 of file MLIRContext.cpp.

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

◆ getThreadPool()

llvm::ThreadPool & 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 579 of file MLIRContext.cpp.

References isMultithreadingEnabled().

Referenced by mlir::failableParallelForEach(), and processBuffer().

◆ getTypeUniquer()

StorageUniquer & MLIRContext::getTypeUniquer ( )

◆ isMultithreadingEnabled()

bool MLIRContext::isMultithreadingEnabled ( )

◆ isOperationRegistered()

bool MLIRContext::isOperationRegistered ( StringRef  name)

Return true if this operation name is registered in this context.

Definition at line 633 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 494 of file MLIRContext.cpp.

References getAvailableDialects(), and getOrLoadDialect().

Referenced by processBuffer(), and roundTripModule().

◆ 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 109 of file MLIRContext.h.

◆ 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 606 of file MLIRContext.cpp.

Referenced by MLIRContext(), and processBuffer().

◆ printStackTraceOnDiagnostic()

void MLIRContext::printStackTraceOnDiagnostic ( bool  enable)

Set the flag specifying if we should attach the current stacktrace when emitting diagnostics.

Definition at line 621 of file MLIRContext.cpp.

Referenced by MLIRContext().

◆ setThreadPool()

void MLIRContext::setThreadPool ( llvm::ThreadPool &  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 561 of file MLIRContext.cpp.

References enableMultithreading(), and isMultithreadingEnabled().

Referenced by processBuffer().

◆ shouldPrintOpOnDiagnostic()

bool MLIRContext::shouldPrintOpOnDiagnostic ( )

Return true if we should attach the operation to diagnostics emitted via Operation::emit.

Definition at line 600 of file MLIRContext.cpp.

◆ shouldPrintStackTraceOnDiagnostic()

bool MLIRContext::shouldPrintStackTraceOnDiagnostic ( )

Return true if we should attach the current stacktrace to diagnostics when emitted.

Definition at line 615 of file MLIRContext.cpp.

Referenced by emitDiag().


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