[gomp5] Fix task reduction VLA handling

Message ID 20181010111734.GU11625@tucnak
State New
Headers show
Series
  • [gomp5] Fix task reduction VLA handling
Related show

Commit Message

Jakub Jelinek Oct. 10, 2018, 11:17 a.m.
Hi!

This patch adds testcases to cover the case when C/C++ array task reductions
are used with VLAs, either whole VLAs or array sections from them.

Tested on x86_64-linux, committed to gomp-5_0-branch.

2018-10-10  Jakub Jelinek  <jakub@redhat.com>

	* omp-low.c (lower_rec_input_clauses): Handle VLAs properly.
	(lower_omp_task_reductions): Likewise.
	* gimplify.c (enum omp_region_type): Add ORT_TASKGROUP.
	(gimple_add_tmp_var, omp_firstprivatize_variable, omp_notice_variable,
	omp_is_private, omp_check_private, gimplify_omp_depend): Handle
	ORT_TASKGROUP like ORT_WORKSHARE.
	(omp_add_variable): Don't add private/firstprivate for VLAs in
	ORT_TASKGROUP.
	(gimplify_expr) <case OMP_TASKGROUP>: Move handling into a separate
	case, make sure to scan omp clauses before gimplifying body.

	* testsuite/libgomp.c-c++-common/task-reduction-5.c (size_t): New
	typedef.
	(bar): Use it instead of __SIZE_TYPE__ directly.
	(foo): Likewise.  Use (~(size_t) 0) instead of __SIZE_MAX__.
	* testsuite/libgomp.c/task-reduction-1.c: New test.
	* testsuite/libgomp.c++/task-reduction-7.C: New test.


	Jakub

Patch

--- gcc/omp-low.c.jj	2018-10-09 14:19:54.672380754 +0200
+++ gcc/omp-low.c	2018-10-09 17:56:27.620900824 +0200
@@ -3909,7 +3909,10 @@  lower_rec_input_clauses (tree clauses, g
 		  if (TREE_CODE (orig_var) == INDIRECT_REF)
 		    x = build_simple_mem_ref (x);
 		  else if (TREE_CODE (orig_var) == ADDR_EXPR)
-		    x = build_fold_addr_expr (x);
+		    {
+		      if (var == TREE_OPERAND (orig_var, 0))
+			x = build_fold_addr_expr (x);
+		    }
 		  bias = fold_convert (sizetype, bias);
 		  x = fold_convert (ptr_type_node, x);
 		  x = fold_build2_loc (clause_loc, POINTER_PLUS_EXPR,
@@ -6798,6 +6801,7 @@  lower_omp_task_reductions (omp_context *
 		var = TREE_OPERAND (var, 0);
 	      else if (TREE_CODE (var) == INDIRECT_REF)
 		var = TREE_OPERAND (var, 0);
+	      tree orig_var = var;
 	      if (is_variable_sized (var))
 		{
 		  gcc_assert (DECL_HAS_VALUE_EXPR_P (var));
@@ -6807,7 +6811,9 @@  lower_omp_task_reductions (omp_context *
 		  gcc_assert (DECL_P (var));
 		}
 	      t = ref = maybe_lookup_decl_in_outer_ctx (var, ctx);
-	      if (TREE_CODE (v) == ADDR_EXPR)
+	      if (orig_var != var)
+		gcc_assert (TREE_CODE (v) == ADDR_EXPR);
+	      else if (TREE_CODE (v) == ADDR_EXPR)
 		t = build_fold_addr_expr (t);
 	      else if (TREE_CODE (v) == INDIRECT_REF)
 		t = build_fold_indirect_ref (t);
--- gcc/gimplify.c.jj	2018-10-09 14:13:25.243928507 +0200
+++ gcc/gimplify.c	2018-10-10 11:55:27.444973925 +0200
@@ -122,6 +122,7 @@  enum gimplify_omp_var_data
 enum omp_region_type
 {
   ORT_WORKSHARE = 0x00,
+  ORT_TASKGROUP = 0x01,
   ORT_SIMD 	= 0x04,
 
   ORT_PARALLEL	= 0x08,
@@ -759,6 +760,7 @@  gimple_add_tmp_var (tree tmp)
 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
 	  while (ctx
 		 && (ctx->region_type == ORT_WORKSHARE
+		     || ctx->region_type == ORT_TASKGROUP
 		     || ctx->region_type == ORT_SIMD
 		     || ctx->region_type == ORT_ACC))
 	    ctx = ctx->outer_context;
@@ -6674,6 +6676,7 @@  omp_firstprivatize_variable (struct gimp
 	    omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
 	}
       else if (ctx->region_type != ORT_WORKSHARE
+	       && ctx->region_type != ORT_TASKGROUP
 	       && ctx->region_type != ORT_SIMD
 	       && ctx->region_type != ORT_ACC
 	       && !(ctx->region_type & ORT_TARGET_DATA))
@@ -6787,7 +6790,7 @@  omp_add_variable (struct gimplify_omp_ct
 	 replacement is private, else FIRSTPRIVATE since we'll need the
 	 address of the original variable either for SHARED, or for the
 	 copy into or out of the context.  */
-      if (!(flags & GOVD_LOCAL))
+      if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
 	{
 	  if (flags & GOVD_MAP)
 	    nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
@@ -7316,6 +7319,7 @@  omp_notice_variable (struct gimplify_omp
   if (n == NULL)
     {
       if (ctx->region_type == ORT_WORKSHARE
+	  || ctx->region_type == ORT_TASKGROUP
 	  || ctx->region_type == ORT_SIMD
 	  || ctx->region_type == ORT_ACC
 	  || (ctx->region_type & ORT_TARGET_DATA) != 0)
@@ -7437,6 +7441,7 @@  omp_is_private (struct gimplify_omp_ctx
     }
 
   if (ctx->region_type != ORT_WORKSHARE
+      && ctx->region_type != ORT_TASKGROUP
       && ctx->region_type != ORT_SIMD
       && ctx->region_type != ORT_ACC)
     return false;
@@ -7494,6 +7499,7 @@  omp_check_private (struct gimplify_omp_c
 	}
     }
   while (ctx->region_type == ORT_WORKSHARE
+	 || ctx->region_type == ORT_TASKGROUP
 	 || ctx->region_type == ORT_SIMD
 	 || ctx->region_type == ORT_ACC);
   return false;
@@ -7693,6 +7699,7 @@  gimplify_omp_depend (tree *list_p, gimpl
 	  struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
 	  while (ctx
 		 && (ctx->region_type == ORT_WORKSHARE
+		     || ctx->region_type == ORT_TASKGROUP
 		     || ctx->region_type == ORT_SIMD
 		     || ctx->region_type == ORT_ACC))
 	    ctx = ctx->outer_context;
@@ -12831,7 +12838,6 @@  gimplify_expr (tree *expr_p, gimple_seq
 
 	case OMP_SECTION:
 	case OMP_MASTER:
-	case OMP_TASKGROUP:
 	case OMP_ORDERED:
 	case OMP_CRITICAL:
 	  {
@@ -12847,24 +12853,6 @@  gimplify_expr (tree *expr_p, gimple_seq
 	      case OMP_MASTER:
 	        g = gimple_build_omp_master (body);
 		break;
-	      case OMP_TASKGROUP:
-		{
-		  gimple_seq cleanup = NULL;
-		  tree fn
-		    = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
-		  g = gimple_build_call (fn, 0);
-		  gimple_seq_add_stmt (&cleanup, g);
-		  g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
-		  body = NULL;
-		  gimple_seq_add_stmt (&body, g);
-		  tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
-		  gimplify_scan_omp_clauses (pclauses, pre_p, ORT_WORKSHARE,
-					     OMP_TASKGROUP);
-		  gimplify_adjust_omp_clauses (pre_p, body, pclauses,
-					       OMP_TASKGROUP);
-		  g = gimple_build_omp_taskgroup (body, *pclauses);
-		}
-		break;
 	      case OMP_ORDERED:
 		g = gimplify_omp_ordered (*expr_p, body);
 		break;
@@ -12884,6 +12872,28 @@  gimplify_expr (tree *expr_p, gimple_seq
 	    gimplify_seq_add_stmt (pre_p, g);
 	    ret = GS_ALL_DONE;
 	    break;
+	  }
+
+	case OMP_TASKGROUP:
+	  {
+	    gimple_seq body = NULL;
+
+	    tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
+	    gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
+				       OMP_TASKGROUP);
+	    gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
+	    gimplify_and_add (OMP_BODY (*expr_p), &body);
+	    gimple_seq cleanup = NULL;
+	    tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
+	    gimple *g = gimple_build_call (fn, 0);
+	    gimple_seq_add_stmt (&cleanup, g);
+	    g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
+	    body = NULL;
+	    gimple_seq_add_stmt (&body, g);
+	    g = gimple_build_omp_taskgroup (body, *pclauses);
+	    gimplify_seq_add_stmt (pre_p, g);
+	    ret = GS_ALL_DONE;
+	    break;
 	  }
 
 	case OMP_ATOMIC:
--- libgomp/testsuite/libgomp.c-c++-common/task-reduction-5.c.jj	2018-10-09 13:56:38.907851288 +0200
+++ libgomp/testsuite/libgomp.c-c++-common/task-reduction-5.c	2018-10-09 15:23:21.860406224 +0200
@@ -1,3 +1,4 @@ 
+typedef __SIZE_TYPE__ size_t;
 extern
 #ifdef __cplusplus
 "C"
@@ -7,7 +8,7 @@  void abort (void);
 int *q;
 
 void
-bar (int *p, int *r, int *t, int s, __SIZE_TYPE__ u)
+bar (int *p, int *r, int *t, int s, size_t u)
 {
   #pragma omp task in_reduction (*: p[0], q[0], r[s - 1], t[0:u + 1])
   {
@@ -20,7 +21,7 @@  bar (int *p, int *r, int *t, int s, __SI
 }
 
 void
-foo (int *p, int *r, int *t, int s, __SIZE_TYPE__ u)
+foo (int *p, int *r, int *t, int s, size_t u)
 {
   int *p2 = p;
   #pragma omp taskgroup task_reduction (*: p[0], q[0], r[s], t[0:u + 1])
@@ -38,7 +39,7 @@  foo (int *p, int *r, int *t, int s, __SI
       t[0] *= 10;
       t[1] *= 11;
     }
-    u = __SIZE_MAX__ / 4;
+    u = (~(size_t) 0) / 4;
     s++;
     p2 = (int *) 0;
     q = (int *) 0;
--- libgomp/testsuite/libgomp.c/task-reduction-1.c.jj	2018-10-09 18:16:16.409159015 +0200
+++ libgomp/testsuite/libgomp.c/task-reduction-1.c	2018-10-09 18:15:55.525504721 +0200
@@ -0,0 +1,137 @@ 
+typedef __SIZE_TYPE__ size_t;
+extern void abort (void);
+
+void
+bar (int *a, int *b, int *c, int (*d)[2], int (*e)[4], size_t n, int f[1][n], int g[1][n * 2])
+{
+  #pragma omp task in_reduction (*: a[:n], b[3:n], c[n:n], d[0][:n], e[0][1:n], f[0][:], g[0][1:n])
+  {
+    a[0] *= 12;
+    a[1] *= 13;
+    b[3] *= 14;
+    b[4] *= 15;
+    c[n] *= 16;
+    c[n + 1] *= 17;
+    d[0][0] *= 18;
+    d[0][1] *= 19;
+    e[0][1] *= 20;
+    e[0][2] *= 21;
+    f[0][0] *= 22;
+    f[0][1] *= 23;
+    g[0][1] *= 24;
+    g[0][2] *= 25;
+  }
+}
+
+void
+foo (size_t n, void *x, void *y, int f[1][n], int g[1][n * 2])
+{
+  int a[n], b[n + 3], c[2 * n];
+  int (*d)[n] = (int (*)[n]) x;
+  int (*e)[n * 2] = (int (*)[n * 2]) y;
+  int i;
+  for (i = 0; i < n; i++)
+    {
+      a[i] = 1;
+      b[i + 3] = 1;
+      c[i + n] = 1;
+      d[0][i] = 1;
+      e[0][i + 1] = 1;
+      f[0][i] = 1;
+      g[0][i + 1] = 1;
+    }
+  #pragma omp taskgroup task_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f[0][:], g[0][1:n])
+  {
+    bar (a, b, c, (int (*)[2]) d, (int (*)[4]) e, n, f, g);
+    #pragma omp task in_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f[0][:], g[0][1:n])
+    {
+      a[0] *= 2;
+      a[1] *= 3;
+      b[3] *= 4;
+      b[4] *= 5;
+      c[n] *= 6;
+      c[n + 1] *= 7;
+      d[0][0] *= 8;
+      d[0][1] *= 9;
+      e[0][1] *= 10;
+      e[0][2] *= 11;
+      f[0][0] *= 12;
+      f[0][1] *= 13;
+      g[0][1] *= 14;
+      g[0][2] *= 15;
+    }
+    n = 0;
+  }
+  if (a[0] != 24 || a[1] != 39 || b[3] != 56 || b[4] != 75)
+    abort ();
+  if (c[2] != 96 || c[3] != 119 || d[0][0] != 144 || d[0][1] != 171)
+    abort ();
+  if (e[0][1] != 200 || e[0][2] != 231 || f[0][0] != 264 || f[0][1] != 299)
+    abort ();
+  if (g[0][1] != 336 || g[0][2] != 375)
+    abort ();
+}
+
+void
+baz (size_t n, void *x, void *y, int f[1][n], int g[1][n * 2])
+{
+  int a[n], b[n + 3], c[2 * n];
+  int (*d)[n] = (int (*)[n]) x;
+  int (*e)[n * 2] = (int (*)[n * 2]) y;
+  int i;
+  for (i = 0; i < n; i++)
+    {
+      a[i] = 1;
+      b[i + 3] = 1;
+      c[i + n] = 1;
+      d[0][i] = 1;
+      e[0][i + 1] = 1;
+      f[0][i] = 1;
+      g[0][i + 1] = 1;
+    }
+  #pragma omp parallel num_threads(2)
+  #pragma omp master
+  #pragma omp taskgroup task_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f[0][:], g[0][1:n])
+  {
+    bar (a, b, c, (int (*)[2]) d, (int (*)[4]) e, n, f, g);
+    #pragma omp task in_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f[0][:], g[0][1:n])
+    {
+      a[0] *= 2;
+      a[1] *= 3;
+      b[3] *= 4;
+      b[4] *= 5;
+      c[n] *= 6;
+      c[n + 1] *= 7;
+      d[0][0] *= 8;
+      d[0][1] *= 9;
+      e[0][1] *= 10;
+      e[0][2] *= 11;
+      f[0][0] *= 12;
+      f[0][1] *= 13;
+      g[0][1] *= 14;
+      g[0][2] *= 15;
+    }
+    n = 0;
+  }
+  if (a[0] != 24 || a[1] != 39 || b[3] != 56 || b[4] != 75)
+    abort ();
+  if (c[2] != 96 || c[3] != 119 || d[0][0] != 144 || d[0][1] != 171)
+    abort ();
+  if (e[0][1] != 200 || e[0][2] != 231 || f[0][0] != 264 || f[0][1] != 299)
+    abort ();
+  if (g[0][1] != 336 || g[0][2] != 375)
+    abort ();
+}
+
+int
+main ()
+{
+  int d[1][2], e[1][4], f[1][2], g[1][4];
+  volatile int two;
+  two = 2;
+  #pragma omp parallel num_threads (2)
+  #pragma omp master
+  foo (two, (void *) d, (void *) e, f, g);
+  baz (two, (void *) d, (void *) e, f, g);
+  return 0;
+}
--- libgomp/testsuite/libgomp.c++/task-reduction-7.C.jj	2018-10-10 12:29:44.335589619 +0200
+++ libgomp/testsuite/libgomp.c++/task-reduction-7.C	2018-10-10 12:19:11.890170681 +0200
@@ -0,0 +1,145 @@ 
+typedef __SIZE_TYPE__ size_t;
+extern "C" void abort ();
+
+void
+bar (int *a, int *b, int *c, int (*d)[2], int (*e)[4], int *f, int *g, size_t n)
+{
+  #pragma omp task in_reduction (*: a[:n], b[3:n], c[n:n], d[0][:n], e[0][1:n], f[:n], g[1:n])
+  {
+    a[0] *= 12;
+    a[1] *= 13;
+    b[3] *= 14;
+    b[4] *= 15;
+    c[n] *= 16;
+    c[n + 1] *= 17;
+    d[0][0] *= 18;
+    d[0][1] *= 19;
+    e[0][1] *= 20;
+    e[0][2] *= 21;
+    f[0] *= 22;
+    f[1] *= 23;
+    g[1] *= 24;
+    g[2] *= 25;
+  }
+}
+
+void
+foo (size_t n, void *x, void *y)
+{
+  int a[n], b[n + 3], c[2 * n];
+  int (*d)[n] = (int (*)[n]) x;
+  int (*e)[n * 2] = (int (*)[n * 2]) y;
+  int fb[n], gb[n * 2];
+  int (&f)[n] = fb;
+  int (&g)[n * 2] = gb;
+  int i;
+  for (i = 0; i < n; i++)
+    {
+      a[i] = 1;
+      b[i + 3] = 1;
+      c[i + n] = 1;
+      d[0][i] = 1;
+      e[0][i + 1] = 1;
+      f[i] = 1;
+      g[i + 1] = 1;
+    }
+  #pragma omp taskgroup task_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f, g[1:n])
+  {
+    bar (a, b, c, (int (*)[2]) d, (int (*)[4]) e, &f[0], &g[0], n);
+    #pragma omp task in_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f, g[1:n])
+    {
+      a[0] *= 2;
+      a[1] *= 3;
+      b[3] *= 4;
+      b[4] *= 5;
+      c[n] *= 6;
+      c[n + 1] *= 7;
+      d[0][0] *= 8;
+      d[0][1] *= 9;
+      e[0][1] *= 10;
+      e[0][2] *= 11;
+      f[0] *= 12;
+      f[1] *= 13;
+      g[1] *= 14;
+      g[2] *= 15;
+    }
+    n = 0;
+  }
+  if (a[0] != 24 || a[1] != 39 || b[3] != 56 || b[4] != 75)
+    abort ();
+  if (c[2] != 96 || c[3] != 119 || d[0][0] != 144 || d[0][1] != 171)
+    abort ();
+  if (e[0][1] != 200 || e[0][2] != 231 || f[0] != 264 || f[1] != 299)
+    abort ();
+  if (g[1] != 336 || g[2] != 375)
+    abort ();
+}
+
+void
+baz (size_t n, void *x, void *y)
+{
+  int a[n], b[n + 3], c[2 * n];
+  int (*d)[n] = (int (*)[n]) x;
+  int (*e)[n * 2] = (int (*)[n * 2]) y;
+  int fb[n], gb[n * 2];
+  int i;
+  for (i = 0; i < n; i++)
+    {
+      a[i] = 1;
+      b[i + 3] = 1;
+      c[i + n] = 1;
+      d[0][i] = 1;
+      e[0][i + 1] = 1;
+      fb[i] = 1;
+      gb[i + 1] = 1;
+    }
+  #pragma omp parallel num_threads(2)
+  #pragma omp master
+  {
+    int (&f)[n] = fb;
+    int (&g)[n * 2] = gb;
+    #pragma omp taskgroup task_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f, g[1:n])
+    {
+      bar (a, b, c, (int (*)[2]) d, (int (*)[4]) e, &f[0], &g[0], n);
+      #pragma omp task in_reduction (*: a, b[3:n], c[n:n], d[0][:n], e[0][1:n], f, g[1:n])
+      {
+	a[0] *= 2;
+	a[1] *= 3;
+	b[3] *= 4;
+	b[4] *= 5;
+	c[n] *= 6;
+	c[n + 1] *= 7;
+	d[0][0] *= 8;
+	d[0][1] *= 9;
+	e[0][1] *= 10;
+	e[0][2] *= 11;
+	f[0] *= 12;
+	f[1] *= 13;
+	g[1] *= 14;
+	g[2] *= 15;
+      }
+      n = 0;
+    }
+  }
+  if (a[0] != 24 || a[1] != 39 || b[3] != 56 || b[4] != 75)
+    abort ();
+  if (c[2] != 96 || c[3] != 119 || d[0][0] != 144 || d[0][1] != 171)
+    abort ();
+  if (e[0][1] != 200 || e[0][2] != 231 || fb[0] != 264 || fb[1] != 299)
+    abort ();
+  if (gb[1] != 336 || gb[2] != 375)
+    abort ();
+}
+
+int
+main ()
+{
+  int d[2], e[4];
+  volatile int two;
+  two = 2;
+  #pragma omp parallel num_threads (2)
+  #pragma omp master
+  foo (two, (void *) d, (void *) e);
+  baz (two, (void *) d, (void *) e);
+  return 0;
+}