llvm / llvm/llvm-project

[Flang][OpenMP] Initialized variables are not treated as shared in task constructs

Open
#201,513 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

When a variable has an explicit initialization, it should be treated as shared in task constructs, but Flang does not seem to do so.

Rationales

* 7.1.1 Variables Referenced in a Construct (OpenMP 6.0)

> * In a task-generating construct, if no default clause is present, a variable for which the data-sharing attribute is not determined by the rules above is shared if the variable is determined to be shared by all implicit tasks bound to the current team in the enclosing context.

* 7.1.2 Variables Referenced in a Region but not in a Construct (OpenMP 6.0)

> * Variables with static storage duration are shared.

* 2 Glossary (OpenMP 6.0)

> **static storage duration**
> For C/C++, the lifetime of an object with static storage duration, as defined by the base language. For Fortran, the lifetime of a variable with a `SAVE` attribute, implicit or explicit, a common block object or a variable declared in a module.

* 8.4 Initialization (Fortran 2023)

> Explicit initialization of a variable that is not in a common block implies the SAVE attribute, which may be confirmed by explicit specification.

### Reproducer

* test.f90

```fortran
integer:: a=1 ! has the SAVE attribute implicitly
integer:: b
data b/1/
integer,save:: c
a=2
b=3
c=4
!$omp task
a=12
b=13
c=14
!$omp end task
if (a/=12) print *,'a:',a
if (b/=13) print *,'b:',b
if (c/=14) print *,'c:',c
print *,'pass'
end
```

* commands

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

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Adding the SAVE attribute to the variable `a` explicitly.

## 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
a: 2
b: 3
c: 4
pass
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.