https://gcc.gnu.org/g:d2febb9b28d48cec23045e7f7f2a55028f359539
commit r17-5968-gd2febb9b28d48cec23045e7f7f2a55028f359539 Author: Richard Biener <[email protected]> Date: Thu Nov 5 11:26:22 2026 +0100 tree-optimization/128451 - use unsigned index arithmetic when lowering VEC_PERM_EXPR When lowering a VEC_PERM_EXPR piecewise, lower_vec_perm computes the element index by masking the mask element with ELEMENTS - 1 in the mask element type. With a signed mask element type that cannot represent ELEMENTS - 1 (for example signed char with 256 elements) build_int_cst wraps the constant to -1, the masking becomes a no-op and negative mask values result in out-of-bounds ARRAY_REF accesses of the temporary vector. Likewise testing the operand selection bit on the sign-extended constant index wrongly selects the second operand for negative mask values when the number of elements is not representable in the mask element type. Fix this by performing all index computations in the unsigned variant of the mask element type. PR tree-optimization/128451 * tree-vect-generic.cc (lower_vec_perm): Use the unsigned variant of the mask element type for index computations and convert the mask elements to it. * gcc.dg/torture/pr128451.c: New test. Diff: --- gcc/testsuite/gcc.dg/torture/pr128451.c | 37 +++++++++++++++++++++++++++++++++ gcc/tree-vect-generic.cc | 7 ++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/gcc.dg/torture/pr128451.c b/gcc/testsuite/gcc.dg/torture/pr128451.c new file mode 100644 index 000000000000..16411e2d2573 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr128451.c @@ -0,0 +1,37 @@ +/* { dg-do run } */ +/* { dg-additional-options "-Wno-psabi" } */ + +typedef signed char V __attribute__((vector_size (256))); +V x, y, r, m; + +__attribute__((noipa)) void +f (void) +{ + r = __builtin_shuffle (x, m); +} + +__attribute__((noipa)) void +g (void) +{ + r = __builtin_shuffle (x, y, (V) { -1, -128, 2 }); +} + +int +main () +{ + for (int i = 0; i < 256; i++) + { + x[i] = i & 0x7f; + y[i] = -1; + m[i] = i; + } + m[0] = -1; + m[1] = -128; + f (); + if (r[0] != x[255] || r[1] != x[128] || r[2] != x[2]) + __builtin_abort (); + g (); + if (r[0] != x[255] || r[1] != x[128] || r[2] != x[2]) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-vect-generic.cc b/gcc/tree-vect-generic.cc index 79d7bd995507..f9cd17db8b49 100644 --- a/gcc/tree-vect-generic.cc +++ b/gcc/tree-vect-generic.cc @@ -1659,7 +1659,11 @@ lower_vec_perm (gimple_stmt_iterator *gsi) tree vect_type = TREE_TYPE (vec0); tree mask_type = TREE_TYPE (mask); tree vect_elt_type = TREE_TYPE (vect_type); - tree mask_elt_type = TREE_TYPE (mask_type); + /* Perform the index computations in an unsigned type, the mask + elements are interpreted modulo the number of elements and with + a signed type masking with ELEMENTS - 1 can be a no-op when + ELEMENTS - 1 is not representable, leaving negative indices. */ + tree mask_elt_type = unsigned_type_for (TREE_TYPE (mask_type)); unsigned HOST_WIDE_INT elements, in_elements; vec<constructor_elt, va_gc> *v; tree constr, t, si, i_val; @@ -1708,6 +1712,7 @@ lower_vec_perm (gimple_stmt_iterator *gsi) { si = size_int (i); i_val = vector_element (gsi, mask, si, &masktmp); + i_val = fold_convert (mask_elt_type, i_val); if (TREE_CODE (i_val) == INTEGER_CST) {
