https://gcc.gnu.org/g:4f7ea11ad0883eb91fc072efdbe55ebc6bdebc9c

commit 4f7ea11ad0883eb91fc072efdbe55ebc6bdebc9c
Author: Andrew Stubbs <[email protected]>
Date:   Mon Nov 2 17:43:46 2026 +0000

    amdgcn, libgomp: Pass thread_limit to SIMT mode kernels
    
    Up to now, it has been safe to assume that the thread_limit and the number 
of
    actual threads launched are the same number, but for SIMT it can happen that
    the user specified a number that is not a whole number of wavefronts (i.e. a
    multiple of 64).  This patch passes the thread_limit value into the kernel 
and
    uses the true value on the device.
    
    GCN_NUM_THREADS / GCN_NUM_WORKERS is still allowed as an override, as 
before.
    
    Assisted-by: Claude Opus 5.5
    
    Claude generated the testcases and assisted with the code.
    
    gcc/ChangeLog:
    
            * config/gcn/gcn-run.cc (main): Initialize omp_thread_limit.
    
    libgomp/ChangeLog:
    
            * config/gcn/libgomp-gcn.h (struct kernargs_abi): Add
            omp_thread_limit.
            * config/gcn/target.c (GOMP_teams4): Do not let thread_limit exceed
            the number of threads launched.
            * config/gcn/team.c (gomp_gcn_enter_kernel): In "simt" mode, take
            thread_limit_var from the kernel arguments.
            * plugin/plugin-gcn.c (omp_simt_kernel_p): New function.
            (parse_target_attributes): Add KERNEL parameter.  Count threads in
            lanes for "simt" mode kernels.
            (run_kernel): Convert a "simt" mode kernel's thread count from lanes
            to wavefronts, and pass it in the kernel arguments.
            (GOMP_OFFLOAD_run): Pass the kernel to parse_target_attributes.
            (GOMP_OFFLOAD_async_run): Likewise.
            * testsuite/libgomp.c/interop-hsa.c (main): Initialize
            omp_thread_limit.
            * testsuite/libgomp.c-c++-common/simt-gcn-num-threads-amdgcn-1.c:
            New test.
            * testsuite/libgomp.c-c++-common/simt-thread-limit-amdgcn-1.c: New
            test.

Diff:
---
 gcc/config/gcn/gcn-run.cc                          |  1 +
 libgomp/config/gcn/libgomp-gcn.h                   |  5 ++
 libgomp/config/gcn/target.c                        |  3 ++
 libgomp/config/gcn/team.c                          |  7 +++
 libgomp/plugin/plugin-gcn.c                        | 48 +++++++++++++++---
 .../simt-gcn-num-threads-amdgcn-1.c                | 33 +++++++++++++
 .../simt-thread-limit-amdgcn-1.c                   | 57 ++++++++++++++++++++++
 libgomp/testsuite/libgomp.c/interop-hsa.c          |  1 +
 8 files changed, 147 insertions(+), 8 deletions(-)

diff --git a/gcc/config/gcn/gcn-run.cc b/gcc/config/gcn/gcn-run.cc
index ffbf929c25a0..5268a66d59a5 100644
--- a/gcc/config/gcn/gcn-run.cc
+++ b/gcc/config/gcn/gcn-run.cc
@@ -715,6 +715,7 @@ main (int argc, char *argv[])
   kernargs->abi.arena_ptr = 0;
   kernargs->abi.stack_ptr = (int64_t) stack;
   kernargs->abi.stack_size_per_thread = stack_size;
+  kernargs->abi.omp_thread_limit = 0;
 
   /* Run constructors on the GPU.  */
   run (init_array_kernel, kernargs);
diff --git a/libgomp/config/gcn/libgomp-gcn.h b/libgomp/config/gcn/libgomp-gcn.h
index 7475be774d25..f4cd6c535099 100644
--- a/libgomp/config/gcn/libgomp-gcn.h
+++ b/libgomp/config/gcn/libgomp-gcn.h
@@ -72,6 +72,11 @@ struct kernargs_abi {
   int64_t stack_ptr;           /* Offset 40.  */
   int arena_size_per_team;     /* Offset 48.  */
   int stack_size_per_thread;   /* Offset 52.  */
+
+  /* For OpenMP kernels in "simt" mode, the number of OpenMP threads (lanes)
+     the launch is for, which may end part way through the last wavefront;
+     zero otherwise.  */
+  int omp_thread_limit;                /* Offset 56.  */
 };
 
 /* This struct is also used in Newlib's libc/sys/amdgcn/write.c.  */
diff --git a/libgomp/config/gcn/target.c b/libgomp/config/gcn/target.c
index 754bf1302102..19c1f3edd098 100644
--- a/libgomp/config/gcn/target.c
+++ b/libgomp/config/gcn/target.c
@@ -59,6 +59,9 @@ GOMP_teams4 (unsigned int num_teams_lower, unsigned int 
num_teams_upper,
       struct gomp_task_icv *icv = gomp_icv (true);
       icv->thread_limit_var
        = thread_limit > INT_MAX ? UINT_MAX : thread_limit;
+      /* There are no more threads than the launch provides.  */
+      if (icv->thread_limit_var > gomp_global_icv.thread_limit_var)
+       icv->thread_limit_var = gomp_global_icv.thread_limit_var;
     }
   if (!num_teams_upper)
     num_teams_upper = ((GOMP_ADDITIONAL_ICVS.nteams > 0
diff --git a/libgomp/config/gcn/team.c b/libgomp/config/gcn/team.c
index 5c8eeedd6d87..0f40a117de3e 100644
--- a/libgomp/config/gcn/team.c
+++ b/libgomp/config/gcn/team.c
@@ -103,6 +103,13 @@ gomp_gcn_enter_kernel (
       gomp_global_icv.nthreads_var = (simt_mode ? 1024 : 16);
       gomp_global_icv.thread_limit_var = (numwavefronts
                                          * (simt_mode ? 64 : 1));
+      /* The plugin launches whole wavefronts, and passes the number of
+        OpenMP threads that was asked for.  */
+      int omp_thread_limit = ((struct kernargs_abi *)
+                             __builtin_gcn_kernarg_ptr ())->omp_thread_limit;
+      if (simt_mode && omp_thread_limit > 0
+         && omp_thread_limit < gomp_global_icv.thread_limit_var)
+       gomp_global_icv.thread_limit_var = omp_thread_limit;
 #else
       gomp_global_icv.nthreads_var = 16;
       gomp_global_icv.thread_limit_var = numwavefronts;
diff --git a/libgomp/plugin/plugin-gcn.c b/libgomp/plugin/plugin-gcn.c
index de133d0a178a..4fdb262f67f3 100644
--- a/libgomp/plugin/plugin-gcn.c
+++ b/libgomp/plugin/plugin-gcn.c
@@ -1304,6 +1304,18 @@ limit_worker_threads (int threads)
   return threads;
 }
 
+/* True if KERNEL is an OpenMP kernel compiled in "simt" mode, where each
+   lane of a wavefront is an OpenMP thread.  Its thread counts (the
+   thread_limit argument, GCN_NUM_THREADS, and gdims[2] of its launch
+   attributes until run_kernel converts them) count lanes, not wavefronts.  */
+
+static bool
+omp_simt_kernel_p (struct kernel_info *kernel)
+{
+  return (kernel->kind == KIND_OPENMP
+         && kernel->description->simt_mode == GCN_KERNEL_SIMT_MODE);
+}
+
 /* This sets the maximum number of teams to twice the number of GPU Compute
    Units to avoid memory waste and corresponding memory access faults.  */
 
@@ -1326,11 +1338,14 @@ static bool
 parse_target_attributes (void **input,
                         struct GOMP_kernel_launch_attributes *def,
                         struct GOMP_kernel_launch_attributes **result,
-                        struct agent_info *agent)
+                        struct agent_info *agent, struct kernel_info *kernel)
 {
   if (!input)
     GOMP_PLUGIN_fatal ("No target arguments provided");
 
+  /* Threads per wavefront, as the thread counts here count.  */
+  int lanes = omp_simt_kernel_p (kernel) ? 64 : 1;
+
   bool grid_attrs_found = false;
   bool gcn_dims_found = false;
   int gcn_teams = 0;
@@ -1363,7 +1378,8 @@ parse_target_attributes (void **input,
              gcn_teams = limit_teams (val, agent);
              break;
            case GOMP_TARGET_ARG_THREAD_LIMIT:
-             gcn_threads = limit_worker_threads (val);
+             /* run_kernel limits a count of lanes.  */
+             gcn_threads = lanes == 1 ? limit_worker_threads (val) : val;
              break;
            default:
              ;
@@ -1393,7 +1409,7 @@ parse_target_attributes (void **input,
       if (gcn_teams <= 0 && gcn_threads <= 0)
        {
          /* Set up a reasonable number of teams and threads.  */
-         gcn_threads = gfx900_workaround_p ? 4 : 16; // 8;
+         gcn_threads = (gfx900_workaround_p ? 4 : 16) * lanes; // 8;
          def->gdims[0] = get_cu_count (agent); // * (40 / gcn_threads);
          def->gdims[2] = gcn_threads;
        }
@@ -1405,11 +1421,11 @@ parse_target_attributes (void **input,
        }
       else if (gcn_teams > 0 && gcn_threads <= 0)
        {
-         int max_threads = gfx900_workaround_p ? 4 : 16;
+         int max_threads = (gfx900_workaround_p ? 4 : 16) * lanes;
 
          /* Auto-scale the number of threads with the number of teams.  */
          def->gdims[0] = gcn_teams;
-         def->gdims[2] = 16; // get_cu_count (agent) * 40 / gcn_teams;
+         def->gdims[2] = 16 * lanes; // get_cu_count (agent) * 40 / gcn_teams;
          if (def->gdims[2] == 0)
            def->gdims[2] = 1;
          else if (def->gdims[2] > max_threads)
@@ -2453,6 +2469,18 @@ run_kernel (struct gomp_offload_session *session,
   GCN_DEBUG ("SGPRs: %d, VGPRs: %d\n", kernel->description->sgpr_count,
             kernel->description->vpgr_count);
 
+  /* An OpenMP kernel in "simt" mode counts its threads in lanes.  Launch
+     enough wavefronts for them, and pass the exact count to libgomp.  */
+  int omp_thread_limit = 0;
+  if (kla->ndim == 3 && omp_simt_kernel_p (kernel))
+    {
+      int max_threads = limit_worker_threads (INT_MAX) * 64;
+      omp_thread_limit = override_z_dim ? : kla->gdims[2];
+      if (omp_thread_limit > max_threads)
+       omp_thread_limit = max_threads;
+      kla->gdims[2] = (omp_thread_limit + 63) / 64;
+    }
+
   /* Reduce the number of threads/workers if there are insufficient
      VGPRs available to run the kernels together.  */
   if (kla->ndim == 3 && kernel->description->vpgr_count > 0)
@@ -2466,6 +2494,8 @@ run_kernel (struct gomp_offload_session *session,
                       " per team/gang - reducing to %d threads/workers.\n",
                       kla->gdims[2], max_threads);
          kla->gdims[2] = max_threads;
+         if (omp_thread_limit > max_threads * 64)
+           omp_thread_limit = max_threads * 64;
        }
     }
 
@@ -2519,7 +2549,8 @@ run_kernel (struct gomp_offload_session *session,
   /* Do not allow the dimensions to be overridden when running
      constructors or destructors.  */
   int override_x = kernel->kind == KIND_UNKNOWN ? 0 : override_x_dim;
-  int override_z = kernel->kind == KIND_UNKNOWN ? 0 : override_z_dim;
+  int override_z = (kernel->kind == KIND_UNKNOWN || omp_thread_limit
+                   ? 0 : override_z_dim);
 
   hsa_kernel_dispatch_packet_t *packet;
   packet = ((hsa_kernel_dispatch_packet_t *) command_q->base_address)
@@ -2573,6 +2604,7 @@ run_kernel (struct gomp_offload_session *session,
   prepare_kernel_dispatch (&shadow, kernel, packet->grid_size_x,
                           packet->grid_size_z, kernargs);
   shadow.queue = command_q;
+  kernargs->abi.omp_thread_limit = omp_thread_limit;
 
   if (debug)
     {
@@ -5511,7 +5543,7 @@ GOMP_OFFLOAD_run (struct gomp_offload_session *session, 
void *fn_ptr, void **arg
   /* If we get here then the kernel must be OpenMP.  */
   kernel->kind = KIND_OPENMP;
 
-  if (!parse_target_attributes (args, &def, &kla, agent))
+  if (!parse_target_attributes (args, &def, &kla, agent, kernel))
     {
       GCN_WARNING ("Will not run GCN kernel because the grid size is zero\n");
       return;
@@ -5539,7 +5571,7 @@ GOMP_OFFLOAD_async_run (struct gomp_offload_session 
*session,
   /* If we get here then the kernel must be OpenMP.  */
   kernel->kind = KIND_OPENMP;
 
-  if (!parse_target_attributes (args, &def, &kla, agent))
+  if (!parse_target_attributes (args, &def, &kla, agent, kernel))
     {
       GCN_WARNING ("Will not run GCN kernel because the grid size is zero\n");
       return;
diff --git 
a/libgomp/testsuite/libgomp.c-c++-common/simt-gcn-num-threads-amdgcn-1.c 
b/libgomp/testsuite/libgomp.c-c++-common/simt-gcn-num-threads-amdgcn-1.c
new file mode 100644
index 000000000000..1da2dbf7ea71
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-gcn-num-threads-amdgcn-1.c
@@ -0,0 +1,33 @@
+/* Check GCN_NUM_THREADS on an AMD GCN device in "simt" offload mode, where
+   each lane of a wavefront is a thread, so it counts lanes and need not be a
+   multiple of the wavefront size.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target 
offload_target_amdgcn } } */
+/* { dg-set-target-env-var GCN_NUM_THREADS "100" } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+int
+main (void)
+{
+  int n = 0, l = 0, count = 0;
+
+  #pragma omp target map(tofrom: n, l, count)
+  #pragma omp parallel
+  {
+    #pragma omp atomic
+    count++;
+    if (omp_get_thread_num () == 0)
+      {
+       n = omp_get_num_threads ();
+       l = omp_get_thread_limit ();
+      }
+  }
+  if (n != 100 || l != 100 || count != 100)
+    abort ();
+
+  return 0;
+}
diff --git 
a/libgomp/testsuite/libgomp.c-c++-common/simt-thread-limit-amdgcn-1.c 
b/libgomp/testsuite/libgomp.c-c++-common/simt-thread-limit-amdgcn-1.c
new file mode 100644
index 000000000000..79c5ef068b6a
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-thread-limit-amdgcn-1.c
@@ -0,0 +1,57 @@
+/* Check thread_limit on an AMD GCN device in "simt" offload mode, where each
+   lane of a wavefront is a thread, so a limit counts lanes, up to 1024, and
+   need not be a multiple of the wavefront size.  */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target 
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+static const int limits[] = { 1, 2, 63, 64, 65, 100, 128, 1000, 1024, 2000 };
+
+int
+main (void)
+{
+  for (int k = 0; k < (int) (sizeof (limits) / sizeof (limits[0])); k++)
+    {
+      int tl = limits[k];
+      int expect = tl < 1024 ? tl : 1024;
+      int n1 = 0, l1 = 0, n2 = 0, l2 = 0, n3 = 0, count = 0;
+
+      /* On the target construct, without teams.  */
+      #pragma omp target thread_limit(tl) map(tofrom: n1, l1, count)
+      #pragma omp parallel
+      {
+       #pragma omp atomic
+       count++;
+       if (omp_get_thread_num () == 0)
+         {
+           n1 = omp_get_num_threads ();
+           l1 = omp_get_thread_limit ();
+         }
+      }
+      if (n1 != expect || l1 != expect || count != expect)
+       abort ();
+
+      /* On the teams construct, also with a larger num_threads.  */
+      #pragma omp target teams num_teams(1) thread_limit(tl) \
+                 map(tofrom: n2, l2, n3)
+      {
+       #pragma omp parallel
+       if (omp_get_thread_num () == 0)
+         {
+           n2 = omp_get_num_threads ();
+           l2 = omp_get_thread_limit ();
+         }
+       #pragma omp parallel num_threads(2048)
+       if (omp_get_thread_num () == 0)
+         n3 = omp_get_num_threads ();
+      }
+      if (n2 != expect || l2 != expect || n3 != expect)
+       abort ();
+    }
+
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/interop-hsa.c 
b/libgomp/testsuite/libgomp.c/interop-hsa.c
index 5819a56a574b..28074f6ed2a3 100644
--- a/libgomp/testsuite/libgomp.c/interop-hsa.c
+++ b/libgomp/testsuite/libgomp.c/interop-hsa.c
@@ -148,6 +148,7 @@ main(int argc, char** argv)
   args.stack_ptr = (int64_t)omp_target_alloc (STACKSIZE, device_num);
   args.arena_size_per_team = ARENASIZE;
   args.stack_size_per_thread = STACKSIZE;
+  args.omp_thread_limit = 0;
 
   /* Build the HSA dispatch packet, and insert it into the queue.  */
   uint64_t packet_id = hsa_queue_load_write_index_relaxed (hsa_queue);

Reply via email to