llvm / llvm/llvm-project

[Flang][OpenMP] Incorrect UBOUND values inside WORKSHARE construct at optimization level -O1 or higher

Open
#187,416 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

```
flang version 23.0.0git (https://github.com/llvm/llvm-project.git 36041192cf6c9042615c8d2364cacc698ef89865)
```

When an array with non-constant upper bounds is declared, and its upper bound is queried using the intrinsic function `UBOUND` inside an OpenMP `WORKSHARE` construct, incorrect values are returned when Flang is compiled with optimization level `-O1` or higher.

sample.f90
```fortran
program main
integer(4),dimension(2:3,2:3,2:3,2:3)::a
call sub(a,2,3)
print *,'pass'
end program main
subroutine sub(a,n2,n3)
integer(4),dimension(4)::ub=0
integer(4)::n2,n3
integer(4),dimension(n2:n3,n2:n3,n2:n3,n2:n3)::a
!$omp parallel workshare
ub=ubound(a)
!$omp end parallel workshare
if (any(ub/=3)) print *,"fail",ub
end subroutine sub

```
```
$ export OMP_NUM_THREADS=4
$ flang sample.f90 -fopenmp -O0
$ ./a.out
pass

$ flang sample.f90 -fopenmp -O1
$ ./a.out
fail 3 0 10 0
pass

$ flang sample.f90 -fopenmp -O2
$ ./a.out
fail 3 65535 10 0
pass

$ flang sample.f90 -fopenmp -O3
$ ./a.out
fail 3 65535 10 0
pass
```

### Other Compiler Behaviors

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

```
$ export OMP_NUM_THREADS=4
$ ifx sample.f90 -fopenmp -O0
sample.f90(11): warning #5474: Statement in an OpenMP PARALLEL WORKSHARE construct cannot be parallelized; the statement will only be executed by one thread of one team.
ub=ubound(a)
--^
$ ./a.out
pass

$ ifx sample.f90 -fopenmp -O3
sample.f90(11): warning #5474: Statement in an OpenMP PARALLEL WORKSHARE construct cannot be parallelized; the statement will only be executed by one thread of one team.
ub=ubound(a)
--^
$ ./a.out
pass
```

### Standards Analysis

The relevant specification section is:

OpenMP 6.0 Section 13.4 “workshare Construct.”
This section lists restrictions on statements allowed inside a workshare construct. The tested code uses scalar assignments, which are explicitly permitted.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failure from sample.f90 with Flang using -O0 through -O3 and -fopenmp, then compare the results with gfortran or ifx. Investigate Flang's handling of UBOUND for non-constant bounds inside an OpenMP WORKSHARE construct. Done means the sample reports the expected upper bounds at optimization levels -O1 and higher.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.