<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/192989>192989</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Clang][Flang][OpenMP] `omp_initial_device` / `omp_invalid_device` not accepted as valid device IDs
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang,
flang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Thyre
</td>
</tr>
</table>
<pre>
The OpenMP v5.2 and OpenMP v6.0 specification state (here from OpenMP v6.0):
> Additionally, the predefined identifier `omp_initial_device` can be used as an alias for the host device and the predefined identifier `omp_invalid_device` can be used to specify an invalid device number.
A conforming device number is either a non-negative integer that is less than or equal to the value returned by `omp_get_num_devices` or equal to `omp_initial_device` or `omp_invalid_device`.
With the following handling for both values:
**`omp_initial_device`**:
> When an if clause is present and if-expression evaluates to false, the effect is as if a device clause that specifies `omp_initial_device` as the device number is present, regardless of any other device clause on the directive.
**`omp_invalid_device`**:
> If the device-description evaluates to `omp_invalid_device`, runtime error termination is performed.
While both Clang and Flang any positive integer value, both `omp_initial_device` and `omp_invalid_device` are rejected if the value is a constant in the directive:
**Clang:**
```c
#include <omp.h>
int main( void )
{
#pragma omp target device( -1 /*omp_initial_device missing*/ )
{}
#pragma omp target device( omp_invalid_device )
{}
}
```
```console
$ clang -fopenmp --offload-arch=gfx1101 test.2.c
test.c:5:32: error: argument to 'device' clause must be a non-negative integer value
5 | #pragma omp target device( -1 /*omp_initial_device missing*/ )
| ^~
test.c:7:32: error: argument to 'device' clause must be a non-negative integer value
7 | #pragma omp target device( omp_invalid_device )
| ^~~~~~~~~~~~~~~~~~
/opt/apps/software/Clang/trunk/lib/clang/23/include/omp.h:556:32: note: expanded from macro 'omp_invalid_device'
556 | #define omp_invalid_device -2
| ^~
2 errors generated.
```
**Flang:**
```f90
program main
use omp_lib
! OMP_INITIAL_DEVICE missing
!$OMP TARGET DEVICE( -1 )
!$OMP END TARGET
!$OMP TARGET DEVICE( OMP_INVALID_DEVICE )
!$OMP END TARGET
end program main
```
```console
$ flang -fopenmp --offload-arch=gfx1101 test.f90
error: Semantic errors in test.f90
test.f90:4:18: error: The device expression of the DEVICE clause must be a positive integer expression
!$OMP TARGET DEVICE( -1 )
^^^^^^^^^^^^
test.f90:7:26: error: Must have INTEGER type, but is REAL(4)
!$OMP TARGET DEVICE( OMP_INVALID_DEVICE )
^^^^^^^^^^^^^^^^^^
```
----
For Flang, this is checked during `OmpStructureChecker::Enter` with a `RequiresPositiveParameter` (see [here](https://github.com/llvm/llvm-project/blob/61f33bc546596d4c4a99a25c530942862d00a1d6/flang/lib/Semantics/check-omp-structure.cpp#L4507)).
This check would need to get replaced by a check also allowing `-1` (i.e. `omp_initial_device`) and `-2` (i.e. `omp_invalid_device`) to be passed. The second error (`Must have INTEGER type, but is REAL(4)`) is likely an additional bug that needs some attention...
For Clang, the check should happen in `SemaOpenMP::ActOnOpenMPDeviceNum` (see [here](https://github.com/llvm/llvm-project/blob/5d98f79f52dd56750e826eb6efcd8b5cf41de239/clang/lib/Sema/SemaOpenMP.cpp#L17365)).
Even if handling during code building is fixed, https://github.com/llvm/llvm-project/issues/192980 needs to get fixed as well, so that the device mapping works correctly.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0WF2Po7gS_TXOixVETIDwkAemk6xami_NtnYfWw4uwHfAZm2Tnn65v_2qbEj6K3d67t2Nom5CTLnOqVPlqnBrZaMAtiT9QNLdgo-u1WZ71z4aWBy1eNzetUC_DKA-faWnNGKUK3H-nEUxtQNUspYVd1Irah13QAnbtGCA1kb3TxcTVpCkJLF_J3taCiHxMd51j4TdUNcCHQwIqKUCQaUA5WQtwVCSxbof7qWSTvLuXsBJVkCymFZc0SPQ0YKg3FKuKO8kt7TWxptrtXU0LPeuv2OLE--kuLKF0xPgR9xqWjvbV2N_BBNREpclrbSqtemlap5_TaWlIF0LhnKqtFoqaLiTJ6BSOWg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