Issue 170784
Summary [Flang] [OpenMP] Loop iteration variable in OpenMP `simd` construct incorrectly treated as `private` instead of `lastprivate`
Labels flang
Assignees
Reporter nakazawa-fj
    ```
Version of flang : 22.0.0git(7761a89e1230d8ccbb8e6ee15dc14ea74792a53c)/AArch64
```

In the attached program (`sample.f90`), when using OpenMP `simd` constructs in Fortran with `flang`, the loop iteration variable is handled as `private`. As a result, its value is not propagated after the construct finishes, leading to incorrect behavior. Other compilers (`ifx`, `gfortran`) correctly treat the variable as `lastprivate` per OpenMP specification, and produce the expected result. This indicates a deviation in `flang` from the OpenMP standard.

Specification References:
OpenMP Application Programming Interface Version 6.0
7.5.5 lastprivate clause
 lastprivate ensures the variable's final value after the region is visible outside the construct.

7.1 Clauses Applicable to Combined and Composite Constructs
 Defines the rules for applying clauses in combined and composite constructs.

7.1.1 Variables Referenced in a Construct
 Explicitly states that all loop iteration variables included in a loop or simd construct are lastprivate.
 -> The final value must be updated after the region ends and must be accessible outside the construct.


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

sample.f90
```fortran
    program p
      integer :: k
 k = 100
    !$omp parallel
    !$omp simd
        do k = 12, 12
 end do
    !$omp end simd

    !$omp single
        if (k /= 13) print *, 'error:', k
    !$omp end single

    !$omp end parallel
      print *, 'pass'
    end program p
```
```
$ export OMP_NUM_THREADS=4; flang -fopenmp sample.f90 && ./a.out
  error: 100
  pass
```

```
$ export OMP_NUM_THREADS=4; gfortran -fopenmp sample.f90 && ./a.out
 pass
```

```
$ export OMP_NUM_THREADS=4; ifx -qopenmp sample.f90 && ./a.out
  pass
```

Per the OpenMP specification, the loop iteration variable of a `simd` construct with one associated loop should be `lastprivate`, and its final value should be updated after the end of the region (i.e., the last iteration value is preserved and visible outside the `simd` construct).

Actual:
With `flang`, the loop iteration variable is treated as `private`, so its value is not updated/preserved after the `simd` construct. This causes incorrect values to be observed post-construct (e.g., `k` remains `100`, causing `error: 100`).

 flang: treats `k` as `private` -> incorrect result (`error: 100`)
 gfortran/ifx: treat `k` as `lastprivate` -> correct result (`pass`)

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

Reply via email to