Skip to content

Commit 475e2f1

Browse files
committed
[MLIR][Python] Pass OpView subclasses instead of Operation in rewrite patterns
1 parent 1fde14b commit 475e2f1

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

mlir/lib/Bindings/Python/Rewrite.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ class PyRewritePatternSet {
197197
MlirPatternRewriter rewriter,
198198
void *userData) -> MlirLogicalResult {
199199
nb::handle f(static_cast<PyObject *>(userData));
200-
nb::object res = f(op, PyPatternRewriter(rewriter));
200+
201+
PyMlirContextRef ctx =
202+
PyMlirContext::forContext(mlirOperationGetContext(op));
203+
nb::object opView = PyOperation::forOperation(ctx, op)->createOpView();
204+
205+
nb::object res = f(opView, PyPatternRewriter(rewriter));
201206
return logicalResultFromObject(res);
202207
};
203208
MlirRewritePattern pattern = mlirOpRewritePattenCreate(

mlir/test/python/rewrite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ def run(f):
1717
def testRewritePattern():
1818
def to_muli(op, rewriter):
1919
with rewriter.ip:
20-
new_op = arith.muli(op.operands[0], op.operands[1], loc=op.location)
20+
new_op = arith.muli(op.lhs, op.rhs, loc=op.location)
2121
rewriter.replace_op(op, new_op.owner)
2222

2323
def constant_1_to_2(op, rewriter):
24-
c = op.attributes["value"].value
24+
c = IntegerAttr(op.value).value
2525
if c != 1:
2626
return True # failed to match
2727
with rewriter.ip:
28-
new_op = arith.constant(op.result.type, 2, loc=op.location)
28+
new_op = arith.constant(op.type, 2, loc=op.location)
2929
rewriter.replace_op(op, [new_op])
3030

3131
with Context():

0 commit comments

Comments
 (0)