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

    <tr>
        <th>Summary</th>
        <td>
            Taskgroup task_reduction with target in_reduction segfaults
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          tomdeakin
      </td>
    </tr>
</table>

<pre>
    The following code segfaults at the end of the taskgroup when waiting for all tasks. If I remove nowait on the target tasks, the segfault goes away, which suggests the synchronisation at the end of the taskgroup construct is failing.

Tested with LLVM at commit 9ad0ace2, built for `-fopenmp-targets=nvtpx64` on a node with four A100 GPUs.
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

#include <omp.h>

int main(void) {

  int N = 1000000;
  double *A, *B;
  A = malloc(N*sizeof(double));
  B = malloc(N*sizeof(double));

  double sum = 0.0;

  for (int i = 0; i < N; ++i) {
    A[i] = 1.5;
    B[i] = 2.0;
  }
  
  int num_devices = omp_get_num_devices();
  printf("Num devices: %d\n", num_devices);
  assert (N % num_devices == 0);
  int len = N / num_devices;

  #pragma omp taskgroup task_reduction(+:sum)
  {
    for (int dev = 0; dev < num_devices; ++dev) {
      int start = dev * len;
      int end = start + len;
 
      #pragma omp target map(to: A[start:len], B[start:len]) device(dev) in_reduction(+:sum) nowait
      {
        #pragma omp loop
        for (int i = start; i < end; ++i) {
                sum += A[i] * B[i];
        }
      }
    }
  }

  assert(fabs(sum - (3.0*N)) < 1.0E-10);

  printf("Success\n");

}
```

Some preliminary discussion occurred on [Discourse](https://discourse.llvm.org/t/taskgroup-task-reduction-with-target-in-reduction-segfaults/64683)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVluvozYQ_jXkxQoiJiTkgYdcdquVtlGlPe3rkQED7hqMsEl6-us7YyAxyclKRSSxPRd_882MnVTlH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