https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121995
Bug ID: 121995 Summary: [RISCV] [Miscompile] GCC - riscv64 target, miscompiles with multiplication on unsigned char at -O3 as well as -O2 Product: gcc Version: 16.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: skothadiya at whileone dot in Target Milestone: --- Created attachment 62415 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62415&action=edit Attached reduced testcase GCC (riscv64 target) produces incorrect output for a simple C program when compiled with -O2 or -O3. Description: The C code involves a loop with a multiplication operation on an unsigned char, compiles correctly on x86_64 and produces the expected output. However, when compiled for the riscv64 architecture, the resulting binary yields an incorrect value. The expected output is 160, but the program returns 20. This miscompilation occurs specifically with optimizations enabled at levels -O2 and -O3. A bug has been reported similar to this bug is #121985, However, this is specifically affects an unsigned char variable. Compiler flags: -march=rv64gcv_zvl512b -flto -mrvv-vector-bits=zvl -mrvv-max-lmul=m8 -O3 Reproduction Steps: 1. Compile the test case with GCC for riscv64 using the -O2 or -O3 flag. 2. Execute the compiled binary. 3. Observe the output. COMMANDS: /riscv-gnu-toolchain-build/bin/riscv64-unknown-linux-gnu-gcc -march=rv64gcv_zvl512b -flto -mrvv-vector-bits=zvl -mrvv-max-lmul=m8 -O3 red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv -Wno-unknown-warning-option -Werror -Wfatal-errors -Wall -Wformat -Wno-int-in-bool-context -Wno-dangling-pointer -Wno-compare-distinct-pointer-types -Wno-overflow -Wuninitialized -Warray-bounds -Wreturn-type -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-value -Wno-address -Wno-bool-compare -Wno-pointer-sign -Wno-bool-operation -Wno-tautological-compare -Wno-self-assign -Wno-implicit-const-int-float-conversion -Wno-constant-conversion -Wno-unused-value -Wno-tautological-constant-out-of-range-compare -Wno-constant-logical-operand -Wno-parentheses-equality -Wno-pointer-sign QEMU_CPU=rv64,vlen=512,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /home/skothadiya/CIFUZZRR/riscv-gnu-toolchain-build/bin/qemu-riscv64 user-config.out 1 20 //Expected Output: 160 //Actual Output: 20 -- testcase (red.c) -- unsigned short ak[2]={01,02}; unsigned char a = 5; void b( unsigned short ak[]) { for (unsigned var=0; var<2; var++) for (char as=ak[var]; as<4; as+=1) a *= 2; } int printf(const char *, ...); int main() { long long at; b(ak); at = (long long)a; printf("%llu\n", at); }