diff --git a/mlir/include/mlir/Support/TypeID.h b/mlir/include/mlir/Support/TypeID.h --- a/mlir/include/mlir/Support/TypeID.h +++ b/mlir/include/mlir/Support/TypeID.h @@ -102,7 +102,30 @@ return DenseMapInfo::getHashValue(id.storage); } +/// Defines a TypeID for each instance of this class by using a pointer to the +/// instance. Thus, the copy and move constructor are deleted. +class alignas(8) SelfOwningTypeID { +public: + SelfOwningTypeID() = default; + SelfOwningTypeID(const SelfOwningTypeID &) = delete; + SelfOwningTypeID &operator=(const SelfOwningTypeID &) = delete; + SelfOwningTypeID(SelfOwningTypeID &&) = delete; + SelfOwningTypeID &operator=(SelfOwningTypeID &&) = delete; + + /// Return the TypeID owned by this object. + TypeID getTypeID() const { return TypeID::getFromOpaquePointer(this); } + + /// Allow implicit conversion to the owned TypeID. + operator TypeID() const { return getTypeID(); } +}; + namespace detail { +/// This class is used by explicit TypeID instantions to provide the type id +/// object. +template +struct ExplicitTypeIDInstantion { + static SelfOwningTypeID id; +}; /// The static local instance of each get method must be emitted with /// "default" (public) visibility across all shared libraries, regardless of @@ -156,19 +179,6 @@ llvm::SpecificBumpPtrAllocator ids; }; -/// Defines a TypeID for each instance of this class by using a pointer to the -/// instance. Thus, the copy and move constructor are deleted. -class SelfOwningTypeID { -public: - SelfOwningTypeID() = default; - SelfOwningTypeID(const SelfOwningTypeID &) = delete; - SelfOwningTypeID &operator=(const SelfOwningTypeID &) = delete; - SelfOwningTypeID(SelfOwningTypeID &&) = delete; - SelfOwningTypeID &operator=(SelfOwningTypeID &&) = delete; - - TypeID getTypeID() const { return TypeID::getFromOpaquePointer(this); } -}; - } // namespace mlir // Declare/define an explicit specialization for TypeID: this forces the @@ -182,7 +192,12 @@ namespace mlir { \ namespace detail { \ template <> \ - LLVM_EXTERNAL_VISIBILITY TypeID TypeIDExported::get(); \ + SelfOwningTypeID ExplicitTypeIDInstantion::id; \ + \ + template <> \ + inline LLVM_EXTERNAL_VISIBILITY TypeID TypeIDExported::get() { \ + return ExplicitTypeIDInstantion::id; \ + } \ } \ } @@ -190,10 +205,7 @@ namespace mlir { \ namespace detail { \ template <> \ - LLVM_EXTERNAL_VISIBILITY TypeID TypeIDExported::get() { \ - static TypeID::Storage instance; \ - return TypeID(&instance); \ - } \ + SelfOwningTypeID ExplicitTypeIDInstantion::id = {}; \ } \ }