(root)/
gcc-13.2.0/
libgomp/
config/
gcn/
target.c
       1  /* Copyright (C) 2017-2023 Free Software Foundation, Inc.
       2     Contributed by Mentor Embedded.
       3  
       4     This file is part of the GNU Offloading and Multi Processing Library
       5     (libgomp).
       6  
       7     Libgomp is free software; you can redistribute it and/or modify it
       8     under the terms of the GNU General Public License as published by
       9     the Free Software Foundation; either version 3, or (at your option)
      10     any later version.
      11  
      12     Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
      13     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      14     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      15     more details.
      16  
      17     Under Section 7 of GPL version 3, you are granted additional
      18     permissions described in the GCC Runtime Library Exception, version
      19     3.1, as published by the Free Software Foundation.
      20  
      21     You should have received a copy of the GNU General Public License and
      22     a copy of the GCC Runtime Library Exception along with this program;
      23     see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      24     <http://www.gnu.org/licenses/>.  */
      25  
      26  #include "libgomp.h"
      27  #include "libgomp-gcn.h"
      28  #include <limits.h>
      29  
      30  extern volatile struct gomp_offload_icvs GOMP_ADDITIONAL_ICVS;
      31  
      32  bool
      33  GOMP_teams4 (unsigned int num_teams_lower, unsigned int num_teams_upper,
      34  	     unsigned int thread_limit, bool first)
      35  {
      36    if (!first)
      37      return false;
      38    if (thread_limit)
      39      {
      40        struct gomp_task_icv *icv = gomp_icv (true);
      41        icv->thread_limit_var
      42  	= thread_limit > INT_MAX ? UINT_MAX : thread_limit;
      43      }
      44    unsigned int num_workgroups, workgroup_id;
      45    num_workgroups = __builtin_gcn_dim_size (0);
      46    workgroup_id = __builtin_gcn_dim_pos (0);
      47    /* FIXME: If num_teams_lower > num_workgroups, we want to loop
      48       multiple times at least for some workgroups.  */
      49    (void) num_teams_lower;
      50    if (!num_teams_upper || num_teams_upper >= num_workgroups)
      51      num_teams_upper = num_workgroups;
      52    else if (workgroup_id >= num_teams_upper)
      53      return false;
      54    gomp_num_teams_var = num_teams_upper - 1;
      55    return true;
      56  }
      57  
      58  int
      59  omp_pause_resource (omp_pause_resource_t kind, int device_num)
      60  {
      61    (void) kind;
      62    (void) device_num;
      63    return -1;
      64  }
      65  
      66  int
      67  omp_pause_resource_all (omp_pause_resource_t kind)
      68  {
      69    (void) kind;
      70    return -1;
      71  }
      72  
      73  ialias (omp_pause_resource)
      74  ialias (omp_pause_resource_all)
      75  
      76  void
      77  GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
      78  		 void **hostaddrs, size_t *sizes, unsigned short *kinds,
      79  		 unsigned int flags, void **depend, void **args)
      80  {
      81    (void) flags;
      82    (void) depend;
      83    (void) args;
      84  
      85    if (device != GOMP_DEVICE_HOST_FALLBACK || fn == NULL)
      86      return;
      87  
      88    /* The output data is at ((void*) kernargs)[2].  */
      89    register void **kernargs = (void**) __builtin_gcn_kernarg_ptr ();
      90    struct output *data = (struct output *) kernargs[2];
      91    /* Reserve one slot. */
      92    unsigned int index = __atomic_fetch_add (&data->next_output, 1,
      93  					   __ATOMIC_ACQUIRE);
      94  
      95    if ((unsigned int) (index + 1) < data->consumed)
      96      abort ();  /* Overflow.  */
      97  
      98    /* Spinlock while the host catches up.  */
      99    if (index >= 1024)
     100      while (__atomic_load_n (&data->consumed, __ATOMIC_ACQUIRE)
     101  	   <= (index - 1024))
     102        asm ("s_sleep 64");
     103  
     104    unsigned int slot = index % 1024;
     105    data->queue[slot].value_u64[0] = (uint64_t) fn;
     106    data->queue[slot].value_u64[1] = (uint64_t) mapnum;
     107    data->queue[slot].value_u64[2] = (uint64_t) hostaddrs;
     108    data->queue[slot].value_u64[3] = (uint64_t) sizes;
     109    data->queue[slot].value_u64[4] = (uint64_t) kinds;
     110    data->queue[slot].value_u64[5] = (uint64_t) GOMP_ADDITIONAL_ICVS.device_num;
     111  
     112    data->queue[slot].type = 4; /* Reverse offload.  */
     113    __atomic_store_n (&data->queue[slot].written, 1, __ATOMIC_RELEASE);
     114  
     115    /* Spinlock while the host catches up.  */
     116    while (__atomic_load_n (&data->queue[slot].written, __ATOMIC_ACQUIRE) != 0)
     117      asm ("s_sleep 64");
     118  }
     119  
     120  void
     121  GOMP_target_data_ext (int device, size_t mapnum, void **hostaddrs,
     122  		      size_t *sizes, unsigned short *kinds)
     123  {
     124    (void) device;
     125    (void) mapnum;
     126    (void) hostaddrs;
     127    (void) sizes;
     128    (void) kinds;
     129    __builtin_unreachable ();
     130  }
     131  
     132  void
     133  GOMP_target_end_data (void)
     134  {
     135    __builtin_unreachable ();
     136  }
     137  
     138  void
     139  GOMP_target_update_ext (int device, size_t mapnum, void **hostaddrs,
     140  			size_t *sizes, unsigned short *kinds,
     141  			unsigned int flags, void **depend)
     142  {
     143    (void) device;
     144    (void) mapnum;
     145    (void) hostaddrs;
     146    (void) sizes;
     147    (void) kinds;
     148    (void) flags;
     149    (void) depend;
     150    __builtin_unreachable ();
     151  }
     152  
     153  void
     154  GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
     155  			     size_t *sizes, unsigned short *kinds,
     156  			     unsigned int flags, void **depend)
     157  {
     158    (void) device;
     159    (void) mapnum;
     160    (void) hostaddrs;
     161    (void) sizes;
     162    (void) kinds;
     163    (void) flags;
     164    (void) depend;
     165    __builtin_unreachable ();
     166  }