[Mlir-commits] [mlir] [mlir:python] Prevent crash in DenseElementsAttr. (PR #163564)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Oct 15 07:20:34 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Ingo Müller (ingomueller-net)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/163564.diff


1 Files Affected:

- (modified) mlir/lib/Bindings/Python/IRAttributes.cpp (+4) 


``````````diff
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;

``````````

</details>


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


More information about the Mlir-commits mailing list