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

            Bug ID: 129057
           Summary: [14/15/16/17 Regression] aarch64: vector constructor
                    loses an element with -msve-vector-bits=256
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: aarch64-sve, wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hhhuang at smu dot edu.sg
  Target Milestone: ---
            Target: aarch64*-*-*

Compiler Explorer: https://godbolt.org/z/YTjTKxzYc

Note:
- works: 8.5, 9.5
- fails: 10.2 and later, including 16.2 and trunk (the code came with r10-808,
PR 88837)
- any optimization level, -O0 included
- fine with -msve-vector-bits=128 and with scalable SVE

$ 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 -O2 -march=armv9-a -msve-vector-bits=256 -static t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=32 ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ 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
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ aarch64-linux-gnu-gcc-17 -O2 -march=armv9-a -static t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=32 ./a.out
$ cat t.c
typedef int v8si __attribute__((vector_size(32)));

__attribute__((noipa)) void
g (v8si *out, int a, int b, int c)
{
  *out = (v8si){a, 0, 0, 0, 0, b, c, 0};
}

int
main (void)
{
  v8si r;
  g (&r, 1, 2, 3);
  if (r[5] != 2)
    __builtin_abort ();
  return 0;
}

r[5] should be b but is 0. 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).

g at 256 bits:

g:
        mov     z30.s, w3
        fmov    s31, w2
        insr    z31.s, wzr
        insr    z30.s, wzr
        rev     z31.s, z31.s
        insr    z30.s, wzr
        insr    z30.s, w1
        zip1    z30.s, z30.s, z31.s
        str     z30, [x0]
        ret

The odd elements {0,0,b,0} are built reversed in the low 4 lanes, then REV
reverses all 8 lanes, so b ends up in lane 6 and ZIP1 picks up 0 instead. This
is "Case 2: Vector contains leading constants" in
aarch64_sve_expand_vector_init; Case 4 checks nelts_reqd == nelts before using
REV, Case 2 does not.

Plain C gets the same constructor, {a, 0, 0, 0, 0, b, c, 0}, from BB SLP:

$ aarch64-linux-gnu-gcc-17 -O2 -march=armv9-a -msve-vector-bits=256 -static
t2.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=32 ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ cat t2.c
__attribute__((noipa)) void
f (unsigned *out, const unsigned *in, int lo, int hi,
   unsigned a, unsigned b, unsigned c)
{
  unsigned v0 = a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = b, v6 = c, v7 = 0;
  for (int i = lo; i < hi; i++)
    {
      v0 ^= in[i * 8 + 0]; v1 ^= in[i * 8 + 1];
      v2 ^= in[i * 8 + 2]; v3 ^= in[i * 8 + 3];
      v4 ^= in[i * 8 + 4]; v5 ^= in[i * 8 + 5];
      v6 ^= in[i * 8 + 6]; v7 ^= in[i * 8 + 7];
    }
  out[0] = v0; out[1] = v1; out[2] = v2; out[3] = v3;
  out[4] = v4; out[5] = v5; out[6] = v6; out[7] = v7;
}

int
main (void)
{
  unsigned in[8] = { 0 }, out[8];
  f (out, in, 1, 0, 1, 2, 3);     /* lo > hi: the loop runs zero times */
  if (out[5] != 2)
    __builtin_abort ();
  return 0;
}

This one fails at -O2 with GCC 12 and later (GCC 11 at -O3).
-fno-tree-slp-vectorize makes it disappear.

Reply via email to