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

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Mon Oct 20 06:09:01 PDT 2025


https://github.com/kparzysz commented:

Thanks, it looks a lot clearer now.

This code will only handle one reduction variable per reduction clause. For example, given
```
subroutine f00(x, y)
  integer :: x(:), y(:)
  integer :: i
  !$omp parallel do reduction(+: x(2), y(3))
  do i = 1, 200
    x(2) = x(2) + i
    y(3) = 6 - y(3)
  end do
  !$omp end parallel do
end
```
it generates
```
SUBROUTINE f00 (x, y)
 INTEGER x(:), y(:)
 INTEGER i
  reduction_temp_x(2)=x(2_8)
  reduction_temp_y(3)=y(3_8)
!$OMP PARALLEL DO REDUCTION(+: reduction_temp_x(2),reduction_temp_y(3))
 DO i=1_4,200_4
   x(2_8)=x(2_8)+i    ! unmodified
   reduction_temp_y(3)=6_4-reduction_temp_y(3)
 END DO
!$OMP END PARALLEL DO
  y(3_8)=reduction_temp_y(3)
END SUBROUTINE
```
leaving `x(2) = x(2) + i` unmodified.  Is that a known limitation?

Also, if you modify the testcase a bit:
```
subroutine f00(x, y, j)
  integer :: x(:), y(:)
  integer :: i, j
  !$omp parallel do reduction(+: x(2), y(j))
  do i = 1, 200
    x(2) = x(2) + i
    y(j) = 6 - y(j)
  end do
  !$omp end parallel do
end
```

you get incorrect output:
```
SUBROUTINE f00 (x, y, j)
 INTEGER x(:), y(:)
 INTEGER i, j
  reduction_temp_x(2)=x(2_8)
  reduction_temp_y(2)=y(int(j,kind=8))
!$OMP PARALLEL DO REDUCTION(+: reduction_temp_x(2),reduction_temp_y(2))
 DO i=1_4,200_4
   reduction_temp_y(2)=reduction_temp_y(2)+i
   reduction_temp_y(2)=6_4-y(int(j,kind=8))
 END DO
!$OMP END PARALLEL DO
  y(int(j,kind=8))=reduction_temp_y(2)
END SUBROUTINE
```

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


More information about the flang-commits mailing list