Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mlir/lib/Bindings/Python/Rewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ class PyRewritePatternSet {
MlirPatternRewriter rewriter,
void *userData) -> MlirLogicalResult {
nb::handle f(static_cast<PyObject *>(userData));
nb::object res = f(op, PyPatternRewriter(rewriter));

PyMlirContextRef ctx =
PyMlirContext::forContext(mlirOperationGetContext(op));
Comment on lines +200 to +202
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should change all the signatures in here to be in terms of Py* so that we don't have to do this for* stuff (each one allocates a new Python object...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but we don't need to do that in this PR)

Copy link
Member Author

@PragmaTwice PragmaTwice Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I tried but here is a C callback (The function signature is defined in C API) so it seems hard to use Py* types directly? 🤔

nb::object opView = PyOperation::forOperation(ctx, op)->createOpView();

nb::object res = f(opView, PyPatternRewriter(rewriter));
return logicalResultFromObject(res);
};
MlirRewritePattern pattern = mlirOpRewritePattenCreate(
Expand Down
2 changes: 1 addition & 1 deletion mlir/python/mlir/dialects/arith.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
7 changes: 4 additions & 3 deletions mlir/test/python/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down