llvm / llvm/llvm-project

[Flang][OpenMP] Missing error reporting for declare reduction directives treating omp_in and omp_out as arrays

Open
#193,431 1 comment 0 reactions 0 assignees View on GitHub
flang:frontend flang:openmp
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

In the OpenMP 6.0 specification, UDRs are treated as elemental procedures. Consequently, the OpenMP identifiers `omp_in` and `omp_out` should be treated as scalar variables within these contexts. However, Flang fails to report errors in some cases where these identifiers are used as arrays.

* 7.6.2.1 OpenMP Combiner Expressions

> If the list item is an array or array section, the OpenMP identifiers `omp_in` and `omp_out` each refer to an array element of that list item.

> If a generic name is used in a combiner expression and the list item in the corresponding reduction clause is an array or array section, that generic name is resolved to the specific procedure that is elemental or only has scalar dummy arguments.

> * Any selectors in the designator of `omp_in` and `omp_out` must be component selectors.

### Reproducer

* test.f90

```fortran
interface
subroutine s1(k)
integer k
end
subroutine s2(k,n)
integer k,n
end
end interface
!$omp declare reduction(aaa:integer:s1(omp_out(1))) initializer(omp_priv=0)
!$omp declare reduction(bbb:integer:s2(omp_out,omp_in(1))) initializer(omp_priv=0)
!$omp declare reduction(ccc:integer:omp_out=omp_in(1)) initializer(omp_priv=0)

! catch the error
!$omp declare reduction(ddd:integer:omp_out(1)=omp_in) initializer(omp_priv=0)
end
```

* commands

```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git abc37e6aef740b0af087331e6db807b916f559f5)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang test.f90 -fopenmp
error: Semantic errors in test.f90
test.f90:14:37: error: Left-hand side of assignment is not definable
!$omp declare reduction(ddd:integer:omp_out(1)=omp_in) initializer(omp_priv=0)
^^^^^^^^^^
test.f90:14:37: because: 'omp_out(1_4)' is not a variable or pointer
!$omp declare reduction(ddd:integer:omp_out(1)=omp_in) initializer(omp_priv=0)
^^^^^^^^^^
```

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Specifying array sections instead of array elements:
```fortran
!$omp declare reduction(aaa:integer:s1(omp_out(:))) initializer(omp_priv=0)
!$omp declare reduction(bbb:integer:s2(omp_out,omp_in(:))) initializer(omp_priv=0)
!$omp declare reduction(ccc:integer:omp_out=omp_in(:)) initializer(omp_priv=0)
!$omp declare reduction(ddd:integer:omp_out(:)=omp_in) initializer(omp_priv=0)
```
```
error: Semantic errors in test2.f90
test2.f90:9:40: error: 'omp_out' is not an array
!$omp declare reduction(aaa:integer:s1(omp_out(:))) initializer(omp_priv=0)
^^^^^^^^^^
test2.f90:10:48: error: 'omp_in' is not an array
!$omp declare reduction(bbb:integer:s2(omp_out,omp_in(:))) initializer(omp_priv=0)
^^^^^^^^^
test2.f90:11:45: error: 'omp_in' is not an array
!$omp declare reduction(ccc:integer:omp_out=omp_in(:)) initializer(omp_priv=0)
^^^^^^^^^
test2.f90:12:37: error: 'omp_out' is not an array
!$omp declare reduction(ddd:integer:omp_out(:)=omp_in) initializer(omp_priv=0)
^^^^^^^^^^
```

## 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
test.f90:9:47:

9 | !$omp declare reduction(aaa:integer:s1(omp_out(1))) initializer(omp_priv=0)
| 1
Error: Syntax error in argument list at (1)
test.f90:10:54:

10 | !$omp declare reduction(bbb:integer:s2(omp_out,omp_in(1))) initializer(omp_priv=0)
| 1
Error: Syntax error in argument list at (1)
test.f90:11:7:

11 | !$omp declare reduction(ccc:integer:omp_out=omp_in(1)) initializer(omp_priv=0)
| 1
Error: Unclassifiable OpenMP directive at (1)
test.f90:14:7:

14 | !$omp declare reduction(ddd:integer:omp_out(1)=omp_in) initializer(omp_priv=0)
| 1
Error: Unclassifiable OpenMP directive at (1)
```

* 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
$
```

Contributor guide

Open the contributing guide

Research direction

Start with the test.f90 reproducer and run it with flang -fopenmp to observe which declare reduction cases produce diagnostics. Trace Flang's semantic checking for OpenMP declare reduction combiner expressions, then add coverage so scalar-only omp_in and omp_out uses with array elements are rejected while the existing array-section diagnostics remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.