[Mlir-commits] [mlir] [MLIR][transform][python] add sugared python abstractions for transform dialect (PR #75073)

Maksim Levental llvmlistbot at llvm.org
Wed Dec 13 08:15:32 PST 2023


Martin =?utf-8?q?Lücke?= <martin.luecke at ed.ac.uk>,
Martin =?utf-8?q?Lücke?= <martin.luecke at ed.ac.uk>,
Martin =?utf-8?q?Lücke?= <martin.luecke at ed.ac.uk>,
Martin =?utf-8?q?Lücke?= <martin.luecke at ed.ac.uk>,
Martin =?utf-8?q?Lücke?= <martin.luecke at ed.ac.uk>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/75073 at github.com>


================
@@ -495,6 +495,8 @@ class mlir_type_subclass : public pure_subclass {
           .attr("replace")(superCls.attr("__name__"), captureTypeName);
     });
     if (getTypeIDFunction) {
+      def_staticmethod("get_static_typeid",
+                       [getTypeIDFunction]() { return getTypeIDFunction(); });
----------------
makslevental wrote:

@martin-luecke can you try this patch

```diff
diff --git a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
index 125f9b92937b..e0fd338f7109 100644
--- a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
@@ -18,6 +18,7 @@
 #ifndef MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
 #define MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
 
+#include <pybind11/detail/internals.h>
 #include <pybind11/pybind11.h>
 #include <pybind11/pytypes.h>
 #include <pybind11/stl.h>
@@ -357,6 +358,17 @@ public:
     return *this;
   }
 
+  template <typename Func, typename... Extra>
+  pure_subclass &def_property_readonly_static(const char *name, Func &&f,
+                                              const Extra &...extra) {
+    py::cpp_function cf(std::forward<Func>(f), py::name(name), extra...);
+    auto *staticPropertyType = py::detail::get_internals().static_property_type;
+    auto builtinProperty =
+        py::reinterpret_borrow<py::object>((PyObject *)staticPropertyType);
+    thisClass.attr(name) = builtinProperty(cf);
+    return *this;
+  }
+
   template <typename Func, typename... Extra>
   pure_subclass &def_classmethod(const char *name, Func &&f,
                                  const Extra &...extra) {
@@ -495,8 +507,9 @@ public:
           .attr("replace")(superCls.attr("__name__"), captureTypeName);
     });
     if (getTypeIDFunction) {
-      def_staticmethod("get_static_typeid",
-                       [getTypeIDFunction]() { return getTypeIDFunction(); });
+      def_property_readonly_static("get_static_typeid", [getTypeIDFunction]() {
+        return getTypeIDFunction();
+      });
       py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
           .attr(MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR)(
               getTypeIDFunction())(pybind11::cpp_function(
```

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


More information about the Mlir-commits mailing list