[Mlir-commits] [mlir] 109305e - [mlir] Emit errors when creating unregistered attributes/types when not allowed
River Riddle
llvmlistbot at llvm.org
Fri Apr 2 12:58:07 PDT 2021
Author: River Riddle
Date: 2021-04-02T12:45:35-07:00
New Revision: 109305e1046e68a26a99d3398434615ead1a8d2e
URL: https://github.com/llvm/llvm-project/commit/109305e1046e68a26a99d3398434615ead1a8d2e
DIFF: https://github.com/llvm/llvm-project/commit/109305e1046e68a26a99d3398434615ead1a8d2e.diff
LOG: [mlir] Emit errors when creating unregistered attributes/types when not allowed
This was missed when verification for creating unregistered operations was added.
Differential Revision: https://reviews.llvm.org/D99684
Added:
mlir/test/IR/invalid-unregistered.mlir
Modified:
mlir/lib/IR/BuiltinAttributes.cpp
mlir/lib/IR/BuiltinTypes.cpp
mlir/test/IR/attribute.mlir
mlir/unittests/IR/AttributeTest.cpp
Removed:
################################################################################
diff --git a/mlir/lib/IR/BuiltinAttributes.cpp b/mlir/lib/IR/BuiltinAttributes.cpp
index 947ee143c963..a7b1a6c1da14 100644
--- a/mlir/lib/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/IR/BuiltinAttributes.cpp
@@ -302,6 +302,19 @@ LogicalResult OpaqueAttr::verify(function_ref<InFlightDiagnostic()> emitError,
Type type) {
if (!Dialect::isValidNamespace(dialect.strref()))
return emitError() << "invalid dialect namespace '" << dialect << "'";
+
+ // Check that the dialect is actually registered.
+ MLIRContext *context = dialect.getContext();
+ if (!context->allowsUnregisteredDialects() &&
+ !context->getLoadedDialect(dialect.strref())) {
+ return emitError()
+ << "#" << dialect << "<\"" << attrData << "\"> : " << type
+ << " attribute created with unregistered dialect. If this is "
+ "intended, please call allowUnregisteredDialects() on the "
+ "MLIRContext, or use -allow-unregistered-dialect with "
+ "mlir-opt";
+ }
+
return success();
}
diff --git a/mlir/lib/IR/BuiltinTypes.cpp b/mlir/lib/IR/BuiltinTypes.cpp
index 99b85c362bb4..f792dfeacbf1 100644
--- a/mlir/lib/IR/BuiltinTypes.cpp
+++ b/mlir/lib/IR/BuiltinTypes.cpp
@@ -201,6 +201,19 @@ LogicalResult OpaqueType::verify(function_ref<InFlightDiagnostic()> emitError,
Identifier dialect, StringRef typeData) {
if (!Dialect::isValidNamespace(dialect.strref()))
return emitError() << "invalid dialect namespace '" << dialect << "'";
+
+ // Check that the dialect is actually registered.
+ MLIRContext *context = dialect.getContext();
+ if (!context->allowsUnregisteredDialects() &&
+ !context->getLoadedDialect(dialect.strref())) {
+ return emitError()
+ << "`!" << dialect << "<\"" << typeData << "\">"
+ << "` type created with unregistered dialect. If this is "
+ "intended, please call allowUnregisteredDialects() on the "
+ "MLIRContext, or use -allow-unregistered-dialect with "
+ "mlir-opt";
+ }
+
return success();
}
diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir
index 16f244d88aa3..a61a4d0bed7c 100644
--- a/mlir/test/IR/attribute.mlir
+++ b/mlir/test/IR/attribute.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
+// RUN: mlir-opt %s -split-input-file -allow-unregistered-dialect -verify-diagnostics | FileCheck %s
//===----------------------------------------------------------------------===//
// Test integer attributes
diff --git a/mlir/test/IR/invalid-unregistered.mlir b/mlir/test/IR/invalid-unregistered.mlir
new file mode 100644
index 000000000000..37ac45ef6d2a
--- /dev/null
+++ b/mlir/test/IR/invalid-unregistered.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-opt %s -split-input-file -verify-diagnostics
+
+// expected-error @below {{op created with unregistered dialect}}
+"unregistered_dialect.op"() : () -> ()
+
+// -----
+
+// expected-error @below {{attribute created with unregistered dialect}}
+#attr = #unregistered_dialect.attribute
+
+// -----
+
+// expected-error @below {{type created with unregistered dialect}}
+!type = type !unregistered_dialect.type
diff --git a/mlir/unittests/IR/AttributeTest.cpp b/mlir/unittests/IR/AttributeTest.cpp
index b18fe8a8f755..891abd1a4f23 100644
--- a/mlir/unittests/IR/AttributeTest.cpp
+++ b/mlir/unittests/IR/AttributeTest.cpp
@@ -150,6 +150,7 @@ TEST(DenseSplatTest, BF16Splat) {
TEST(DenseSplatTest, StringSplat) {
MLIRContext context;
+ context.allowUnregisteredDialects();
Type stringType =
OpaqueType::get(Identifier::get("test", &context), "string");
StringRef value = "test-string";
@@ -158,6 +159,7 @@ TEST(DenseSplatTest, StringSplat) {
TEST(DenseSplatTest, StringAttrSplat) {
MLIRContext context;
+ context.allowUnregisteredDialects();
Type stringType =
OpaqueType::get(Identifier::get("test", &context), "string");
Attribute stringAttr = StringAttr::get("test-string", stringType);
More information about the Mlir-commits
mailing list