summaryrefslogtreecommitdiff
path: root/mlir/lib/CAPI/Transforms/Rewrite.cpp
AgeCommit message (Collapse)Author
2025-10-17[MLIR] Fix typo of the word "pattern" in CAPI and docs (#163780)Shenghang Tsai
This includes the rename from `mlirOpRewritePattenCreate` to `mlirOpRewritePatternCreate` in CAPI, and other typo fixes in docs and code comments.
2025-10-11[MLIR][NFC] Clean up rewrite CAPI implementation and Python bindings (#162974)Twice
This is a follow-up PR of #162699. In this PR we clean CAPI and Python bindings of MLIR rewrite part by: - remove all manually-defined `wrap`/`unwrap` functions; - remove useless nanobind-defined Python class `RewritePattern`.
2025-10-11[MLIR][Python] Support Python-defined rewrite patterns (#162699)Twice
This PR adds support for defining custom **`RewritePattern`** implementations directly in the Python bindings. Previously, users could define similar patterns using the PDL dialect’s bindings. However, for more complex patterns, this often required writing multiple Python callbacks as PDL native constraints or rewrite functions, which made the overall logic less intuitive—though it could be more performant than a pure Python implementation (especially for simple patterns). With this change, we introduce an additional, straightforward way to define patterns purely in Python, complementing the existing PDL-based approach. ### Example ```python def to_muli(op, rewriter): with rewriter.ip: new_op = arith.muli(op.operands[0], op.operands[1], loc=op.location) rewriter.replace_op(op, new_op.owner) with Context(): patterns = RewritePatternSet() patterns.add(arith.AddIOp, to_muli) # a pattern that rewrites arith.addi to arith.muli frozen = patterns.freeze() module = ... apply_patterns_and_fold_greedily(module, frozen) ``` --------- Co-authored-by: Maksim Levental <maksim.levental@gmail.com>
2025-10-05[MLIR][Python] Expose the insertion point of pattern rewriter (#161001)Twice
In [#160520](https://github.com/llvm/llvm-project/pull/160520), we discussed the current limitations of PDL rewriting in Python (see [this comment](https://github.com/llvm/llvm-project/pull/160520#issuecomment-3332326184)). At the moment, we cannot create new operations in PDL native (python) rewrite functions because the `PatternRewriter` APIs are not exposed. This PR introduces bindings to retrieve the insertion point of the `PatternRewriter`, enabling users to create new operations within Python rewrite functions. With this capability, more complex rewrites e.g. with branching and loops that involve op creations become possible. --------- Co-authored-by: Maksim Levental <maksim.levental@gmail.com>
2025-10-03[MLIR] Apply clang-tidy fixes for misc-use-internal-linkage in Rewrite.cpp (NFC)Mehdi Amini
2025-09-25[MLIR][Python] Add bindings for PDL constraint function registering (#160520)Twice
This is a follow-up to #159926. That PR (#159926) exposed native rewrite function registration in PDL through the C API and Python, enabling use with `pdl.apply_native_rewrite`. In this PR, we add support for native constraint functions in PDL via `pdl.apply_native_constraint`, further completing the PDL API.
2025-09-24[MLIR][Python] Add bindings for PDL native rewrite function registering ↵Twice
(#159926) In the MLIR Python bindings, we can currently use PDL to define simple patterns and then execute them with the greedy rewrite driver. However, when dealing with more complex patterns—such as constant folding for integer addition—we find that we need `apply_native_rewrite` to actually perform arithmetic (i.e., compute the sum of two constants). For example, consider the following PDL pseudocode: ```mlir pdl.pattern : benefit(1) { %a0 = pdl.attribute %a1 = pdl.attribute %c0 = pdl.operation "arith.constant" {value = %a0} %c1 = pdl.operation "arith.constant" {value = %a1} %op = pdl.operation "arith.addi"(%c0, %c1) %sum = pdl.apply_native_rewrite "addIntegers"(%a0, %a1) %new_cst = pdl.operation "arith.constant" {value = %sum} pdl.replace %op with %new_cst } ``` Here, `addIntegers` cannot be expressed in PDL alone—it requires a *native rewrite function*. This PR introduces a mechanism to support exactly that, allowing complex rewrite patterns to be expressed in Python and enabling many passes to be implemented directly in Python as well. As a test case, we defined two new operations (`myint.constant` and `myint.add`) in Python and implemented a constant-folding rewrite pattern for them. The core code looks like this: ```python m = Module.create() with InsertionPoint(m.body): @pdl.pattern(benefit=1, sym_name="myint_add_fold") def pat(): ... op0 = pdl.OperationOp(name="myint.add", args=[v0, v1], types=[t]) @pdl.rewrite() def rew(): sum = pdl.apply_native_rewrite( [pdl.AttributeType.get()], "add_fold", [a0, a1] ) newOp = pdl.OperationOp( name="myint.constant", attributes={"value": sum}, types=[t] ) pdl.ReplaceOp(op0, with_op=newOp) def add_fold(rewriter, results, values): a0, a1 = values results.push_back(IntegerAttr.get(i32, a0.value + a1.value)) pdl_module = PDLModule(m) pdl_module.register_rewrite_function("add_fold", add_fold) ``` The idea is previously discussed in Discord #mlir-python channel with @makslevental. --------- Co-authored-by: Maksim Levental <maksim.levental@gmail.com>
2025-09-08[MLIR][Python] Add a python function to apply patterns with MlirOperation ↵Twice
(#157487) In https://github.com/llvm/llvm-project/pull/94714, we add a python function `apply_patterns_and_fold_greedily` which accepts an `MlirModule` as the argument type. However, sometimes we want to apply patterns with an `MlirOperation` argument, and there is currently no python API to convert an `MlirOperation` to `MlirModule`. So here we overload this function `apply_patterns_and_fold_greedily` to do this (also a corresponding new C API `mlirApplyPatternsAndFoldGreedilyWithOp`)
2025-03-31[MLIR][NFC] Fix incomplete boundary comments. (#133516)Han-Chung Wang
I observed that we have the boundary comments in the codebase like: ``` //===----------------------------------------------------------------------===// // ... //===----------------------------------------------------------------------===// ``` I also observed that there are incomplete boundary comments. The revision is generated by a script that completes the boundary comments. ``` //===----------------------------------------------------------------------===// // ... ... ``` Signed-off-by: hanhanW <hanhan0912@gmail.com>
2024-12-20[mlir] Enable decoupling two kinds of greedy behavior. (#104649)Jacques Pienaar
The greedy rewriter is used in many different flows and it has a lot of convenience (work list management, debugging actions, tracing, etc). But it combines two kinds of greedy behavior 1) how ops are matched, 2) folding wherever it can. These are independent forms of greedy and leads to inefficiency. E.g., cases where one need to create different phases in lowering and is required to applying patterns in specific order split across different passes. Using the driver one ends up needlessly retrying folding/having multiple rounds of folding attempts, where one final run would have sufficed. Of course folks can locally avoid this behavior by just building their own, but this is also a common requested feature that folks keep on working around locally in suboptimal ways. For downstream users, there should be no behavioral change. Updating from the deprecated should just be a find and replace (e.g., `find ./ -type f -exec sed -i 's|applyPatternsAndFoldGreedily|applyPatternsGreedily|g' {} \;` variety) as the API arguments hasn't changed between the two.
2024-07-16[mlir] Add RewriterBase to the C API (#98962)Fehr Mathieu
This exposes most of the `RewriterBase` methods to the C API. This allows to manipulate both the `IRRewriter` and the `PatternRewriter`. The `IRRewriter` can be created from the C API, while the `PatternRewriter` cannot. The missing operations are the ones taking `Block::iterator` and `Region::iterator` as parameters, as they are not exposed by the C API yet AFAIK. The Python bindings for these methods and classes are not implemented.
2024-06-11[mlir] Add PDL C & Python usage (#94714)Jacques Pienaar
Following a rather direct approach to expose PDL usage from C and then Python. This doesn't yes plumb through adding support for custom matchers through this interface, so constrained to basics initially. This also exposes greedy rewrite driver. Only way currently to define patterns is via PDL (just to keep small). The creation of the PDL pattern module could be improved to avoid folks potentially accessing the module used to construct it post construction. No ergonomic work done yet. --------- Signed-off-by: Jacques Pienaar <jpienaar@google.com>