[mlir] `transform.include` does not handle nested specs with the same name
Repro:
// RUN: mlir-opt %s --transform-interpreter
module @td_module_4 attributes {transform.with_named_sequence} {
module @foo_module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () {
transform.print {name="foo_module"}
transform.yield
}
}
module @bar_module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () {
transform.print {name="bar_module"}
transform.yield
}
}
transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () {
transform.include @foo_module::@__transform_main failures(suppress) (%arg0) : (!transform.any_op) -> ()
transform.include @bar_module::@__transform_main failures(suppress) (%arg0) : (!transform.any_op) -> ()
transform.yield
}
}
This prints [[[ IR printer: foo_module top-level ]]] only but I'd expect it to also print [[[ IR printer: bar_module top-level ]]].
If I rename the nested specs to __transform_main1 and __transform_main2, both foo_module and bar_module get printed.
@llvm/issue-subscribers-bug
Author: Jakub Kuderski (kuhar)
module @td_module_4 attributes {transform.with_named_sequence} { module @foo_module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () { transform.print {name="foo_module"} transform.yield } } module @bar_module attributes {transform.with_named_sequence} { transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () { transform.print {name="bar_module"} transform.yield } } transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) -> () { transform.include @foo_module::@__transform_main failures(suppress) (%arg0) : (!transform.any_op) -> () transform.include @bar_module::@__transform_main failures(suppress) (%arg0) : (!transform.any_op) -> () transform.yield } }
This prints `[[[ IR printer: foo_module top-level ]]]` only but I'd expect it to also print `[[[ IR printer: bar_module top-level ]]]`.
If I rename the nested specs to `__transform_main1` and `__transform_main2`, both `foo_module` and `bar_module` get printed.
</details>
@kuhar I've opened PR #151323 which solves this issue. The reason the code shared in this bug does not work is because of the strategy used for finding the entry point. The way the entry point is found currently is by walking the module in pre-order looking for the transform.named_sequence operation which match the entry-point's name. With the code you shared, the order of visited transform.named_sequence operations will be:
- the one in the
foo_module. - the one in the
bar_module - the on in the top-level module.
Since they all match the entry-point's name, the first one was selected, and we exit early from the walker. This means that the interpreter will only execute the first print statement, giving you the output that you see.
In the PR I linked I favoured named_sequences in the top-level module over ones in the nested modules even if the nested ones come first in a pre-order.
Another way of solving this would be by having a new iteration order for the walker. At the moment, there is only PreOrder and PostOrder, but potentially one could define a Breadth First Search Order which would visit operations at each level of nestedness before proceeding to the next one. In his particular example, while foo_module and bar_module would be visited first, their transform.named_sequence operations would not be visited until after the named sequence in the top level is visited. The order would be:
- foo module
- bar module
- named sequence in top-level module
- named sequence in foo module
- named sequence in bar module
We can chat more about this if you prefer this solution.
I think this makes sense. One other alternative would be to do a full walk (without interrupting), recording the full path to each of the entrypoints found, and then selecting the one with least nesting or erroring out when there's more than one.
cc: @qedawkins @ftynse