https://gcc.gnu.org/g:f2109616b64f21d9c2f94719df4cf02adbdacf19
commit r17-6044-gf2109616b64f21d9c2f94719df4cf02adbdacf19 Author: Tamar Christina <[email protected]> Date: Mon Nov 9 08:03:08 2026 +0000 AArch64: disable vect mode auto-detection when using aarch64_force_autovec_mode When using --param=aarch64-autovec-mode=VNx4QI, we currently still allow the vectorizer to try an automatically selected mode first. The generic code starts the candidate list with VOIDmode, which asks the vectorizer to choose a mode from the loop's element types. It then calls the target hook to add the other candidates. When aarch64_force_autovec_mode is set, the AArch64 hook appends that mode and returns zero. But returning zero only disables cost comparisons between candidates. It doesn't remove the VOIDmode entry already in the list. If the first attempt succeeds, we can therefore finish without trying the mode requested on the command line. For example, with --param=aarch64-autovec-mode=VNx4QI we want to use the partial-vector policy with 32-bit containers for byte and halfword elements. Starting from a full vector can instead leave us with widening unpacks that the partial mode was intended to avoid. This patch clears the candidate list before adding the forced mode. That leaves the requested mode as the only candidate and avoids the initial autodetection attempt. The tests cover full and partial SVE modes and the Advanced SIMD policies, using loops over bytes, halfwords and words. They also check that the forced mode takes priority over a conflicting -mautovec-preference, including with the unlimited cost model, and that VNx4QI uses widening byte loads without integer unpacks. gcc/ChangeLog: * config/aarch64/aarch64.cc (aarch64_autovectorize_vector_modes): Clear the candidate list before adding aarch64_force_autovec_mode, removing the initial autodetection entry when a mode is forced. gcc/testsuite/ChangeLog: * gcc.target/aarch64/force_autovec_mode.h: New test support header. * gcc.target/aarch64/force_autovec_mode_1.c: New test. * gcc.target/aarch64/force_autovec_mode_2.c: New test. * gcc.target/aarch64/force_autovec_mode_3.c: New test. * gcc.target/aarch64/force_autovec_mode_4.c: New test. * gcc.target/aarch64/sve/force_autovec_mode_1.c: New test. * gcc.target/aarch64/sve/force_autovec_mode_2.c: New test. * gcc.target/aarch64/sve/force_autovec_mode_3.c: New test. * gcc.target/aarch64/sve/force_autovec_mode_4.c: New test. * gcc.target/aarch64/sve/force_autovec_mode_5.c: New test. Diff: --- gcc/config/aarch64/aarch64.cc | 1 + .../gcc.target/aarch64/force_autovec_mode.h | 24 +++++++++++++++++++++ .../gcc.target/aarch64/force_autovec_mode_1.c | 8 +++++++ .../gcc.target/aarch64/force_autovec_mode_2.c | 7 ++++++ .../gcc.target/aarch64/force_autovec_mode_3.c | 7 ++++++ .../gcc.target/aarch64/force_autovec_mode_4.c | 7 ++++++ .../gcc.target/aarch64/sve/force_autovec_mode_1.c | 25 ++++++++++++++++++++++ .../gcc.target/aarch64/sve/force_autovec_mode_2.c | 11 ++++++++++ .../gcc.target/aarch64/sve/force_autovec_mode_3.c | 7 ++++++ .../gcc.target/aarch64/sve/force_autovec_mode_4.c | 7 ++++++ .../gcc.target/aarch64/sve/force_autovec_mode_5.c | 7 ++++++ 11 files changed, 111 insertions(+) diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 63926a62a983..afe817408475 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -24280,6 +24280,7 @@ aarch64_autovectorize_vector_modes (vector_modes *modes, bool) if (aarch64_force_autovec_mode != VOIDmode) { + modes->truncate (0); modes->safe_push (aarch64_force_autovec_mode); return 0; } diff --git a/gcc/testsuite/gcc.target/aarch64/force_autovec_mode.h b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode.h new file mode 100644 index 000000000000..169e8bea95d4 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode.h @@ -0,0 +1,24 @@ +/* Exercise the mode policy with different element widths. */ + +void +add_byte (unsigned char *restrict dst, const unsigned char *restrict src, + unsigned n) +{ + for (unsigned i = 0; i < n; ++i) + dst[i] = src[i] + 1; +} + +void +add_halfword (unsigned short *restrict dst, const unsigned short *restrict src, + unsigned n) +{ + for (unsigned i = 0; i < n; ++i) + dst[i] = src[i] + 1; +} + +void +add_word (unsigned *restrict dst, const unsigned *restrict src, unsigned n) +{ + for (unsigned i = 0; i < n; ++i) + dst[i] = src[i] + 1; +} diff --git a/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_1.c b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_1.c new file mode 100644 index 000000000000..d032f46db78c --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_1.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -march=armv8.2-a+sve -msve-vector-bits=scalable -mautovec-preference=sve-only --param=aarch64-autovec-mode=V8QI -fdump-tree-vect-details" } */ + +/* The forced Advanced SIMD mode takes priority over the SVE preference. */ +#include "force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode V8QI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_2.c b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_2.c new file mode 100644 index 000000000000..ce761f4f58b7 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_2.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -march=armv8-a --param=aarch64-autovec-mode=V16QI -fdump-tree-vect-details" } */ + +#include "force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode V16QI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_3.c b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_3.c new file mode 100644 index 000000000000..5cbd06ef8b3b --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -march=armv8-a --param=aarch64-autovec-mode=V4HI -fdump-tree-vect-details" } */ + +#include "force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode V4HI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_4.c b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_4.c new file mode 100644 index 000000000000..6f82daa3051f --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/force_autovec_mode_4.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -march=armv8-a --param=aarch64-autovec-mode=V2SI -fdump-tree-vect-details" } */ + +#include "force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode V2SI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_1.c b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_1.c new file mode 100644 index 000000000000..a65c0d64ca44 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_1.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -msve-vector-bits=scalable -mautovec-preference=sve-only --param=aarch64-autovec-mode=VNx4QI -fdump-tree-vect-details" } */ + +void +byte_first (unsigned *restrict dst, const unsigned char *restrict src, + unsigned bias, unsigned n) +{ + for (unsigned i = 0; i < n; ++i) + dst[i] = (unsigned) src[i] + bias; +} + +/* The first data reference selects a full float vector. The byte load + must still follow the forced partial-vector policy. */ +void +float_first (float *restrict dst, const float *restrict x, + const unsigned char *restrict src, unsigned n) +{ + for (unsigned i = 0; i < n; ++i) + dst[i] = x[i] + (float) src[i]; +} + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 2 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode VNx4QI" 2 "vect" } } */ +/* { dg-final { scan-assembler {\tld1b\tz[0-9]+\.s,} } } */ +/* { dg-final { scan-assembler-not {\t[su]unpk} } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_2.c b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_2.c new file mode 100644 index 000000000000..689bef25910f --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_2.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -msve-vector-bits=scalable -mautovec-preference=asimd-only --param=aarch64-autovec-mode=VNx4QI -fvect-cost-model=unlimited -fdump-tree-vect-details" } */ + +/* The forced SVE mode takes priority over the Advanced SIMD preference, + including when cost comparisons are disabled. */ +#include "force_autovec_mode_1.c" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 2 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode VNx4QI" 2 "vect" } } */ +/* { dg-final { scan-assembler {\tld1b\tz[0-9]+\.s,} } } */ +/* { dg-final { scan-assembler-not {\t[su]unpk} } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_3.c b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_3.c new file mode 100644 index 000000000000..b3f4161f5e52 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -msve-vector-bits=scalable -mautovec-preference=sve-only --param=aarch64-autovec-mode=VNx16QI -fdump-tree-vect-details" } */ + +#include "../force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode VNx16QI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_4.c b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_4.c new file mode 100644 index 000000000000..6f8d1f552e38 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_4.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -msve-vector-bits=scalable -mautovec-preference=sve-only --param=aarch64-autovec-mode=VNx8QI -fdump-tree-vect-details" } */ + +#include "../force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode VNx8QI" 3 "vect" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_5.c b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_5.c new file mode 100644 index 000000000000..c32355a8e0be --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/force_autovec_mode_5.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -msve-vector-bits=scalable -mautovec-preference=sve-only --param=aarch64-autovec-mode=VNx2QI -fdump-tree-vect-details" } */ + +#include "../force_autovec_mode.h" + +/* { dg-final { scan-tree-dump-times "vectorized 1 loops in function" 3 "vect" } } */ +/* { dg-final { scan-tree-dump-times "Choosing vector mode VNx2QI" 3 "vect" } } */
