[Mlir-commits] [mlir] [mlir] Add nullptr checks in SparseElementsAttr parser (PR #133222)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 27 02:29:58 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: Longsheng Mou (CoTinker)

<details>
<summary>Changes</summary>

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
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} : () -> ()

``````````

</details>


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


More information about the Mlir-commits mailing list