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

            Bug ID: 129227
           Summary: [16/17 Regression] s390x: out-of-bounds store with
                    -fno-vect-cost-model: no iterations left after the
                    alignment prolog
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: 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: s390x-*-*

Compiler Explorer (assembly only, 16.2 and 15.3):
https://godbolt.org/z/Ya5KMaGs8

Note:
- fails at -O2, -O3 with -march=z15 -fno-vect-cost-model: s390x 16.2,
  trunk bc49f1e37014 (20261127)
- works: 11.2 to 15.3; -O1, -Os; the default cost model
- --param=vect-max-peeling-for-alignment=0 hides it
- -fno-aggressive-loop-optimizations hides it: the bound of 4
  iterations comes from the size of s.f1
- 16.2's prolog always runs 7 iterations and it warns "iteration 4
  invokes undefined behavior"; everything below is trunk
- not bisected

$ s390x-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=s390x-linux-gnu-gcc-17
Target: s390x-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 --with-arch=z196
--target=s390x-linux-gnu --program-prefix=s390x-linux-gnu-
--with-sysroot=/usr/s390x-linux-gnu --with-native-system-header-dir=/include
--with-as=/usr/bin/s390x-linux-gnu-as --with-ld=/usr/bin/s390x-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)
$ s390x-linux-gnu-gcc-17 -O2 -march=z15 -fno-vect-cost-model -Wall -Wextra \
    -static t.c
$ qemu-s390x ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ s390x-linux-gnu-gcc-17 -O2 -march=z15 -Wall -Wextra -static t.c
$ qemu-s390x ./a.out
$ cat t.c
struct { unsigned char f0, f1[4], g[40]; } s;

__attribute__((noipa)) void
f (unsigned n, unsigned long x)
{
  for (unsigned i = 0; i < n; i++)
    s.f1[i] = x + i;
}

int
main (void)
{
  f (4, 1);
  for (int k = 0; k < 40; k++)
    if (s.g[k])
      __builtin_abort ();
  return 0;
}

f (4, 1) only stores s.f1[0] to s.f1[3], so the program should exit with 0.
It aborts: s.g[0] to s.g[15] are 5 to 20.
No warnings with -Wall -Wextra on trunk; ASan/UBSan report nothing (x86_64
16.2, -O0).

$ s390x-linux-gnu-gcc-17 -O2 -march=z15 -fno-vect-cost-model \
    -fdump-tree-vect-details -S t.c
$ grep -h 'bounded by 3)' t.c.*.vect | sort -u
 is executed at most 3 (bounded by 3) + 1 times in loop 1.
$ grep -h '6:26.*\(peeling = \|prolog loop\|partial\)' t.c.*.vect
t.c:6:26: note:  operating on partial vectors.
t.c:6:26: note:  known peeling = 7.
t.c:6:26: note:  niters for prolog loop: MIN_EXPR <n_7(D), 7>
$ grep -B9 vstl t.s
        sr      %r2,%r12
        vab     %v24,%v21,%v23
        clfi    %r2,15
        llgfr   %r2,%r2
        locghinle       %r2,16
        llgfr   %r3,%r12
        aghi    %r2,-1
        la      %r5,1(%r3,%r11)
        llcr    %r12,%r2
        vstl    %v24,%r12,0(%r5)

The prolog runs MIN (n, 7) = 4 iterations, so none is left, but the vstl is
reached without a test. Its byte index is 0 - 1, which llcr makes 255: 16 bytes
are stored.
vect_do_peeling builds the guard that skips the vector loop only inside if
(epilog_peeling), which seems to be why. PR 129093 is about the same place; its
patch needs a nonzero cost threshold, so it seems not to cover this (not
tested).

Reply via email to