| Issue |
239285
|
| Summary |
[flang][OpenACC] DO CONCURRENT LOCAL_INIT/REDUCE under an OpenACC loop ignores an enclosing DO CONCURRENT's LOCAL variable
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
When an OpenACC loop directive applies to an inner `DO CONCURRENT` whose `LOCAL_INIT` or `REDUCE` names a variable that is `LOCAL` in an enclosing (non-OpenACC) `DO CONCURRENT`, the generated `acc.firstprivate` / `acc.reduction` operates on the subprogram's variable, not on the enclosing construct's copy. Per F2023 11.1.7.5 p2 and p3, the outside variable is "the variable with the same name in the innermost executable construct or scoping unit that includes the DO CONCURRENT construct", here the outer construct entity.
```fortran
! An OpenACC loop directive on an inner DO CONCURRENT whose locality-spec names
! a variable that is LOCAL in an enclosing (non-OpenACC) DO CONCURRENT.
subroutine inner_local_init(n, inner)
integer :: n, j, k, w, inner(2, n)
w = 1
do concurrent (k=1:n) local(w)
w = 100 + k
!$acc parallel loop
do concurrent (j=1:2) local_init(w)
inner(j, k) = w
end do
end do
end subroutine
subroutine inner_reduce(n, outer)
integer :: n, j, k, t, outer(n)
t = 1000
do concurrent (k=1:n) local(t)
t = 10*k
!$acc parallel loop
do concurrent (j=1:4) reduce(+:t)
t = t + j
end do
outer(k) = t
end do
end subroutine
```
`flang -fc1 -fopenacc -emit-hlfir acc_inner_locality.f90`, trimmed. `%7` is the subprogram's variable in both functions:
```mlir
// inner_local_init
fir.do_concurrent.loop (%arg2) = (%18) to (%20) step (%c1) local(@_QFinner_local_initEw_private_i32 %7#0 -> %arg3 : !fir.ref<i32>) {
%24:2 = hlfir.declare %arg3 uniq_name("_QFinner_local_initEw") : ...
...
acc.parallel combined(loop) {
%28 = acc.firstprivate varPtr(%7#0 : !fir.ref<i32>) recipe(@firstprivatization_ref_i32) name("w") -> !fir.ref<i32>
// inner_reduce
fir.do_concurrent.loop (%arg2) = (%15) to (%17) step (%c1) local(@_QFinner_reduceEt_private_i32 %7#0 -> %arg3 : !fir.ref<i32>) {
%21:2 = hlfir.declare %arg3 uniq_name("_QFinner_reduceEt") : ...
...
acc.parallel combined(loop) {
%29 = acc.reduction varPtr(%7#0 : !fir.ref<i32>) recipe(@reduction_add_ref_i32) name("t") -> !fir.ref<i32>
...
acc.loop combined(parallel) private(%30 : !fir.ref<i32>) reduction(%29 : !fir.ref<i32>) ... {
%31:2 = hlfir.declare %29 uniq_name("_QFinner_reduceEt") : ...
...
%33 = fir.load %21#0 : !fir.ref<i32>
...
hlfir.assign %35 to %21#0 : i32, !fir.ref<i32>
```
So in `inner_local_init` each inner iteration starts from `w = 1` instead of `100 + k`. In `inner_reduce` the reduction operand is the subprogram's `t`, and the loop body reads and writes the outer construct's copy (`%21`) directly instead of the reduction variable (`%31`); `outer(k)` should be `10*k + 10`.
Expected: `varPtr` is the enclosing construct's copy (`%24#0`, `%21#0`), and the `REDUCE` body uses the reduction variable.
`processDoConcurrentLocalitySpecs` in `flang/lib/Lower/OpenACC.cpp` takes the address from `name.symbol->GetUltimate()` for `REDUCE`, `LOCAL` and `LOCAL_INIT` (lines 1879, 1918, 1947 at 960bcc81f88e), which skips the enclosing `LOCAL` construct entity. For `REDUCE` there is also no `localSymPairs` entry, so the inner construct entity referenced in the body is not rebound to the reduction variable.
Seen with flang built from main at c44e0095624a, and the same with PR 238647 applied (which fixes the corresponding lookup for the non-OpenACC delayed-privatization path). The cited lines are unchanged on current main.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs