[v3,8/8] cp: Fix another assumption in the FE about constant vector indices.

Message ID 20241129035404.3363162-9-tejas.belagod@arm.com
State Committed
Commit b5df3eefd70064973fc5cb41c391d71781b8d3ac
Headers
Series aarch64: Enable C/C++ operations on SVE ACLE types. |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 fail Test failed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed

Commit Message

Tejas Belagod Nov. 29, 2024, 3:54 a.m. UTC
  This patch adds a change to handle VLA's poly indices.

gcc/ChangeLog:

	* cp/decl.cc (reshape_init_array_1): Handle poly indices.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/sve-sizeless-1.C: Update test to test initialize error.
	* g++.dg/ext/sve-sizeless-2.C: Likewise.
---
 gcc/cp/decl.cc                            | 11 ++++++++---
 gcc/testsuite/g++.dg/ext/sve-sizeless-1.C | 11 +++++++++++
 gcc/testsuite/g++.dg/ext/sve-sizeless-2.C |  9 +++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 80485f0a428..4b6a5191a8a 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -6894,15 +6894,20 @@  reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
 
   if (sized_array_p)
     {
+      poly_uint64 midx;
       /* Minus 1 is used for zero sized arrays.  */
       if (integer_all_onesp (max_index))
 	return new_init;
 
-      if (tree_fits_uhwi_p (max_index))
-	max_index_cst = tree_to_uhwi (max_index);
+      if (tree_fits_poly_uint64_p (max_index))
+	midx = tree_to_poly_uint64 (max_index);
       /* sizetype is sign extended, not zero extended.  */
       else
-	max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
+	midx = tree_to_poly_uint64 (fold_convert (size_type_node, max_index));
+
+      /* For VLA vectors, we restict the number of elements in the constructor
+	 to lower bound of the VLA elements.  */
+      max_index_cst = constant_lower_bound (midx);
     }
 
   /* Loop until there are no more initializers.  */
diff --git a/gcc/testsuite/g++.dg/ext/sve-sizeless-1.C b/gcc/testsuite/g++.dg/ext/sve-sizeless-1.C
index adee37a0551..0a5c80b92b8 100644
--- a/gcc/testsuite/g++.dg/ext/sve-sizeless-1.C
+++ b/gcc/testsuite/g++.dg/ext/sve-sizeless-1.C
@@ -124,6 +124,8 @@  void thrower2 () throw (svint8_t); // { dg-error {cannot throw or catch SVE type
 void thrower3 () throw (svint8_t); // { dg-error {cannot throw or catch SVE type 'svint8_t'} "" { target c++98_only } }
 #endif
 
+extern int bar (void);
+
 // Main tests for statements and expressions.
 
 void
@@ -161,6 +163,15 @@  statements (int n)
   svint8_t init_sve_sc5 = {};
   svint8_t init_sve_sc6 = { sve_sc1 };
   svint8_t init_sve_sc7 = { sve_sh1 }; // { dg-error {cannot convert 'svint16_t' to 'svint8_t'} }
+  svint32_t init_sve_vc1 = { 0, 1 };
+  svint32_t init_sve_vc2 = { 0, bar () };
+  svint32_t init_sve_vc3 = { bar (), n };
+  svint32_t init_sve_vc4 = { 0, 1, 2, 3 };
+  svint32_t init_sve_vc5 = { 0, 1, bar (), 3 };
+  svint32_t init_sve_vc6 = { 0, 1, 2, 3, 4 }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc7 = { 0, 1, 2, 3, bar () }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc8 = { 0, 1, 2, 3, 4, 5 }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc9 = { 0, bar (), 2, 3, 4, n }; // { dg-error {too many initializers for 'svint32_t'} }
 
   // Constructor calls.
 
diff --git a/gcc/testsuite/g++.dg/ext/sve-sizeless-2.C b/gcc/testsuite/g++.dg/ext/sve-sizeless-2.C
index 394ac1e4579..87937d060d2 100644
--- a/gcc/testsuite/g++.dg/ext/sve-sizeless-2.C
+++ b/gcc/testsuite/g++.dg/ext/sve-sizeless-2.C
@@ -161,6 +161,15 @@  statements (int n)
   svint8_t init_sve_sc5 = {};
   svint8_t init_sve_sc6 = { sve_sc1 };
   svint8_t init_sve_sc7 = { sve_sh1 }; // { dg-error {cannot convert 'svint16_t' to 'svint8_t'} }
+  svint32_t init_sve_vc1 = { 0, 1 };
+  svint32_t init_sve_vc2 = { 0, bar () };
+  svint32_t init_sve_vc3 = { bar (), n };
+  svint32_t init_sve_vc4 = { 0, 1, 2, 3, 4, 5, 6, 7 };
+  svint32_t init_sve_vc5 = { 0, 1, bar (), 3, 4, 5, n, 7 };
+  svint32_t init_sve_vc6 = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc7 = { 0, 1, 2, 3, bar (), 5, 6, 7, n }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc8 = { 0, bar (), 2, 3, 4, n, 5, 6, 7, 8, 9 }; // { dg-error {too many initializers for 'svint32_t'} }
+  svint32_t init_sve_vc9 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // { dg-error {too many initializers for 'svint32_t'} }
 
   // Constructor calls.