[Mlir-commits] [mlir] [MLIR][Python] Impl XOpInterface(s) from Python, with X=Transform and X=MemoryEffects (PR #176920)

Rolf Morel llvmlistbot at llvm.org
Wed Feb 11 08:53:57 PST 2026


================
@@ -9,13 +9,80 @@
 #ifndef MLIR_BINDINGS_PYTHON_REWRITE_H
 #define MLIR_BINDINGS_PYTHON_REWRITE_H
 
-#include "mlir/Bindings/Python/NanobindUtils.h"
+#include "mlir-c/Rewrite.h"
+#include "mlir/Bindings/Python/IRCore.h"
+
+#include <nanobind/nanobind.h>
+
+namespace nb = nanobind;
 
 namespace mlir {
 namespace python {
 namespace MLIR_BINDINGS_PYTHON_DOMAIN {
+
+/// CRTP Base class for rewriter wrappers.
+template <typename DerivedTy>
+class PyRewriterBase {
+public:
+  PyRewriterBase(MlirRewriterBase rewriter)
+      : base(rewriter),
+        ctx(PyMlirContext::forContext(mlirRewriterBaseGetContext(base))) {}
+
+  PyInsertionPoint getInsertionPoint() const {
+    MlirBlock block = mlirRewriterBaseGetInsertionBlock(base);
+    MlirOperation op = mlirRewriterBaseGetOperationAfterInsertion(base);
+
+    if (mlirOperationIsNull(op)) {
+      MlirOperation owner = mlirBlockGetParentOperation(block);
+      auto parent = PyOperation::forOperation(ctx, owner);
+      return PyInsertionPoint(PyBlock(parent, block));
+    }
+
+    return PyInsertionPoint(PyOperation::forOperation(ctx, op));
+  }
+
+  void replaceOp(MlirOperation op, MlirOperation newOp) {
----------------
rolfmorel wrote:

Inlined these functions to their `bind()` bindings.

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


More information about the Mlir-commits mailing list