middle-end/112622 - convert and vector-to-float

Message ID 20231120140107.821F83890433@sourceware.org
State Accepted
Headers
Series middle-end/112622 - convert and vector-to-float |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Richard Biener Nov. 20, 2023, 2 p.m. UTC
  The following avoids ICEing when trying to convert a vector to
a scalar float.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	PR middle-end/112622
	* convert.cc (convert_to_real_1): Use element_precision
	where a vector type might appear.  Provide specific
	diagnostic for unexpected vector argument.

	* gcc.dg/pr112622.c: New testcase.
---
 gcc/convert.cc                                            | 6 +++++-
 gcc/testsuite/gcc.dg/pr112622.c                           | 5 +++++
 gcc/testsuite/gcc.dg/simd-2.c                             | 4 ++--
 gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_1.c | 8 ++++----
 gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_2.c | 8 ++++----
 5 files changed, 20 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr112622.c
  

Patch

diff --git a/gcc/convert.cc b/gcc/convert.cc
index ac6af7026a7..46c8bcb31f8 100644
--- a/gcc/convert.cc
+++ b/gcc/convert.cc
@@ -292,7 +292,7 @@  convert_to_real_1 (tree type, tree expr, bool fold_p)
 	case NEGATE_EXPR:
 	  if (!flag_rounding_math
 	      && FLOAT_TYPE_P (itype)
-	      && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
+	      && element_precision (type) < element_precision (itype))
 	    {
 	      tree arg = convert_to_real_1 (type, TREE_OPERAND (expr, 0),
 					    fold_p);
@@ -334,6 +334,10 @@  convert_to_real_1 (tree type, tree expr, bool fold_p)
       error ("pointer value used where a floating-point was expected");
       return error_mark_node;
 
+    case VECTOR_TYPE:
+      error ("vector value used where a floating-point was expected");
+      return error_mark_node;
+
     default:
       error ("aggregate value used where a floating-point was expected");
       return error_mark_node;
diff --git a/gcc/testsuite/gcc.dg/pr112622.c b/gcc/testsuite/gcc.dg/pr112622.c
new file mode 100644
index 00000000000..c73587ced99
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr112622.c
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+
+typedef __attribute__((__vector_size__(4))) float V;
+
+V v = (float)-v; /* { dg-error "vector value used" } */
diff --git a/gcc/testsuite/gcc.dg/simd-2.c b/gcc/testsuite/gcc.dg/simd-2.c
index d24e076218d..411bb49a5e1 100644
--- a/gcc/testsuite/gcc.dg/simd-2.c
+++ b/gcc/testsuite/gcc.dg/simd-2.c
@@ -33,10 +33,10 @@  hanneke ()
   foo = a; /* { dg-error "incompatible types when assigning" } */
 
   /* Casted assignment between scalar and SIMD of same size.  */
-  foo = (typeof (foo)) foo2; /* { dg-error "aggregate value used where a floating-point was expected" } */
+  foo = (typeof (foo)) foo2; /* { dg-error "vector value used where a floating-point was expected" } */
 
   /* Casted assignment between scalar and SIMD of different size.  */
-  foo1 = (typeof (foo1)) foo2; /* { dg-error "aggregate value used where a floating-point was expected" } */
+  foo1 = (typeof (foo1)) foo2; /* { dg-error "vector value used where a floating-point was expected" } */
 
   /* Operators on compatible SIMD types.  */
   a += b + b;
diff --git a/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_1.c b/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_1.c
index 8bc3f4a7b8f..5d19dcb7b6d 100644
--- a/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_1.c
+++ b/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_1.c
@@ -123,12 +123,12 @@  __m128bf16 footest (__m128bf16 vector0)
   (void) glob_bfloat_vec;
   (__m128bf16) glob_bfloat_vec;
 
-  (__bf16) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
+  (__bf16) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
   (short) glob_bfloat_vec; /* { dg-error {cannot convert a vector of type '__m128bf16' {aka '__vector\(8\) __bf16'} to type 'short int' which has different size} } */
   (int) glob_bfloat_vec; /* { dg-error {cannot convert a vector of type '__m128bf16' {aka '__vector\(8\) __bf16'} to type 'int' which has different size} } */
-  (_Float16) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
-  (float) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
-  (double) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
+  (_Float16) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
+  (float) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
+  (double) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
 
   (__v8si) glob_bfloat_vec; /* { dg-error {cannot convert a value of type '__m128bf16' {aka '__vector\(8\) __bf16'} to vector type '__vector\(8\) int' which has different size} } */
   (__m256) glob_bfloat_vec; /* { dg-error {cannot convert a value of type '__m128bf16' {aka '__vector\(8\) __bf16'} to vector type '__vector\(8\) float' which has different size} } */
diff --git a/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_2.c b/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_2.c
index 2a8a535daae..d4e6fc8cd23 100644
--- a/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_2.c
+++ b/gcc/testsuite/gcc.target/i386/vect-bfloat16-typecheck_2.c
@@ -116,12 +116,12 @@  __m256bf16 footest (__m256bf16 vector0)
   (void) glob_bfloat_vec;
   (__m256bf16) glob_bfloat_vec;
 
-  (__bf16) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
+  (__bf16) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
   (short) glob_bfloat_vec; /* { dg-error {cannot convert a vector of type '__m256bf16' {aka '__vector\(16\) __bf16'} to type 'short int' which has different size} } */
   (int) glob_bfloat_vec; /* { dg-error {cannot convert a vector of type '__m256bf16' {aka '__vector\(16\) __bf16'} to type 'int' which has different size} } */
-  (_Float16) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
-  (float) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
-  (double) glob_bfloat_vec; /* { dg-error {aggregate value used where a floating-point was expected} } */
+  (_Float16) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
+  (float) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
+  (double) glob_bfloat_vec; /* { dg-error {vector value used where a floating-point was expected} } */
 
   (__v8si) glob_bfloat_vec;
   (__m256) glob_bfloat_vec;