[Flang][OpenMP] Incorrect execution result when using multiple declare reduction directives with the same identifier for different types
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Summary
According to the OpenMP 6.0 specification, multiple `declare reduction` directives with the same identifier for different types are permitted. However, the execution result is incorrect.
* 7.6.14 `declare_reduction` Directive
> If the directive specifies several types then the behavior is as if a `declare_reduction` directive was specified for each type.
> A reduction identifier must not be re-declared in the current scope for the same type or for a type that is compatible according to the base language rules.
### Reproducer
* test.f90
```fortran
!$omp declare reduction(ax:integer(4):omp_out=omp_out+omp_in) &
!$omp initializer(omp_priv=0)
!$omp declare reduction(ax:real(4):omp_out=omp_out+omp_in) &
!$omp initializer(omp_priv=0.0)
real(4)::r
r=0.0
!$omp parallel do reduction(ax:r)
do i=1,10
r = r + 1.0
end do
!$omp end parallel do
if (abs(r-10.0)>0.001) print *,'error',r
print *, 'pass'
end
```
* commands
```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git 5347264191e74840adc740db1aa41b7a9d33670a)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang test.f90 -fopenmp && ./a.out
error 1.1754943E-38
pass
```
## Non-problematic Conditions
The issue does not occur under the following conditions:
* Setting `OMP_NUM_THREADS` to 1.
* Removing the redundant `declare reduction` directives.
## 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
pass
```
Contributor guide
Research direction
Start by reproducing the failure with test.f90 using Flang and -fopenmp, then trace how Flang handles multiple declare reduction directives with the same identifier for different types. The work is done when the reproducer prints the correct result, 10.0, with multiple threads and a regression test covers the case.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100