Skip to content

[mlir] Improve error handling for dense attribute parsing in complex types #133220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mlir/lib/AsmParser/AttributeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,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.
Expand Down
15 changes: 15 additions & 0 deletions mlir/test/IR/invalid-builtin-attributes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Expand Down