https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123597

            Bug ID: 123597
           Summary: [14/15/16 Regression][OpenMP] Local variable wrongly
                    hoisted (and shared) with templates and collapse(2)
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: openmp, wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
                CC: schulz.benjamin at googlemail dot com
  Target Milestone: ---

Reported off list by Benjamin Schulz (CCed).
The slightly reduced testcase as follows.

With GCC 13 and for 'no_template', the original dump has the correct:

                for (i = 0; i < (long int) N; i++ )
                  for (j = 0; j < (long int) N; j++ )
                    {
                      {
                        int sum = 0;

However, since GCC 14, the template version has hoisted the out of the inner
loops:

          long int i;
          long int j;
          int sum = 0;

          #pragma omp for collapse(2) nowait
              for (i = 0; i < (long int) N; i++ )
                for (j = 0; j < (long int) N; j++ )

→ 'sum' is shared by all threads.

------------
int N = 5;

void no_template (int *A)
{
  #pragma omp parallel for collapse(2)
  for (long i = 0; i < N; i++)
    for (long j = 0; j < N; j++)
      {
        int sum = 0;
        for (long k = 0; k < N; k++)
          sum += 2;
        A[i + N*j] = sum;
      }
}

template<typename T>
void with_template (T *A)
{
  #pragma omp parallel for collapse(2)
  for (long i = 0; i < N; i++)
    for (long j = 0; j < N; j++)
      {
        T sum = 0;
        for (long k = 0; k < N; k++)
          sum += 2;
        A[i + N*j] = sum;
      }
}

void use_template(int *B)
{
  with_template (B);
}

Reply via email to