From 06e2c78680d753d97b0cd6b7a86b4dbd0dbfb1e9 Mon Sep 17 00:00:00 2001 From: Twice Date: Mon, 13 Oct 2025 11:56:57 +0800 Subject: [PATCH] [MLIR][Python] Pass OpView subclasses instead of Operation in rewrite patterns (#163080) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a follow-up PR for #162699. Currently, in the function where we define rewrite patterns, the `op` we receive is of type `ir.Operation` rather than a specific `OpView` type (such as `arith.AddIOp`). This means we can’t conveniently access certain parts of the operation — for example, we need to use `op.operands[0]` instead of `op.lhs`. The following example code illustrates this situation. ```python def to_muli(op, rewriter): # op is typed ir.Operation instead of arith.AddIOp pass patterns.add(arith.AddIOp, to_muli) ``` In this PR, we convert the operation to its corresponding `OpView` subclass before invoking the rewrite pattern callback, making it much easier to write patterns. --------- Co-authored-by: Maksim Levental --- mlir/lib/Bindings/Python/Rewrite.cpp | 7 ++++++- mlir/python/mlir/dialects/arith.py | 2 +- mlir/test/python/rewrite.py | 7 ++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/mlir/lib/Bindings/Python/Rewrite.cpp b/mlir/lib/Bindings/Python/Rewrite.cpp index 47685567d535..5ddb3fbbb131 100644 --- a/mlir/lib/Bindings/Python/Rewrite.cpp +++ b/mlir/lib/Bindings/Python/Rewrite.cpp @@ -197,7 +197,12 @@ public: 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/python/mlir/dialects/arith.py b/mlir/python/mlir/dialects/arith.py index 92da5df9bce6..88e8502a29ea 100644 --- a/mlir/python/mlir/dialects/arith.py +++ b/mlir/python/mlir/dialects/arith.py @@ -92,7 +92,7 @@ class ConstantOp(ConstantOp): @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 acf7db23db91..821e47085a5b 100644 --- a/mlir/test/python/rewrite.py +++ b/mlir/test/python/rewrite.py @@ -17,15 +17,16 @@ 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) + assert isinstance(op, arith.AddIOp) + 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 = 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():