https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129297
Bug ID: 129297
Summary: [15/16/17 Regression] wrong code for generic vector
shift/multiply when all SSE registers are fixed
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: zunnoorawan at hotmail dot com
Target Milestone: ---
When every SSE register is fixed, GCC 15 and later silently miscompile
element-wise shifts and multiplies on a generic 2 x 64-bit vector. The
operation is carried out as a single 128-bit integer operation, so bits
cross the element boundary. GCC 14 and earlier generate correct code
for the same input. (No warning or error is emitted.)
Testcase:
typedef unsigned long long u64;
typedef u64 v2 __attribute__ ((vector_size (16)));
__attribute__ ((noipa)) void shr (v2 *r, v2 *x) { *r = *x >> 5; }
__attribute__ ((noipa)) void shl (v2 *r, v2 *x) { *r = *x << 3; }
__attribute__ ((noipa)) void mul (v2 *r, v2 *x, v2 *y) { *r = *x * *y; }
int
main (void)
{
volatile u64 a = 0x9abcdef012345678ULL, b = 0xfedcba9876543210ULL;
volatile u64 c = 0x1111111122222222ULL, d = 0x3333333344444444ULL;
u64 x[2] __attribute__ ((aligned (16))) = { a, b };
u64 y[2] __attribute__ ((aligned (16))) = { c, d };
u64 r[2] __attribute__ ((aligned (16)));
shr ((v2 *) r, (v2 *) x);
if (r[0] != a >> 5 || r[1] != b >> 5)
__builtin_abort ();
shl ((v2 *) r, (v2 *) x);
if (r[0] != a << 3 || r[1] != b << 3)
__builtin_abort ();
mul ((v2 *) r, (v2 *) x, (v2 *) y);
if (r[0] != a * c || r[1] != b * d)
__builtin_abort ();
return 0;
}
Command:
gcc -O1 testcase.c \
-ffixed-xmm0 -ffixed-xmm1 -ffixed-xmm2 -ffixed-xmm3 \
-ffixed-xmm4 -ffixed-xmm5 -ffixed-xmm6 -ffixed-xmm7 \
-ffixed-xmm8 -ffixed-xmm9 -ffixed-xmm10 -ffixed-xmm11 \
-ffixed-xmm12 -ffixed-xmm13 -ffixed-xmm14 -ffixed-xmm15
./a.out
Result: the program aborts. Expected: exit status 0.
The first check already fails. shr returns
{ 0x84d5e6f78091a2b3, 0x07f6e5d4c3b2a190 }
instead of
{ 0x04d5e6f78091a2b3, 0x07f6e5d4c3b2a190 }
i.e. the low 5 bits of element 1 were shifted into the top of element 0.
shl and mul are wrong in the same way (element 1 receives bits/cross
terms from element 0).
Code generated for shr at -O1 (trunk):
shr:
movq (%rsi), %rax
movq 8(%rsi), %rdx
shrdq $5, %rdx, %rax
shrq $5, %rdx
movq %rax, (%rdi)
movq %rdx, 8(%rdi)
ret
Versions (x86_64-linux-gnu):
12.4.0, 13.3.0, 14.2.0 correct at -O0, -O1, -O2
15.2.0, 16.2.0 wrong at -O0, -O1, -O2
17.0.0 20261204 (7f81a926a) wrong at -O0, -O1, -O2, -O3
17.0.0 20261203 (Compiler Explorer, 8f38977ffd79) wrong at -O0, -O1, -O2,
-O3
Controls, on trunk at -O1:
- without the -ffixed-xmm* options: correct
- with -mno-sse and no -ffixed options: correct (the operations are
lowered to per-element scalar code)
- with one SSE register left free (xmm0..xmm14 or xmm1..xmm15
fixed): no wrong code; it ICEs instead with "unable to generate
reloads" for lshrv2di3 in curr_insn_transform
Analysis:
The vector shift survives veclower (from -fdump-tree-optimized):
_2 = _1 >> 5; // vector(2) long long unsigned int
and expand emits it as a TImode shift:
(lshiftrt:TI (reg:TI 105) (const_int 5))
I suspect the GCC 15 rewrite of get_compute_type in tree-vect-generic.cc, but
have not bisected.
Build details:
Compiler Explorer trunk:
gcc version 17.0.0 20261203 (experimental)
(Compiler-Explorer-Build-gcc-8f38977ffd79d05bcf36ed9cddd291fed77acc8b-binutils-2.47)
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20261203/configure
--prefix=/opt/compiler-explorer/gcc-build/staging --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,objc,obj-c++,go,d,rust,m2,cobol,algol68
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-time=yes
--enable-linker-build-id --enable-lto --enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build-gcc-8f38977ffd79d05bcf36ed9cddd291fed77acc8b-binutils-2.47
Local trunk build (commit 7f81a926addc8cbe665879b0feeb8f23b23de2f1):
gcc version 17.0.0 20261204 (experimental) (GCC)
Target: x86_64-pc-linux-gnu
Configured with: ../trunk-full/configure --prefix=/opt/gcc-trunk
--disable-bootstrap --enable-languages=c --disable-multilib --disable-nls
--disable-libsanitizer --disable-libgomp --disable-libquadmath --disable-libssp
--disable-libvtv --disable-libatomic --disable-libitm --disable-lto
--disable-plugin CFLAGS='-O1 -g0' CXXFLAGS='-O1 -g0'