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

            Bug ID: 129130
           Summary: [16/17 Regression] RISC-V: vectorized loop reads past
                    the end of an array at -O3 -march=rv64gcv
           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: riscv64*-*-*

Created attachment 66324
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66324&action=edit
preprocessed t.c

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

Note:
- fails: trunk bc49f1e37014 (20261127) at -O3 -march=rv64gcv (QEMU 11.1.3)
- 16.1, 16.2 and trunk 871ec62eab32 (20261122) pass by default
- the same three fail with -mno-autovec-segment; 15.3 passes with it
- not bisected
- fine at -O2 and with --param=vect-partial-vector-usage=0
- -fno-aggressive-loop-optimizations hides it
- PR 128433's fix (r17-5994) does not cover this

$ riscv64-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc-17
Target: riscv64-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=rv64gc
--with-abi=lp64d --target=riscv64-linux-gnu --program-prefix=riscv64-linux-gnu-
--with-sysroot=/usr/riscv64-linux-gnu --with-native-system-header-dir=/include
--with-as=/usr/bin/riscv64-linux-gnu-as --with-ld=/usr/bin/riscv64-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)
$ riscv64-linux-gnu-gcc-17 -O3 -march=rv64gcv -static t.c
$ qemu-riscv64 ./a.out
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
$ riscv64-linux-gnu-gcc-16 -dumpfullversion
16.2.0
$ riscv64-linux-gnu-gcc-16 -O3 -march=rv64gcv -mno-autovec-segment -static t.c
$ qemu-riscv64 ./a.out
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
$ cat t.c
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

struct S { uint32_t a[72]; };

__attribute__((noipa)) uint32_t
f (struct S *q, uint32_t lo, uint32_t hi)
{
  uint32_t s = 0;
  for (uint32_t i = lo; i < hi; i++)
    {
      s += q->a[i * 3] ^ 0xff;
      s += q->a[i * 3 + 1];
      s += q->a[i * 3 + 2] ^ 0xff;
    }
  return s;
}

int
main (void)
{
  long pg = sysconf (_SC_PAGESIZE);
  char *m = mmap (0, 2 * pg, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (m == MAP_FAILED || mprotect (m + pg, pg, PROT_NONE))
    abort ();
  /* q->a ends where an unmapped page begins */
  struct S *q = (struct S *) (m + pg) - 1;
  for (int j = 0; j < 72; j++)
    q->a[j] = j;
  if (f (q, 0, 24) != 11388)
    abort ();
  return 0;
}

f (q, 0, 24) reads q->a[0] .. q->a[71] only. The array ends at an unmapped
page, and the fault address is the first byte of that page, in a vle32.v. i is
unsigned and i * 3 + 2 is at most 71, so the loop has no undefined behaviour.
No warnings with -Wall -Wextra; same with -fno-strict-aliasing -fwrapv;
ASan/UBSan clean on x86_64.

$ riscv64-linux-gnu-gcc-17 -O3 -march=rv64gcv -fdump-tree-vect-details -c t.c
$ sed -n '/with vector mode V16QI/,/Analysis succeeded/p' t.c.*.vect \
    | grep -E '^t.c:12:27.*(vector mode|single element|gaps|partial vec)'
t.c:12:27: note:  ***** Re-trying analysis with vector mode V16QI
t.c:12:27: note:   Detected single element interleaving q_14(D)->a[_4] step 12
t.c:12:27: note:   Detected single element interleaving q_14(D)->a[_6] step 12
t.c:12:27: note:   Detected single element interleaving q_14(D)->a[_1] step 12
t.c:12:27: note:  operating on partial vectors.
t.c:12:27: note:  ***** Analysis succeeded with vector mode V16QI

V16QI is the mode chosen. Each load is a single-element group with step 12,
i.e. a gap of two elements, and the loop runs on partial vectors without an
epilogue. The RVVM1SI analysis prints "Data access with gaps requires scalar
epilogue loop" for the same loads; this one does not.
15.3 with -mno-autovec-segment also picks V16QI, but without partial vectors.
With -mno-autovec-segment 16.2 picks V16QI with partial vectors too; by default
it picks variable length vectors.

Reply via email to