[flang-commits] [flang] [flang][OpenMP] Improve reduction of Scalar ArrayElement types (PR #163940)

Jack Styles via flang-commits flang-commits at lists.llvm.org
Fri Oct 17 07:17:36 PDT 2025


Stylie777 wrote:

@kiranchandramohan From my understanding addressing this in Lowering is not possible. An array element is an expression and the reference to the Symbol is used, which in this case would be the full array. This is ok for Array Sections, but if the user is only using 1 single array element, it does not make sense to reduce the full array, hence the rewrite to the temp so it can pick up a symbol which is just a single value.

With this rewrite, the MLIR example you provided with a single integer being reduced is what ends up being generated, but its not possible to do this using the array element itself because of the process I explained above.

@kparzysz an example of the lowering as it is would be, say for a small program
```
program test
    integer :: a(2)
    integer :: i

    !$omp do reduction(+:a(2))
    do i=1,5
        a(2) = a(2) + i
    end do
    !$omp end do

end program test
```
When the reduction appears in High Level FIR, its generated as
```
reduction(byref @add_reduction_byref_box_2xi32 %7 -> %arg1 : !fir.ref<!fir.box<!fir.array<2xi32>>>)
```
As the user only needs 1 element of the array, it does not make sense to use the whole array in the reduction. This patch would then turn that into the following, with the assignment to and from the temp in the relevant places:
```
reduction(@add_reduction_i32 %7#0 -> %arg1 : !fir.ref<i32>) {
```

With the example you have given in the subroutine `foo(a, b)`, that does reassign after the loop correctly as it will detect the use of the array element within the loop and use one of those occurrences on ArrayElement in the Parse Tree to build the reassignment Expression. The issue I have is because I am moving the use of an ArrayElement for the reassignment Expression, if it's not used within the do loop, I cannot make it. But then if it's not used within the loop, do we need to reassign it back? Probably not which was the justification for taking this approach because in reality the value would not change anyway if you reduced the whole array.

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


More information about the flang-commits mailing list