c++: Fix up constant expression __builtin_convertvector folding [PR104472]

Message ID 20220211101348.GO2646553@tucnak
State New
Headers
Series c++: Fix up constant expression __builtin_convertvector folding [PR104472] |

Commit Message

Jakub Jelinek Feb. 11, 2022, 10:13 a.m. UTC
  Hi!

The following testcase ICEs, because due to the -frounding-math
fold_const_call fails, which is it returns NULL, and returning NULL from
cxx_eval* is wrong, all the callers rely on them to either return folded
value or original with *non_constant_p = true.

The following patch does that, and additionally falls through into the
default case where there is diagnostics for the !ctx->quiet case too.

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

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

	PR c++/104472
	* constexpr.cc (cxx_eval_internal_function) <case IFN_VEC_CONVERT>:
	Only return fold_const_call result if it is non-NULL.  Otherwise
	fall through into the default: case to return t, set *non_constant_p
	and emit diagnostics if needed.

	* g++.dg/cpp0x/constexpr-104472.C: New test.


	Jakub
  

Comments

Jason Merrill Feb. 11, 2022, 12:48 p.m. UTC | #1
On 2/11/22 05:13, Jakub Jelinek wrote:
> Hi!
> 
> The following testcase ICEs, because due to the -frounding-math
> fold_const_call fails, which is it returns NULL, and returning NULL from
> cxx_eval* is wrong, all the callers rely on them to either return folded
> value or original with *non_constant_p = true.
> 
> The following patch does that, and additionally falls through into the
> default case where there is diagnostics for the !ctx->quiet case too.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2022-02-11  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/104472
> 	* constexpr.cc (cxx_eval_internal_function) <case IFN_VEC_CONVERT>:
> 	Only return fold_const_call result if it is non-NULL.  Otherwise
> 	fall through into the default: case to return t, set *non_constant_p
> 	and emit diagnostics if needed.
> 
> 	* g++.dg/cpp0x/constexpr-104472.C: New test.
> 
> --- gcc/cp/constexpr.cc.jj	2022-02-08 20:15:33.997295271 +0100
> +++ gcc/cp/constexpr.cc	2022-02-10 12:33:48.243321592 +0100
> @@ -1840,13 +1840,10 @@ cxx_eval_internal_function (const conste
>   						 false, non_constant_p,
>   						 overflow_p);
>   	if (TREE_CODE (arg) == VECTOR_CST)
> -	  return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg);
> -	else
> -	  {
> -	    *non_constant_p = true;
> -	    return t;
> -	  }
> +	  if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
> +	    return r;
>         }
> +      /* FALLTHRU */
>   
>       default:
>         if (!ctx->quiet)
> --- gcc/testsuite/g++.dg/cpp0x/constexpr-104472.C.jj	2022-02-10 12:44:09.869720657 +0100
> +++ gcc/testsuite/g++.dg/cpp0x/constexpr-104472.C	2022-02-10 12:46:25.881838766 +0100
> @@ -0,0 +1,9 @@
> +// PR c++/104472
> +// { dg-options "-O2 -frounding-math" }
> +// { dg-add-options float16 }
> +// { dg-require-effective-target float16 }
> +
> +typedef short __attribute__((__vector_size__ (16))) V;
> +typedef _Float16 __attribute__((__vector_size__ (16))) F;
> +
> +V v = __builtin_convertvector (__builtin_convertvector ((V){5534}, F), V) < 8;
> 
> 	Jakub
>
  

Patch

--- gcc/cp/constexpr.cc.jj	2022-02-08 20:15:33.997295271 +0100
+++ gcc/cp/constexpr.cc	2022-02-10 12:33:48.243321592 +0100
@@ -1840,13 +1840,10 @@  cxx_eval_internal_function (const conste
 						 false, non_constant_p,
 						 overflow_p);
 	if (TREE_CODE (arg) == VECTOR_CST)
-	  return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg);
-	else
-	  {
-	    *non_constant_p = true;
-	    return t;
-	  }
+	  if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
+	    return r;
       }
+      /* FALLTHRU */
 
     default:
       if (!ctx->quiet)
--- gcc/testsuite/g++.dg/cpp0x/constexpr-104472.C.jj	2022-02-10 12:44:09.869720657 +0100
+++ gcc/testsuite/g++.dg/cpp0x/constexpr-104472.C	2022-02-10 12:46:25.881838766 +0100
@@ -0,0 +1,9 @@ 
+// PR c++/104472
+// { dg-options "-O2 -frounding-math" }
+// { dg-add-options float16 }
+// { dg-require-effective-target float16 }
+
+typedef short __attribute__((__vector_size__ (16))) V;
+typedef _Float16 __attribute__((__vector_size__ (16))) F;
+
+V v = __builtin_convertvector (__builtin_convertvector ((V){5534}, F), V) < 8;