[Mlir-commits] [mlir] 907335c - [mlir:python] Prevent crash in DenseElementsAttr. (#163564)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Oct 20 06:13:04 PDT 2025
Author: Ingo Müller
Date: 2025-10-20T15:13:00+02:00
New Revision: 907335c00c283e72373d4dd1b86bd057357c7f01
URL: https://github.com/llvm/llvm-project/commit/907335c00c283e72373d4dd1b86bd057357c7f01
DIFF: https://github.com/llvm/llvm-project/commit/907335c00c283e72373d4dd1b86bd057357c7f01.diff
LOG: [mlir:python] Prevent crash in DenseElementsAttr. (#163564)
This PR fixes a crash in the `bf_getbuffer` implementation of
`PyDenseElementsAttribute` that occurred when an element type was not
supported, such as `bf16`. I believe that supportion `bf16` is not
possible with that protocol but that's out of the scope of this PR.
Previsouly, the code raised an `std::exception` out of `bf_getbuffer`
that nanobind does not catch (see also pybind/pybind11#3336). The PR
makes the function catch all `std::exception`s and manually raises a
Python exception instead.
Signed-off-by: Ingo Müller <ingomueller at google.com>
Added:
Modified:
mlir/lib/Bindings/Python/IRAttributes.cpp
Removed:
################################################################################
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 045c0fbf4630f..c0a945e3f4f3b 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -1306,6 +1306,10 @@ PyType_Slot PyDenseElementsAttribute::slots[] = {
e.restore();
nb::chain_error(PyExc_BufferError, "Error converting attribute to buffer");
return -1;
+ } catch (std::exception &e) {
+ nb::chain_error(PyExc_BufferError,
+ "Error converting attribute to buffer: %s", e.what());
+ return -1;
}
view->obj = obj;
view->ndim = 1;
More information about the Mlir-commits
mailing list