llvm / llvm/llvm-project

[Flang][OpenMP] Execution abnormally terminates when allocatable/pointer variables follow common blocks in a copyin clause

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

This issue occurs when an allocatable or pointer variable, which is declared as threadprivate alongside a common block, is specified in a `copyin` clause. In such cases, the program unexpectedly terminates. It appears that the addresses of these variables become zero, leading to a segmentation fault.

### Reproducer

* test.f90

```fortran
integer,pointer :: k2
integer,allocatable:: i2
integer :: n2
common /cmn/ n2
save::k2,i2
!$omp threadprivate(/cmn/,k2,i2 )
integer ::omp_get_thread_num

allocate(k2,i2)
i2=302
n2=202
k2=102
call omp_set_num_threads(4)

!$omp parallel copyin(k2,i2,/cmn/ )
print *,'start thread num:' ,omp_get_thread_num(),'loc(n2):',loc(n2),'loc(k2):',loc(k2),'loc(i2):',loc(i2)
if (i2/=302) print *,302
if (n2/=202) print *,202
if (k2/=102) print *,102
print *,'end thread num:',omp_get_thread_num()
!$omp end parallel

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
start thread num: 1 loc(n2): 140172297506112 loc(k2): 0 loc(i2): 0
start thread num: 2 loc(n2): 140172163288384 loc(k2): 0 loc(i2): 0
start thread num: 0 loc(n2): 93935348337200 loc(k2): 93935348870096 loc(i2):
93935348970544
end thread num: 0
start thread num: 3 loc(n2): 140172096179520 loc(k2): 0 loc(i2): 0
Segmentation fault (core dumped)
```

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Listing the common block last in the `copyin` clause:
```diff
@@ -12,7 +12,7 @@
k2=102
call omp_set_num_threads(4)

-!$omp parallel copyin(/cmn/,k2,i2 )
+!$omp parallel copyin(k2,i2,/cmn/ )
print *,'start thread num:' ,omp_get_thread_num(),'loc(n2):',loc(n2),'loc(k2):',loc(k2),'loc(i2):',loc(i2)
if (i2/=302) print *,302
if (n2/=202) print *,202
```

## 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
start thread num: 0 loc(n2): 139820443595056 loc(k2): 31300000 loc(i2): 31300032
end thread num: 0
start thread num: 3 loc(n2): 139820426802864 loc(k2): 31300000 loc(i2): 139820043078512
end thread num: 3
start thread num: 2 loc(n2): 139820435195568 loc(k2): 31300000 loc(i2): 139820177296240
end thread num: 2
start thread num: 1 loc(n2): 139820443588272 loc(k2): 31300000 loc(i2): 139820311513968
end thread num: 1
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
start thread num: 0 loc(n2): 5092672 loc(k2):
139773458808800 loc(i2): 139773458808768
start thread num: 1 loc(n2): 139773458726784 loc(k2):
139773458808800 loc(i2): 139773458808768
start thread num: 3 loc(n2): 139773458661248 loc(k2):
139773458808800 loc(i2): 139773458808768
start thread num: 2 loc(n2): 139773458694016 loc(k2):
139773458808800 loc(i2): 139773458808768
end thread num: 0
end thread num: 1
end thread num: 3
end thread num: 2
pass
```
Please note that the address of each `i2` should be different for each thread.

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.