<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63197>63197</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang issue with nested openmp
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jakub-homola
      </td>
    </tr>
</table>

<pre>
    Hello,

I am having an issue when using nested OpenMP in clang. Using the `num_threads(3)` and `if(false)` clauses on an inner parallel region somehow propagates the number of threads to the outside, and the following outer parallel region then uses the number of threads that was set in that inner parallel region.

As you can see in the output of the program (below), the first parallel region is launched with 64 threads (`AAA 64`). The second parallel region launched with 64 threads (`BBB 64`) contains a nested parallel region. I use the `num_threads(3)` and `if(false)` clauses. The inner parallel region is launched with only a single thread (`XXX 1`), which is expected because of the `if(false)`. The following parallel region should be again launched with 64 threads, but as you can see, it is launched with only 3 threads (`DDD 3`), which is weird. The last parallel region is then correctly launched with 64 threads (`EEE 64`).

I tried disabling nested parallelism by calling `omp_set_max_active_levels(1)` at the start of the program, but that did not help.

If I remove the `if(false)` clause, the program works as expected.

If I use e.g. `num_threads(32)` on DDD parallel region, it is launched as expected with 32 threads - the program works as expected.

Using g++, the program works as expected.

I am almost certain this is a bug, and I would appreciate if it was fixed.

I was primarily testing with clang version 13.0.1 on a CentOS 7 machine, with clang 16.0.0 on Ubuntu-18.04 I observe the same weird behavior. If you need more details, please ask.

Thanks,

Jakub

-----

The reproducer code (does not reproduce 100 % of times, might need to be run multiple times):
```
#include <cstdio>
#include <omp.h>

int main()
{
    #pragma omp parallel
    #pragma omp single
 {
        printf("AAA %2d\n", omp_get_num_threads());
    }

 printf("\n");

    #pragma omp parallel
    #pragma omp single
    {
        printf("BBB %2d\n", omp_get_num_threads());

        #pragma omp parallel num_threads(3) if(false)
        {
            printf("XXX %2d\n", omp_get_num_threads());
        }

        printf("CCC %2d\n", omp_get_num_threads());
    }
    
    printf("\n");

 #pragma omp parallel
    #pragma omp single
    {
 printf("DDD %2d\n", omp_get_num_threads());
    }
    
 printf("\n");

    #pragma omp parallel
    #pragma omp single
    {
        printf("EEE %2d\n", omp_get_num_threads());
 }

    return 0;
}
```

Compile with:
```
clang++ -g -fopenmp source.cpp -o program.x
```

Expected output:
```
AAA 64

BBB 64
XXX  1
CCC 64

DDD 64

EEE 64
```

Observed output:
```
AAA 64

BBB 64
XXX  1
CCC 64

DDD  3

EEE 64
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV19v4zYM_zTKCxHDlhM3echDm7RYBww3YHfAvRWyzdi6ypIhyU377QfKTpo_zt3u1gErAtcWLfJH8kdSFs7JSiOu2PyOzTcT0fna2NU38dzl09o0RolJbsq31W-olGF8zeINi2_76yOIBmrxInUFQoN0rkPY1aihc7Sm0Xks4VOL-o8_QWoolNBVBF-C1NcILIt11zz52qIoHeOLlPEly2IQuiSh3DK-2ArlcFgvlOgcOjA6WNQaLbTCCqVQgcVKGg3ONFibHbTWtKISHl2wpbsmRwtmC4M58CYITOedLJHxdTBLS1ujlNkRStP5ERO-d_K65lp42AkHDj05Hp5H0UbHAb118GY6KIQGh9hvDPjazvfqkbyqrGiA8UWOyuwoMHzdo5bW-Qus0oESnS5qLGEnfQ3Z7ICT8QXL4tvbW8hmLIsZX0bwuUZwWBhdXqj6gZ67u7uDHiiM9kJqB2LPg3PP4ZFC-K940KMdp8GF30arNxBA5FM4QB-Qf_36FZIeOAVzV8uiJgX42mJB2HMsyOA-CSOQeijvxLlgZW06RYpAVEJejyXZzzsP4oQLtCr9FZ_SszxsNhtIR7zZobRlj1OJcaYEYhfGWiy8evtRwu_v79-Jc9oZvJVYQimdyNVRL9iblK6B_A0KoYKUZbFp2ieH_qkRr0-i8PIFnxS-oCI2JHs2-BB954U9r4d92EKllbIEbTzUqNpTXFt4BIuNecFreRyota-pfbntjH12lJQ9JS71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