Issue 170782
Summary [Flang] [OpenMP] Inconsistent scope handling for unassociated DO variables in OpenMP SIMD Constructs (collapse(1))
Labels flang
Assignees
Reporter nakazawa-fj
    ```
Version of flang : 22.0.0git(7761a89e1230d8ccbb8e6ee15dc14ea74792a53c)/AArch64
```

In the attached program (`sample.f90`), when using `!$omp simd collapse(1)`, the `DO variable` of an inner loop that is not associated with the `simd construct` is treated differently by flang. According to the `OpenMP specification` \
(version 6.0), such variables should be shared according to the `implicit data-sharing attribute rules`, but flang incorrectly treats them as `private`. This results in unexpected behavior
after the `simd region` ends.

Specification References:
OpenMP Application Programming Interface Version 6.0
6.4.5:collapse Clause
12.4:simd Construct

With `collapse(1)`, only the immediately following loop is associated with the `simd Construct`. Other loops remain unassociated
and should follow `implicit data-sharing rules`, which means they should be `shared`.

The following are the test program, Flang, Gfortran and ifx compilation/execution results.

sample.f90
```fortran
program test
  implicit none
  integer :: n, k
  integer :: omp_get_thread_num

  !$omp parallel
  k = 0
  !$omp simd collapse(1)
  do n = 3, 3
     do k = 4, 4
        print '(1x,"In  k= ",i2," k-address:",z16.16," thread num: ",i2)', k, loc(k), omp_get_thread_num()
     end do
  end do
  !$omp end simd

  if (omp_get_thread_num() == 0) then
     print '(1x,"Out k= ",i2," k-address:",z16.16," thread num:  0")', k, loc(k)
     if (k /= 5) print *, 'error', k
  end if

  !$omp end parallel
end program
```
```
$ export OMP_NUM_THREADS=4; flang -fopenmp sample.f90; ./a.out
 In  k=  4 k-address:0000FFFF91C2C610 thread num:  2
 In  k=  4 k-address:0000FFFF925CC5D0 thread num:  1
 In  k=  4 k-address:0000FFFF9128C650 thread num:  3
 In  k=  4 k-address:0000FFFFF2440CA0 thread num:  0
 Out k=  0 k-address:0000FFFFF2440D08 thread num:  0
 error 0
```

```
$ export OMP_NUM_THREADS=4; gfortran -fopenmp sample.f90; ./a.out
 In  k=  4 k-address:0000FFFFE3809588 thread num:  0
 Out k=  5 k-address:0000FFFFE3809588 thread num:  0
 In  k=  4 k-address:0000FFFFB170B768 thread num:  2
 In  k=  4 k-address:0000FFFFB20AA768 thread num:  1
 In  k=  4 k-address:0000FFFFB0D6C768 thread num:  3
```

```
$ export OMP_NUM_THREADS=4; ifx -qopenmp sample.f90; ./a.out
 In  k=  4 k-address:00007FF7CF37EA1C thread num:  1
 In  k=  4 k-address:00007FF7CE37AA9C thread num:  3
 In  k=  4 k-address:00007FFCDC13791C thread num:  0
 In  k=  4 k-address:00007FF7CEB7CA1C thread num:  2
 Out k=  0 k-address:00007FFCDC137920 thread num:  0
 error           0
```

Expected:
 The unassociated inner DO variable `k` is shared.
 After the SIMD region ends, `k` should be 5 per Fortran DO rules; `loc(k)` for `Out` is consistent for the shared outer `k`.

Actual:
 flang/ifx: `k` behaves as private; `Out k` remains 0 (unexpected), `loc(k)` differs across threads in `In` prints,
            indicating per-thread privatization; the `Out` value is not updated to 5.
 gfortran: `k` behaves as shared; `Out k` is 5 (expected), `loc(k)` for `Out` matches the shared outer `k`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to