Skip to content

Commit 1cf6786

Browse files
authored
[mlir] Improve error handling for dense attribute parsing in complex types (#133220)
- For splat dense attributes, the number of parsed elements must be 2. - For non-splat dense attributes, the number of parsed elements must be twice the number of elements in the type. Fixes #132859.
1 parent ad48fff commit 1cf6786

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

mlir/lib/AsmParser/AttributeParser.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,19 @@ DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) {
566566
if (ComplexType complexTy = dyn_cast<ComplexType>(eltType)) {
567567
eltType = complexTy.getElementType();
568568
isComplex = true;
569+
// Complex types have 2 elements.
570+
if (shape.empty() && storage.size() != 2) {
571+
p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
572+
<< complexTy << ") expected 2 elements";
573+
return nullptr;
574+
}
575+
if (!shape.empty() &&
576+
storage.size() != static_cast<size_t>(type.getNumElements()) * 2) {
577+
p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
578+
<< type << ") expected " << type.getNumElements() * 2
579+
<< " elements";
580+
return nullptr;
581+
}
569582
}
570583

571584
// Handle integer and index types.

mlir/test/IR/invalid-builtin-attributes.mlir

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ func.func @elementsattr_toolarge1() -> () {
6363

6464
// -----
6565

66+
// expected-error@+1 {{parsed 1 elements, but type ('complex<i64>') expected 2 elements}}
67+
#attr = dense<0> : tensor<2xcomplex<i64>>
68+
69+
// -----
70+
71+
// expected-error@+1 {{parsed 2 elements, but type ('tensor<2xcomplex<i64>>') expected 4 elements}}
72+
#attr = dense<[0, 1]> : tensor<2xcomplex<i64>>
73+
74+
// -----
75+
76+
// expected-error@+1 {{parsed 3 elements, but type ('tensor<2xcomplex<i64>>') expected 4 elements}}
77+
#attr = dense<[0, (0, 1)]> : tensor<2xcomplex<i64>>
78+
79+
// -----
80+
6681
func.func @elementsattr_toolarge2() -> () {
6782
"foo"(){bar = dense<[-777]> : tensor<1xi8>} : () -> () // expected-error {{integer constant out of range}}
6883
}

0 commit comments

Comments
 (0)