[Mlir-commits] [mlir] ee903a2 - Improve error message when creating an op that isn't registered in the context

Mehdi Amini llvmlistbot at llvm.org
Tue Sep 7 13:42:46 PDT 2021


Author: Mehdi Amini
Date: 2021-09-07T20:42:30Z
New Revision: ee903a207b767566b4a65f5519c545cccba28d28

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

LOG: Improve error message when creating an op that isn't registered in the context

This prints a more helpful error for folks who aren't intrinsically
familiar with the system.

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/Builders.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h
index 102fb3dc40e98..c7aa98d7fe07c 100644
--- a/mlir/include/mlir/IR/Builders.h
+++ b/mlir/include/mlir/IR/Builders.h
@@ -10,6 +10,7 @@
 #define MLIR_IR_BUILDERS_H
 
 #include "mlir/IR/OpDefinition.h"
+#include "llvm/Support/Compiler.h"
 
 namespace mlir {
 
@@ -388,14 +389,24 @@ class OpBuilder : public Builder {
   /// Creates an operation given the fields represented as an OperationState.
   Operation *createOperation(const OperationState &state);
 
+private:
+  /// Helper for sanity checking preconditions for create* methods below.
+  void checkHasAbstractOperation(const OperationName &name) {
+    if (LLVM_UNLIKELY(!name.getAbstractOperation()))
+      llvm::report_fatal_error(
+          "Building op `" + name.getStringRef().str() +
+          "` but it isn't registered in this MLIRContext: the dialect may not "
+          "be loaded or this operation isn't registered by the dialect. See "
+          "also https://mlir.llvm.org/getting_started/Faq/"
+          "#registered-loaded-dependent-whats-up-with-dialects-management");
+  }
+
+public:
   /// Create an operation of specific op type at the current insertion point.
   template <typename OpTy, typename... Args>
   OpTy create(Location location, Args &&...args) {
     OperationState state(location, OpTy::getOperationName());
-    if (!state.name.getAbstractOperation())
-      llvm::report_fatal_error("Building op `" +
-                               state.name.getStringRef().str() +
-                               "` but it isn't registered in this MLIRContext");
+    checkHasAbstractOperation(state.name);
     OpTy::build(*this, state, std::forward<Args>(args)...);
     auto *op = createOperation(state);
     auto result = dyn_cast<OpTy>(op);
@@ -412,10 +423,7 @@ class OpBuilder : public Builder {
     // Create the operation without using 'createOperation' as we don't want to
     // insert it yet.
     OperationState state(location, OpTy::getOperationName());
-    if (!state.name.getAbstractOperation())
-      llvm::report_fatal_error("Building op `" +
-                               state.name.getStringRef().str() +
-                               "` but it isn't registered in this MLIRContext");
+    checkHasAbstractOperation(state.name);
     OpTy::build(*this, state, std::forward<Args>(args)...);
     Operation *op = Operation::create(state);
 


        


More information about the Mlir-commits mailing list