https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129240
Bug ID: 129240
Summary: RISC-V: first-order recurrence vectorized with
SELECT_VL takes element VLMAX-1 instead of vl-1
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: hd701108 at gmail dot com
Target Milestone: ---
Target: riscv64-unknown-linux-gnu
Testcase (attached; it includes no headers):
__attribute__((noipa)) void
rec (int *restrict out, const int *x, int n)
{
int prev = x[0];
for (int i = 0; i < n; ++i)
{
int next = x[i + 1];
out[i] = prev * 3 + next;
prev = next;
}
}
main() calls rec() for n = 1, 4, ..., 289 and aborts if
out[i] != x[i] * 3 + x[i + 1].
riscv64-unknown-linux-gnu-gcc -O2 -march=rv64gcv -mabi=lp64d -S
pr-recurrence.c
Trunk emits this for rec:
ble a2,zero,.L5
lw a5,0(a1)
vsetvli a4,zero,e32,m1,ta,ma
csrr a3,vlenb
vmv.v.x v2,a5
vmv.v.i v4,3
srli t0,a3,2
addi a1,a1,4
addi t1,t0,-1 # t1 = VLMAX - 1
.L3:
vsetvli t2,a2,e8,mf4,ta,ma # t2 = vl of this chunk
vmv1r.v v3,v2 # v3 = previous chunk
vle32.v v2,0(a1)
vsetvli a6,zero,e32,m1,ta,ma
slli a7,t2,2
sub a2,a2,t2
add a1,a1,a7
vslideup.vi v1,v2,1
vsetivli zero,1,e32,m1,tu,ma
vslidedown.vx v1,v3,t1 # element VLMAX-1 of v3
vsetvli zero,t2,e32,m1,ta,ma
vmadd.vv v1,v4,v2
vse32.v v1,0(a0)
add a0,a0,a7
bne a2,zero,.L3
.L5:
ret
The value carried into the next chunk is element VLMAX-1 of the previous
chunk. It should be element vl-1, the last element that chunk loaded.
RVV 1.0 section 6.3 lets vsetvli return any vl with
ceil(AVL/2) <= vl <= VLMAX when AVL < 2*VLMAX, so a chunk that is not the
last one can be shorter than VLMAX, and then the slide reads a tail element.
Run results under QEMU 11.1.3 user mode with a small patch that makes
vsetvli return ceil(AVL/2) in that case (static binaries):
GCC 14.2.0 (Ubuntu cross compiler), -O2: aborts at VLEN 128, 256, 512, 1024
GCC 15.2.0 (Bianbu native compiler), -O2: aborts at VLEN 128, 256, 512, 1024
the same binaries on unpatched QEMU: pass
-O0: pass
-O2 -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations: still
aborts
-Wall -Wextra gives no warnings, and the testcase is clean with
-fsanitize=address,undefined on x86_64. The Compiler Explorer builds of
GCC 14.2.0, 15.2.0, 16.2.0 and trunk all emit the same loop. GCC 13.4.0
does not vectorize it. The RVV hardware we have (SpacemiT X60 and X100)
returns vl = min(AVL, VLMAX), so the bug does not show there.
PR112438 was the same problem for the induction variable (SELECT_VL
giving less than VF in a non-final iteration); this is the first-order
recurrence path. The same loop shape is in libyuv (SsimWindowRow_C, where
we found it), and under the patched QEMU we also saw wrong results from
Ubuntu 26.04 riscv64 builds of OpenBLAS (SDISNA), mbedTLS
(mbedtls_mpi_shift_l) and libtiff (Predictor=2 encoding).
gcc -v of the trunk compiler:
Target: riscv64-unknown-linux-gnu
Configured with: /opt/.build/riscv64-unknown-linux-gnu/src/gcc/configure
--build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu
--target=riscv64-unknown-linux-gnu
--prefix=/opt/compiler-explorer/riscv64/gcc-trunk-20261201/riscv64-unknown-linux-gnu
--exec_prefix=/opt/compiler-explorer/riscv64/gcc-trunk-20261201/riscv64-unknown-linux-gnu
--with-sysroot=/opt/compiler-explorer/riscv64/gcc-trunk-20261201/riscv64-unknown-linux-gnu/riscv64-unknown-linux-gnu/sysroot
--enable-languages=c,c++,fortran,d,objc,obj-c++,go --with-arch=rv64gc
--with-abi=lp64d --with-pkgversion='crosstool-NG UNKNOWN' --enable-__cxa_atexit
--enable-libmudflap --enable-libgomp --enable-libssp --enable-libquadmath
--enable-libquadmath-support --disable-libsanitizer --disable-libmpx
--with-gmp=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-mpfr=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-mpc=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-isl=/opt/.build/riscv64-unknown-linux-gnu/buildtools --enable-lto
--enable-threads=posix --enable-default-pie --enable-target-optspace
--disable-plugin --disable-nls --disable-multilib
--with-local-prefix=/opt/compiler-explorer/riscv64/gcc-trunk-20261201/riscv64-unknown-linux-gnu/riscv64-unknown-linux-gnu/sysroot
--enable-long-long
Thread model: posix
gcc version 17.0.0 20261201 (experimental) (crosstool-NG UNKNOWN)
Found with rvvprobe, our RVV test tool.
Hao Du (杜浩, GitHub snozzz, [email protected])
Tong Liu (刘通, GitHub I3eg1nner, [email protected])