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

            Bug ID: 119904
           Summary: [OpenMP] wrong code as map item sorting does not work
                    - fails with: libgomp: Pointer target of array section
                    wasn't mapped
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: openmp, wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
  Target Milestone: ---

I think this might be related to some existing bug like PR113867 and/or
PR113724 - especially as the there is sorting done in
omp_gather_mapping_groups_1

The problem is that the following mapping FAILS
   map(test_lst)  map(tofrom: test_lst[0][:1])

while this mapping WORKS:
  map(tofrom: test_lst[0][:1]) map(test_lst)

The problem is that 'map(test_lst)' has to be mapped first, i.e. it needs
to be ordered such that the mapping happens first. As GCC evaluates clauses
from right to left, the second one works.

As mentioned, some clause ordering happens already – but obviously not in this
case.

-----------------------

int main ()
{
  int *array[512], *array2[512];
  for (int i = 0; i < 512; i++)
    {
      array[i] = (int*) __builtin_malloc (sizeof (int));
      array2[i] = (int*) __builtin_malloc (sizeof (int));
      array[i][0] = 1;
      array2[i][0] = -1;
    }

  // FAILS: libgomp: Pointer target of array section wasn't mapped
  #pragma omp target map(array) map(tofrom: array[0][:1])
  for (int i = 0; i < 512; i++)
    array[i][0] = 2;

  // WORKS
  #pragma omp target map(tofrom: array2[0][:1]) map(array2)
  for (int i = 0; i < 512; i++)
    array2[i][0] = -2;

  int sum = 0;
  int sum2 = 0;
  for (int i = 0; i < 512; i++)
    {
      sum += array[i][0];
      sum2 += array2[i][0];
      __builtin_free (array[i]);
      __builtin_free (array2[i]);
    }
  if (sum != 2*512 || sum2 != -2*512)
    __builtin_abort ();
  return 0;
}

-----------------------

A fancier version would use:
  int **array = (int**) malloc(sizeof(int*)*512);
which requires mapping 'array[:512]' and attaching it to 'array'

Reply via email to