[gomp4.1] C++ iterators with #omp ordered depend(sink:)

Aldy Hernandez aldyh@redhat.com
Thu Jul 16 05:52:00 GMT 2015


On 07/15/2015 08:21 AM, Jakub Jelinek wrote:
> On Wed, Jul 15, 2015 at 07:47:12AM -0700, Aldy Hernandez wrote:
>> This fixes the problem with C++ iterators not working as sink() iterator
>> variables.
>>
>> OK for branch?
>
> I wonder (though not 100% sure about) if, because we really need the
> OMP_FOR_ORIG_DECLS on OMP_FOR only, no other construct can be ordered,
> it wouldn't make more sense to make
> #define OMP_FOR_ORIG_DECLS(NODE)   TREE_OPERAND (OMP_FOR_CHECK (NODE), 6)
> (note, FOR rather than LOOP) and only use 7 arguments on OMP_FOR and
> not all the other loop constructs.  That would of course mean
> guarding all setters and users of OMP_FOR_ORIG_DECLS (x) with
> if (TREE_CODE (x) == OMP_FOR), but that is just 2 locations.

As I said on IRC, this looks ugly, but I can see why you wouldn't want 
the extra word on all loop variants.  I've implemented it as requested.

>
>> --- a/gcc/gimplify.c
>> +++ b/gcc/gimplify.c
>> @@ -7311,7 +7311,11 @@ gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
>>         gcc_assert (DECL_P (decl));
>>         gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
>>   		  || POINTER_TYPE_P (TREE_TYPE (decl)));
>> -      gimplify_omp_ctxp->iter_vars.quick_push (decl);
>> +      if (OMP_FOR_ORIG_DECLS (for_stmt))
>> +	gimplify_omp_ctxp->iter_vars.quick_push
>> +	  (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
>> +      else
>> +	gimplify_omp_ctxp->iter_vars.quick_push (decl);
>
> No matter the decision about above, I think you want to quick_push
> here twice for each decl (or have two vectors, but a single one is probably
> better), and rewrite all uses of the orig decl to the new decl.
> Note, gimplify_omp_for can also do similar replacement of an orig decl
> with a new decl (even for OMP_FOR_ORIG_DECLS == NULL case aka C),
> when the iterator var is addressable.

I added various more tests as suggested-- one that takes the address of 
an iterator, one with non-dependent iterator in a template, and one with 
a dependent iterator in a template.  It took some massaging in pt.c, but 
everything is working now.

>
>> +#pragma omp parallel for ordered(1)
>> +  for (it = v.begin(); it < v.end(); ++it)
>> +    {
>> +#pragma omp ordered depend(sink:it)
>> +    std::cout << *it << '\n';
>> +    }
>> +}
>
> I'd try to avoid adding such blatanly wrong testcases, even just
> for parsing, perhaps one day we'll want to diagnose it.
> Depending on the current iteration is just wrong (like on a future one).
> And with no depend(source) it will just hang.
> So, can you make it depend(sink:it-1) and add
> #pragma omp ordered depend(source) after it?

Sure.  I also moved the test form libgomp to the C++ testsuite.

Tested with languages=c,c++ on x86-64 Linux.

OK for branch?

-------------- next part --------------
commit f55eced4ac6b045101a90914a8f27e99d26cfddf
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Tue Jul 14 19:23:09 2015 -0700

    	* gimplify.c (gimplify_omp_for): Use OMP_FOR_ORIG_DECLS.
    	* tree.def (omp_for): Add new operand.
    	* tree.h (OMP_FOR_ORIG_DECLS): New macro.
    c-family/
    	* c-common.h (c_finish_omp_for): Add argument.
    	* c-omp.c (c_finish_omp_for): Set OMP_FOR_ORIG_DECLS.
    cp/
    	* cp-tree.h (finish_omp_for): Add new argument.
    	* parser.c (cp_parser_omp_for_loop): Pass new argument.
    	* pt.c (tsubst_omp_for_iterator): New argument orig_declv.
    	Set OMP_FOR_ORIG_DECLS from orig_declv if available.
    	(tsubst_expr): Pass new vector to tsubst_omp_for_iterator.
    	* semantics.c (finish_omp_for): Pass original DECLs to
    	c_finish_omp_for.
    	Set OMP_FOR_ORIG_DECLS.
    c/
    	* c-parser.c (c_parser_omp_for_loop): Pass new argument to
    	c_finish_omp_for.

diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 202c8f9..7e857c3 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1245,7 +1245,7 @@ extern void c_finish_omp_flush (location_t);
 extern void c_finish_omp_taskwait (location_t);
 extern void c_finish_omp_taskyield (location_t);
 extern tree c_finish_omp_for (location_t, enum tree_code, tree, tree, tree,
-			      tree, tree, tree);
+			      tree, tree, tree, tree);
 extern tree c_finish_oacc_wait (location_t, tree, tree);
 extern void c_omp_split_clauses (location_t, enum tree_code, omp_clause_mask,
 				 tree, tree *);
diff --git a/gcc/c-family/c-omp.c b/gcc/c-family/c-omp.c
index f020a80..49cf5c7 100644
--- a/gcc/c-family/c-omp.c
+++ b/gcc/c-family/c-omp.c
@@ -432,6 +432,10 @@ c_omp_for_incr_canonicalize_ptr (location_t loc, tree decl, tree incr)
 
 /* Validate and generate OMP_FOR.
    DECLV is a vector of iteration variables, for each collapsed loop.
+
+   ORIG_DECLV, if non-NULL, is a vector with the original iteration
+   variables (prior to any transformations, by say, C++ iterators).
+
    INITV, CONDV and INCRV are vectors containing initialization
    expressions, controlling predicates and increment expressions.
    BODY is the body of the loop and PRE_BODY statements that go before
@@ -439,7 +443,8 @@ c_omp_for_incr_canonicalize_ptr (location_t loc, tree decl, tree incr)
 
 tree
 c_finish_omp_for (location_t locus, enum tree_code code, tree declv,
-		  tree initv, tree condv, tree incrv, tree body, tree pre_body)
+		  tree orig_declv, tree initv, tree condv, tree incrv,
+		  tree body, tree pre_body)
 {
   location_t elocus;
   bool fail = false;
@@ -678,6 +683,8 @@ c_finish_omp_for (location_t locus, enum tree_code code, tree declv,
       OMP_FOR_INCR (t) = incrv;
       OMP_FOR_BODY (t) = body;
       OMP_FOR_PRE_BODY (t) = pre_body;
+      if (code == OMP_FOR)
+	OMP_FOR_ORIG_DECLS (t) = orig_declv;
 
       SET_EXPR_LOCATION (t, locus);
       return add_stmt (t);
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 90486d2..0a42072 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -13696,7 +13696,7 @@ c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
      an error from the initialization parsing.  */
   if (!fail)
     {
-      stmt = c_finish_omp_for (loc, code, declv, initv, condv,
+      stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
 			       incrv, body, NULL);
       if (stmt)
 	{
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 650473d..8513917 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6027,7 +6027,7 @@ extern tree begin_omp_task			(void);
 extern tree finish_omp_task			(tree, tree);
 extern tree finish_omp_for			(location_t, enum tree_code,
 						 tree, tree, tree, tree, tree,
-						 tree, tree);
+						 tree, tree, tree);
 extern void finish_omp_atomic			(enum tree_code, enum tree_code,
 						 tree, tree, tree, tree, tree,
 						 bool);
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 5eaf7d9..b5d9508 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -31328,8 +31328,8 @@ cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
   if (declv == NULL_TREE)
     ret = NULL_TREE;
   else
-    ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
-			  pre_body, clauses);
+    ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
+			  body, pre_body, clauses);
 
   while (nbraces)
     {
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index c6320c9..8a2ce0a 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -13816,8 +13816,8 @@ static tree *omp_parallel_combined_clauses;
 /* Substitute one OMP_FOR iterator.  */
 
 static void
-tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
-			 tree condv, tree incrv, tree *clauses,
+tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
+			 tree initv, tree condv, tree incrv, tree *clauses,
 			 tree args, tsubst_flags_t complain, tree in_decl,
 			 bool integral_constant_expression_p)
 {
@@ -13828,6 +13828,16 @@ tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
 
   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
+
+  if (orig_declv && OMP_FOR_ORIG_DECLS (t))
+    {
+      tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
+      tree spec = retrieve_local_specialization (o);
+      if (spec)
+	o = spec;
+      TREE_VEC_ELT (orig_declv, i) = o;
+    }
+
   decl = TREE_OPERAND (init, 0);
   init = TREE_OPERAND (init, 1);
   tree decl_expr = NULL_TREE;
@@ -14459,6 +14469,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
       {
 	tree clauses, body, pre_body;
 	tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
+	tree orig_declv = NULL_TREE;
 	tree incrv = NULL_TREE;
 	int i;
 
@@ -14468,6 +14479,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 	if (OMP_FOR_INIT (t) != NULL_TREE)
 	  {
 	    declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
+	    if (TREE_CODE (t) == OMP_FOR)
+	      orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
 	    initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
 	    condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
 	    incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
@@ -14481,8 +14494,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 
 	if (OMP_FOR_INIT (t) != NULL_TREE)
 	  for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
-	    tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
-				     &clauses, args, complain, in_decl,
+	    tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
+				     incrv, &clauses, args, complain, in_decl,
 				     integral_constant_expression_p);
 	omp_parallel_combined_clauses = NULL;
 
@@ -14491,8 +14504,9 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 	body = pop_stmt_list (body);
 
 	if (OMP_FOR_INIT (t) != NULL_TREE)
-	  t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
-			      condv, incrv, body, pre_body, clauses);
+	  t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
+			      orig_declv, initv, condv, incrv, body, pre_body,
+			      clauses);
 	else
 	  {
 	    t = make_node (TREE_CODE (t));
@@ -22151,7 +22165,7 @@ dependent_template_id_p (tree tmpl, tree args)
 }
 
 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
-   is dependent.  */
+   are dependent.  */
 
 bool
 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 5e7a94d..9babfe2 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -7235,8 +7235,9 @@ handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
    sk_omp scope.  */
 
 tree
-finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
-		tree condv, tree incrv, tree body, tree pre_body, tree clauses)
+finish_omp_for (location_t locus, enum tree_code code, tree declv,
+		tree orig_declv, tree initv, tree condv, tree incrv,
+		tree body, tree pre_body, tree clauses)
 {
   tree omp_for = NULL, orig_incr = NULL;
   tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
@@ -7302,6 +7303,9 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
       TREE_VEC_ELT (initv, i) = init;
     }
 
+  if (!orig_declv || !TREE_VEC_LENGTH (orig_declv))
+    orig_declv = copy_node (declv);
+
   if (dependent_omp_for_p (declv, initv, condv, incrv))
     {
       tree stmt;
@@ -7325,6 +7329,8 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
       OMP_FOR_BODY (stmt) = body;
       OMP_FOR_PRE_BODY (stmt) = pre_body;
       OMP_FOR_CLAUSES (stmt) = clauses;
+      if (code == OMP_FOR)
+	OMP_FOR_ORIG_DECLS (stmt) = orig_declv;
 
       SET_EXPR_LOCATION (stmt, locus);
       return add_stmt (stmt);
@@ -7430,8 +7436,8 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
   if (code == CILK_FOR && !processing_template_decl)
     block = push_stmt_list ();
 
-  omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
-			      body, pre_body);
+  omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
+			      incrv, body, pre_body);
 
   if (omp_for == NULL)
     {
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 9ba3f37..ed3fe3e 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -7311,7 +7311,11 @@ gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
       gcc_assert (DECL_P (decl));
       gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
 		  || POINTER_TYPE_P (TREE_TYPE (decl)));
-      gimplify_omp_ctxp->iter_vars.quick_push (decl);
+      if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
+	gimplify_omp_ctxp->iter_vars.quick_push
+	  (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
+      else
+	gimplify_omp_ctxp->iter_vars.quick_push (decl);
 
       /* Make sure the iteration variable is private.  */
       tree c = NULL_TREE;
diff --git a/gcc/testsuite/c-c++-common/gomp/sink-2.c b/gcc/testsuite/c-c++-common/gomp/sink-2.c
new file mode 100644
index 0000000..7a075d4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/sink-2.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+
+void bar (int *);
+
+void
+foo ()
+{
+  int i,j;
+#pragma omp parallel for ordered(1)
+  for (i=0; i < 100; ++i)
+    {
+#pragma omp ordered depend(sink:i-1)
+    bar(&i);
+    }
+}
diff --git a/gcc/testsuite/g++.dg/gomp/sink-2.C b/gcc/testsuite/g++.dg/gomp/sink-2.C
new file mode 100644
index 0000000..d1681a7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/sink-2.C
@@ -0,0 +1,64 @@
+/* { dg-do compile } */
+
+/* Tests iterators are allowed in ordered loops and that we keep track
+   of the original iterator DECL for diagnostic purposes.  */
+
+#include <iostream>
+#include <vector>
+
+/* Test plain iterator.  */
+void foo1 ()
+{
+  std::vector<int> v;
+  for (int i=1; i<=5; i++) v.push_back(i);
+
+  std::vector<int>::const_iterator it;
+
+#pragma omp parallel for ordered(1)
+  for (it = v.begin(); it < v.end(); ++it)
+    {
+#pragma omp ordered depend(sink:it-1)
+    std::cout << *it << '\n';
+#pragma omp ordered depend(source)
+    }
+}
+
+/* Test non-dependent iterator in a template.  */
+template <int N>
+void foo2 ()
+{
+  std::vector<int> v;
+  for (int i=1; i<=5; i++) v.push_back(i);
+
+  std::vector<int>::const_iterator it;
+#pragma omp parallel for ordered(1)
+  for (it = v.begin(); it < v.end(); ++it)
+    {
+#pragma omp ordered depend(sink:it-1)
+    std::cout << *it << '\n';
+#pragma omp ordered depend(source)
+    }
+}
+
+/* Test dependent iterator in a template.  */
+template <typename T>
+void foo3 ()
+{
+  std::vector<T> v;
+  for (int i=1; i<=5; i++) v.push_back(i);
+
+  typename std::vector<T>::const_iterator it;
+#pragma omp parallel for ordered(1)
+  for (it = v.begin(); it < v.end(); ++it)
+    {
+#pragma omp ordered depend(sink:it-1)
+    std::cout << *it << '\n';
+#pragma omp ordered depend(source)
+    }
+}  
+
+int main ()
+{
+  foo2 <0> ();
+  foo3 <int> ();
+}
diff --git a/gcc/tree.def b/gcc/tree.def
index 6703b57..64e0727 100644
--- a/gcc/tree.def
+++ b/gcc/tree.def
@@ -1087,36 +1087,41 @@ DEFTREECODE (OMP_TASK, "omp_task", tcc_statement, 2)
 	from INIT, COND, and INCR that are technically part of the
 	OMP_FOR structured block, but are evaluated before the loop
 	body begins.
+   Operand 6: OMP_FOR_ORIG_DECLS: If non-NULL, list of DECLs initialized
+	in OMP_FOR_INIT.  In some cases, like C++ iterators, the original
+	DECL init has been lost in gimplification and now contains a
+	temporary (D.nnnn).  This list contains the original DECLs in
+	the source.
 
    VAR must be an integer or pointer variable, which is implicitly thread
    private.  N1, N2 and INCR are required to be loop invariant integer
    expressions that are evaluated without any synchronization.
    The evaluation order, frequency of evaluation and side-effects are
    unspecified by the standards.  */
-DEFTREECODE (OMP_FOR, "omp_for", tcc_statement, 6)
+DEFTREECODE (OMP_FOR, "omp_for", tcc_statement, 7)
 
 /* OpenMP - #pragma omp simd [clause1 ... clauseN]
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (OMP_SIMD, "omp_simd", tcc_statement, 6)
 
 /* Cilk Plus - #pragma simd [clause1 ... clauseN]
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (CILK_SIMD, "cilk_simd", tcc_statement, 6)
 
 /* Cilk Plus - _Cilk_for (..)
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (CILK_FOR, "cilk_for", tcc_statement, 6)
 
 /* OpenMP - #pragma omp distribute [clause1 ... clauseN]
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (OMP_DISTRIBUTE, "omp_distribute", tcc_statement, 6)
 
 /* OpenMP - #pragma omp taskloop [clause1 ... clauseN]
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (OMP_TASKLOOP, "omp_taskloop", tcc_statement, 6)
 
 /* OpenMP - #pragma acc loop [clause1 ... clauseN]
-   Operands like for OMP_FOR.  */
+   Operands like operands 1-6 of OMP_FOR.  */
 DEFTREECODE (OACC_LOOP, "oacc_loop", tcc_statement, 6)
 
 /* OpenMP - #pragma omp teams [clause1 ... clauseN]
diff --git a/gcc/tree.h b/gcc/tree.h
index 7ea7a93..9db3c40 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -1260,6 +1260,8 @@ extern void protected_set_expr_location (tree, location_t);
 #define OMP_FOR_COND(NODE)	   TREE_OPERAND (OMP_LOOP_CHECK (NODE), 3)
 #define OMP_FOR_INCR(NODE)	   TREE_OPERAND (OMP_LOOP_CHECK (NODE), 4)
 #define OMP_FOR_PRE_BODY(NODE)	   TREE_OPERAND (OMP_LOOP_CHECK (NODE), 5)
+/* Note that this is only available for OMP_FOR, hence OMP_FOR_CHECK.  */
+#define OMP_FOR_ORIG_DECLS(NODE)   TREE_OPERAND (OMP_FOR_CHECK (NODE), 6)
 
 #define OMP_SECTIONS_BODY(NODE)    TREE_OPERAND (OMP_SECTIONS_CHECK (NODE), 0)
 #define OMP_SECTIONS_CLAUSES(NODE) TREE_OPERAND (OMP_SECTIONS_CHECK (NODE), 1)


More information about the Gcc-patches mailing list