]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
isel: Check bounds before converting VIEW_CONVERT to VEC_SET.
authorAvinash Jayakar <avinashd@linux.ibm.com>
Sat, 8 Nov 2025 04:27:59 +0000 (09:57 +0530)
committerAvinash Jayakar <avinashd@linux.ibm.com>
Sat, 8 Nov 2025 12:28:45 +0000 (17:58 +0530)
The function gimple_expand_vec_set_expr in the isel pass, converted
VIEW_CONVERT_EXPR to VEC_SET_EXPR without checking the bounds on the index,
which cause ICE on targets that supported VEC_SET_EXPR like x86 and powerpc.
This patch adds a bound check on the index operand and rejects the conversion
if index is out of bound.

2025-11-08  Avinash Jayakar  <avinashd@linux.ibm.com>

gcc/ChangeLog:
PR tree-optimization/122126
* gimple-isel.cc (gimple_expand_vec_set_extract_expr): Add bound check.

gcc/testsuite/ChangeLog:
PR tree-optimization/122126
* gcc.dg/pr122126_vextr.c: New test.
* gcc.dg/pr122126_vset.c: New test.

gcc/gimple-isel.cc
gcc/testsuite/gcc.dg/pr122126_vextr.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr122126_vset.c [new file with mode: 0644]

index b5dc579ff4679b0141f2b907e1aa6b12a7a9672f..e745608904e3db77a88d861bc6512afe67a82480 100644 (file)
@@ -102,6 +102,16 @@ gimple_expand_vec_set_extract_expr (struct function *fun,
       tree pos = TREE_OPERAND (ref, 1);
 
       tree view_op0 = TREE_OPERAND (op0, 0);
+
+      tree idx = TREE_OPERAND (ref, 1);
+      // if index is a constant, then check the bounds
+      poly_uint64 idx_poly;
+      if (poly_int_tree_p (idx, &idx_poly))
+       {
+         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (view_op0));
+         if (known_gt (idx_poly, nelts))
+           return false;
+       }
       machine_mode outermode = TYPE_MODE (TREE_TYPE (view_op0));
       machine_mode extract_mode = TYPE_MODE (TREE_TYPE (ref));
 
diff --git a/gcc/testsuite/gcc.dg/pr122126_vextr.c b/gcc/testsuite/gcc.dg/pr122126_vextr.c
new file mode 100644 (file)
index 0000000..b598aa0
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-additional-options "-mavx2" { target avx2 } } */
+
+#define vect16 __attribute__((vector_size(16)))
+void ub_set() {
+  volatile vect16 unsigned BS_VAR_0;
+  unsigned a = BS_VAR_0[12];
+}
diff --git a/gcc/testsuite/gcc.dg/pr122126_vset.c b/gcc/testsuite/gcc.dg/pr122126_vset.c
new file mode 100644 (file)
index 0000000..85b2c68
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-additional-options "-mavx2" { target avx2 } } */
+
+#define vect16 __attribute__((vector_size(16)))
+void ub_set() {
+  volatile vect16 unsigned BS_VAR_0;
+  BS_VAR_0[12] = 4;
+}