lower-bitint: Ensure we don't get coalescing ICEs for (ab) SSA_NAMEs used in mul/div/mod [PR113567]

Message ID Zc2/bqycCBI74x2y@tucnak
State Unresolved
Headers
Series lower-bitint: Ensure we don't get coalescing ICEs for (ab) SSA_NAMEs used in mul/div/mod [PR113567] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Feb. 15, 2024, 7:38 a.m. UTC
  Hi!

The build_bitint_stmt_ssa_conflicts hook has a special case for
multiplication, division and modulo, where to ensure there is no overlap
between lhs and rhs1/rhs2 arrays we make the lhs conflict with the
operands.
On the following testcase, we have
  # a_1(ab) = PHI <a_2(D)(0), a_3(ab)(3)>
lab:
  a_3(ab) = a_1(ab) % 3;
before lowering and this special case causes a_3(ab) and a_1(ab) to
conflict, but the PHI requires them not to conflict, so we ICE because we
can't find some partitioning that will work.

The following patch fixes this by special casing such statements before
the partitioning, force the inputs of the multiplication/division which
have large/huge _BitInt (ab) lhs into new non-(ab) SSA_NAMEs initialized
right before the multiplication/division.  This allows the partitioning
to work then, as it has the possibility to use a different partition for
the */% operands.

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

2024-02-15  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/113567
	* gimple-lower-bitint.cc (gimple_lower_bitint): For large/huge
	_BitInt multiplication, division or modulo with
	SSA_NAME_OCCURS_IN_ABNORMAL_PHI lhs and at least one of rhs1 and rhs2
	force the affected inputs into a new SSA_NAME.

	* gcc.dg/bitint-90.c: New test.


	Jakub
  

Comments

Richard Biener Feb. 15, 2024, 8:37 a.m. UTC | #1
On Thu, 15 Feb 2024, Jakub Jelinek wrote:

> Hi!
> 
> The build_bitint_stmt_ssa_conflicts hook has a special case for
> multiplication, division and modulo, where to ensure there is no overlap
> between lhs and rhs1/rhs2 arrays we make the lhs conflict with the
> operands.
> On the following testcase, we have
>   # a_1(ab) = PHI <a_2(D)(0), a_3(ab)(3)>
> lab:
>   a_3(ab) = a_1(ab) % 3;
> before lowering and this special case causes a_3(ab) and a_1(ab) to
> conflict, but the PHI requires them not to conflict, so we ICE because we
> can't find some partitioning that will work.
> 
> The following patch fixes this by special casing such statements before
> the partitioning, force the inputs of the multiplication/division which
> have large/huge _BitInt (ab) lhs into new non-(ab) SSA_NAMEs initialized
> right before the multiplication/division.  This allows the partitioning
> to work then, as it has the possibility to use a different partition for
> the */% operands.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2024-02-15  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/113567
> 	* gimple-lower-bitint.cc (gimple_lower_bitint): For large/huge
> 	_BitInt multiplication, division or modulo with
> 	SSA_NAME_OCCURS_IN_ABNORMAL_PHI lhs and at least one of rhs1 and rhs2
> 	force the affected inputs into a new SSA_NAME.
> 
> 	* gcc.dg/bitint-90.c: New test.
> 
> --- gcc/gimple-lower-bitint.cc.jj	2024-02-12 20:45:50.156275452 +0100
> +++ gcc/gimple-lower-bitint.cc	2024-02-14 18:17:36.630664828 +0100
> @@ -5973,6 +5973,47 @@ gimple_lower_bitint (void)
>  	      {
>  	      default:
>  		break;
> +	      case MULT_EXPR:
> +	      case TRUNC_DIV_EXPR:
> +	      case TRUNC_MOD_EXPR:
> +		if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s))
> +		  {
> +		    location_t loc = gimple_location (stmt);
> +		    gsi = gsi_for_stmt (stmt);
> +		    tree rhs1 = gimple_assign_rhs1 (stmt);
> +		    tree rhs2 = gimple_assign_rhs2 (stmt);
> +		    /* For multiplication and division with (ab)
> +		       lhs and one or both operands force the operands
> +		       into new SSA_NAMEs to avoid coalescing failures.  */
> +		    if (TREE_CODE (rhs1) == SSA_NAME
> +			&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
> +		      {
> +			first_large_huge = 0;
> +			tree t = make_ssa_name (TREE_TYPE (rhs1));
> +			g = gimple_build_assign (t, SSA_NAME, rhs1);
> +			gsi_insert_before (&gsi, g, GSI_SAME_STMT);
> +			gimple_set_location (g, loc);
> +			gimple_assign_set_rhs1 (stmt, t);
> +			if (rhs1 == rhs2)
> +			  {
> +			    gimple_assign_set_rhs2 (stmt, t);
> +			    rhs2 = t;
> +			  }
> +			update_stmt (stmt);
> +		      }
> +		    if (TREE_CODE (rhs2) == SSA_NAME
> +			&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2))
> +		      {
> +			first_large_huge = 0;
> +			tree t = make_ssa_name (TREE_TYPE (rhs2));
> +			g = gimple_build_assign (t, SSA_NAME, rhs2);
> +			gsi_insert_before (&gsi, g, GSI_SAME_STMT);
> +			gimple_set_location (g, loc);
> +			gimple_assign_set_rhs2 (stmt, t);
> +			update_stmt (stmt);
> +		      }
> +		  }
> +		break;
>  	      case LROTATE_EXPR:
>  	      case RROTATE_EXPR:
>  		{
> --- gcc/testsuite/gcc.dg/bitint-90.c.jj	2024-02-14 18:24:20.546018881 +0100
> +++ gcc/testsuite/gcc.dg/bitint-90.c	2024-02-14 18:24:09.900167668 +0100
> @@ -0,0 +1,23 @@
> +/* PR tree-optimization/113567 */
> +/* { dg-do compile { target bitint } } */
> +/* { dg-options "-O2" } */
> +
> +#if __BITINT_MAXWIDTH__ >= 129
> +_BitInt(129) v;
> +
> +void
> +foo (_BitInt(129) a, int i)
> +{
> +  __label__  l1, l2;
> +  i &= 1;
> +  void *p[] = { &&l1, &&l2 };
> +l1:
> +  a %= 3;
> +  v = a;
> +  i = !i;
> +  goto *(p[i]);
> +l2:;
> +}
> +#else
> +int i;
> +#endif
> 
> 	Jakub
> 
>
  

Patch

--- gcc/gimple-lower-bitint.cc.jj	2024-02-12 20:45:50.156275452 +0100
+++ gcc/gimple-lower-bitint.cc	2024-02-14 18:17:36.630664828 +0100
@@ -5973,6 +5973,47 @@  gimple_lower_bitint (void)
 	      {
 	      default:
 		break;
+	      case MULT_EXPR:
+	      case TRUNC_DIV_EXPR:
+	      case TRUNC_MOD_EXPR:
+		if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s))
+		  {
+		    location_t loc = gimple_location (stmt);
+		    gsi = gsi_for_stmt (stmt);
+		    tree rhs1 = gimple_assign_rhs1 (stmt);
+		    tree rhs2 = gimple_assign_rhs2 (stmt);
+		    /* For multiplication and division with (ab)
+		       lhs and one or both operands force the operands
+		       into new SSA_NAMEs to avoid coalescing failures.  */
+		    if (TREE_CODE (rhs1) == SSA_NAME
+			&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
+		      {
+			first_large_huge = 0;
+			tree t = make_ssa_name (TREE_TYPE (rhs1));
+			g = gimple_build_assign (t, SSA_NAME, rhs1);
+			gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+			gimple_set_location (g, loc);
+			gimple_assign_set_rhs1 (stmt, t);
+			if (rhs1 == rhs2)
+			  {
+			    gimple_assign_set_rhs2 (stmt, t);
+			    rhs2 = t;
+			  }
+			update_stmt (stmt);
+		      }
+		    if (TREE_CODE (rhs2) == SSA_NAME
+			&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2))
+		      {
+			first_large_huge = 0;
+			tree t = make_ssa_name (TREE_TYPE (rhs2));
+			g = gimple_build_assign (t, SSA_NAME, rhs2);
+			gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+			gimple_set_location (g, loc);
+			gimple_assign_set_rhs2 (stmt, t);
+			update_stmt (stmt);
+		      }
+		  }
+		break;
 	      case LROTATE_EXPR:
 	      case RROTATE_EXPR:
 		{
--- gcc/testsuite/gcc.dg/bitint-90.c.jj	2024-02-14 18:24:20.546018881 +0100
+++ gcc/testsuite/gcc.dg/bitint-90.c	2024-02-14 18:24:09.900167668 +0100
@@ -0,0 +1,23 @@ 
+/* PR tree-optimization/113567 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-O2" } */
+
+#if __BITINT_MAXWIDTH__ >= 129
+_BitInt(129) v;
+
+void
+foo (_BitInt(129) a, int i)
+{
+  __label__  l1, l2;
+  i &= 1;
+  void *p[] = { &&l1, &&l2 };
+l1:
+  a %= 3;
+  v = a;
+  i = !i;
+  goto *(p[i]);
+l2:;
+}
+#else
+int i;
+#endif