veclower: Fix up -fcompare-debug issue in expand_vector_comparison [PR104307]

Message ID 20220201091849.GT2646553@tucnak
State New
Headers
Series veclower: Fix up -fcompare-debug issue in expand_vector_comparison [PR104307] |

Commit Message

Jakub Jelinek Feb. 1, 2022, 9:18 a.m. UTC
  Hi!

The following testcase fails -fcompare-debug, because expand_vector_comparison
since r11-1786-g1ac9258cca8030745d3c0b8f63186f0adf0ebc27 sets
vec_cond_expr_only when it sees some use other than VEC_COND_EXPR that uses
the lhs in its condition.
Obviously we should ignore debug stmts when doing so, e.g. by not pushing
them to uses.
That would be a 2 liner change, but while looking at it, I'm also worried
about VEC_COND_EXPRs that would use the lhs in more than one operand,
like VEC_COND_EXPR <lhs, lhs, something> or VEC_COND_EXPR <lhs, something, lhs>
(sure, they ought to be folded, but what if they weren't).  Because if
something like that happens, then FOR_EACH_IMM_USE_FAST would push the same
stmt multiple times and expand_vector_condition can return true even when
it modifies it (for vector bool masking).
And lastly, it seems quite wasteful to safe_push statements that will just
cause vec_cond_expr_only = false; and break; in the second loop, both for
cases like 1000 immediate non-VEC_COND_EXPR uses and for cases like
999 VEC_COND_EXPRs with lhs in cond followed by a single non-VEC_COND_EXPR
use.  So this patch only pushes VEC_COND_EXPRs there.  As
expand_vector_condition modifies the IL, it checks the condition again as
before.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-02-01  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/104307
	* tree-vect-generic.cc (expand_vector_comparison): Don't push debug
	stmts to uses vector, just set vec_cond_expr_only to false for
	non-VEC_COND_EXPRs instead of pushing them into uses.  Treat
	VEC_COND_EXPRs that use lhs not just in rhs1, but rhs2 or rhs3 too
	like non-VEC_COND_EXPRs.

	* gcc.target/i386/pr104307.c: New test.


	Jakub
  

Comments

Richard Biener Feb. 1, 2022, 9:29 a.m. UTC | #1
On Tue, 1 Feb 2022, Jakub Jelinek wrote:

> Hi!
> 
> The following testcase fails -fcompare-debug, because expand_vector_comparison
> since r11-1786-g1ac9258cca8030745d3c0b8f63186f0adf0ebc27 sets
> vec_cond_expr_only when it sees some use other than VEC_COND_EXPR that uses
> the lhs in its condition.
> Obviously we should ignore debug stmts when doing so, e.g. by not pushing
> them to uses.
> That would be a 2 liner change, but while looking at it, I'm also worried
> about VEC_COND_EXPRs that would use the lhs in more than one operand,
> like VEC_COND_EXPR <lhs, lhs, something> or VEC_COND_EXPR <lhs, something, lhs>
> (sure, they ought to be folded, but what if they weren't).  Because if
> something like that happens, then FOR_EACH_IMM_USE_FAST would push the same
> stmt multiple times and expand_vector_condition can return true even when
> it modifies it (for vector bool masking).
> And lastly, it seems quite wasteful to safe_push statements that will just
> cause vec_cond_expr_only = false; and break; in the second loop, both for
> cases like 1000 immediate non-VEC_COND_EXPR uses and for cases like
> 999 VEC_COND_EXPRs with lhs in cond followed by a single non-VEC_COND_EXPR
> use.  So this patch only pushes VEC_COND_EXPRs there.  As
> expand_vector_condition modifies the IL, it checks the condition again as
> before.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

So I think it's all fine besides the handling of VEC_COND_EXPRs where
the use is in rhs1 and rhs2 and/or rhs3 - I don't really understand
your worry here but shouldn't the stmt end up on the vector at least
once?  You can use gimple_assign_rhs1_ptr to see whether the
use is the rhs1 use comparing that with USE_PTR IIRC.  Btw, if you
never push VEC_COND_EXPRs with such double-use it's not necessary
to check again in the second loop?

That said, the other changes look reasonable.

Thanks,
Richard.

> 2022-02-01  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR middle-end/104307
> 	* tree-vect-generic.cc (expand_vector_comparison): Don't push debug
> 	stmts to uses vector, just set vec_cond_expr_only to false for
> 	non-VEC_COND_EXPRs instead of pushing them into uses.  Treat
> 	VEC_COND_EXPRs that use lhs not just in rhs1, but rhs2 or rhs3 too
> 	like non-VEC_COND_EXPRs.
> 
> 	* gcc.target/i386/pr104307.c: New test.
> 
> --- gcc/tree-vect-generic.cc.jj	2022-01-20 11:30:45.641577244 +0100
> +++ gcc/tree-vect-generic.cc	2022-01-31 18:01:29.062568721 +0100
> @@ -436,29 +436,43 @@ expand_vector_comparison (gimple_stmt_it
>       feeding a VEC_COND_EXPR statement.  */
>    auto_vec<gimple *> uses;
>    FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
> -    uses.safe_push (USE_STMT (use_p));
> -
> -  for (unsigned i = 0; i < uses.length (); i ++)
>      {
> -      gassign *use = dyn_cast<gassign *> (uses[i]);
> -      if (use != NULL
> +      gimple *use = USE_STMT (use_p);
> +      if (is_gimple_debug (use))
> +	continue;
> +      if (is_gimple_assign (use)
>  	  && gimple_assign_rhs_code (use) == VEC_COND_EXPR
> -	  && gimple_assign_rhs1 (use) == lhs)
> -	{
> -	  gimple_stmt_iterator it = gsi_for_stmt (use);
> -	  if (!expand_vector_condition (&it, dce_ssa_names))
> -	    {
> -	      vec_cond_expr_only = false;
> -	      break;
> -	    }
> -	}
> +	  && gimple_assign_rhs1 (use) == lhs
> +	  && gimple_assign_rhs2 (use) != lhs
> +	  && gimple_assign_rhs3 (use) != lhs)
> +	uses.safe_push (use);
>        else
> -	{
> -	  vec_cond_expr_only = false;
> -	  break;
> -	}
> +	vec_cond_expr_only = false;
>      }
>  
> +  if (vec_cond_expr_only)
> +    for (gimple *use : uses)
> +      {
> +	if (is_gimple_assign (use)
> +	    && gimple_assign_rhs_code (use) == VEC_COND_EXPR
> +	    && gimple_assign_rhs1 (use) == lhs
> +	    && gimple_assign_rhs2 (use) != lhs
> +	    && gimple_assign_rhs3 (use) != lhs)
> +	  {
> +	    gimple_stmt_iterator it = gsi_for_stmt (use);
> +	    if (!expand_vector_condition (&it, dce_ssa_names))
> +	      {
> +		vec_cond_expr_only = false;
> +		break;
> +	      }
> +	  }
> +	else
> +	  {
> +	    vec_cond_expr_only = false;
> +	    break;
> +	  }
> +      }
> +
>    if (!uses.is_empty () && vec_cond_expr_only)
>      return NULL_TREE;
>  
> --- gcc/testsuite/gcc.target/i386/pr104307.c.jj	2022-01-31 17:34:42.163145798 +0100
> +++ gcc/testsuite/gcc.target/i386/pr104307.c	2022-01-31 17:35:14.111696698 +0100
> @@ -0,0 +1,6 @@
> +/* PR middle-end/104307 */
> +/* { dg-do compile } */
> +/* { dg-require-effective-target int128 } */
> +/* { dg-options "-O2 -mavx512f -fcompare-debug " } */
> +
> +#include "pr78669.c"
> 
> 	Jakub
> 
>
  
Jakub Jelinek Feb. 1, 2022, 9:46 a.m. UTC | #2
On Tue, Feb 01, 2022 at 10:29:03AM +0100, Richard Biener wrote:
> So I think it's all fine besides the handling of VEC_COND_EXPRs where
> the use is in rhs1 and rhs2 and/or rhs3 - I don't really understand
> your worry here but shouldn't the stmt end up on the vector at least
> once?  You can use gimple_assign_rhs1_ptr to see whether the

My worry is that
  FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
    uses.safe_push (USE_STMT (use_p));
for a stmt with multiple uses of lhs pushes the same
stmt multiple times.
And then
      if (a_is_comparison)
        a = gimplify_build2 (gsi, code, type, a1, a2);
      a1 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a, b);
      a2 = gimplify_build1 (gsi, BIT_NOT_EXPR, type, a);
      a2 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a2, c);
      a = gimplify_build2 (gsi, BIT_IOR_EXPR, type, a1, a2);
      gimple_assign_set_rhs_from_tree (gsi, a);
      update_stmt (gsi_stmt (*gsi));
will modify it (though the above at least will not remove the
stmt and update it in place I think) and then it won't be
a VEC_COND_EXPR anymore.
To me the non-cond uses in VEC_COND_EXPR conceptually look like
any other unhandled uses that the second loop clears
vec_cond_expr_only on.  But I don't have a testcase, dunno if it is even
possible.

> use is the rhs1 use comparing that with USE_PTR IIRC.  Btw, if you
> never push VEC_COND_EXPRs with such double-use it's not necessary
> to check again in the second loop?

I was just trying to be extra cautious in case expand_vector_comparison
modifies some other stmts, but maybe it is just expand_vector_comparison
in veclower and no other function that modifies anything but the
current stmt (+ pushes some new preparation statements and follow-up
statements).
So perhaps indeed:
+  if (vec_cond_expr_only)
+    for (gimple *use : uses)
+      {
+	gimple_stmt_iterator it = gsi_for_stmt (use);
+	if (!expand_vector_condition (&it, dce_ssa_names))
+	  {
+	    vec_cond_expr_only = false;
+	    break;
+	  }
+      }
for the second loop is enough.

But sure, if you prefer all I can do:
   FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
-    uses.safe_push (USE_STMT (use_p));
+    if (!is_gimple_debug (USE_STMT (use_p)))
+      uses.safe_push (USE_STMT (use_p));

and keep the rest for GCC 13.

	Jakub
  
Richard Biener Feb. 1, 2022, 9:56 a.m. UTC | #3
On Tue, 1 Feb 2022, Jakub Jelinek wrote:

> On Tue, Feb 01, 2022 at 10:29:03AM +0100, Richard Biener wrote:
> > So I think it's all fine besides the handling of VEC_COND_EXPRs where
> > the use is in rhs1 and rhs2 and/or rhs3 - I don't really understand
> > your worry here but shouldn't the stmt end up on the vector at least
> > once?  You can use gimple_assign_rhs1_ptr to see whether the
> 
> My worry is that
>   FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
>     uses.safe_push (USE_STMT (use_p));
> for a stmt with multiple uses of lhs pushes the same
> stmt multiple times.
> And then
>       if (a_is_comparison)
>         a = gimplify_build2 (gsi, code, type, a1, a2);
>       a1 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a, b);
>       a2 = gimplify_build1 (gsi, BIT_NOT_EXPR, type, a);
>       a2 = gimplify_build2 (gsi, BIT_AND_EXPR, type, a2, c);
>       a = gimplify_build2 (gsi, BIT_IOR_EXPR, type, a1, a2);
>       gimple_assign_set_rhs_from_tree (gsi, a);
>       update_stmt (gsi_stmt (*gsi));
> will modify it (though the above at least will not remove the
> stmt and update it in place I think) and then it won't be
> a VEC_COND_EXPR anymore.

Ah, OK.  Sure, pushing the stmt multiple times looks bogus and indeed
if we see we'll visit it a second time for a rhs{2,3} use there's
no point in pushing it in the first place.

> To me the non-cond uses in VEC_COND_EXPR conceptually look like
> any other unhandled uses that the second loop clears
> vec_cond_expr_only on.  But I don't have a testcase, dunno if it is even
> possible.
> 
> > use is the rhs1 use comparing that with USE_PTR IIRC.  Btw, if you
> > never push VEC_COND_EXPRs with such double-use it's not necessary
> > to check again in the second loop?
> 
> I was just trying to be extra cautious in case expand_vector_comparison
> modifies some other stmts, but maybe it is just expand_vector_comparison
> in veclower and no other function that modifies anything but the
> current stmt (+ pushes some new preparation statements and follow-up
> statements).
> So perhaps indeed:
> +  if (vec_cond_expr_only)
> +    for (gimple *use : uses)
> +      {
> +	gimple_stmt_iterator it = gsi_for_stmt (use);
> +	if (!expand_vector_condition (&it, dce_ssa_names))
> +	  {
> +	    vec_cond_expr_only = false;
> +	    break;
> +	  }
> +      }
> for the second loop is enough.

Yes, I think so.

> But sure, if you prefer all I can do:
>    FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
> -    uses.safe_push (USE_STMT (use_p));
> +    if (!is_gimple_debug (USE_STMT (use_p)))
> +      uses.safe_push (USE_STMT (use_p));
> 
> and keep the rest for GCC 13.

No, I think the change is fine with the second loop adjusted.

Thanks,
Richard.
  

Patch

--- gcc/tree-vect-generic.cc.jj	2022-01-20 11:30:45.641577244 +0100
+++ gcc/tree-vect-generic.cc	2022-01-31 18:01:29.062568721 +0100
@@ -436,29 +436,43 @@  expand_vector_comparison (gimple_stmt_it
      feeding a VEC_COND_EXPR statement.  */
   auto_vec<gimple *> uses;
   FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs)
-    uses.safe_push (USE_STMT (use_p));
-
-  for (unsigned i = 0; i < uses.length (); i ++)
     {
-      gassign *use = dyn_cast<gassign *> (uses[i]);
-      if (use != NULL
+      gimple *use = USE_STMT (use_p);
+      if (is_gimple_debug (use))
+	continue;
+      if (is_gimple_assign (use)
 	  && gimple_assign_rhs_code (use) == VEC_COND_EXPR
-	  && gimple_assign_rhs1 (use) == lhs)
-	{
-	  gimple_stmt_iterator it = gsi_for_stmt (use);
-	  if (!expand_vector_condition (&it, dce_ssa_names))
-	    {
-	      vec_cond_expr_only = false;
-	      break;
-	    }
-	}
+	  && gimple_assign_rhs1 (use) == lhs
+	  && gimple_assign_rhs2 (use) != lhs
+	  && gimple_assign_rhs3 (use) != lhs)
+	uses.safe_push (use);
       else
-	{
-	  vec_cond_expr_only = false;
-	  break;
-	}
+	vec_cond_expr_only = false;
     }
 
+  if (vec_cond_expr_only)
+    for (gimple *use : uses)
+      {
+	if (is_gimple_assign (use)
+	    && gimple_assign_rhs_code (use) == VEC_COND_EXPR
+	    && gimple_assign_rhs1 (use) == lhs
+	    && gimple_assign_rhs2 (use) != lhs
+	    && gimple_assign_rhs3 (use) != lhs)
+	  {
+	    gimple_stmt_iterator it = gsi_for_stmt (use);
+	    if (!expand_vector_condition (&it, dce_ssa_names))
+	      {
+		vec_cond_expr_only = false;
+		break;
+	      }
+	  }
+	else
+	  {
+	    vec_cond_expr_only = false;
+	    break;
+	  }
+      }
+
   if (!uses.is_empty () && vec_cond_expr_only)
     return NULL_TREE;
 
--- gcc/testsuite/gcc.target/i386/pr104307.c.jj	2022-01-31 17:34:42.163145798 +0100
+++ gcc/testsuite/gcc.target/i386/pr104307.c	2022-01-31 17:35:14.111696698 +0100
@@ -0,0 +1,6 @@ 
+/* PR middle-end/104307 */
+/* { dg-do compile } */
+/* { dg-require-effective-target int128 } */
+/* { dg-options "-O2 -mavx512f -fcompare-debug " } */
+
+#include "pr78669.c"