]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libgomp/target.c
Update copyright years.
[thirdparty/gcc.git] / libgomp / target.c
index cd4cc1b01ca778d1e94df13a2057479bde31ddb9..1367e9cce6c31784023a9e22f1f85336e0501805 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013-2023 Free Software Foundation, Inc.
+/* Copyright (C) 2013-2024 Free Software Foundation, Inc.
    Contributed by Jakub Jelinek <jakub@redhat.com>.
 
    This file is part of the GNU Offloading and Multi Processing Library
@@ -47,6 +47,7 @@
 
 /* Define another splay tree instantiation - for reverse offload.  */
 #define splay_tree_prefix reverse
+#define splay_tree_static
 #define splay_tree_c
 #include "splay-tree.h"
 
@@ -702,7 +703,7 @@ gomp_map_pointer (struct target_mem_desc *tgt, struct goacc_asyncqueue *aq,
   if (n == NULL)
     {
       if (allow_zero_length_array_sections)
-       cur_node.tgt_offset = 0;
+       cur_node.tgt_offset = cur_node.host_start;
       else
        {
          gomp_mutex_unlock (&devicep->lock);
@@ -741,7 +742,7 @@ gomp_map_fields_existing (struct target_mem_desc *tgt,
 
   cur_node.host_start = (uintptr_t) hostaddrs[i];
   cur_node.host_end = cur_node.host_start + sizes[i];
-  splay_tree_key n2 = splay_tree_lookup (mem_map, &cur_node);
+  splay_tree_key n2 = gomp_map_0len_lookup (mem_map, &cur_node);
   kind = get_kind (short_mapkind, kinds, i);
   implicit = get_implicit (short_mapkind, kinds, i);
   if (n2
@@ -838,8 +839,20 @@ gomp_attach_pointer (struct gomp_device_descr *devicep,
 
       if ((void *) target == NULL)
        {
-         gomp_mutex_unlock (&devicep->lock);
-         gomp_fatal ("attempt to attach null pointer");
+         /* As a special case, allow attaching NULL host pointers.  This
+            allows e.g. unassociated Fortran pointers to be mapped
+            properly.  */
+         data = 0;
+
+         gomp_debug (1,
+                     "%s: attaching NULL host pointer, target %p "
+                     "(struct base %p)\n", __FUNCTION__, (void *) devptr,
+                     (void *) (n->tgt->tgt_start + n->tgt_offset));
+
+         gomp_copy_host2dev (devicep, aq, (void *) devptr, (void *) &data,
+                             sizeof (void *), true, cbufp);
+
+         return;
        }
 
       s.host_start = target + bias;
@@ -850,9 +863,8 @@ gomp_attach_pointer (struct gomp_device_descr *devicep,
        {
          if (allow_zero_length_array_sections)
            /* When allowing attachment to zero-length array sections, we
-              allow attaching to NULL pointers when the target region is not
-              mapped.  */
-           data = 0;
+              copy the host pointer when the target region is not mapped.  */
+           data = target;
          else
            {
              gomp_mutex_unlock (&devicep->lock);
@@ -1072,7 +1084,8 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
            tgt->list[i].offset = 0;
          continue;
        }
-      else if ((kind & typemask) == GOMP_MAP_STRUCT)
+      else if ((kind & typemask) == GOMP_MAP_STRUCT
+              || (kind & typemask) == GOMP_MAP_STRUCT_UNORD)
        {
          size_t first = i + 1;
          size_t last = i + sizes[i];
@@ -1096,7 +1109,8 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
                  tgt->list[i].key = NULL;
                  if (!aq
                      && gomp_to_device_kind_p (get_kind (short_mapkind, kinds, i)
-                                               & typemask))
+                                               & typemask)
+                     && sizes[i] != 0)
                    gomp_coalesce_buf_add (&cbuf,
                                           tgt_size - cur_node.host_end
                                           + (uintptr_t) hostaddrs[i],
@@ -1455,6 +1469,20 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
                    tgt->list[i].offset = OFFSET_INLINED;
                  }
                continue;
+             case GOMP_MAP_STRUCT_UNORD:
+               if (sizes[i] > 1)
+                 {
+                   void *first = hostaddrs[i + 1];
+                   for (size_t j = i + 1; j < i + sizes[i]; j++)
+                     if (hostaddrs[j + 1] != first)
+                       {
+                         gomp_mutex_unlock (&devicep->lock);
+                         gomp_fatal ("Mapped array elements must be the "
+                                     "same (%p vs %p)", first,
+                                     hostaddrs[j + 1]);
+                       }
+                 }
+               /* Fallthrough.  */
              case GOMP_MAP_STRUCT:
                first = i + 1;
                last = i + sizes[i];
@@ -1463,7 +1491,17 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
                                    + sizes[last];
                if (tgt->list[first].key != NULL)
                  continue;
+               if (sizes[last] == 0)
+                 cur_node.host_end++;
                n = splay_tree_lookup (mem_map, &cur_node);
+               if (sizes[last] == 0)
+                 cur_node.host_end--;
+               if (n == NULL && cur_node.host_start == cur_node.host_end)
+                 {
+                   gomp_mutex_unlock (&devicep->lock);
+                   gomp_fatal ("Struct pointer member not mapped (%p)",
+                               (void*) hostaddrs[first]);
+                 }
                if (n == NULL)
                  {
                    size_t align = (size_t) 1 << (kind >> rshift);
@@ -1563,9 +1601,40 @@ gomp_map_vars_internal (struct gomp_device_descr *devicep,
              k->host_end = k->host_start + sizeof (void *);
            splay_tree_key n = splay_tree_lookup (mem_map, k);
            if (n && n->refcount != REFCOUNT_LINK)
-             gomp_map_vars_existing (devicep, aq, n, k, &tgt->list[i],
-                                     kind & typemask, false, implicit, cbufp,
-                                     refcount_set);
+             {
+               if (field_tgt_clear != FIELD_TGT_EMPTY)
+                 {
+                   /* For this condition to be true, there must be a
+                      duplicate struct element mapping.  This can happen with
+                      GOMP_MAP_STRUCT_UNORD mappings, for example.  */
+                   tgt->list[i].key = n;
+                   if (openmp_p)
+                     {
+                       assert ((n->refcount & REFCOUNT_STRUCTELEM) != 0);
+                       assert (field_tgt_structelem_first != NULL);
+
+                       if (i == field_tgt_clear)
+                         {
+                           n->refcount |= REFCOUNT_STRUCTELEM_FLAG_LAST;
+                           field_tgt_structelem_first = NULL;
+                         }
+                     }
+                   if (i == field_tgt_clear)
+                     field_tgt_clear = FIELD_TGT_EMPTY;
+                   gomp_increment_refcount (n, refcount_set);
+                   tgt->list[i].copy_from
+                     = GOMP_MAP_COPY_FROM_P (kind & typemask);
+                   tgt->list[i].always_copy_from
+                     = GOMP_MAP_ALWAYS_FROM_P (kind & typemask);
+                   tgt->list[i].is_attach = false;
+                   tgt->list[i].offset = 0;
+                   tgt->list[i].length = k->host_end - k->host_start;
+                 }
+               else
+                 gomp_map_vars_existing (devicep, aq, n, k, &tgt->list[i],
+                                         kind & typemask, false, implicit,
+                                         cbufp, refcount_set);
+             }
            else
              {
                k->aux = NULL;
@@ -2256,11 +2325,20 @@ gomp_load_image_to_device (struct gomp_device_descr *devicep, unsigned version,
   void **host_funcs_end  = ((void ***) host_table)[1];
   void **host_var_table  = ((void ***) host_table)[2];
   void **host_vars_end   = ((void ***) host_table)[3];
+  void **host_ind_func_table = NULL;
+  void **host_ind_funcs_end  = NULL;
 
-  /* The func table contains only addresses, the var table contains addresses
-     and corresponding sizes.  */
+  if (GOMP_VERSION_SUPPORTS_INDIRECT_FUNCS (version))
+    {
+      host_ind_func_table = ((void ***) host_table)[4];
+      host_ind_funcs_end  = ((void ***) host_table)[5];
+    }
+
+  /* The func and ind_func tables contain only addresses, the var table
+     contains addresses and corresponding sizes.  */
   int num_funcs = host_funcs_end - host_func_table;
   int num_vars  = (host_vars_end - host_var_table) / 2;
+  int num_ind_funcs = (host_ind_funcs_end - host_ind_func_table);
 
   /* Load image to device and get target addresses for the image.  */
   struct addr_pair *target_table = NULL;
@@ -2273,7 +2351,9 @@ gomp_load_image_to_device (struct gomp_device_descr *devicep, unsigned version,
   num_target_entries
     = devicep->load_image_func (devicep->target_id, version,
                                target_data, &target_table,
-                               rev_lookup ? &rev_target_fn_table : NULL);
+                               rev_lookup ? &rev_target_fn_table : NULL,
+                               num_ind_funcs
+                                 ? (uint64_t *) host_ind_func_table : NULL);
 
   if (num_target_entries != num_funcs + num_vars
       /* "+1" due to the additional ICV struct.  */
@@ -2698,7 +2778,7 @@ gomp_unload_device (struct gomp_device_descr *devicep)
   if (devicep->state == GOMP_DEVICE_INITIALIZED)
     {
       unsigned i;
-      
+
       /* Unload from device all images registered at the moment.  */
       for (i = 0; i < num_offload_images; i++)
        {
@@ -4132,7 +4212,8 @@ GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
   size_t i, j;
   if ((flags & GOMP_TARGET_FLAG_EXIT_DATA) == 0)
     for (i = 0; i < mapnum; i++)
-      if ((kinds[i] & 0xff) == GOMP_MAP_STRUCT)
+      if ((kinds[i] & 0xff) == GOMP_MAP_STRUCT
+         || (kinds[i] & 0xff) == GOMP_MAP_STRUCT_UNORD)
        {
          gomp_map_vars (devicep, sizes[i] + 1, &hostaddrs[i], NULL, &sizes[i],
                         &kinds[i], true, &refcount_set,
@@ -4230,7 +4311,8 @@ gomp_target_task_fn (void *data)
       htab_t refcount_set = htab_create (ttask->mapnum);
       if ((ttask->flags & GOMP_TARGET_FLAG_EXIT_DATA) == 0)
        for (i = 0; i < ttask->mapnum; i++)
-         if ((ttask->kinds[i] & 0xff) == GOMP_MAP_STRUCT)
+         if ((ttask->kinds[i] & 0xff) == GOMP_MAP_STRUCT
+             || (ttask->kinds[i] & 0xff) == GOMP_MAP_STRUCT_UNORD)
            {
              gomp_map_vars (devicep, ttask->sizes[i] + 1, &ttask->hostaddrs[i],
                             NULL, &ttask->sizes[i], &ttask->kinds[i], true,