https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129111

            Bug ID: 129111
           Summary: [14/15/16/17 Regression] aarch64: wrong gather address
                    at -O3 with SVE for a strided load in a loop counting
                    down
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: aarch64-sve, wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hhhuang at smu dot edu.sg
  Target Milestone: ---
            Target: aarch64*-*-*

Compiler Explorer (assembly only): https://godbolt.org/z/f8fqfcs5r

Note:
- works: 13.4
- fails: 14.1 to trunk (bc49f1e37014, 20261127) at -O3 with SVE
- fine at -O2, without SVE, and with -fno-tree-loop-vectorize
- -fno-aggressive-loop-optimizations hides it
- filed separately as suggested in bug 121393 comment 10

$ aarch64-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc-17
Target: aarch64-linux-gnu
Configured with: ../gcc-17-20261127/configure --prefix=/w/out/gcc-trunk
--program-suffix=-17 --enable-languages=c --disable-bootstrap
--disable-multilib --disable-nls --disable-libsanitizer
--target=aarch64-linux-gnu --program-prefix=aarch64-linux-gnu-
--with-sysroot=/usr/aarch64-linux-gnu --with-native-system-header-dir=/include
--with-as=/usr/bin/aarch64-linux-gnu-as --with-ld=/usr/bin/aarch64-linux-gnu-ld
--disable-shared --disable-threads --disable-libssp --disable-libgomp
--disable-libquadmath --disable-libatomic
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261127 (experimental) (GCC)
$ aarch64-linux-gnu-gcc-17 -O3 -march=armv8.2-a+sve -static t.c
$ qemu-aarch64 -cpu max ./a.out
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
$ aarch64-linux-gnu-gcc-17 -O2 -march=armv8.2-a+sve -static t.c
$ qemu-aarch64 -cpu max ./a.out
$ cat t.c
unsigned a[100], b[400];

__attribute__((noipa)) void
f (unsigned n)
{
  for (unsigned i = n; i-- > 0;)
    a[i] = b[i * 4];
}

int
main (void)
{
  for (unsigned i = 0; i < 400; i++)
    b[i] = i;
  f (100);                      /* a[i] = b[4 * i] for i = 99 .. 0 */
  for (unsigned i = 0; i < 100; i++)
    if (a[i] != 4 * i)
      __builtin_abort ();
  return 0;
}

The loop has no undefined behaviour: i is unsigned, it reads b[396] .. b[0].
No warnings with -Wall -Wextra.
Same with -fno-strict-aliasing -fwrapv.
-fsanitize=address,undefined is clean on x86_64 (gcc 16.2).

In f the base of the gather, x1, is the first access, b[(n - 1) * 4].
The 32-bit offsets are 0, 4294967292, 4294967288, ... and are zero-extended:

        index   z31.s, #0, #-4
        ld1w    z30.s, p7/z, [x1, z31.s, uxtw 2]

13.4 gathers from &b (x4) with the offsets i * 4:

        lsl     z0.s, z1.s, #2
        ld1w    z0.s, p0/z, [x4, z0.s, uxtw 2]

Reply via email to