Running passes in a loop

I would like to run certain sequence of passes in a loop. On high level this would look like this:

  1. Run passes Sequence_A (pass A1, pass A2, …)
  2. Loop start
  3. Run passes Sequence_B (pass B1, pass B2, …)
  4. Perform some analysis, go back to step 2 if not an end of loop
  5. Run passes Sequence_C (pass C1, pass C2, …)

So for Sequence_B I would like to run it in a loop, each time it will start with the same state of IR, modify it, then there would be some analysis step that would produce some feedback so that on next iteration Sequence_B will be run with different configuration. It could be understood as some loop way of finding optimal solution where on each iteration different settings are applied and loop ends only where certain criteria is met.

So far I’ve managed to achieve this two ways.

  1. Using dynamic passes - have a pass that inside will have a loop that will initialize mlir::OpPassManager dynPm as needed for Sequence_B and call runPipeline(dynPm, funcOp) on it
  2. Splitting whole compilation into 3 stages, each with different mlir::PassManager pm initialized and separate call to The one for Sequence_B will be called in a loop

Are there any other ways to do it? Maybe more structured way in MLIR framework to represent such flow?

Nested pass manager with dynamic initialization sounds like the good conceptual match here. You want to run a different sequence (different number of repetitions in your case) of passes depending on some runtime condition in the input IR. That’s what this was designed for.

1 Like