[Flang][OpenMP] Incorrect semantic error for declare reduction with use association
- 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 for different types and with the same identifier should be accepted. However, Flang incorrectly reports a semantic error when use association is involved.
* 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.
* Fortran 2023 §7.5.2.4 Determination of derived types
> Data entities also have the same type if they are declared with reference to different derived-type definitions that specify the same type name, all have the SEQUENCE attribute or all have the BIND attribute, have no components with PRIVATE accessibility, and have components that agree in order, name, and attributes. Otherwise, they are of different derived types.
### Reproducer
* test.f90
```fortran
module mod
type ty1
integer :: ii
end type
!$omp declare reduction(max:ty1 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
end module
program main
use mod
type ty2
integer :: ii
end type
!$omp declare reduction(max:ty2 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
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: Semantic errors in test.f90
test.f90:13:9: error: Duplicate definition of 'op.max' in DECLARE REDUCTION
!$omp declare reduction(max:ty2 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
## Non-problematic Conditions
The issue does not occur under the following conditions:
* Using different identifiers:
```fortran
module mod
type ty1
integer :: ii
end type
!$omp declare reduction(max1:ty1 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
end module
program main
use mod
type ty2
integer :: ii
end type
!$omp declare reduction(max2:ty2 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
print *,'pass'
end
```
* Avoiding use association:
```fortran
program main
type ty1
integer :: ii
end type
!$omp declare reduction(max:ty1 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
type ty2
integer :: ii
end type
!$omp declare reduction(max:ty2 :omp_out=omp_in) INITIALIZER(OMP_PRIV=OMP_ORIG)
print *,'pass'
end
```
## 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
Assessment
This issue has not been assessed yet.