From 475e2f11585a5ae588f0187cc21b6290145dadad Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Sun, 12 Oct 2025 23:29:09 +0800 Subject: [PATCH 1/3] [MLIR][Python] Pass OpView subclasses instead of Operation in rewrite patterns --- mlir/lib/Bindings/Python/Rewrite.cpp | 7 ++++++- mlir/test/python/rewrite.py | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mlir/lib/Bindings/Python/Rewrite.cpp b/mlir/lib/Bindings/Python/Rewrite.cpp index 47685567d5355..5ddb3fbbb1317 100644 --- a/mlir/lib/Bindings/Python/Rewrite.cpp +++ b/mlir/lib/Bindings/Python/Rewrite.cpp @@ -197,7 +197,12 @@ class PyRewritePatternSet { MlirPatternRewriter rewriter, void *userData) -> MlirLogicalResult { nb::handle f(static_cast(userData)); - nb::object res = f(op, PyPatternRewriter(rewriter)); + + PyMlirContextRef ctx = + PyMlirContext::forContext(mlirOperationGetContext(op)); + nb::object opView = PyOperation::forOperation(ctx, op)->createOpView(); + + nb::object res = f(opView, PyPatternRewriter(rewriter)); return logicalResultFromObject(res); }; MlirRewritePattern pattern = mlirOpRewritePattenCreate( diff --git a/mlir/test/python/rewrite.py b/mlir/test/python/rewrite.py index acf7db23db914..523e6a40f7470 100644 --- a/mlir/test/python/rewrite.py +++ b/mlir/test/python/rewrite.py @@ -17,15 +17,15 @@ def run(f): def testRewritePattern(): def to_muli(op, rewriter): with rewriter.ip: - new_op = arith.muli(op.operands[0], op.operands[1], loc=op.location) + new_op = arith.muli(op.lhs, op.rhs, loc=op.location) rewriter.replace_op(op, new_op.owner) def constant_1_to_2(op, rewriter): - c = op.attributes["value"].value + c = IntegerAttr(op.value).value if c != 1: return True # failed to match with rewriter.ip: - new_op = arith.constant(op.result.type, 2, loc=op.location) + new_op = arith.constant(op.type, 2, loc=op.location) rewriter.replace_op(op, [new_op]) with Context(): From 7d2dfeb453e4ad9817e39349f50bc620f126bae7 Mon Sep 17 00:00:00 2001 From: Twice Date: Mon, 13 Oct 2025 09:22:53 +0800 Subject: [PATCH 2/3] Update mlir/test/python/rewrite.py Co-authored-by: Maksim Levental --- mlir/test/python/rewrite.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mlir/test/python/rewrite.py b/mlir/test/python/rewrite.py index 523e6a40f7470..d983691e60d59 100644 --- a/mlir/test/python/rewrite.py +++ b/mlir/test/python/rewrite.py @@ -17,6 +17,7 @@ def run(f): def testRewritePattern(): def to_muli(op, rewriter): with rewriter.ip: + assert isinstance(op, arith.AddIOp) new_op = arith.muli(op.lhs, op.rhs, loc=op.location) rewriter.replace_op(op, new_op.owner) From 79c5e615ce2b329bf5ec278a9bed3bce2da11b03 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Mon, 13 Oct 2025 10:39:01 +0800 Subject: [PATCH 3/3] fix arith.ConstantOp.value --- mlir/python/mlir/dialects/arith.py | 2 +- mlir/test/python/rewrite.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/python/mlir/dialects/arith.py b/mlir/python/mlir/dialects/arith.py index 92da5df9bce66..88e8502a29eae 100644 --- a/mlir/python/mlir/dialects/arith.py +++ b/mlir/python/mlir/dialects/arith.py @@ -92,7 +92,7 @@ def type(self): @property def value(self): - return Attribute(self.operation.attributes["value"]) + return self.operation.attributes["value"] @property def literal_value(self) -> Union[int, float]: diff --git a/mlir/test/python/rewrite.py b/mlir/test/python/rewrite.py index d983691e60d59..821e47085a5bd 100644 --- a/mlir/test/python/rewrite.py +++ b/mlir/test/python/rewrite.py @@ -22,7 +22,7 @@ def to_muli(op, rewriter): rewriter.replace_op(op, new_op.owner) def constant_1_to_2(op, rewriter): - c = IntegerAttr(op.value).value + c = op.value.value if c != 1: return True # failed to match with rewriter.ip: