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

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP] incorrect concurrent target reduction
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          ye-luo
      </td>
    </tr>
</table>

<pre>
    ```
#include <iostream>
#include <vector>

#define N 4

int main()
{
  std::vector<int> avec(N);
  int* a = avec.data();
  #pragma omp parallel for
 for(int i = 0; i < N; i++)
  {
    a[i] = 0;
    #pragma omp target teams distribute parallel for reduction(+:a[i])
    for(int j =0 ;j< N; j++)
      a[i] += 1;
  }

  for(int i = 0; i < N; i++)
 std::cout << a[i] << std::endl;
}
```
running `clang++ -fopenmp --offload-arch=sm_80 main.cpp && ./a.out`
expect
```
results: 4 4 4 4
```
but I got random failure
```
results: 0 4 0 0
results: 4 0 4 0
results: 4 0 4 0
```

Turning on debugging info
```
OMP_TARGET_OFFLOAD=mandatory OMP_NUM_THREADS=32 LIBOMPTARGET_DEBUG=1 ./a.out >& out && grep "Moving 4 bytes\|result" out
Libomptarget --> Moving 4 bytes (hst:0x00005572a0d9c27c) -> (tgt:0x00007f7112600000)
Libomptarget --> Moving 4 bytes (hst:0x00005572a0d9c278) -> (tgt:0x00007f7112600200)
Libomptarget --> Moving 4 bytes (hst:0x00005572a0d9c274) -> (tgt:0x00007f7112600400)
Libomptarget --> Moving 4 bytes (hst:0x00005572a0d9c270) -> (tgt:0x00007f7112600600)
Libomptarget --> Moving 4 bytes (tgt:0x00007f7112600000) -> (hst:0x00005572a0d9c27c)
Libomptarget --> Moving 4 bytes (tgt:0x00007f7112600400) -> (hst:0x00005572a0d9c274)
Libomptarget --> Moving 4 bytes (tgt:0x00007f7112600600) -> (hst:0x00005572a0d9c270)
Libomptarget --> Moving 4 bytes (tgt:0x00007f7112600200) -> (hst:0x00005572a0d9c278)
results: 4 0 4 0
```
Mapping and transfers seem OK to me. The failure was miserable.

setting `OMP_NUM_THREADS=1` the test passes reliably.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysll1v4joTxz_NcDMCOXYIyQUX0JR9Vs9Sqj3d68pJhuAqsSPb6W6__ZEDDfRs1Xa1VaMyYV5-f8av0jlVa6IlzNcwzyey9wdjl080bXozKUz1tISEnR6WA1sBF0qXTV8RgrhSxnlLsgVx_Zr7kUpv7Nn5HFLRXmnCG4wvHUp7bKXSwFPg2cmzWB8NROcrECsQq-eqV0p7ENcoH6kEnt6EJDGGBydfoUQQ-RAyq6SXp9rnMOCis7JuJZq2w05a2TTU4N7YU0SweBq0qaEUA7EezCu8GUzg6-HJxpJnzYgS5msF83zMPbteor20NXn0JFuHlXLeqqL39EISWqr60itz7NEaxOq5_gUfLzQ_BC5DEOuHUfHDb4pfCg11c4wum7TILwcK_7wp4-CVpvchLoRe9GZ4H4NIV82IP8NfzkTba610jZCwspG6PiJxujcd6bbD6dTs942R1VTa8gAid-19yoYZNiu7DoEnwBOcAd_Imen9WJh-dVT615nk-sY7ECuMj3-vhhW9x69YG49W6sq0uJeq6S29V5NhjAxfYw2et7__70Id_t_1dmiS0VhR0dd1eFF6b17N2W1v7-9W379c393vNptvu1UOIm-lrqQ39gmD--bH9v7uf9-vV_k_IHLB8dvX9W57e8rKr9c_voDIo3NbMSx_nuBgHlteWwrt51vzGOTEWDx5cjC_gsXV8dcB5yHhqOqbKkzbndbHdBqW_MtMBJ4enAexYr8YY2w-X3DJqqzkixJ4hkMK8NTX55jFfhFFPAkmG6fp35DSd0n8k0jxu6T4k0jsXVLyp6Q3xmAkvTGWf0uKP0SKP4GUfIj0Gd3jHyKdz9QPbiBb2XUBL3WF3krt9mQdOqIWd_9Hb7ClGd4d6Hlzw5_SYascWVk0NLvchRx5f9qqf99DIkgY-gOhJ-exk86RQ0uNkkXzdC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