llvm / llvm/llvm-project

[Flang][OpenMP] Inner loop-iteration variables are not private in parallel construct

Open
#207,125 0 comments 0 reactions 0 assignees View on GitHub
flang:openmp
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

According to OpenMP 6.0, when the outermost loop of a nested loop is associated with an OpenMP `do` directive, the iteration variables of inner loops should be treated as private within the innermost parallel construct. However, Flang fails to implement this correctly, seemingly treating them as shared variables instead.

* 7.1.1 Variables Referenced in a Construct (OpenMP 6.0)

> * The loop-iteration variable in any affected loop of a `loop` or `simd` construct is lastprivate.
> * The loop-iteration variable in any affected loop of a loop-nest-associated directive is otherwise private.
>
> * Loop-iteration variables inside `parallel`, `teams`, `taskgraph`, or task-generating constructs are private in the innermost such construct that encloses the loop.

### Reproducer

* test.f90

```fortran
use omp_lib, only: omp_set_num_threads
integer::i3=99, i2=10
integer::a=1
integer::b=1
call omp_set_num_threads(2)

!$omp parallel
!$omp do
do i2=1,2
do i3=1,2
b = b + a
enddo
enddo
!$omp enddo
!if (i2/=10) print *,'Error i2',i2
if (i3/= 3) print *,'Error i3',i3
!$omp end parallel
print *,"pass"
end
```

* commands

```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git 2301475676b208c555273e7c1ac5d4470d5ee70e)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang test.f90 -fopenmp && ./a.out
Error i3 99
Error i3 99
pass
```

## Other Compilers

* GFortran
```console
$ gfortran --version
GNU Fortran (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gfortran test.f90 -fopenmp && ./a.out
pass
```

* Intel Fortran
```console
$ ifx --version
ifx (IFX) 2025.3.2 20260112
Copyright (C) 1985-2026 Intel Corporation. All rights reserved.

$ ifx -O0 test.f90 -qopenmp && ./a.out
Error i3 -180681839
Error i3 0
pass
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling and running the provided test.f90 reproducer with Flang and -fopenmp, confirming the repeated “Error i3 99” output. Trace Flang’s handling of nested OpenMP do loops inside a parallel construct and verify that the inner loop variable is private. Done means the reproducer no longer reports the error and the behavior matches the stated OpenMP 6.0 rules.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.