https://gcc.gnu.org/g:9b007ab575a4a71f4b5458fc214eee87f31d6a21
commit r17-6341-g9b007ab575a4a71f4b5458fc214eee87f31d6a21 Author: Kyrylo Tkachov <[email protected]> Date: Wed Nov 11 05:12:10 2026 -0800 aarch64: Do vaba accumulation in unsigned type [PR128556] vaba_<s> intrinsics are folded to ABD followed by a PLUS_EXPR on the accumulator. For signed element types the PLUS_EXPR is in the signed type, which has undefined overflow although the intrinsic wraps. Later folds then assume no overflow. For example vshr_n_s8 (vaba_s8 (a, x, y), 1) becomes an AVG_FLOOR and returns a value that is not truncated to 8 bits. Before: _2 = .ABD (x, y); _1 = acc + _2; (signed, UB on overflow) After: _2 = .ABD (x, y); _3 = VIEW_CONVERT_EXPR<uvec> (acc); _4 = VIEW_CONVERT_EXPR<uvec> (_2); _5 = _3 + _4; _1 = VIEW_CONVERT_EXPR<svec> (_5); This is the same approach gimple_arith_base::fold uses for vadd. The saba instruction is still generated for the plain accumulate case. Bootstrapped and tested on aarch64-none-linux-gnu. gcc/ChangeLog: PR target/128556 * config/aarch64/aarch64-neon-builtins-base.cc (gimple_abs_diff::fold): Perform the accumulation in the unsigned type. gcc/testsuite/ChangeLog: PR target/128556 * gcc.target/aarch64/simd/pr128556.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/config/aarch64/aarch64-neon-builtins-base.cc | 9 ++++++- gcc/testsuite/gcc.target/aarch64/simd/pr128556.c | 33 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gcc/config/aarch64/aarch64-neon-builtins-base.cc b/gcc/config/aarch64/aarch64-neon-builtins-base.cc index 718b9d543551..4dd4f684fed2 100644 --- a/gcc/config/aarch64/aarch64-neon-builtins-base.cc +++ b/gcc/config/aarch64/aarch64-neon-builtins-base.cc @@ -1311,7 +1311,14 @@ struct gimple_abs_diff : public gimple_function_base if (accumulate) { auto acc = gimple_call_arg (f.call, 0); - ret = fold_build2 (PLUS_EXPR, TREE_TYPE (acc), acc, ret); + auto type = TREE_TYPE (acc); + /* The accumulation wraps, so do it in the unsigned type to avoid + introducing signed overflow. */ + auto uns_type = unsigned_type_for (type); + ret = f.force_val (fold_build2 (PLUS_EXPR, uns_type, + f.force_val (build_cast (uns_type, acc)), + f.force_val (build_cast (uns_type, ret)))); + ret = build_cast (type, ret); } return this->maybe_optimization_barrier (f, ret); diff --git a/gcc/testsuite/gcc.target/aarch64/simd/pr128556.c b/gcc/testsuite/gcc.target/aarch64/simd/pr128556.c new file mode 100644 index 000000000000..0e8e405d6bec --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/simd/pr128556.c @@ -0,0 +1,33 @@ +/* PR target/128556 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include <arm_neon.h> + +__attribute__((noipa)) int8x8_t +f (int8x8_t acc, int8x8_t x, int8x8_t y) +{ + return vshr_n_s8 (vaba_s8 (acc, x, y), 1); +} + +__attribute__((noipa)) int8x16_t +g (int8x16_t acc, int8x16_t x, int8x16_t y) +{ + return vshrq_n_s8 (vabaq_s8 (acc, x, y), 1); +} + +int +main (void) +{ + int8_t acc[16] = { 100 }; + int8_t xs[16] = { 0 }; + int8_t ys[16] = { 100 }; + + /* The accumulation wraps: (int8_t) (100 + 100) = -56. */ + if (vget_lane_s8 (f (vld1_s8 (acc), vld1_s8 (xs), vld1_s8 (ys)), 0) != -28) + __builtin_abort (); + if (vgetq_lane_s8 (g (vld1q_s8 (acc), vld1q_s8 (xs), vld1q_s8 (ys)), 0) + != -28) + __builtin_abort (); + return 0; +}
