llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir] `transform.include` does not handle nested specs with the same name

Open kuhar opened this issue 1 year ago • 4 comments

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.

kuhar avatar Dec 11 '24 16:12 kuhar

@llvm/issue-subscribers-bug

Author: Jakub Kuderski (kuhar)

Repro: ```mlir // 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.
</details>

llvmbot avatar Dec 11 '24 16:12 llvmbot

@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:

  1. the one in the foo_module.
  2. the one in the bar_module
  3. 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:

  1. foo module
  2. bar module
  3. named sequence in top-level module
  4. named sequence in foo module
  5. named sequence in bar module

We can chat more about this if you prefer this solution.

amd-eochoalo avatar Jul 30 '25 12:07 amd-eochoalo

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.

kuhar avatar Jul 30 '25 20:07 kuhar

cc: @qedawkins @ftynse

kuhar avatar Jul 30 '25 20:07 kuhar