<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/82754>82754</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[OpenMP] Metadirective uses default for non-const user condition
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Thyre
</td>
</tr>
</table>
<pre>
### Description
I've been looking at a few lesser used OpenMP directives recently and ran across `metadirective`. While the syntax always looks a bit intimidating, I wanted to know more about it and experimented with a few test cases. While doing this, I've noticed that Clang seems to use the `default` case whenever a user condition is used. To show this issue, I've prepared the following reproducer.
### Reproducer
```c++
#include <cassert>
#include <iostream>
#include <omp.h>
int main( int argc, char** argv )
{
constexpr size_t num_threads = 4;
constexpr int constexpr_val = 1;
#pragma omp metadirective \
when( user= {condition(constexpr_val > 0)} : parallel num_threads( num_threads ) ) \
default()
{
#pragma omp single
assert( num_threads == omp_get_num_threads() );
}
const int const_val = 1;
#pragma omp metadirective \
when( user= {condition(const_val > 0)} : parallel num_threads( num_threads ) ) \
default()
{
#pragma omp single
assert( num_threads == omp_get_num_threads() );
}
int non_const_val = 1;
#pragma omp metadirective \
when( user= {condition(non_const_val > 0)} : parallel num_threads( num_threads ) ) \
default()
{
#pragma omp single
assert( num_threads == omp_get_num_threads() );
}
}
```
Here, I just want to make sure that the `metadirective` works. In theory, all three cases should result in the same: Having a parallel region with four threads. However, this is not the case with LLVM 15.0.7 and newer. Here's the output from LLVM trunk:
```console
$ clang --version
clang version 19.0.0git (https://github.com/llvm/llvm-project.git 1fe6be8794964c011aeba7a66bd2dcd891d21ab0)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/apps/software/Clang/trunk/bin
$ clang++ -fopenmp reproducer.cpp
$ ./a.out
a.out: reproducer.cpp:33: int main(int, char **): Assertion `num_threads == omp_get_num_threads()' failed.
[1] 1133016 IOT instruction (core dumped) ./a.out
```
NVHPC 23.9 handles this test perfectly fine. oneAPI 2024.0 fails the same way and GCC 12.3.0 even fails the first assertion. Replacing the `non_const_val` with a true user condition (like `std::cin > non_const_val`) doesn't change the outcome of the test.
Looking at the IR, it seems like the metadirective is replaced by the default, resulting in no check at all if the parallel region should be used: https://godbolt.org/z/9oM5ooeE1
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV81u4zgSfhr6UrAgUbZlH3xw7PYmQGenMQhmjwEllixOKFIgKTuZp18UJf9idoABBo013O6ILNbPV6z6SsJ7dTCIazZ_YvPdRPShsW791nw5nJRWfq0Zz4cv7NBXTnVBWcPSHUs3w-8L48URoUQ0oK39UOYAIoCAGk-g0Xt00HuU8EuH5vUHSOWwCuqIHhxWaIL-AmEkOGFAVM56D2yRthjERZIt0gT-0yiNEBoE_2WC-AShT-LLR5seBJQqgDJBtUqKoMyB8S28wEmYgBKChQ9jT9BahyBK2wdQIZrFzw6dajGKnVRoRs8D-gCV8OjPpqWl0EKjfFQ9hG1sUBUZaESArRbmAB6x9WSx94O_bJFKrEWvA1ukUSecGjR4RAeCpBxU1khFyILyEa0E3iz4xp6iQVDe93hjtnPYCRftItRWa3si5xx2zsq-Qpfcpuiaw18vAnf7i3T4Vow_0fd8SplK9xKB5dtKUCYDy7_92a6yPjgU7f_Ytm2XNNe9-KtMgFYow_iS8gbCHSqKsGqEY3zD-IaWjsD4ajxWjI4BAAHmA352Drz6A98DmL59D41DIT2wfAczlj_dmrs_RAYvT-9HoeOZ7HKGpBnPOycOrQDbdnB3IYHNt1dB-lBCKRLKJqlixdMlp4wvH219g5TiKnbA8g10wgmtUd8GQcruYuKr4d_F8vlO8eUForM3d1BdFu_i8cocNI5SY24fTeY7CsW23fsBw_u9c6M_d4gNpnd3sMfIr3j_LKz_QZzHz_8L3LcmBnUU3vXx6gmBbqx5_1nAP9r62eD_BeI3n3_2rt8Cf7n553Z6m5BndEMDh997HyIvEUe04gPB9w4HBhnp4pH94GT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