[Mlir-commits] [mlir] [mlir][python] value casting (PR #69644)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Tue Oct 31 02:48:51 PDT 2023


================
@@ -231,7 +231,7 @@ class Sliceable {
   /// Returns the element at the given slice index. Supports negative indices
   /// by taking elements in inverse order. Returns a nullptr object if out
   /// of bounds.
-  pybind11::object getItem(intptr_t index) {
+  virtual pybind11::object getItem(intptr_t index) {
----------------
ftynse wrote:

This class is CRTP, so it should be possible to do something like

```
pybind11::object getItem(intptr_t index) {
  index = wrapIndex(index);
  // other fluff
  return static_cast<Derived *>(this)->getItemImpl(index);
}

pybind11::object getItemImpl(intptr_t index) {
  // default implementation lives here
}
```

and have some subclasses provide different implementations of `getItemImpl`.

I also suspect it is possible to avoid the C++/Python back-and-forth here. The current implementation calls `getRawElement` that returns an instance of `ElementTy`, casts it to `py::object` and hands it off to the override that calls `maybeDownCast` via a python attribute. It should be possible to just directly call `maybeDownCast` on an instance of `ElementTy` since we have it. Potentially with some `llvm::is_detected` templating around the element class.

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


More information about the Mlir-commits mailing list