[Mlir-commits] [mlir] [MLIR][Transform] apply_registered_pass op's options as a dict (PR #143159)
Maksim Levental
llvmlistbot at llvm.org
Sat Jun 7 15:17:04 PDT 2025
================
@@ -214,6 +214,88 @@ def __init__(
super().__init__(_get_op_results_or_values(operands), loc=loc, ip=ip)
+ at register_attribute_builder("ParamOperandIndexAttr")
+def _paramOperandIndexAttr(x: int, context) -> Attribute:
+ return Attribute.parse(f"#transform.param_operand_index<{x}>", context=context)
+
+
+ at _ods_cext.register_operation(_Dialect, replace=True)
+class ApplyRegisteredPassOp(ApplyRegisteredPassOp):
+ def __init__(
+ self,
+ result: Type,
+ pass_name: Union[str, StringAttr],
+ target: Union[Operation, Value, OpView],
+ *,
+ options: Dict[
+ Union[str, StringAttr],
+ Union[Attribute, Value, Operation, OpView],
+ ] = {},
----------------
makslevental wrote:
two things:
1. `....... = {}` is bad juju - in python default args are mutable, so subsequent calls will get stale values for `options` ([SO](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument))
2. the whole parser refactor/rewrite/whole-hog aside, you can still make this "nicer" by letting people pass the pass args (no pun intended) via `kwargs`. so something like
```python
def __init__(blah blah, *, loc=None, ip=None, **options):
...
```
will enable you to call this op via
```python
transform.apply_registered_pass(blahblah, opt1=True, opt2=5, opt3="foo")
```
where the `options` kwarg dict value within the `__init__` will look like
```python
options = {"opt1": True, "opt2": 5, "opt3": "foo"}
```
and in cases where opt args have hypens you can either use the widget I pasted above (the one that converts from underscores to hyphens, my preference) or you can use a "trick":
```python
transform.apply_registered_pass(blahblah, **{"top-down": False, "max-iterations": max_iter})
```
where the `options` kwarg dict value within the `__init__` will look like
```python
options = {"top-down": False, "max-iterations": max_iter}
```
Note btw this is just a suggestion - what you have here is ofc fine, just slightly unpython since these options are basically exactly kwargs.
https://github.com/llvm/llvm-project/pull/143159
More information about the Mlir-commits
mailing list