Improve diagnostic when parsing a custom op without registering the dialect
authorMehdi Amini <joker.eph@gmail.com>
Wed, 16 Nov 2022 21:35:26 +0000 (21:35 +0000)
committerMehdi Amini <joker.eph@gmail.com>
Wed, 16 Nov 2022 21:42:33 +0000 (21:42 +0000)
Reviewed By: rriddle

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

mlir/lib/AsmParser/Parser.cpp
mlir/test/IR/invalid.mlir

index 4bb4fdf..9b6d665 100644 (file)
@@ -1871,8 +1871,24 @@ OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
       defaultDialect = iface->getDefaultDialect();
   } else {
     Optional<Dialect::ParseOpHook> dialectHook;
-    if (Dialect *dialect = opNameInfo->getDialect())
-      dialectHook = dialect->getParseOperationHook(opName);
+    Dialect *dialect = opNameInfo->getDialect();
+    if (!dialect) {
+      InFlightDiagnostic diag =
+          emitError(opLoc) << "Dialect `" << opNameInfo->getDialectNamespace()
+                           << "' not found for custom op '" << originalOpName
+                           << "' ";
+      if (originalOpName != opName)
+        diag << " (tried '" << opName << "' as well)";
+      auto &note = diag.attachNote();
+      note << "Registered dialects: ";
+      llvm::interleaveComma(getContext()->getAvailableDialects(), note,
+                            [&](StringRef dialect) { note << dialect; });
+      note << " ; for more info on dialect registration see "
+              "https://mlir.llvm.org/getting_started/Faq/"
+              "#registered-loaded-dependent-whats-up-with-dialects-management";
+      return nullptr;
+    }
+    dialectHook = dialect->getParseOperationHook(opName);
     if (!dialectHook) {
       InFlightDiagnostic diag =
           emitError(opLoc) << "custom op '" << originalOpName << "' is unknown";
index 064b2ed..226dfdd 100644 (file)
@@ -112,6 +112,14 @@ func.func @non_operation() {
 
 // -----
 
+func.func @unknown_dialect_operation() {
+  // expected-error@below {{Dialect `foo' not found for custom op 'foo.asd'}}
+  // expected-note-re@below {{Registered dialects:{{.*}} test{{.*}}}}
+  foo.asd
+}
+
+// -----
+
 func.func @non_operation() {
   // expected-error@+1 {{custom op 'asd' is unknown (tried 'func.asd' as well)}}
   asd