]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openmp: Don't optimize shared to firstprivate on task with depend clause
authorJakub Jelinek <jakub@redhat.com>
Fri, 18 Dec 2020 20:43:20 +0000 (21:43 +0100)
committerKwok Cheung Yeung <kcy@codesourcery.com>
Fri, 22 Jan 2021 16:19:13 +0000 (08:19 -0800)
The attached testcase is miscompiled, because we optimize shared clauses
to firstprivate when task body can't modify the variable even when the
task has depend clause.  That is wrong, because firstprivate means the
variable will be copied immediately when the task is created, while with
depend clause some other task might change it later before the dependencies
are satisfied and the task should observe the value only after the change.

2020-12-18  Jakub Jelinek  <jakub@redhat.com>

* gimplify.c (struct gimplify_omp_ctx): Add has_depend member.
(gimplify_scan_omp_clauses): Set it to true if OMP_CLAUSE_DEPEND
appears on OMP_TASK.
(gimplify_adjust_omp_clauses_1, gimplify_adjust_omp_clauses): Force
GOVD_WRITTEN on shared variables if task construct has depend clause.

* testsuite/libgomp.c/task-6.c: New test.

(cherry picked from commit 8b60459465252c7d47b58abf83fae2aa84915b03)

gcc/ChangeLog.omp
gcc/gimplify.c
libgomp/ChangeLog.omp
libgomp/testsuite/libgomp.c/task-6.c [new file with mode: 0644]

index 590dc27373af3c8cdd09ff4c6283477661696721..4e2340462bd46f02c0bc61505bad59dc1971ee1e 100644 (file)
@@ -1,3 +1,14 @@
+2021-01-22  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+       Backport from mainline
+       2020-12-18  Jakub Jelinek  <jakub@redhat.com>
+
+       * gimplify.c (struct gimplify_omp_ctx): Add has_depend member.
+       (gimplify_scan_omp_clauses): Set it to true if OMP_CLAUSE_DEPEND
+       appears on OMP_TASK.
+       (gimplify_adjust_omp_clauses_1, gimplify_adjust_omp_clauses): Force
+       GOVD_WRITTEN on shared variables if task construct has depend clause.
+
 2021-01-22  Kwok Cheung Yeung  <kcy@codesourcery.com>
 
        Backport from mainline
index f5769403461673072e61da093f93da24564dd913..b90ba5bbbf16edbc248c3269d15815756c26c4be 100644 (file)
@@ -245,6 +245,7 @@ struct gimplify_omp_ctx
   bool target_firstprivatize_array_bases;
   bool add_safelen1;
   bool order_concurrent;
+  bool has_depend;
   int defaultmap[4];
   hash_map<tree, oacc_array_mapping_info> *decl_data_clause;
 };
@@ -9563,6 +9564,8 @@ gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
              remove = true;
              break;
            }
+         if (code == OMP_TASK)
+           ctx->has_depend = true;
          break;
 
        case OMP_CLAUSE_TO:
@@ -10298,6 +10301,11 @@ gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
            return 0;
        }
       code = OMP_CLAUSE_SHARED;
+      /* Don't optimize shared into firstprivate for read-only vars
+        on tasks with depend clause, we shouldn't try to copy them
+        until the dependencies are satisfied.  */
+      if (gimplify_omp_ctxp->has_depend)
+       flags |= GOVD_WRITTEN;
     }
   else if (flags & GOVD_PRIVATE)
     code = OMP_CLAUSE_PRIVATE;
@@ -10674,6 +10682,10 @@ gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
                  OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
                  OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
                }
+              if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
+                 && ctx->has_depend
+                 && DECL_P (decl))
+               n->value |= GOVD_WRITTEN;
              if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
                  && (n->value & GOVD_WRITTEN) == 0
                  && DECL_P (decl)
index bc500000af86060d8f0be2209db6b3e899459f56..3518bc8eae518cd0d47ffd818b87ab86f7c11326 100644 (file)
@@ -1,3 +1,10 @@
+2021-01-22  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+       Backport from mainline
+       2020-12-18  Jakub Jelinek  <jakub@redhat.com>
+
+       * testsuite/libgomp.c/task-6.c: New test.
+
 2021-01-22  Kwok Cheung Yeung  <kcy@codesourcery.com>
 
        Backport from mainline
diff --git a/libgomp/testsuite/libgomp.c/task-6.c b/libgomp/testsuite/libgomp.c/task-6.c
new file mode 100644 (file)
index 0000000..e5fc758
--- /dev/null
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main ()
+{
+  int x = 0, y = 0;
+  #pragma omp parallel shared(x, y)
+  #pragma omp master
+  {
+    #pragma omp task depend(out:y) shared(x, y)
+    {
+      sleep (1);
+      x = 1;
+      y = 1;
+    }
+    #pragma omp task depend(inout:y) shared(x, y)
+    {
+      if (x != 1 || y != 1)
+       abort ();
+      y++;
+    }
+  }
+  if (x != 1 || y != 2)
+    abort ();
+  x = 0;
+  y = 0;
+  #pragma omp parallel
+  #pragma omp master
+  {
+    #pragma omp task depend(out:y)
+    {
+      sleep (1);
+      x = 1;
+      y = 1;
+    }
+    #pragma omp task depend(inout:y)
+    {
+      if (x != 1 || y != 1)
+       abort ();
+      y++;
+    }
+  }
+  if (x != 1 || y != 2)
+    abort ();
+  return 0;
+}