[Mlir-commits] [llvm] [mlir] [mlir python] Port Python core code to nanobind. (PR #118583)

Peter Hawkins llvmlistbot at llvm.org
Thu Dec 12 06:50:37 PST 2024


================
@@ -205,14 +316,13 @@ class PyDenseArrayAttribute : public PyConcreteAttribute<DerivedT> {
     EltTy dunderNext() {
       // Throw if the index has reached the end.
       if (nextIndex >= mlirDenseArrayGetNumElements(attr.get()))
-        throw py::stop_iteration();
+        throw nb::stop_iteration();
----------------
hawkinsp wrote:

Yes, I agree. C++ exceptions are slow and shouldn't be used in cases that happen frequently. Not new or changed in this PR, though.

We had a similar problem in JAX for a different exception (`KeyError`) and method (`__getattr__`). It's not hard to attach a classic C API method to a nanobind or pybind class if it turns out to matter for performance, e.g., see: https://github.com/openxla/xla/blob/f652407e382f7eb2fe58ae0eec6b8edd31e68f63/xla/python/py_device.cc#L314

In fact we do this trick in both directions. We also attach nanobind-defined methods to classes defined using the low level C APIs.

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


More information about the Mlir-commits mailing list