[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.
This commit is contained in:
Longsheng Mou
2025-04-01 16:27:43 +08:00
committed by GitHub
parent ad48fffb53
commit 1cf6786e32
2 changed files with 28 additions and 0 deletions

View File

@@ -566,6 +566,19 @@ DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) {
if (ComplexType complexTy = dyn_cast<ComplexType>(eltType)) {
eltType = complexTy.getElementType();
isComplex = true;
// Complex types have 2 elements.
if (shape.empty() && storage.size() != 2) {
p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
<< complexTy << ") expected 2 elements";
return nullptr;
}
if (!shape.empty() &&
storage.size() != static_cast<size_t>(type.getNumElements()) * 2) {
p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
<< type << ") expected " << type.getNumElements() * 2
<< " elements";
return nullptr;
}
}
// Handle integer and index types.

View File

@@ -63,6 +63,21 @@ func.func @elementsattr_toolarge1() -> () {
// -----
// expected-error@+1 {{parsed 1 elements, but type ('complex<i64>') expected 2 elements}}
#attr = dense<0> : tensor<2xcomplex<i64>>
// -----
// expected-error@+1 {{parsed 2 elements, but type ('tensor<2xcomplex<i64>>') expected 4 elements}}
#attr = dense<[0, 1]> : tensor<2xcomplex<i64>>
// -----
// expected-error@+1 {{parsed 3 elements, but type ('tensor<2xcomplex<i64>>') expected 4 elements}}
#attr = dense<[0, (0, 1)]> : tensor<2xcomplex<i64>>
// -----
func.func @elementsattr_toolarge2() -> () {
"foo"(){bar = dense<[-777]> : tensor<1xi8>} : () -> () // expected-error {{integer constant out of range}}
}