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

            Bug ID: 129063
           Summary: [15/16/17 Regression] aarch64: wrong code in an
                    early-break loop at -O3 -msve-vector-bits=512
           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: https://godbolt.org/z/frEsM7TEb

Note:
- works: 13.4, 14.4
- fails: 15.1 and later, including 16.2 and trunk (20261122 snapshot,
871ec62eab32)
- needs -O3 and -msve-vector-bits=512 or 1024; fine with 128, 256 and scalable
SVE
- at 512 bits: fails when n + skip > UINT_MAX, fine otherwise (skip = how many
elements a is past a 128-byte boundary, 0 to 15)
- --param vect-partial-vector-usage=0 or -fno-tree-loop-vectorize makes it
disappear

$ aarch64-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc-17
COLLECT_LTO_WRAPPER=/w/out/gcc-trunk/libexec/gcc/aarch64-linux-gnu/17.0.0/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../gcc-17-20261122/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 20261122 (experimental) (GCC)
$ aarch64-linux-gnu-gcc-17 -O3 -march=armv9-a -msve-vector-bits=512 -static t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=64 ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ aarch64-linux-gnu-gcc-17 -O3 -march=armv9-a -msve-vector-bits=512 --param
vect-partial-vector-usage=0 -static t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=64 ./a.out
$ aarch64-linux-gnu-gcc-17 -O2 -march=armv9-a -msve-vector-bits=512 -static t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=64 ./a.out
$ cat t.c
__attribute__((noipa)) unsigned
f (unsigned long *a, unsigned n, unsigned long k)
{
  unsigned i;
  for (i = 0; i < n; i++)
    {
      if (a[i] == k)
        break;
      if (i == 16)
        break;
    }
  return i;
}

static unsigned long b[32] __attribute__((aligned (256)));

int
main (void)
{
  b[3] = 42;
  if (f (b + 2, -1u, 42) != 1)   /* b + 2 is not vector-aligned; stops at b[3]
*/
    __builtin_abort ();
  return 0;
}

f should return 1 but returns 14. No warnings with -Wall -Wextra,
-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations makes no
difference, and -fsanitize=address,undefined is clean on x86_64 (gcc 16.2).

The start of f at 512 bits (VF is 16: i is 32 bits wide, so a[i] is read as two
vectors of 8):

f:
        cbz     w1, .L13
        sub     w3, w1, #1
        cmp     w3, 15
        bls     .L14
        ubfx    w4, w0, 3, 4            // skip = lanes to mask off, 2 here
        ptrue   p15.b, vl64
        add     w6, w1, w4              // n + skip in 32 bits: 0xffffffff + 2
= 1
        and     x5, x0, -121
        mov     z31.d, x2
        ptrue   p14.d, vl8
        whilelo p13.s, wzr, w4          // lanes below skip
        neg     w3, w4
        whilelo p6.s, wzr, w6           // lanes below n + skip: only lane 0
        bic     p6.b, p15/z, p6.b, p13.b    // mask of the first iteration:
empty

n + skip wraps in 32 bits, so the first vector iteration has no active lane and
the match at a[1] is missed. The mask for the next iteration, whilelo (16, n +
skip), is empty as well, so the loop exits there with i = VF - skip = 14.

Reply via email to