[Mlir-commits] [mlir] [mlir][python] Fix segfault in DenseResourceElementsAttr.get_from_buffer for 0-d tensors (PR #183070)
Rahul Kayaith
llvmlistbot at llvm.org
Tue Feb 24 07:02:03 PST 2026
https://github.com/rkayaith created https://github.com/llvm/llvm-project/pull/183070
When `ndim == 0`, `view->strides[view->ndim - 1]` is an out-of-bounds access (unsigned underflow to `SIZE_MAX`). Use `view->itemsize` for alignment instead, since a scalar buffer is trivially aligned to its element size.
Fixes iree-org/iree-turbine#1312.
>From 9be01544451bc581537283b8028ae333e574d6e6 Mon Sep 17 00:00:00 2001
From: Rahul Kayaith <rkayaith at gmail.com>
Date: Tue, 24 Feb 2026 14:55:30 +0000
Subject: [PATCH] [mlir][python] Fix segfault in
DenseResourceElementsAttr.get_from_buffer for 0-d tensors
When ndim == 0, `view->strides[view->ndim - 1]` is an out-of-bounds
access (unsigned underflow to SIZE_MAX). Use `view->itemsize` for
alignment instead, since a scalar buffer is trivially aligned to its
element size.
Fixes iree-org/iree-turbine#1312.
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 2 ++
mlir/test/python/ir/array_attributes.py | 16 ++++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 8c4a2dcd5a7f7..d322933eccd1c 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -1086,6 +1086,8 @@ PyDenseResourceElementsAttribute::getFromBuffer(
size_t inferredAlignment;
if (alignment)
inferredAlignment = *alignment;
+ else if (view->ndim == 0)
+ inferredAlignment = view->itemsize;
else
inferredAlignment = view->strides[view->ndim - 1];
diff --git a/mlir/test/python/ir/array_attributes.py b/mlir/test/python/ir/array_attributes.py
index c1e2dd5f5ae5e..9b4249b0c686b 100644
--- a/mlir/test/python/ir/array_attributes.py
+++ b/mlir/test/python/ir/array_attributes.py
@@ -621,6 +621,22 @@ def test_attribute(context, mview):
print("EXIT FUNCTION")
+# CHECK-LABEL: TEST: testGetDenseResourceElementsAttrScalar
+ at run
+def testGetDenseResourceElementsAttrScalar():
+ with Context(), Location.unknown():
+ element_type = IntegerType.get_signless(64)
+ tensor_type = RankedTensorType.get((), element_type)
+ resource = DenseResourceElementsAttr.get_from_buffer(
+ memoryview(np.array(42, dtype=np.int64)), "scalar", tensor_type
+ )
+ module = Module.create()
+ module.operation.attributes["test.resource"] = resource
+ # CHECK: test.resource = dense_resource<scalar> : tensor<i64>
+ # CHECK: scalar: "0x080000002A00000000000000"
+ print(module)
+
+
# CHECK-LABEL: TEST: testDanglingResource
print("TEST: testDanglingResource")
# see https://github.com/llvm/llvm-project/pull/149414, https://github.com/llvm/llvm-project/pull/150137, https://github.com/llvm/llvm-project/pull/150561
More information about the Mlir-commits
mailing list