public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [gomp] Fix privatization of #pragma omp for iteration var(s) for OpenMP 3.0
@ 2008-06-27 14:17 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2008-06-27 14:17 UTC (permalink / raw)
  To: gcc-patches

Hi!

When the iteration var(s) aren't mentioned in private or lastprivate
clauses of #pragma omp for, we used to optimize if the iteration
variable was already private (such as local variable or argument
in case of an orphaned construct, or private within containing
#pragma omp parallel) by reusing the private var for the iterator.
That was ok for OpenMP 2.5, which said the value of the original
list item after privatization is undefined.  But OpenMP 3.0 mandates
that it doesn't change if you don't directly fiddle with it (through
pointers, etc.), so this optimization is no longer valid for OpenMP 3.0
- we have to privatize the iteration variables always.

Regtested on x86_64-linux.

2008-06-27  Jakub Jelinek  <jakub@redhat.com>

	* gimplify.c (omp_is_private): Don't return true if decl
	is not already private on #pragma omp for or #pragma omp parallel for.

	* gcc.dg/gomp/pr27388-3.c: Adjust dg-final.

	* testsuite/libgomp.c/loop-10.c: New test.
	* libgomp.c/loop-3.c (main): Add lastprivate clause.
	* libgomp.c++/loop-6.C (main): Likewise.

--- gcc/gimplify.c.jj	2008-06-23 08:40:35.000000000 +0200
+++ gcc/gimplify.c	2008-06-27 13:39:05.000000000 +0200
@@ -5049,15 +5049,16 @@ omp_is_private (struct gimplify_omp_ctx 
 	    error ("iteration variable %qs should not be reduction",
 		   IDENTIFIER_POINTER (DECL_NAME (decl)));
 	}
-      return true;
+      return (ctx == gimplify_omp_ctxp
+	      || (ctx->region_type == ORT_COMBINED_PARALLEL
+		  && gimplify_omp_ctxp->outer_context == ctx));
     }
 
   if (ctx->region_type != ORT_WORKSHARE)
     return false;
   else if (ctx->outer_context)
     return omp_is_private (ctx->outer_context, decl);
-  else
-    return !is_global_var (decl);
+  return false;
 }
 
 /* Return true if DECL is private within a parallel region
--- libgomp/testsuite/libgomp.c/loop-10.c.jj	2008-06-27 13:30:27.000000000 +0200
+++ libgomp/testsuite/libgomp.c/loop-10.c	2008-06-27 13:40:42.000000000 +0200
@@ -0,0 +1,30 @@
+extern void abort (void);
+
+int i = 8;
+
+int main (void)
+{
+  int j = 7, k = 0;
+  #pragma omp for
+  for (i = 0; i < 10; i++)
+    ;
+  #pragma omp for
+  for (j = 0; j < 10; j++)
+    ;
+  /* OpenMP 3.0 newly guarantees that the original list items can't
+     be shared with the privatized omp for iterators, even when
+     the original list items are already private.  */
+  if (i != 8 || j != 7)
+    abort ();
+  #pragma omp parallel private (i) reduction (+:k)
+  {
+    i = 6;
+    #pragma omp for
+    for (i = 0; i < 10; i++)
+      ;
+    k = (i != 6);
+  }
+  if (k)
+    abort ();
+  return 0;
+}
--- libgomp/testsuite/libgomp.c/loop-3.c.jj	2006-10-05 00:24:40.000000000 +0200
+++ libgomp/testsuite/libgomp.c/loop-3.c	2008-06-27 13:42:11.000000000 +0200
@@ -8,10 +8,11 @@ static int test(void)
   return ++count > 0;
 }
 
+int i;
+
 int main()
 {
-  int i;
-  #pragma omp for
+  #pragma omp for lastprivate (i)
   for (i = 0; i < 10; ++i)
     {
       if (test())
--- libgomp/testsuite/libgomp.c++/loop-6.C.jj	2006-10-05 00:24:39.000000000 +0200
+++ libgomp/testsuite/libgomp.c++/loop-6.C	2008-06-27 13:49:28.000000000 +0200
@@ -8,10 +8,11 @@ static int test(void)
   return ++count > 0;
 }
 
+int i;
+
 int main()
 {
-  int i;
-  #pragma omp for
+  #pragma omp for lastprivate (i)
   for (i = 0; i < 10; ++i)
     {
       if (test())
--- gcc/testsuite/gcc.dg/gomp/pr27388-3.c.jj	2008-06-27 13:49:54.000000000 +0200
+++ gcc/testsuite/gcc.dg/gomp/pr27388-3.c	2008-06-27 13:49:54.000000000 +0200
@@ -19,5 +19,5 @@ foo (void)
   }
 }
 
-/* { dg-final { scan-tree-dump-times "omp for\[^\\n\]*private" 0 "omplower" } } */
+/* { dg-final { scan-tree-dump-times "omp for\[^\\n\]*private" 2 "omplower" } } */
 /* { dg-final { cleanup-tree-dump "omplower" } } */

	Jakub

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-06-27 14:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-27 14:17 [gomp] Fix privatization of #pragma omp for iteration var(s) for OpenMP 3.0 Jakub Jelinek

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).