]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
omp-expand.cc: Fix wrong code with non-rectangular loop nest [PR111017]
authorTobias Burnus <tobias@codesourcery.com>
Sat, 19 Aug 2023 05:49:06 +0000 (07:49 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Sat, 19 Aug 2023 05:49:06 +0000 (07:49 +0200)
Before commit r12-5295-g47de0b56ee455e, all gimple_build_cond in
expand_omp_for_* were inserted with
  gsi_insert_before (gsi_p, cond_stmt, GSI_SAME_STMT);
except the one dealing with the multiplicative factor that was
  gsi_insert_after (gsi, cond_stmt, GSI_CONTINUE_LINKING);

That commit for PR103208 fixed the issue of some missing regimplify of
operands of GIMPLE_CONDs by moving the condition handling to the new function
expand_omp_build_cond. While that function has an 'bool after = false'
argument to switch between the two variants.

However, all callers ommited this argument. This commit reinstates the
prior behavior by passing 'true' for the factor != 0 condition, fixing
the included testcase.

PR middle-end/111017
gcc/
* omp-expand.cc (expand_omp_for_init_vars): Pass after=true
to expand_omp_build_cond for 'factor != 0' condition, resulting
in pre-r12-5295-g47de0b56ee455e code for the gimple insert.

libgomp/
* testsuite/libgomp.c-c++-common/non-rect-loop-1.c: New test.

gcc/omp-expand.cc
libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c [new file with mode: 0644]

index db58b3cb49b667d20f135fae7c1e7bb8fd666d34..1a4d625fea3075989f47c8dd23966039f42142b3 100644 (file)
@@ -2562,7 +2562,8 @@ expand_omp_for_init_vars (struct omp_for_data *fd, gimple_stmt_iterator *gsi,
              tree factor = fd->factor;
              gcond *cond_stmt
                = expand_omp_build_cond (gsi, NE_EXPR, factor,
-                                        build_zero_cst (TREE_TYPE (factor)));
+                                        build_zero_cst (TREE_TYPE (factor)),
+                                        true);
              edge e = split_block (gsi_bb (*gsi), cond_stmt);
              basic_block bb0 = e->src;
              e->flags = EDGE_TRUE_VALUE;
diff --git a/libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c b/libgomp/testsuite/libgomp.c-c++-common/non-rect-loop-1.c
new file mode 100644 (file)
index 0000000..fbd462b
--- /dev/null
@@ -0,0 +1,72 @@
+/* PR middle-end/111017  */
+
+#include <omp.h>
+
+#define DIM 32
+#define N (DIM*DIM)
+
+int
+main ()
+{
+  int a[N], b[N], c[N];
+  int dim = DIM;
+
+  for (int i = 0; i < N; i++)
+    {
+      a[i] = 3*i;
+      b[i] = 7*i;
+      c[i] = 42;
+    }
+
+  #pragma omp parallel for collapse(2)
+  for (int i = 0; i < DIM; i++)
+    for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+      c[j] = a[j] + b[j];
+
+  for (int i = 0; i < DIM; i++)
+    for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+      if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j)
+       __builtin_abort ();
+  for (int i = 0; i < N; i++)
+    c[i] = 42;
+
+  #pragma omp parallel for collapse(2)
+  for (int i = 0; i < dim; i++)
+    for (int j = (i*dim); j < (i*dim + dim); j++)
+      c[j] = a[j] + b[j];
+
+  for (int i = 0; i < DIM; i++)
+    for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+      if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j)
+       __builtin_abort ();
+  for (int i = 0; i < N; i++)
+    c[i] = 42;
+
+  for (int dev = 0; dev <= omp_get_num_devices(); dev++)
+    {
+      #pragma omp target teams loop device(dev) map(to:a,b) map(from:c)
+      for (int i = 0; i < DIM; i++)
+       for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+         c[j] = a[j] + b[j];
+
+      for (int i = 0; i < DIM; i++)
+       for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+         if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j)
+           __builtin_abort ();
+      for (int i = 0; i < N; i++)
+       c[i] = 42;
+
+      #pragma omp target teams loop device(dev) map(to:a,b) map(from:c)
+      for (int i = 0; i < dim; i++)
+       for (int j = (i*dim); j < (i*dim + dim); j++)
+         c[j] = a[j] + b[j];
+
+      for (int i = 0; i < DIM; i++)
+       for (int j = (i*DIM); j < (i*DIM + DIM); j++)
+         if (c[j] != a[j] + b[j] || c[j] != 3*j +7*j)
+           __builtin_abort ();
+      for (int i = 0; i < N; i++)
+       c[i] = 42;
+    }
+  return 0;
+}