[Mlir-commits] [mlir] cf4f97c - [mlir] Add classes to define new TypeIDs at runtime

Alex Zinenko llvmlistbot at llvm.org
Wed Feb 9 14:24:54 PST 2022


Author: Mathieu Fehr
Date: 2022-02-09T23:24:49+01:00
New Revision: cf4f97c8122e70b7b624508c4866c41907400e2b

URL: https://github.com/llvm/llvm-project/commit/cf4f97c8122e70b7b624508c4866c41907400e2b
DIFF: https://github.com/llvm/llvm-project/commit/cf4f97c8122e70b7b624508c4866c41907400e2b.diff

LOG: [mlir] Add classes to define new TypeIDs at runtime

TypeIDAllocator enables the allocation of new TypeIDs at runtime,
that are unique during the lifetime of the allocator.

NonMovableTypeIDOwner is a class used to define a new TypeID for each instance
of a class, using the instance address. This class cannot be copied or moved.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D104534

Added: 
    

Modified: 
    mlir/include/mlir/Support/TypeID.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Support/TypeID.h b/mlir/include/mlir/Support/TypeID.h
index badab11b8e047..53b448f68b1d1 100644
--- a/mlir/include/mlir/Support/TypeID.h
+++ b/mlir/include/mlir/Support/TypeID.h
@@ -17,6 +17,7 @@
 #include "mlir/Support/LLVM.h"
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/Hashing.h"
+#include "llvm/Support/Allocator.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
 
 namespace mlir {
@@ -92,6 +93,8 @@ class TypeID {
 
   // See TypeIDExported below for an explanation of the trampoline behavior.
   friend struct detail::TypeIDExported;
+
+  friend class TypeIDAllocator;
 };
 
 /// Enable hashing TypeID.
@@ -138,6 +141,34 @@ TypeID TypeID::get() {
   return detail::TypeIDExported::get<Trait>();
 }
 
+/// This class provides a way to define new TypeIDs at runtime.
+/// When the allocator is destructed, all allocated TypeIDs become invalid and
+/// therefore should not be used.
+class TypeIDAllocator {
+public:
+  /// Allocate a new TypeID, that is ensured to be unique for the lifetime
+  /// of the TypeIDAllocator.
+  TypeID allocate() { return TypeID(ids.Allocate()); }
+
+private:
+  /// The TypeIDs allocated are the addresses of the 
diff erent storages.
+  /// Keeping those in memory ensure uniqueness of the TypeIDs.
+  llvm::SpecificBumpPtrAllocator<TypeID::Storage> 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


        


More information about the Mlir-commits mailing list