[Mlir-commits] [mlir] [MLIR][Python] Call `notifyOperationInserted` while constructing new op in rewrite patterns (PR #163694)

Maksim Levental llvmlistbot at llvm.org
Thu Oct 16 11:32:59 PDT 2025


================
@@ -202,7 +207,15 @@ class PyRewritePatternSet {
           PyMlirContext::forContext(mlirOperationGetContext(op));
       nb::object opView = PyOperation::forOperation(ctx, op)->createOpView();
 
-      nb::object res = f(opView, PyPatternRewriter(rewriter));
+      PyPatternRewriter pyRewriter(rewriter);
+      nb::object listener = nb::cast(pyRewriter.getListener());
+
+      listener.attr("__enter__")();
+      auto exit = llvm::make_scope_exit([listener] {
+        listener.attr("__exit__")(nb::none(), nb::none(), nb::none());
+      });
----------------
makslevental wrote:

i think both the addition of `PyRewriterBaseListener` and the idea of somehow calling notify on listeners is a great idea. i think using it like this (in addition to adding it to the default threadcontextstack) is not a good idea. 2 possibilities:

1. having a separate threadcontextstack just for listeners (but also - is this how listeners are actually composed? aren't there just like parent->child relationship? i don't remember right this second)
2. passing in the listener to `add` (here) explicitly

also btw ADT isn't actually safe to use like this i think? i need to double check

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


More information about the Mlir-commits mailing list