MLIR 22.0.0git
mlir::TypeID Class Reference

This class provides an efficient unique identifier for a specific C++ type. More...

#include "mlir/Support/TypeID.h"

Public Member Functions

 TypeID ()
bool operator== (const TypeID &other) const
 Comparison operations.
bool operator!= (const TypeID &other) const
const voidgetAsOpaquePointer () const
 Methods for supporting PointerLikeTypeTraits.
friend::llvm::hash_code hash_value (TypeID id)
 Enable hashing TypeID.

Static Public Member Functions

template<typename T>
static TypeID get ()
 Construct a type info object for the given type T.
template<template< typename > class Trait>
static TypeID get ()
static TypeID getFromOpaquePointer (const void *pointer)

Friends

class TypeIDAllocator

Detailed Description

This class provides an efficient unique identifier for a specific C++ type.

This allows for a C++ type to be compared, hashed, and stored in an opaque context. This class is similar in some ways to std::type_index, but can be used for any type. For example, this class could be used to implement LLVM style isa/dyn_cast functionality for a type hierarchy:

struct Base { Base(TypeID typeID) : typeID(typeID) {} TypeID typeID; };

struct DerivedA : public Base { DerivedA() : Base(TypeID::get<DerivedA>()) {}

static bool classof(const Base *base) { return base->typeID == TypeID::get<DerivedA>(); } };

void foo(Base *base) { if (DerivedA *a = llvm::dyn_cast<DerivedA>(base)) ... }

C++ RTTI is a notoriously difficult topic; given the nature of shared libraries many different approaches fundamentally break down in either the area of support (i.e. only certain types of classes are supported), or in terms of performance (e.g. by using string comparison). This class intends to strike a balance between performance and the setup required to enable its use.

Assume we are adding support for some class Foo, below are the set of ways in which a given c++ type may be supported:

  • Explicitly via MLIR_DECLARE_EXPLICIT_TYPE_ID and MLIR_DEFINE_EXPLICIT_TYPE_ID
    • This method explicitly defines the type ID for a given type using the given macros. These should be placed at the top-level of the file (i.e. not within any namespace or class). This is the most effective and efficient method, but requires explicit annotations for each type.

      Example:

      // Foo.h MLIR_DECLARE_EXPLICIT_TYPE_ID(Foo);

      // Foo.cpp MLIR_DEFINE_EXPLICIT_TYPE_ID(Foo);

  • Explicitly via MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID
    • This method explicitly defines the type ID for a given type by annotating the class directly. This has similar effectiveness and efficiency to the above method, but should only be used on internal classes; i.e. those with definitions constrained to a specific library (generally classes in anonymous namespaces).

      Example:

      namespace { class Foo { public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(Foo) }; } // namespace

  • Implicitly via a fallback using the type name
    • This method implicitly defines a type ID for a given type by using the type name. This method requires nothing explicitly from the user, but pays additional access and initialization cost. Given that this method uses the name of the type, it may not be used for types defined in anonymous namespaces (which is asserted when it can be detected). String names do not provide any guarantees on uniqueness in these contexts.
    • This behavior may be forced even in the presence of explicit declarations by specifying MLIR_USE_FALLBACK_TYPE_IDS.

Definition at line 107 of file TypeID.h.

Constructor & Destructor Documentation

◆ TypeID()

mlir::TypeID::TypeID ( )
inline

Definition at line 115 of file TypeID.h.

References get(), and TypeID().

Referenced by get(), getFromOpaquePointer(), hash_value(), operator!=(), operator==(), and TypeID().

Member Function Documentation

◆ get() [1/2]

template<template< typename > class Trait>
TypeID mlir::TypeID::get ( )
static

Construct a type info object for the given type T.

Definition at line 245 of file TypeID.h.

References mlir::detail::TypeIDResolver< T, Enable >::resolveTypeID(), and TypeID().

Referenced by mlir::bufferization::OneShotAnalysisState::addExtension(), mlir::transform::TransformState::addExtension(), mlir::DialectRegistry::addExtensions(), mlir::AsmDialectResourceHandleBase< DerivedT, ResourceT, DialectT >::AsmDialectResourceHandleBase(), mlir::detail::StorageUserBase< ConcreteType, BaseType, StorageType, detail::AttributeUniquer, Traits... >::attachInterface(), mlir::Op< ConcreteType, Traits >::attachInterface(), mlir::AsmDialectResourceHandleBase< DerivedT, ResourceT, DialectT >::classof(), mlir::bufferization::OneShotAnalysisState::classof(), mlir::GenericLatticeAnchorBase< ConcreteT, Value >::classof(), mlir::Op< ConcreteType, Traits >::classof(), mlir::PassWrapper< PassT, BaseT >::classof(), mlir::pdl_to_pdl_interp::BoolNode::classof(), mlir::pdl_to_pdl_interp::ExitNode::classof(), mlir::pdl_to_pdl_interp::SuccessNode::classof(), mlir::pdl_to_pdl_interp::SwitchNode::classof(), mlir::pdll::ast::Node::NodeBase< T, BaseT >::classof(), mlir::pdll::ast::Type::TypeBase< ImplT, BaseT >::classof(), mlir::tracing::ActionImpl< PassExecutionAction >::classof(), mlir::tracing::BreakpointBase< Derived >::classof(), mlir::tracing::BreakpointManagerBase< Derived >::classof(), mlir::arm_sme::impl::EnableArmStreamingBase< DerivedT >::clonePass(), mlir::Dialect::declarePromisedInterface(), llvm::isa_impl< T, ::mlir::Dialect, std::enable_if_t< std::is_base_of<::mlir::Dialect, T >::value > >::doit(), llvm::isa_impl< T, ::mlir::StateStackFrame >::doit(), mlir::StorageUniquer::get(), mlir::StorageUniquer::get(), mlir::detail::AnalysisMap::getCachedAnalysis(), mlir::SideEffects::Effect::Base< DerivedEffect, Effect >::getEffectID(), mlir::bufferization::OneShotAnalysisState::getExtension(), mlir::transform::TransformState::getExtension(), mlir::detail::DialectInterfaceBase< ConcreteType, DialectInterface >::getInterfaceID(), mlir::detail::Interface< ConcreteType, ValueT, Traits, BaseType, BaseTrait >::Trait< ConcreteT >::getInterfaceID(), mlir::detail::Interface< ConcreteType, Attribute, Traits, Attribute, AttributeTrait::TraitBase >::getInterfaceID(), mlir::DataFlowSolver::getLeaderAnchorOrSelf(), mlir::OperationState::getOrAddProperties(), mlir::DataFlowSolver::getOrCreateState(), mlir::MLIRContext::getOrLoadDialect(), mlir::SideEffects::Resource::Base< DerivedResource, BaseResource >::getResourceID(), mlir::detail::StorageUserBase< ConcreteType, BaseType, StorageType, detail::AttributeUniquer, Traits... >::getTypeID(), mlir::OperationName::hasInterface(), mlir::Dialect::hasPromisedInterface(), mlir::dialect_extension_detail::hasPromisedInterface(), mlir::AbstractAttribute::hasTrait(), mlir::AbstractType::hasTrait(), mlir::detail::storage_user_base_impl::hasTrait(), mlir::op_definition_impl::hasTrait(), mlir::OperationName::hasTrait(), mlir::DialectRegistry::insert(), mlir::DialectRegistry::insertDynamic(), mlir::detail::PreservedAnalyses::isAll(), mlir::DataFlowSolver::isEquivalent(), mlir::detail::PreservedAnalyses::isPreserved(), mlir::OperationName::Impl::isRegistered(), mlir::DataFlowSolver::lookupState(), mlir::OperationName::mightHaveInterface(), mlir::OperationName::mightHaveTrait(), mlir::OperationName::OperationName(), mlir::detail::PreservedAnalyses::preserve(), mlir::detail::PreservedAnalyses::preserveAll(), mlir::StorageUniquer::registerParametricStorageType(), mlir::transform::TransformState::removeExtension(), mlir::arith::ConstantFloatOp::resolveTypeID(), mlir::arith::ConstantIndexOp::resolveTypeID(), mlir::arith::ConstantIntOp::resolveTypeID(), mlir::linalg::BatchMatmulTransposeAOp::resolveTypeID(), mlir::linalg::BatchMatmulTransposeBOp::resolveTypeID(), mlir::linalg::MatmulTransposeAOp::resolveTypeID(), mlir::linalg::MatmulTransposeBOp::resolveTypeID(), TypeID(), mlir::DataFlowSolver::unionLatticeAnchors(), and mlir::OperationState::useProperties().

◆ get() [2/2]

template<template< typename > class Trait>
TypeID mlir::TypeID::get ( )
static

References TypeID().

◆ getAsOpaquePointer()

const void * mlir::TypeID::getAsOpaquePointer ( ) const
inline

Methods for supporting PointerLikeTypeTraits.

Definition at line 132 of file TypeID.h.

Referenced by llvm::PointerLikeTypeTraits< mlir::TypeID >::getAsVoidPointer().

◆ getFromOpaquePointer()

◆ hash_value()

friend::llvm::hash_code mlir::TypeID::hash_value ( TypeID id)

Enable hashing TypeID.

References TypeID().

◆ operator!=()

bool mlir::TypeID::operator!= ( const TypeID & other) const
inline

Definition at line 121 of file TypeID.h.

References TypeID().

◆ operator==()

bool mlir::TypeID::operator== ( const TypeID & other) const
inline

Comparison operations.

Definition at line 118 of file TypeID.h.

References TypeID().

◆ TypeIDAllocator

friend class TypeIDAllocator
friend

Definition at line 148 of file TypeID.h.

References TypeIDAllocator.

Referenced by TypeIDAllocator.


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