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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 27 19:11:17 PDT 2025


Author: Longsheng Mou
Date: 2025-03-28T10:11:14+08:00
New Revision: a6cb5cc0f0b6448e9b2d05017ea17fccf1eb1feb

URL: https://github.com/llvm/llvm-project/commit/a6cb5cc0f0b6448e9b2d05017ea17fccf1eb1feb
DIFF: https://github.com/llvm/llvm-project/commit/a6cb5cc0f0b6448e9b2d05017ea17fccf1eb1feb.diff

LOG: [mlir] Add nullptr checks in SparseElementsAttr parser  (#133222)

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

Added: 
    

Modified: 
    mlir/lib/AsmParser/AttributeParser.cpp
    mlir/test/IR/invalid-builtin-attributes.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index 89b5970cf5e2f..52ab736bac03a 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -1081,6 +1081,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
@@ -1091,6 +1093,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..d2c11536404ea 100644
--- a/mlir/test/IR/invalid-builtin-attributes.mlir
+++ b/mlir/test/IR/invalid-builtin-attributes.mlir
@@ -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} : () -> ()


        


More information about the Mlir-commits mailing list