[Mlir-commits] [mlir] [MLIR][Python] Support Python-defined passes in MLIR (PR #156000)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sun Aug 31 19:20:15 PDT 2025
================
@@ -20,6 +22,81 @@ using namespace mlir::python;
namespace {
+// A base class for defining passes in Python
+// Users are expected to subclass this and implement the `run` method, e.g.
+// ```
+// class MyPass(mlir.passmanager.Pass):
+// def __init__(self):
+// super().__init__("MyPass", ..)
+// # other init stuff..
+// def run(self, operation):
+// # do something with operation..
+// pass
+// ```
+class PyPassBase {
+public:
+ PyPassBase(std::string name, std::string argument, std::string description,
+ std::string opName)
+ : name(std::move(name)), argument(std::move(argument)),
+ description(std::move(description)), opName(std::move(opName)) {
+ callbacks.construct = [](void *obj) {};
+ callbacks.destruct = [](void *obj) {
+ nb::handle(static_cast<PyObject *>(obj)).dec_ref();
+ };
+ callbacks.run = [](MlirOperation op, MlirExternalPass, void *obj) {
+ auto handle = nb::handle(static_cast<PyObject *>(obj));
+ nb::cast<PyPassBase *>(handle)->run(op);
+ };
+ callbacks.clone = [](void *obj) -> void * {
+ nb::object copy = nb::module_::import_("copy");
+ nb::object deepcopy = copy.attr("deepcopy");
+ return deepcopy(obj).release().ptr();
+ };
+ callbacks.initialize = nullptr;
----------------
PragmaTwice wrote:
`initialize` is not yet ported to python side yet.
https://github.com/llvm/llvm-project/pull/156000
More information about the Mlir-commits
mailing list