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

Conversation

CoTinker
Copy link
Contributor

This PR adds nullptr checks in the SparseElementsAttr parser to improve robustness and prevent crashes. Fixes #132891.

This PR adds nullptr checks in the SparseElementsAttr parser to improve robustness and prevent crashes.
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Mar 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 27, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Longsheng Mou (CoTinker)

Changes

This PR adds nullptr checks in the SparseElementsAttr parser to improve robustness and prevent crashes. Fixes #132891.


Full diff: https://github.com/llvm/llvm-project/pull/133222.diff

2 Files Affected:

  • (modified) mlir/lib/AsmParser/AttributeParser.cpp (+4)
  • (modified) mlir/test/IR/invalid-builtin-attributes.mlir (+8)
diff --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index 2013d3623711b..cf2b3aaebde46 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -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
@@ -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);
diff --git a/mlir/test/IR/invalid-builtin-attributes.mlir b/mlir/test/IR/invalid-builtin-attributes.mlir
index 10988be91d84a..ed91b85819308 100644
--- a/mlir/test/IR/invalid-builtin-attributes.mlir
+++ b/mlir/test/IR/invalid-builtin-attributes.mlir
@@ -109,6 +109,14 @@ func.func @invalid_tensor_literal() {
 
 // -----
 
+func.func @invalid_tensor_literal() {
+  // expected-error @+2 {{unexpected decimal integer literal for a floating point value}}
+  // expected-note @+1 {{add a trailing dot to make the literal a float}}
+  "foo"(){bar = sparse<[0, 0], 0101> : tensor<1xf16>} : () -> ()
+}
+
+// -----
+
 func.func @hexadecimal_float_leading_minus() {
   // expected-error @+1 {{hexadecimal float literal should not have a leading minus}}
   "foo"() {value = -0x7fff : f16} : () -> ()

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG with the test coverage question addressed

@CoTinker CoTinker merged commit a6cb5cc into llvm:main Mar 28, 2025
11 checks passed
@CoTinker CoTinker deleted the nullptr branch March 28, 2025 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir] SparseElementsAttr verifier crashes
3 participants