]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openacc: Remove unused partitioning in "kernels" regions
authorFrederik Harwath <frederik@codesourcery.com>
Tue, 16 Nov 2021 15:17:48 +0000 (16:17 +0100)
committerKwok Cheung Yeung <kcy@codesourcery.com>
Fri, 12 May 2023 18:13:48 +0000 (19:13 +0100)
With the old "kernels" handling, unparallelized regions would
get executed with 1x1x1 partitioning even if the user provided
explicit num_gangs, num_workers clauses etc.

This commit restores this behavior by removing unused partitioning
after assigning the parallelism dimensions to loops.

gcc/ChangeLog:

        * omp-offload.cc (oacc_remove_unused_partitioning): New function
        for removing partitioning that is not used by any loop.
        (oacc_validate_dims): Call oacc_remove_unused_partitioning and
        enable warnings about unused partitioning.

libgomp/ChangeLog:

        * testsuite/libgomp.oacc-c-c++-common/acc_prof-kernels-1.c: Adjust
        expectations.

gcc/ChangeLog.omp
gcc/omp-offload.cc
libgomp/ChangeLog.omp
libgomp/testsuite/libgomp.oacc-c-c++-common/acc_prof-kernels-1.c

index b6a18d13df066938f7b493a3976e50ce4a0e5da4..20cc3f8938f5cf0ba3f7399baa5654b188c64700 100644 (file)
@@ -1,3 +1,10 @@
+2021-11-16  Frederik Harwath <frederik@codesourcery.com>
+
+       * omp-offload.cc (oacc_remove_unused_partitioning): New function
+       for removing partitioning that is not used by any loop.
+       (oacc_validate_dims): Call oacc_remove_unused_partitioning and
+       enable warnings about unused partitioning.
+
 2021-11-16  Frederik Harwath <frederik@codesourcery.com>
 
        * graph.cc (oacc_get_fn_attrib): New declaration.
index c3b833d4fd8540874349319419d38203cfd19342..ed40a04788a9c034eeae601df66375e525855ffa 100644 (file)
@@ -1198,6 +1198,39 @@ oacc_parse_default_dims (const char *dims)
   targetm.goacc.validate_dims (NULL_TREE, oacc_min_dims, -2, 0);
 }
 
+/* Remove parallelism dimensions below LEVEL which are not set in USED
+   from DIMS and emit a warning pointing to the location of FN. */
+
+static void
+oacc_remove_unused_partitioning (tree fn, int *dims, int level, unsigned used)
+{
+
+  bool host_compiler = true;
+#ifdef ACCEL_COMPILER
+  host_compiler = false;
+#endif
+
+  static char const *const axes[] =
+      /* Must be kept in sync with GOMP_DIM enumeration.  */
+      { "gang", "worker", "vector" };
+
+  char removed_partitions[20] = "\0";
+  for (int ix = level >= 0 ? level : 0; ix != GOMP_DIM_MAX; ix++)
+    if (!(used & GOMP_DIM_MASK (ix)) && dims[ix] >= 0)
+      {
+        if (host_compiler)
+          {
+            strcat (removed_partitions, axes[ix]);
+            strcat (removed_partitions, " ");
+          }
+        dims[ix] = -1;
+      }
+  if (removed_partitions[0] != '\0')
+    warning_at (DECL_SOURCE_LOCATION (fn), OPT_Wopenacc_parallelism,
+                "removed %spartitioning from %<kernels%> region",
+                removed_partitions);
+}
+
 /* Validate and update the dimensions for offloaded FN.  ATTRS is the
    raw attribute.  DIMS is an array of dimensions, which is filled in.
    LEVEL is the partitioning level of a routine, or -1 for an offload
@@ -1218,6 +1251,7 @@ oacc_validate_dims (tree fn, tree attrs, int *dims, int level, unsigned used)
   for (ix = 0; ix != GOMP_DIM_MAX; ix++)
     {
       purpose[ix] = TREE_PURPOSE (pos);
+
       tree val = TREE_VALUE (pos);
       dims[ix] = val ? TREE_INT_CST_LOW (val) : -1;
       pos = TREE_CHAIN (pos);
@@ -1227,14 +1261,15 @@ oacc_validate_dims (tree fn, tree attrs, int *dims, int level, unsigned used)
 #ifdef ACCEL_COMPILER
   check = false;
 #endif
+
+  static char const *const axes[] =
+      /* Must be kept in sync with GOMP_DIM enumeration.  */
+      { "gang", "worker", "vector" };
+
   if (check
       && warn_openacc_parallelism
-      && !lookup_attribute ("oacc kernels", DECL_ATTRIBUTES (fn))
-      && !lookup_attribute ("oacc parallel_kernels_graphite", DECL_ATTRIBUTES (fn)))
+      && !lookup_attribute ("oacc kernels", DECL_ATTRIBUTES (fn)))
     {
-      static char const *const axes[] =
-      /* Must be kept in sync with GOMP_DIM enumeration.  */
-       { "gang", "worker", "vector" };
       for (ix = level >= 0 ? level : 0; ix != GOMP_DIM_MAX; ix++)
        if (dims[ix] < 0)
          ; /* Defaulting axis.  */
@@ -1245,14 +1280,20 @@ oacc_validate_dims (tree fn, tree attrs, int *dims, int level, unsigned used)
                      "region contains %s partitioned code but"
                      " is not %s partitioned", axes[ix], axes[ix]);
        else if (!(used & GOMP_DIM_MASK (ix)) && dims[ix] != 1)
+         {
          /* The dimension is explicitly partitioned to non-unity, but
             no use is made within the region.  */
          warning_at (DECL_SOURCE_LOCATION (fn), OPT_Wopenacc_parallelism,
                      "region is %s partitioned but"
                      " does not contain %s partitioned code",
                      axes[ix], axes[ix]);
+          }
     }
 
+  if (lookup_attribute ("oacc parallel_kernels_graphite",
+                         DECL_ATTRIBUTES (fn)))
+    oacc_remove_unused_partitioning  (fn, dims, level, used);
+
   bool changed = targetm.goacc.validate_dims (fn, dims, level, used);
 
   /* Default anything left to 1 or a partitioned default.  */
index 5084714e1d5834844f5a7d183f46cabcaf974330..483c2e284ec3d2ad5fd7394ab8fa42a4886c8d4f 100644 (file)
@@ -1,3 +1,8 @@
+2021-11-16  Frederik Harwath <frederik@codesourcery.com>
+
+       * testsuite/libgomp.oacc-c-c++-common/acc_prof-kernels-1.c: Adjust
+       expectations.
+
 2021-11-16  Frederik Harwath <frederik@codesourcery.com>
 
        * testsuite/libgomp.oacc-fortran/parallel-loop-auto-reduction-2.f90:
index 2cd2c98ddf1cd62c94abac90da14a8ed1fffaf22..67cf87db726b87199cb37dd097ddc8567ce9d9b6 100644 (file)
@@ -25,6 +25,8 @@
 
 #include <acc_prof.h>
 
+/* { dg-skip-if "'kernels' not analyzed by Graphite at -O0" { *-*-* } { "-O0" } { "" } } */
+/* { dg-additional-options "-Wopenacc-parallelism" } */
 
 /* Use explicit 'copyin' clauses, to work around "'firstprivate'
    optimizations", which will cause the value at the point of call to be used
@@ -59,6 +61,7 @@ static int state = -1;
 static acc_device_t acc_device_type;
 static int acc_device_num;
 static int num_gangs, num_workers, vector_length;
+static int real_num_workers;
 static int async;
 
 
@@ -114,12 +117,8 @@ static void cb_enqueue_launch_start (acc_prof_info *prof_info, acc_event_info *e
     assert (event_info->launch_event.num_workers >= 1);
   else
     {
-#ifdef __OPTIMIZE__
-      assert (event_info->launch_event.num_workers == num_workers);
-#else
-      /* See 'num_gangs' above.  */
-      assert (event_info->launch_event.num_workers == 1);
-#endif
+      /* Unused partitioning levels get removed from "kernels" region. */
+      assert (event_info->launch_event.num_workers == real_num_workers);
     }
   if (vector_length < 1)
     assert (event_info->launch_event.vector_length >= 1);
@@ -210,6 +209,7 @@ int main()
   /* Parallelism dimensions: literal.  */
   num_gangs = 30;
   num_workers = 3;
+  real_num_workers = 1;
   vector_length = 5;
   {
 #define N 100
@@ -226,6 +226,8 @@ int main()
     {
       /* { dg-note {beginning 'parloops' part in OpenACC 'kernels' region} {} { target *-*-* } .+1 } */
       for (int i = 0; i < N; ++i)
+      /* { dg-warning "region is worker partitioned but does not contain worker partitioned code" "" { target *-*-* } .-1 } */
+      /* { dg-warning "removed worker partitioning from 'kernels' region" "" { target *-*-* } .-2 } */
        x[i] = i * i;
     }
     if (acc_device_type == acc_device_host)
@@ -244,6 +246,9 @@ int main()
   /* Parallelism dimensions: variable.  */
   num_gangs = 22;
   num_workers = 5;
+  /* No worker loop and hence, in a kernels region, worker partitioning
+     should be removed. */
+  real_num_workers = 1;
   vector_length = 7;
   {
 #define N 100
@@ -260,6 +265,8 @@ int main()
     {
       /* { dg-note {beginning 'parloops' part in OpenACC 'kernels' region} {} { target *-*-* } .+1 } */
       for (int i = 0; i < N; ++i)
+      /* { dg-warning "region is worker partitioned but does not contain worker partitioned code" "" { target *-*-* } .-1 } */
+      /* { dg-warning "removed worker partitioning from 'kernels' region" "" { target *-*-* } .-2 } */
        x[i] = i * i;
     }
     if (acc_device_type == acc_device_host)