Skip to content

Commit bcfa193

Browse files
River707memfrob
authored andcommitted
[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
1 parent 5c5d2c9 commit bcfa193

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

mlir/lib/IR/BuiltinAttributes.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ LogicalResult OpaqueAttr::verify(function_ref<InFlightDiagnostic()> emitError,
302302
Type type) {
303303
if (!Dialect::isValidNamespace(dialect.strref()))
304304
return emitError() << "invalid dialect namespace '" << dialect << "'";
305+
306+
// Check that the dialect is actually registered.
307+
MLIRContext *context = dialect.getContext();
308+
if (!context->allowsUnregisteredDialects() &&
309+
!context->getLoadedDialect(dialect.strref())) {
310+
return emitError()
311+
<< "#" << dialect << "<\"" << attrData << "\"> : " << type
312+
<< " attribute created with unregistered dialect. If this is "
313+
"intended, please call allowUnregisteredDialects() on the "
314+
"MLIRContext, or use -allow-unregistered-dialect with "
315+
"mlir-opt";
316+
}
317+
305318
return success();
306319
}
307320

mlir/lib/IR/BuiltinTypes.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ LogicalResult OpaqueType::verify(function_ref<InFlightDiagnostic()> emitError,
201201
Identifier dialect, StringRef typeData) {
202202
if (!Dialect::isValidNamespace(dialect.strref()))
203203
return emitError() << "invalid dialect namespace '" << dialect << "'";
204+
205+
// Check that the dialect is actually registered.
206+
MLIRContext *context = dialect.getContext();
207+
if (!context->allowsUnregisteredDialects() &&
208+
!context->getLoadedDialect(dialect.strref())) {
209+
return emitError()
210+
<< "`!" << dialect << "<\"" << typeData << "\">"
211+
<< "` type created with unregistered dialect. If this is "
212+
"intended, please call allowUnregisteredDialects() on the "
213+
"MLIRContext, or use -allow-unregistered-dialect with "
214+
"mlir-opt";
215+
}
216+
204217
return success();
205218
}
206219

mlir/test/IR/attribute.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
1+
// RUN: mlir-opt %s -split-input-file -allow-unregistered-dialect -verify-diagnostics | FileCheck %s
22

33
//===----------------------------------------------------------------------===//
44
// Test integer attributes
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2+
3+
// expected-error @below {{op created with unregistered dialect}}
4+
"unregistered_dialect.op"() : () -> ()
5+
6+
// -----
7+
8+
// expected-error @below {{attribute created with unregistered dialect}}
9+
#attr = #unregistered_dialect.attribute
10+
11+
// -----
12+
13+
// expected-error @below {{type created with unregistered dialect}}
14+
!type = type !unregistered_dialect.type

mlir/unittests/IR/AttributeTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ TEST(DenseSplatTest, BF16Splat) {
150150

151151
TEST(DenseSplatTest, StringSplat) {
152152
MLIRContext context;
153+
context.allowUnregisteredDialects();
153154
Type stringType =
154155
OpaqueType::get(Identifier::get("test", &context), "string");
155156
StringRef value = "test-string";
@@ -158,6 +159,7 @@ TEST(DenseSplatTest, StringSplat) {
158159

159160
TEST(DenseSplatTest, StringAttrSplat) {
160161
MLIRContext context;
162+
context.allowUnregisteredDialects();
161163
Type stringType =
162164
OpaqueType::get(Identifier::get("test", &context), "string");
163165
Attribute stringAttr = StringAttr::get("test-string", stringType);

0 commit comments

Comments
 (0)