Skip to content

[mlir] Add nullptr checks in SparseElementsAttr parser #133222

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 3 commits into from
Mar 28, 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
4 changes: 4 additions & 0 deletions mlir/lib/AsmParser/AttributeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,8 @@ Attribute Parser::parseSparseElementsAttr(Type attrType) {
indicesType = RankedTensorType::get(indiceParser.getShape(), indiceEltType);
}
auto indices = indiceParser.getAttr(indicesLoc, indicesType);
if (!indices)
return nullptr;

// If the values are a splat, set the shape explicitly based on the number of
// indices. The number of indices is encoded in the first dimension of the
Expand All @@ -1095,6 +1097,8 @@ Attribute Parser::parseSparseElementsAttr(Type attrType) {
? RankedTensorType::get({indicesType.getDimSize(0)}, valuesEltType)
: RankedTensorType::get(valuesParser.getShape(), valuesEltType);
auto values = valuesParser.getAttr(valuesLoc, valuesType);
if (!values)
return nullptr;

// Build the sparse elements attribute by the indices and values.
return getChecked<SparseElementsAttr>(loc, type, indices, values);
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/IR/invalid-builtin-attributes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func.func @invalid_tensor_literal() {

// -----

func.func @invalid_sparse_indices() {
// expected-error @+1 {{expected integer elements, but parsed floating-point}}
"foo"(){bar = sparse<0.5, 1> : tensor<1xi16>} : () -> ()
}

// -----

func.func @invalid_sparse_values() {
// expected-error @+1 {{expected integer elements, but parsed floating-point}}
"foo"(){bar = sparse<0, 1.1> : tensor<1xi16>} : () -> ()
}

// -----

func.func @hexadecimal_float_leading_minus() {
// expected-error @+1 {{hexadecimal float literal should not have a leading minus}}
"foo"() {value = -0x7fff : f16} : () -> ()
Expand Down