[flang-commits] [flang] [Flang][OpenMP] Create MLIR optimization pass to push index allocations into loop body and remove them if redundant (PR #67010)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Fri Sep 22 07:16:36 PDT 2023


skatrak wrote:

Just as an additional note, what we actually want is to remove the alloca and use the `int32 %cnt` value instead whenever possible. But if the address of the variable is passed to another function from inside of the loop body (in fact, that would be the default in Fortran if we called some subprocedure with the index variable as argument), then that wouldn't be possible. That is the case for which we'd be looking to bring the alloca inside of the loop body, to at least avoid passing it from outside and just have it as a stack variable. I hope between the comments we're making the need for this pass or something with the same effect a bit clearer.

If a pass like this were to be the agreed solution, it seems like we should add the `AutomaticAllocationScope` trait to the OpenMP loop operations impacted by it (`omp.wsloop`, `omp.simdloop` and maybe `omp.taskloop` or others), which I suppose should address your concern about runaway allocations. But we've discussed that maybe there is a way we can rely on existing LLVM infrastructure to address this: the `CodeExtractor` class that would be used here seems like it should be able to identify the outside alloca as something that can be sunk into the outlined function, and once that is done I think it's safe to assume that at some point in the LLVM optimization pipeline there would be a pass that would identify this allocation as redundant if it was only used to store the contents of the index, which is already available as an SSA value. I'm currently looking into this possibility and I'll close this PR if I find that a simpler approach is possible.

https://github.com/llvm/llvm-project/pull/67010


More information about the flang-commits mailing list