[Mlir-commits] [mlir] [mlir] Add nullptr checks in SparseElementsAttr parser (PR #133222)
Longsheng Mou
llvmlistbot at llvm.org
Thu Mar 27 04:48:01 PDT 2025
https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/133222
>From eddb318aad87951288324ec88f119bdae0c642b6 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 27 Mar 2025 17:24:45 +0800
Subject: [PATCH 1/3] [mlir] Add nullptr checks in SparseElementsAttr parser
This PR adds nullptr checks in the SparseElementsAttr parser to improve robustness and prevent crashes.
---
mlir/lib/AsmParser/AttributeParser.cpp | 4 ++++
1 file changed, 4 insertions(+)
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);
>From cf2e4e369d7612243e18c99e0c2a85a540a01889 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 27 Mar 2025 17:26:58 +0800
Subject: [PATCH 2/3] add test
---
mlir/test/IR/invalid-builtin-attributes.mlir | 8 ++++++++
1 file changed, 8 insertions(+)
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} : () -> ()
>From 01c19449f6ead42d86dae8058e972780ae19f6e9 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 27 Mar 2025 19:47:52 +0800
Subject: [PATCH 3/3] Update tests
---
mlir/test/IR/invalid-builtin-attributes.mlir | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/mlir/test/IR/invalid-builtin-attributes.mlir b/mlir/test/IR/invalid-builtin-attributes.mlir
index ed91b85819308..d2c11536404ea 100644
--- a/mlir/test/IR/invalid-builtin-attributes.mlir
+++ b/mlir/test/IR/invalid-builtin-attributes.mlir
@@ -109,10 +109,16 @@ 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 @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>} : () -> ()
}
// -----
More information about the Mlir-commits
mailing list