]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect: Use type precision in reduction epilogue [PR123097].
authorRobin Dapp <rdapp@ventanamicro.com>
Fri, 12 Dec 2025 08:52:16 +0000 (09:52 +0100)
committerRobin Dapp <rdapp@oss.qualcomm.com>
Fri, 19 Dec 2025 18:41:53 +0000 (19:41 +0100)
In the PR we extract non-existent bits/elements from a vector.  This is
because we use TYPE_SIZE (vectype) for a boolean vector which returns 8
instead of 4 for RVV's vector (4) <signed-boolean:1>.

The patch uses TYPE_VECTOR_SUBPARTS instead and multiplies its result
with vector_element_bits to get the proper number of elements and size.

PR tree-optimization/123097

gcc/ChangeLog:

* tree-vect-loop.cc (vect_create_epilog_for_reduction):
Calculate vector size by number of elements * bit size per
element.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/autovec/pr123097-run.c: New test.
* gcc.target/riscv/rvv/autovec/pr123097.c: New test.

gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097-run.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097.c [new file with mode: 0644]
gcc/tree-vect-loop.cc

diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097-run.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097-run.c
new file mode 100644 (file)
index 0000000..f536c82
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target riscv_v_ok } */
+/* { dg-options "-O3 -march=rv64gcv -mabi=lp64d" } */
+
+long long a;
+int b[22];
+_Bool c[22];
+
+int main() {
+    for (long f=0; f<10; ++f)
+      b[f] = c[f] = 1;
+    for (int f=0; f<5; f++)
+      for( int g=0; g<5; g++)
+        c[g*2] &= (_Bool)b[2*f];
+    for (long f=0; f<2; ++f)
+      a ^= c[f];
+    if (a != 0)
+      __builtin_abort ();
+}
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr123097.c
new file mode 100644 (file)
index 0000000..7f15389
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -march=rv64gcv -mabi=lp64d -fdump-tree-optimized" } */
+
+long long a;
+int b[22];
+_Bool c[22];
+
+int main() {
+    for (long f=0; f<10; ++f)
+      b[f] = c[f] = 1;
+    for (int f=0; f<5; f++)
+      for( int g=0; g<5; g++)
+        c[g*2] &= (_Bool)b[2*f];
+    for (long f=0; f<2; ++f)
+      a ^= c[f];
+    if (a != 0)
+      __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "BIT_FIELD_REF <\\{ -1, -1, -1, -1 \\}," "optimized" } } */
index 00b21ecfc9586501d0e68e2ecacdfb5c013df0fa..b15589a711b135d661eff5773811a6aa09d5e56a 100644 (file)
@@ -6019,13 +6019,11 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
 
       if (reduce_with_shift && (!slp_reduc || group_size == 1))
        {
-         tree bitsize = TYPE_SIZE (TREE_TYPE (vectype1));
-         int element_bitsize = tree_to_uhwi (bitsize);
+         int element_bitsize = vector_element_bits (vectype1);
          /* Enforced by vectorizable_reduction, which disallows SLP reductions
             for variable-length vectors and also requires direct target support
             for loop reductions.  */
-         int vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype1));
-         int nelements = vec_size_in_bits / element_bitsize;
+         int nelements = TYPE_VECTOR_SUBPARTS (vectype1).to_constant ();
          vec_perm_builder sel;
          vec_perm_indices indices;
 
@@ -6066,7 +6064,8 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
                             "extract scalar result\n");
 
          new_temp = gimple_build (&stmts, BIT_FIELD_REF, TREE_TYPE (vectype1),
-                                  new_temp, bitsize, bitsize_zero_node);
+                                  new_temp, bitsize_int (element_bitsize),
+                                  bitsize_zero_node);
          new_temp = gimple_convert (&stmts, scalar_type, new_temp);
          gsi_insert_seq_before (&exit_gsi, stmts, GSI_SAME_STMT);
          scalar_results.safe_push (new_temp);
@@ -6088,8 +6087,9 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
                             "Reduce using scalar code.\n");
 
          tree compute_type = TREE_TYPE (vectype1);
-         unsigned vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype1));
          unsigned element_bitsize = vector_element_bits (vectype1);
+         unsigned vec_size_in_bits = element_bitsize
+           * TYPE_VECTOR_SUBPARTS (vectype1).to_constant ();
          tree bitsize = bitsize_int (element_bitsize);
          gimple_seq stmts = NULL;
          FOR_EACH_VEC_ELT (reduc_inputs, i, vec_temp)