public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871]
@ 2022-06-08  6:08 Jakub Jelinek
  2022-06-09 12:49 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-06-08  6:08 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches

Hi!

As the following testcase shows, BIT_FIELD_REF result doesn't have to have
just integral type, it can also have vector type.  And in that case
cxx_eval_bit_field_ref just ICEs on it because it is unprepared for that
case, creates the initial value with build_int_cst (sure, that one could be
easily replaced with build_zero_cst) and then expects it can through shifts,
ands and ors come up with the final value, but that doesn't work for
vectors.

We already call fold_ternary if whole is a VECTOR_CST, this patch does the
same if the result doesn't have integral type.  And, there is no guarantee
fold_ternary will succeed and the callers certainly don't expect NULL
being returned, so it also diagnoses those as non-constant and returns
original t in that case.

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

2022-06-08  Jakub Jelinek  <jakub@redhat.com>

	PR c++/105871
	* constexpr.cc (cxx_eval_bit_field_ref): For BIT_FIELD_REF with
	non-integral result type use fold_ternary too like for BIT_FIELD_REFs
	from VECTOR_CST.  If fold_ternary returns NULL, diagnose non-constant
	expression, set *non_constant_p and return t, instead of returning
	NULL.

	* g++.dg/pr105871.C: New test.

--- gcc/cp/constexpr.cc.jj	2022-06-03 11:20:13.000000000 +0200
+++ gcc/cp/constexpr.cc	2022-06-07 13:43:13.157127740 +0200
@@ -4198,9 +4198,16 @@ cxx_eval_bit_field_ref (const constexpr_
   if (*non_constant_p)
     return t;
 
-  if (TREE_CODE (whole) == VECTOR_CST)
-    return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
-			 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
+  if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
+    {
+      if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
+				 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
+	return r;
+      if (!ctx->quiet)
+	error ("%qE is not a constant expression", orig_whole);
+      *non_constant_p = true;
+      return t;
+    }
 
   start = TREE_OPERAND (t, 2);
   istart = tree_to_shwi (start);
--- gcc/testsuite/g++.dg/pr105871.C.jj	2022-06-07 13:56:02.743241969 +0200
+++ gcc/testsuite/g++.dg/pr105871.C	2022-06-07 13:56:29.042975525 +0200
@@ -0,0 +1,12 @@
+// PR c++/105871
+// { dg-do compile }
+// { dg-options "-Wno-psabi" }
+
+typedef __attribute__((__vector_size__ ( 1))) unsigned char U;
+typedef __attribute__((__vector_size__ (16))) unsigned char V;
+
+U
+foo (void)
+{
+  return __builtin_shufflevector ((U){}, (V){}, 0);
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871]
  2022-06-08  6:08 [PATCH] c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871] Jakub Jelinek
@ 2022-06-09 12:49 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-06-09 12:49 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On 6/8/22 02:08, Jakub Jelinek wrote:
> Hi!
> 
> As the following testcase shows, BIT_FIELD_REF result doesn't have to have
> just integral type, it can also have vector type.  And in that case
> cxx_eval_bit_field_ref just ICEs on it because it is unprepared for that
> case, creates the initial value with build_int_cst (sure, that one could be
> easily replaced with build_zero_cst) and then expects it can through shifts,
> ands and ors come up with the final value, but that doesn't work for
> vectors.
> 
> We already call fold_ternary if whole is a VECTOR_CST, this patch does the
> same if the result doesn't have integral type.  And, there is no guarantee
> fold_ternary will succeed and the callers certainly don't expect NULL
> being returned, so it also diagnoses those as non-constant and returns
> original t in that case.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2022-06-08  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/105871
> 	* constexpr.cc (cxx_eval_bit_field_ref): For BIT_FIELD_REF with
> 	non-integral result type use fold_ternary too like for BIT_FIELD_REFs
> 	from VECTOR_CST.  If fold_ternary returns NULL, diagnose non-constant
> 	expression, set *non_constant_p and return t, instead of returning
> 	NULL.
> 
> 	* g++.dg/pr105871.C: New test.
> 
> --- gcc/cp/constexpr.cc.jj	2022-06-03 11:20:13.000000000 +0200
> +++ gcc/cp/constexpr.cc	2022-06-07 13:43:13.157127740 +0200
> @@ -4198,9 +4198,16 @@ cxx_eval_bit_field_ref (const constexpr_
>     if (*non_constant_p)
>       return t;
>   
> -  if (TREE_CODE (whole) == VECTOR_CST)
> -    return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
> -			 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
> +  if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
> +    {
> +      if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
> +				 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
> +	return r;
> +      if (!ctx->quiet)
> +	error ("%qE is not a constant expression", orig_whole);
> +      *non_constant_p = true;
> +      return t;
> +    }
>   
>     start = TREE_OPERAND (t, 2);
>     istart = tree_to_shwi (start);
> --- gcc/testsuite/g++.dg/pr105871.C.jj	2022-06-07 13:56:02.743241969 +0200
> +++ gcc/testsuite/g++.dg/pr105871.C	2022-06-07 13:56:29.042975525 +0200
> @@ -0,0 +1,12 @@
> +// PR c++/105871
> +// { dg-do compile }
> +// { dg-options "-Wno-psabi" }
> +
> +typedef __attribute__((__vector_size__ ( 1))) unsigned char U;
> +typedef __attribute__((__vector_size__ (16))) unsigned char V;
> +
> +U
> +foo (void)
> +{
> +  return __builtin_shufflevector ((U){}, (V){}, 0);
> +}
> 
> 	Jakub
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-09 12:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08  6:08 [PATCH] c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871] Jakub Jelinek
2022-06-09 12:49 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).