public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10608] openmp: Fix up taskloop reduction ICE if taskloop has no iterations [PR100471]
Date: Tue, 10 May 2022 08:17:41 +0000 (GMT)	[thread overview]
Message-ID: <20220510081741.5B13838346A8@sourceware.org> (raw)

https://gcc.gnu.org/g:49bfa539e240cd769179751f18c472f9a4ea2248

commit r10-10608-g49bfa539e240cd769179751f18c472f9a4ea2248
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue May 11 09:07:47 2021 +0200

    openmp: Fix up taskloop reduction ICE if taskloop has no iterations [PR100471]
    
    When a taskloop doesn't have any iterations, GOMP_taskloop* takes an early
    return, doesn't create any tasks and more importantly, doesn't create
    a taskgroup and doesn't register task reductions.  But, the code emitted
    in the callers assumes task reductions have been registered and performs
    the reduction handling and task reduction unregistration.  The pointer
    to the task reduction private variables is reused, on input it is the alignment
    and only on output it is the pointer, so in the case taskloop with no iterations
    the caller attempts to dereference the alignment value as if it was a pointer
    and crashes.  We could in the early returns register the task reductions
    only to have them looped over and unregistered in the caller, but I think
    it is better to tell the caller there is nothing to task reduce and bypass
    all that.
    
    2021-05-11  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/100471
            * omp-low.c (lower_omp_task_reductions): For OMP_TASKLOOP, if data
            is 0, bypass the reduction loop including
            GOMP_taskgroup_reduction_unregister call.
    
            * taskloop.c (GOMP_taskloop): If GOMP_TASK_FLAG_REDUCTION and not
            GOMP_TASK_FLAG_NOGROUP, when doing early return clear the task
            reduction pointer.
            * testsuite/libgomp.c/task-reduction-4.c: New test.
    
    (cherry picked from commit 98acbb3111fcb5e57d5e63d46c0d92f4e53e3c2a)

Diff:
---
 gcc/omp-low.c                                  | 12 +++++++++++-
 libgomp/taskloop.c                             | 22 +++++++++++++++++-----
 libgomp/testsuite/libgomp.c/task-reduction-4.c | 21 +++++++++++++++++++++
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/gcc/omp-low.c b/gcc/omp-low.c
index 625566acea1..0e46495f724 100644
--- a/gcc/omp-low.c
+++ b/gcc/omp-low.c
@@ -8295,7 +8295,7 @@ lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
   tree num_thr_sz = create_tmp_var (size_type_node);
   tree lab1 = create_artificial_label (UNKNOWN_LOCATION);
   tree lab2 = create_artificial_label (UNKNOWN_LOCATION);
-  tree lab3 = NULL_TREE;
+  tree lab3 = NULL_TREE, lab7 = NULL_TREE;
   gimple *g;
   if (code == OMP_FOR || code == OMP_SECTIONS)
     {
@@ -8360,6 +8360,14 @@ lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
 	      NULL_TREE, NULL_TREE);
   tree data = create_tmp_var (pointer_sized_int_node);
   gimple_seq_add_stmt (end, gimple_build_assign (data, t));
+  if (code == OMP_TASKLOOP)
+    {
+      lab7 = create_artificial_label (UNKNOWN_LOCATION);
+      g = gimple_build_cond (NE_EXPR, data,
+			     build_zero_cst (pointer_sized_int_node),
+			     lab1, lab7);
+      gimple_seq_add_stmt (end, g);
+    }
   gimple_seq_add_stmt (end, gimple_build_label (lab1));
   tree ptr;
   if (TREE_CODE (TYPE_SIZE_UNIT (record_type)) == INTEGER_CST)
@@ -8723,6 +8731,8 @@ lower_omp_task_reductions (omp_context *ctx, enum tree_code code, tree clauses,
       g = gimple_build_call (t, 1, build_fold_addr_expr (avar));
     }
   gimple_seq_add_stmt (end, g);
+  if (lab7)
+    gimple_seq_add_stmt (end, gimple_build_label (lab7));
   t = build_constructor (atype, NULL);
   TREE_THIS_VOLATILE (t) = 1;
   gimple_seq_add_stmt (end, gimple_build_assign (avar, t));
diff --git a/libgomp/taskloop.c b/libgomp/taskloop.c
index c3476249ef9..1d8ba3a4315 100644
--- a/libgomp/taskloop.c
+++ b/libgomp/taskloop.c
@@ -51,20 +51,32 @@ GOMP_taskloop (void (*fn) (void *), void *data, void (*cpyfn) (void *, void *),
 
   /* If parallel or taskgroup has been cancelled, don't start new tasks.  */
   if (team && gomp_team_barrier_cancelled (&team->barrier))
-    return;
+    {
+    early_return:
+      if ((flags & (GOMP_TASK_FLAG_NOGROUP | GOMP_TASK_FLAG_REDUCTION))
+	  == GOMP_TASK_FLAG_REDUCTION)
+	{
+	  struct gomp_data_head { TYPE t1, t2; uintptr_t *ptr; };
+	  uintptr_t *ptr = ((struct gomp_data_head *) data)->ptr;
+	  /* Tell callers GOMP_taskgroup_reduction_register has not been
+	     called.  */
+	  ptr[2] = 0;
+	}
+      return;
+    }
 
 #ifdef TYPE_is_long
   TYPE s = step;
   if (step > 0)
     {
       if (start >= end)
-	return;
+	goto early_return;
       s--;
     }
   else
     {
       if (start <= end)
-	return;
+	goto early_return;
       s++;
     }
   UTYPE n = (end - start + s) / step;
@@ -73,13 +85,13 @@ GOMP_taskloop (void (*fn) (void *), void *data, void (*cpyfn) (void *, void *),
   if (flags & GOMP_TASK_FLAG_UP)
     {
       if (start >= end)
-	return;
+	goto early_return;
       n = (end - start + step - 1) / step;
     }
   else
     {
       if (start <= end)
-	return;
+	goto early_return;
       n = (start - end - step - 1) / -step;
     }
 #endif
diff --git a/libgomp/testsuite/libgomp.c/task-reduction-4.c b/libgomp/testsuite/libgomp.c/task-reduction-4.c
new file mode 100644
index 00000000000..7ca1d02fa19
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/task-reduction-4.c
@@ -0,0 +1,21 @@
+/* PR middle-end/100471 */
+
+extern void abort (void);
+
+int c;
+
+int
+main ()
+{
+#pragma omp parallel
+#pragma omp single
+  {
+    int r = 0, i;
+    #pragma omp taskloop reduction(+:r)
+    for (i = 0; i < c; i++)
+      r++;
+    if (r != 0)
+      abort ();
+  }
+  return 0;
+}


                 reply	other threads:[~2022-05-10  8:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220510081741.5B13838346A8@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).