[Mlir-commits] [mlir] 97e77ac - Add more explicit error message when creating a type or attribute for an unregistered dialect (NFC)

Mehdi Amini llvmlistbot at llvm.org
Tue Sep 8 09:59:32 PDT 2020


Author: Mehdi Amini
Date: 2020-09-08T16:59:26Z
New Revision: 97e77ac0ed80877cda58b1dddf98890cc7b0d167

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

LOG: Add more explicit error message when creating a type or attribute for an unregistered dialect (NFC)

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/AttributeSupport.h
    mlir/include/mlir/IR/TypeSupport.h
    mlir/include/mlir/Support/StorageUniquer.h
    mlir/lib/Support/StorageUniquer.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/AttributeSupport.h b/mlir/include/mlir/IR/AttributeSupport.h
index 35084a20493f..c0e3a0bb9b26 100644
--- a/mlir/include/mlir/IR/AttributeSupport.h
+++ b/mlir/include/mlir/IR/AttributeSupport.h
@@ -16,6 +16,7 @@
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/StorageUniquerSupport.h"
 #include "llvm/ADT/PointerIntPair.h"
+#include "llvm/ADT/Twine.h"
 
 namespace mlir {
 class MLIRContext;
@@ -142,6 +143,14 @@ class AttributeUniquer {
   static typename std::enable_if_t<
       !std::is_same<typename T::ImplType, AttributeStorage>::value, T>
   get(MLIRContext *ctx, Args &&...args) {
+#ifndef NDEBUG
+    if (!ctx->getAttributeUniquer().isParametricStorageInitialized(
+            T::getTypeID()))
+      llvm::report_fatal_error(llvm::Twine("can't create Attribute '") +
+                               llvm::getTypeName<T>() +
+                               "' because storage uniquer isn't initialized: "
+                               "the dialect was likely not loaded.");
+#endif
     return ctx->getAttributeUniquer().get<typename T::ImplType>(
         [ctx](AttributeStorage *storage) {
           initializeAttributeStorage(storage, ctx, T::getTypeID());
@@ -153,6 +162,14 @@ class AttributeUniquer {
   static typename std::enable_if_t<
       std::is_same<typename T::ImplType, AttributeStorage>::value, T>
   get(MLIRContext *ctx) {
+#ifndef NDEBUG
+    if (!ctx->getAttributeUniquer().isSingletonStorageInitialized(
+            T::getTypeID()))
+      llvm::report_fatal_error(llvm::Twine("can't create Attribute '") +
+                               llvm::getTypeName<T>() +
+                               "' because storage uniquer isn't initialized: "
+                               "the dialect was likely not loaded.");
+#endif
     return ctx->getAttributeUniquer().get<typename T::ImplType>(T::getTypeID());
   }
 

diff  --git a/mlir/include/mlir/IR/TypeSupport.h b/mlir/include/mlir/IR/TypeSupport.h
index ace5eaa73345..c1de58957915 100644
--- a/mlir/include/mlir/IR/TypeSupport.h
+++ b/mlir/include/mlir/IR/TypeSupport.h
@@ -15,6 +15,7 @@
 
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/StorageUniquerSupport.h"
+#include "llvm/ADT/Twine.h"
 
 namespace mlir {
 class Dialect;
@@ -126,6 +127,13 @@ struct TypeUniquer {
   static typename std::enable_if_t<
       !std::is_same<typename T::ImplType, TypeStorage>::value, T>
   get(MLIRContext *ctx, Args &&...args) {
+#ifndef NDEBUG
+    if (!ctx->getTypeUniquer().isParametricStorageInitialized(T::getTypeID()))
+      llvm::report_fatal_error(llvm::Twine("can't create type '") +
+                               llvm::getTypeName<T>() +
+                               "' because storage uniquer isn't initialized: "
+                               "the dialect was likely not loaded.");
+#endif
     return ctx->getTypeUniquer().get<typename T::ImplType>(
         [&](TypeStorage *storage) {
           storage->initialize(AbstractType::lookup(T::getTypeID(), ctx));
@@ -137,6 +145,13 @@ struct TypeUniquer {
   static typename std::enable_if_t<
       std::is_same<typename T::ImplType, TypeStorage>::value, T>
   get(MLIRContext *ctx) {
+#ifndef NDEBUG
+    if (!ctx->getTypeUniquer().isSingletonStorageInitialized(T::getTypeID()))
+      llvm::report_fatal_error(llvm::Twine("can't create type '") +
+                               llvm::getTypeName<T>() +
+                               "' because storage uniquer isn't initialized: "
+                               "the dialect was likely not loaded.");
+#endif
     return ctx->getTypeUniquer().get<typename T::ImplType>(T::getTypeID());
   }
 

diff  --git a/mlir/include/mlir/Support/StorageUniquer.h b/mlir/include/mlir/Support/StorageUniquer.h
index eb04688be190..d0a6170805bf 100644
--- a/mlir/include/mlir/Support/StorageUniquer.h
+++ b/mlir/include/mlir/Support/StorageUniquer.h
@@ -210,6 +210,16 @@ class StorageUniquer {
     return get<Storage>(TypeID::get<Storage>());
   }
 
+  /// Test if there is a singleton storage uniquer initialized for the provided
+  /// TypeID. This is only useful for debugging/diagnostic purpose: the uniquer
+  /// is initialized when a dialect is loaded.
+  bool isSingletonStorageInitialized(TypeID id);
+
+  /// Test if there is a parametric storage uniquer initialized for the provided
+  /// TypeID. This is only useful for debugging/diagnostic purpose: the uniquer
+  /// is initialized when a dialect is loaded.
+  bool isParametricStorageInitialized(TypeID id);
+
   /// Changes the mutable component of 'storage' by forwarding the trailing
   /// arguments to the 'mutate' function of the derived class.
   template <typename Storage, typename... Args>

diff  --git a/mlir/lib/Support/StorageUniquer.cpp b/mlir/lib/Support/StorageUniquer.cpp
index 73578b5c91ac..a3e296e99e73 100644
--- a/mlir/lib/Support/StorageUniquer.cpp
+++ b/mlir/lib/Support/StorageUniquer.cpp
@@ -89,6 +89,9 @@ struct StorageUniquerImpl {
   // Parametric Storage
   //===--------------------------------------------------------------------===//
 
+  /// Check if an instance of a parametric storage class exists.
+  bool hasParametricStorage(TypeID id) { return parametricUniquers.count(id); }
+
   /// Get or create an instance of a parametric type.
   BaseStorage *
   getOrCreate(TypeID id, unsigned hashValue,
@@ -176,6 +179,9 @@ struct StorageUniquerImpl {
     return singletonInstance;
   }
 
+  /// Check if an instance of a singleton storage class exists.
+  bool hasSingleton(TypeID id) { return singletonInstances.count(id); }
+
   //===--------------------------------------------------------------------===//
   // Instance Storage
   //===--------------------------------------------------------------------===//
@@ -227,6 +233,16 @@ auto StorageUniquer::getSingletonImpl(TypeID id) -> BaseStorage * {
   return impl->getSingleton(id);
 }
 
+/// Test is the storage singleton is initialized.
+bool StorageUniquer::isSingletonStorageInitialized(TypeID id) {
+  return impl->hasSingleton(id);
+}
+
+/// Test is the parametric storage is initialized.
+bool StorageUniquer::isParametricStorageInitialized(TypeID id) {
+  return impl->hasParametricStorage(id);
+}
+
 /// Implementation for registering an instance of a derived type with default
 /// storage.
 void StorageUniquer::registerSingletonImpl(


        


More information about the Mlir-commits mailing list