[Mlir-commits] [mlir] [MLIR][Python] Support dialect conversion in python bindings (PR #177782)
Maksim Levental
llvmlistbot at llvm.org
Wed Jan 28 20:48:37 PST 2026
================
@@ -409,9 +582,97 @@ void populateRewriteSubmodule(nb::module_ &m) {
If possible, the operation is cast to its corresponding OpView subclass
before being passed to the callable.
benefit: The benefit of the pattern, defaulting to 1.)")
+ .def(
+ "add_conversion",
+ [](PyRewritePatternSet &self, nb::handle root, const nb::callable &fn,
+ PyTypeConverter &typeConverter, unsigned benefit) {
+ std::string opName =
+ nb::cast<std::string>(root.attr("OPERATION_NAME"));
+ self.addConversion(
+ mlirStringRefCreate(opName.data(), opName.size()), benefit, fn,
+ typeConverter);
+ },
+ "root"_a, "fn"_a, "type_converter"_a, "benefit"_a = 1,
+ R"(
+ Add a new conversion pattern on the specified root operation,
+ using the provided callable for matching and rewriting,
+ and assign it the given benefit.
+
+ Args:
+ root: The root operation to which this pattern applies.
+ This may be either an OpView subclass (e.g., ``arith.AddIOp``) or
+ an operation name string (e.g., ``"arith.addi"``).
+ fn: The callable to use for matching and rewriting,
+ which takes an operation, its adaptor,
+ the type converter and a pattern rewriter as arguments.
+ The match is considered successful iff the callable returns
+ a value where ``bool(value)`` is ``False`` (e.g. ``None``).
+ If possible, the operation is cast to its corresponding OpView subclass
+ before being passed to the callable.
+ type_converter: The type converter to convert types in the IR.
+ benefit: The benefit of the pattern, defaulting to 1.)")
.def("freeze", &PyRewritePatternSet::freeze,
"Freeze the pattern set into a frozen one.");
+ nb::class_<PyConversionPatternRewriter, PyPatternRewriter>(
+ m, "ConversionPatternRewriter");
+
+ nb::class_<PyConversionTarget>(m, "ConversionTarget")
+ .def(
+ "__init__",
+ [](PyConversionTarget &self, DefaultingPyMlirContext context) {
+ new (&self) PyConversionTarget(context.get()->get());
+ },
+ "context"_a = nb::none())
+ .def(
+ "add_legal_op",
+ [](PyConversionTarget &self, const nb::args &ops) {
----------------
makslevental wrote:
and now i understand the idea behind `"OPERATION_NAME"` - it's like `mlir::OperationName`
https://github.com/llvm/llvm-project/pull/177782
More information about the Mlir-commits
mailing list