https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129158
Bug ID: 129158
Summary: [14/15/16/17 Regression] rs6000: vec_replace_unaligned
of the same constant as unsigned int and unsigned long
long is CSE'd together
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
Target Milestone: ---
Host: x86_64-pc-linux-gnu
Target: powerpc64le-linux-gnu
Fails with trunk (gcc 17.0.0 20261129, 389e48d33719) and with 14.1.0 through
16.2.0; 11.2.0 through 13.4.0 work.
Target: powerpc64le-linux-gnu with -mcpu=power10.
vec_replace_unaligned of 0x10 as an unsigned int writes 4 bytes (vinsw); as an
unsigned long long it writes 8 bytes (vinsd). The two results differ, so f must
return 0.
#include <altivec.h>
#include <string.h>
__attribute__((noipa)) int f (vector unsigned char v)
{
vector unsigned char x = vec_replace_unaligned (v, 0x10u, 0);
vector unsigned char y = vec_replace_unaligned (v, 0x10ull, 0);
return memcmp (&x, &y, 16) == 0;
}
int main (void)
{
if (f (vec_splats ((unsigned char) 0xff)))
__builtin_abort ();
return 0;
}
powerpc64le-linux-gnu-gcc -O2 -mcpu=power10 t.c && qemu-ppc64le -cpu
power10 ./a.out
The program is expected to exit 0. On a powerpc64le-linux-gnu cross build of
GCC 17.0.0 20261129 (experimental), git commit 389e48d33719, compiling with -O2
-mcpu=power10 and running the binary under qemu-ppc64le -cpu power10 aborts.
-O3 and -Os abort as well. At -Os only vinsw remains, and both memcmp operands
are that same vector. -O0, -O1, -O2 -fno-forward-propagate and -O2
-fno-rerun-cse-after-loop exit 0. The same -O2 command also aborts on 14.1.0
through 16.2.0.
On little-endian, the -O0 bytes are ff ff ff ff ff ff ff ff ff ff ff ff 10 00
00 00 versus ff ff ff ff ff ff ff ff 10 00 00 00 00 00 00 00. An -O0
-fsanitize=undefined -fno-sanitize-recover=all build exits 0.
Trunk -O2 code for f:
li 2,16
vinsw 2,2,0
vcmpequb. 2,2,2
setbc 3,24
blr
11.2.0 through 13.4.0 work at -O0 through -Os. Those releases use a different
vec_replace_unaligned prototype (the first argument is a typed element vector,
not vector unsigned char), so the test above does not compile there; the
equivalent operation still emits both vinsw and vinsd.
Godbolt (trunk): https://godbolt.org/z/jdxnn8rq1
vreplace_un_<mode> in config/rs6000/vsx.md is an unspec:V16QI with a single
UNSPEC_REPLACE_UN for SI, SF, DI and DF. Whether vinsw or vinsd is used depends
only on the mode of operand 2. fwprop1 adds the same REG_EQUAL note to both
insns with the element replaced by (const_int 16), which has no mode, and cse2
deletes the vinsd. float versus double is not affected, because a const_double
keeps its mode. The shared V16QI unspec came with b51795c832cf in GCC 14.
This is the same kind of bug as PR128310, which was fixed for i386 crc32 by
using a separate unspec per width. That fix does not cover this rs6000 pattern.
A possible direction is to use separate unspecs for the word and doubleword
forms of vreplace_un. I have not tested a patch.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.