From fa6eb9bfafc7eda92950b4edbd26e502f1b29d9c Mon Sep 17 00:00:00 2001 From: River Riddle Date: Wed, 8 May 2019 10:23:10 -0700 Subject: [PATCH] Add a new ClassID utility class that allows for generating unique identifiers for class types. This replaces the duplicated functionality of AnalysisID/PassID/etc. -- PiperOrigin-RevId: 247237835 --- mlir/include/mlir/IR/Dialect.h | 2 +- mlir/include/mlir/IR/TypeSupport.h | 12 ++---------- mlir/include/mlir/IR/Types.h | 3 ++- mlir/include/mlir/Pass/AnalysisManager.h | 7 +------ mlir/include/mlir/Pass/PassInstrumentation.h | 3 ++- mlir/include/mlir/Pass/PassRegistry.h | 8 ++------ mlir/include/mlir/Support/STLExtras.h | 8 ++++++++ mlir/lib/IR/MLIRContext.cpp | 6 +++--- 8 files changed, 21 insertions(+), 28 deletions(-) diff --git a/mlir/include/mlir/IR/Dialect.h b/mlir/include/mlir/IR/Dialect.h index 6a4fdf7..928c6f7 100644 --- a/mlir/include/mlir/IR/Dialect.h +++ b/mlir/include/mlir/IR/Dialect.h @@ -194,7 +194,7 @@ protected: }; // Register a type with its given unqiue type identifer. - void addType(const TypeID *const typeID); + void addType(const ClassID *const typeID); // Enable support for unregistered operations. void allowUnknownOperations(bool allow = true) { allowUnknownOps = allow; } diff --git a/mlir/include/mlir/IR/TypeSupport.h b/mlir/include/mlir/IR/TypeSupport.h index fc25ea2..d8504de 100644 --- a/mlir/include/mlir/IR/TypeSupport.h +++ b/mlir/include/mlir/IR/TypeSupport.h @@ -28,18 +28,10 @@ #include namespace mlir { +struct ClassID; class Dialect; class MLIRContext; -/// TypeID is used to provide a unique address identifier for derived Type -/// classes. -struct TypeID { - template static TypeID *getID() { - static TypeID id; - return &id; - } -}; - //===----------------------------------------------------------------------===// // TypeStorage //===----------------------------------------------------------------------===// @@ -123,7 +115,7 @@ private: /// Get the dialect that registered the type with the provided typeid. static const Dialect &lookupDialectForType(MLIRContext *ctx, - const TypeID *const typeID); + const ClassID *const typeID); }; } // namespace detail diff --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h index e18b131..d2c6e90 100644 --- a/mlir/include/mlir/IR/Types.h +++ b/mlir/include/mlir/IR/Types.h @@ -22,6 +22,7 @@ #include "mlir/IR/TypeSupport.h" #include "mlir/Support/LLVM.h" #include "mlir/Support/LogicalResult.h" +#include "mlir/Support/STLExtras.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMapInfo.h" @@ -135,7 +136,7 @@ public: using ImplType = StorageType; /// Return a unique identifier for the concrete type. - static TypeID *getTypeID() { return TypeID::getID(); } + static ClassID *getTypeID() { return ClassID::getID(); } protected: /// Get or create a new ConcreteType instance within the context. This diff --git a/mlir/include/mlir/Pass/AnalysisManager.h b/mlir/include/mlir/Pass/AnalysisManager.h index 561ff7b..f62443e 100644 --- a/mlir/include/mlir/Pass/AnalysisManager.h +++ b/mlir/include/mlir/Pass/AnalysisManager.h @@ -28,12 +28,7 @@ namespace mlir { /// A special type used by analyses to provide an address that identifies a /// particular analysis set or a concrete analysis type. -struct AnalysisID { - template static AnalysisID *getID() { - static AnalysisID id; - return &id; - } -}; +using AnalysisID = ClassID; //===----------------------------------------------------------------------===// // Analysis Preservation and Concept Modeling diff --git a/mlir/include/mlir/Pass/PassInstrumentation.h b/mlir/include/mlir/Pass/PassInstrumentation.h index e1ff54b..0f42706 100644 --- a/mlir/include/mlir/Pass/PassInstrumentation.h +++ b/mlir/include/mlir/Pass/PassInstrumentation.h @@ -19,11 +19,12 @@ #define MLIR_PASS_PASSINSTRUMENTATION_H_ #include "mlir/Support/LLVM.h" +#include "mlir/Support/STLExtras.h" #include "llvm/ADT/Any.h" #include "llvm/ADT/StringRef.h" namespace mlir { -struct AnalysisID; +using AnalysisID = ClassID; class Pass; namespace detail { diff --git a/mlir/include/mlir/Pass/PassRegistry.h b/mlir/include/mlir/Pass/PassRegistry.h index 800484a..faa6472 100644 --- a/mlir/include/mlir/Pass/PassRegistry.h +++ b/mlir/include/mlir/Pass/PassRegistry.h @@ -24,6 +24,7 @@ #define MLIR_PASS_PASSREGISTRY_H_ #include "mlir/Support/LLVM.h" +#include "mlir/Support/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" @@ -40,12 +41,7 @@ using PassAllocatorFunction = std::function; /// A special type used by transformation passes to provide an address that can /// act as a unique identifier during pass registration. -struct alignas(8) PassID { - template static PassID *getID() { - static PassID id; - return &id; - } -}; +using PassID = ClassID; /// Structure to group information about a passes and pass pipelines (argument /// to invoke via mlir-opt, description, pass pipeline builder). diff --git a/mlir/include/mlir/Support/STLExtras.h b/mlir/include/mlir/Support/STLExtras.h index 33434fe..7201c19 100644 --- a/mlir/include/mlir/Support/STLExtras.h +++ b/mlir/include/mlir/Support/STLExtras.h @@ -63,6 +63,14 @@ inline void interleaveComma(const Container &c, raw_ostream &os) { interleave(c.begin(), c.end(), [&](T a) { os << a; }, [&]() { os << ", "; }); } +/// A special type used to provide an address for a given class that can act as +/// a unique identifier during pass registration. +struct alignas(8) ClassID { + template static ClassID *getID() { + static ClassID id; + return &id; + } +}; } // end namespace mlir // Allow tuples to be usable as DenseMap keys. diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp index 249a1e1..8c0e98c 100644 --- a/mlir/lib/IR/MLIRContext.cpp +++ b/mlir/lib/IR/MLIRContext.cpp @@ -324,7 +324,7 @@ public: llvm::StringMap registeredOperations; /// This is a mapping from type identifier to Dialect for registered types. - DenseMap registeredTypes; + DenseMap registeredTypes; /// These are identifiers uniqued into this MLIRContext. llvm::StringMap identifiers; @@ -553,7 +553,7 @@ void Dialect::addOperation(AbstractOperation opInfo) { } /// Register a dialect-specific type with the current context. -void Dialect::addType(const TypeID *const typeID) { +void Dialect::addType(const ClassID *const typeID) { auto &impl = context->getImpl(); // Lock access to the context registry. @@ -822,7 +822,7 @@ StorageUniquer &MLIRContext::getTypeUniquer() { return getImpl().typeUniquer; } /// Get the dialect that registered the type with the provided typeid. const Dialect &TypeUniquer::lookupDialectForType(MLIRContext *ctx, - const TypeID *const typeID) { + const ClassID *const typeID) { auto &impl = ctx->getImpl(); auto it = impl.registeredTypes.find(typeID); assert(it != impl.registeredTypes.end() && "typeID is not registered."); -- 2.7.4