c++, v2: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295]

Message ID Y1JKrxD7/o9itqqG@tucnak
State Unresolved
Headers
Series c++, v2: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295] |

Checks

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

Commit Message

Jakub Jelinek Oct. 21, 2022, 7:30 a.m. UTC
  On Thu, Oct 20, 2022 at 10:51:14AM -0400, Jason Merrill wrote:
> That seems like a bug; for VECTOR_TYPE we should fold even if !changed.
> 
> > Also, the reason for the short-cutting is I think trying to avoid
> > allocating a new CONSTRUCTOR when nothing changes and we just create
> > GC garbage by it.
> 
> We might limit the shortcut to non-vector types by hoisting the vector check
> in reduced_constant_expression_p out of the CONSTRUCTOR_NO_CLEARING
> condition:
> 
> >       if (CONSTRUCTOR_NO_CLEARING (t))
> >         {
> >           if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
> >             /* An initialized vector would have a VECTOR_CST.  */
> >             return false;
> 
> then we could remove the fold in the shortcut.

Ok, so like this?
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-10-21  Jakub Jelinek  <jakub@redhat.com>

	PR c++/107295
	* constexpr.cc (reduced_constant_expression_p) <case CONSTRUCTOR>:
	Return false for VECTOR_TYPE CONSTRUCTORs even without
	CONSTRUCTOR_NO_CLEARING set on them.
	(cxx_eval_bare_aggregate): If constant but !changed, fold before
	returning VECTOR_TYPE_P CONSTRUCTOR.
	(cxx_eval_constant_expression) <case CONSTRUCTOR>: Don't fold
	TREE_CONSTANT CONSTRUCTOR, just return it.

	* g++.dg/ext/vector42.C: New test.



	Jakub
  

Comments

Jason Merrill Oct. 21, 2022, 1:19 p.m. UTC | #1
On 10/21/22 03:30, Jakub Jelinek wrote:
> On Thu, Oct 20, 2022 at 10:51:14AM -0400, Jason Merrill wrote:
>> That seems like a bug; for VECTOR_TYPE we should fold even if !changed.
>>
>>> Also, the reason for the short-cutting is I think trying to avoid
>>> allocating a new CONSTRUCTOR when nothing changes and we just create
>>> GC garbage by it.
>>
>> We might limit the shortcut to non-vector types by hoisting the vector check
>> in reduced_constant_expression_p out of the CONSTRUCTOR_NO_CLEARING
>> condition:
>>
>>>        if (CONSTRUCTOR_NO_CLEARING (t))
>>>          {
>>>            if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
>>>              /* An initialized vector would have a VECTOR_CST.  */
>>>              return false;
>>
>> then we could remove the fold in the shortcut.
> 
> Ok, so like this?
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2022-10-21  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/107295
> 	* constexpr.cc (reduced_constant_expression_p) <case CONSTRUCTOR>:
> 	Return false for VECTOR_TYPE CONSTRUCTORs even without
> 	CONSTRUCTOR_NO_CLEARING set on them.
> 	(cxx_eval_bare_aggregate): If constant but !changed, fold before
> 	returning VECTOR_TYPE_P CONSTRUCTOR.
> 	(cxx_eval_constant_expression) <case CONSTRUCTOR>: Don't fold
> 	TREE_CONSTANT CONSTRUCTOR, just return it.
> 
> 	* g++.dg/ext/vector42.C: New test.
> 
> --- gcc/cp/constexpr.cc.jj	2022-10-19 11:20:28.960225787 +0200
> +++ gcc/cp/constexpr.cc	2022-10-20 18:43:42.952440364 +0200
> @@ -3104,12 +3104,12 @@ reduced_constant_expression_p (tree t)
>       case CONSTRUCTOR:
>         /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR.  */
>         tree field;
> +      if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
> +	/* An initialized vector would have a VECTOR_CST.  */
> +	return false;
>         if (CONSTRUCTOR_NO_CLEARING (t))
>   	{
> -	  if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
> -	    /* An initialized vector would have a VECTOR_CST.  */
> -	    return false;
> -	  else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
> +	  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
>   	    {
>   	      /* There must be a valid constant initializer at every array
>   		 index.  */
> @@ -4956,8 +4956,14 @@ cxx_eval_bare_aggregate (const constexpr
>   	  TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
>   	}
>       }
> -  if (*non_constant_p || !changed)
> +  if (*non_constant_p)
>       return t;
> +  if (!changed)
> +    {
> +      if (VECTOR_TYPE_P (type))
> +	t = fold (t);
> +      return t;
> +    }
>     t = ctx->ctor;
>     if (!t)
>       t = build_constructor (type, NULL);
> @@ -7387,11 +7393,10 @@ cxx_eval_constant_expression (const cons
>       case CONSTRUCTOR:
>         if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
>   	{
> -	  /* Don't re-process a constant CONSTRUCTOR, but do fold it to
> -	     VECTOR_CST if applicable.  */
> +	  /* Don't re-process a constant CONSTRUCTOR.  */
>   	  verify_constructor_flags (t);
>   	  if (TREE_CONSTANT (t))
> -	    return fold (t);
> +	    return t;
>   	}
>         r = cxx_eval_bare_aggregate (ctx, t, lval,
>   				   non_constant_p, overflow_p);
> --- gcc/testsuite/g++.dg/ext/vector42.C.jj	2022-10-20 17:57:42.767848544 +0200
> +++ gcc/testsuite/g++.dg/ext/vector42.C	2022-10-20 17:57:42.767848544 +0200
> @@ -0,0 +1,12 @@
> +// PR c++/107295
> +// { dg-do compile { target c++11 } }
> +
> +template <typename T> struct A {
> +  typedef T __attribute__((vector_size (sizeof (int)))) V;
> +};
> +template <int, typename T> using B = typename A<T>::V;
> +template <typename T> using V = B<4, T>;
> +using F = V<float>;
> +constexpr F a = F () + 0.0f;
> +constexpr F b = F () + (float) 0.0;
> +constexpr F c = F () + (float) 0.0L;
> 
> 
> 	Jakub
>
  

Patch

--- gcc/cp/constexpr.cc.jj	2022-10-19 11:20:28.960225787 +0200
+++ gcc/cp/constexpr.cc	2022-10-20 18:43:42.952440364 +0200
@@ -3104,12 +3104,12 @@  reduced_constant_expression_p (tree t)
     case CONSTRUCTOR:
       /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR.  */
       tree field;
+      if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
+	/* An initialized vector would have a VECTOR_CST.  */
+	return false;
       if (CONSTRUCTOR_NO_CLEARING (t))
 	{
-	  if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
-	    /* An initialized vector would have a VECTOR_CST.  */
-	    return false;
-	  else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
+	  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
 	    {
 	      /* There must be a valid constant initializer at every array
 		 index.  */
@@ -4956,8 +4956,14 @@  cxx_eval_bare_aggregate (const constexpr
 	  TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
 	}
     }
-  if (*non_constant_p || !changed)
+  if (*non_constant_p)
     return t;
+  if (!changed)
+    {
+      if (VECTOR_TYPE_P (type))
+	t = fold (t);
+      return t;
+    }
   t = ctx->ctor;
   if (!t)
     t = build_constructor (type, NULL);
@@ -7387,11 +7393,10 @@  cxx_eval_constant_expression (const cons
     case CONSTRUCTOR:
       if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
 	{
-	  /* Don't re-process a constant CONSTRUCTOR, but do fold it to
-	     VECTOR_CST if applicable.  */
+	  /* Don't re-process a constant CONSTRUCTOR.  */
 	  verify_constructor_flags (t);
 	  if (TREE_CONSTANT (t))
-	    return fold (t);
+	    return t;
 	}
       r = cxx_eval_bare_aggregate (ctx, t, lval,
 				   non_constant_p, overflow_p);
--- gcc/testsuite/g++.dg/ext/vector42.C.jj	2022-10-20 17:57:42.767848544 +0200
+++ gcc/testsuite/g++.dg/ext/vector42.C	2022-10-20 17:57:42.767848544 +0200
@@ -0,0 +1,12 @@ 
+// PR c++/107295
+// { dg-do compile { target c++11 } }
+
+template <typename T> struct A {
+  typedef T __attribute__((vector_size (sizeof (int)))) V;
+};
+template <int, typename T> using B = typename A<T>::V;
+template <typename T> using V = B<4, T>;
+using F = V<float>;
+constexpr F a = F () + 0.0f;
+constexpr F b = F () + (float) 0.0;
+constexpr F c = F () + (float) 0.0L;