]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up handling of captured vars in lambdas in OpenMP clauses [PR93931]
authorJakub Jelinek <jakub@redhat.com>
Thu, 19 Mar 2020 11:22:47 +0000 (12:22 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 7 Apr 2020 18:55:10 +0000 (20:55 +0200)
Without the parser.c change we were ICEing on the testcase, because while the
uses of the captured vars inside of the constructs were replaced with capture
proxy decls, we didn't do that for decls in OpenMP clauses.

With that fixed, we don't ICE anymore, but the testcase is miscompiled and FAILs
at runtime.  This is because the capture proxy decls have DECL_VALUE_EXPR and
during gimplification we were gimplifying those to their DECL_VALUE_EXPRs.
That is fine for shared vars, but for privatized ones we must not do that.
So that is what the cp-gimplify.c changes do.  Had to add a DECL_CONTEXT check
before calling is_capture_proxy because some VAR_DECLs don't have DECL_CONTEXT
set (yet) and is_capture_proxy relies on that being non-NULL always.

2020-03-19  Jakub Jelinek  <jakub@redhat.com>

PR c++/93931
* parser.c (cp_parser_omp_var_list_no_open): Call process_outer_var_ref
on outer_automatic_var_p decls.
* cp-gimplify.c (cxx_omp_disregard_value_expr): Return true also for
capture proxy decls.

* testsuite/libgomp.c++/pr93931.C: New test.

gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c
gcc/cp/parser.c
libgomp/ChangeLog
libgomp/testsuite/libgomp.c++/pr93931.C [new file with mode: 0644]

index 8afe6aca339f7479a7e7496af3937248f4c74b1c..d11c4c62ddf3ec72e5cce2ee6f9ecc77b5bdf56d 100644 (file)
@@ -1,6 +1,14 @@
 2020-04-07  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2020-03-19  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/93931
+       * parser.c (cp_parser_omp_var_list_no_open): Call process_outer_var_ref
+       on outer_automatic_var_p decls.
+       * cp-gimplify.c (cxx_omp_disregard_value_expr): Return true also for
+       capture proxy decls.
+
        2020-03-17  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/90995
index 4be45abca88e20863ac96ad5842e3830a8bafe69..0e15b1c8147511e265dc15b0a0abb3ee42622e36 100644 (file)
@@ -2192,12 +2192,17 @@ cxx_omp_finish_clause (tree c, gimple_seq *)
 bool
 cxx_omp_disregard_value_expr (tree decl, bool shared)
 {
-  return !shared
-        && VAR_P (decl)
-        && DECL_HAS_VALUE_EXPR_P (decl)
-        && DECL_ARTIFICIAL (decl)
-        && DECL_LANG_SPECIFIC (decl)
-        && DECL_OMP_PRIVATIZED_MEMBER (decl);
+  if (shared)
+    return false;
+  if (VAR_P (decl)
+      && DECL_HAS_VALUE_EXPR_P (decl)
+      && DECL_ARTIFICIAL (decl)
+      && DECL_LANG_SPECIFIC (decl)
+      && DECL_OMP_PRIVATIZED_MEMBER (decl))
+    return true;
+  if (VAR_P (decl) && DECL_CONTEXT (decl) && is_capture_proxy (decl))
+    return true;
+  return false;
 }
 
 /* Fold expression X which is used as an rvalue if RVAL is true.  */
index 0219920be8f53153726d9d6a9a177d522117d9bb..00e950d2e9370abbe7057a10bdb3f1eb1cc29fde 100644 (file)
@@ -32563,6 +32563,8 @@ cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
                                           token->location);
            }
        }
+      if (outer_automatic_var_p (decl))
+       decl = process_outer_var_ref (decl, tf_warning_or_error);
       if (decl == error_mark_node)
        ;
       else if (kind != 0)
index f4aa8c63481bd86e5e4f7f2a38eb317d9ab03c72..ee18a9945909cabad96f70ab55d9139232657dab 100644 (file)
@@ -1,3 +1,11 @@
+2020-04-07  Jakub Jelinek  <jakub@redhat.com>
+
+       Backported from mainline
+       2020-03-19  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/93931
+       * testsuite/libgomp.c++/pr93931.C: New test.
+
 2020-03-17  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
diff --git a/libgomp/testsuite/libgomp.c++/pr93931.C b/libgomp/testsuite/libgomp.c++/pr93931.C
new file mode 100644 (file)
index 0000000..4d4232e
--- /dev/null
@@ -0,0 +1,120 @@
+// PR c++/93931
+// { dg-do run }
+// { dg-options "-O2 -std=c++14" }
+
+extern "C" void abort ();
+
+void
+sink (int &x)
+{
+  int *volatile p;
+  p = &x;
+  (*p)++;
+}
+
+int
+foo ()
+{
+  int r = 0;
+  [&r] () {
+#pragma omp parallel for reduction(+ : r)
+    for (int i = 0; i < 1024; ++i)
+      r += i;
+  } ();
+  return r;
+}
+
+int
+bar ()
+{
+  int l = 0;
+  [&l] () {
+#pragma omp parallel for lastprivate (l)
+    for (int i = 0; i < 1024; ++i)
+      l = i;
+  } ();
+  return l;
+}
+
+void
+baz ()
+{
+  int f = 18;
+  [&f] () {
+#pragma omp parallel for firstprivate (f)
+    for (int i = 0; i < 1024; ++i)
+      {
+       sink (f);
+       f += 3;
+       sink (f);
+       if (f != 23)
+         abort ();
+       sink (f);
+       f -= 7;
+       sink (f);
+      }
+  } ();
+  if (f != 18)
+    abort ();
+}
+
+int
+qux ()
+{
+  int r = 0;
+  [&] () {
+#pragma omp parallel for reduction(+ : r)
+    for (int i = 0; i < 1024; ++i)
+      r += i;
+  } ();
+  return r;
+}
+
+int
+corge ()
+{
+  int l = 0;
+  [&] () {
+#pragma omp parallel for lastprivate (l)
+    for (int i = 0; i < 1024; ++i)
+      l = i;
+  } ();
+  return l;
+}
+
+void
+garply ()
+{
+  int f = 18;
+  [&] () {
+#pragma omp parallel for firstprivate (f)
+    for (int i = 0; i < 1024; ++i)
+      {
+       sink (f);
+       f += 3;
+       sink (f);
+       if (f != 23)
+         abort ();
+       sink (f);
+       f -= 7;
+       sink (f);
+      }
+  } ();
+  if (f != 18)
+    abort ();
+}
+
+int
+main ()
+{
+  if (foo () != 1024 * 1023 / 2)
+    abort ();
+  if (bar () != 1023)
+    abort ();
+  baz ();
+  if (qux () != 1024 * 1023 / 2)
+    abort ();
+  if (corge () != 1023)
+    abort ();
+  garply ();
+}