https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112855
Bug ID: 112855 Summary: [14] RISC-V vector: overwriting stack args Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: patrick at rivosinc dot com Target Milestone: --- reduced testcase: int printf(char *, ...); int a; int b = 100; int c[25]; int d; int main() { int e; d = 0; for (; d < 5; d++) { e = 0; for (; e < 5; e++) c[d * 5 + e] = 0; } if (b) if (a) for (;;) ; b++; int volatile f = *c; printf("%d\n", b); } Tested using qemu with these commands: > ./bin/riscv64-unknown-linux-gnu-gcc -march=rv64gcv -mabi=lp64d -O3 red.c -o > rv64gcv.out > ./bin/riscv64-unknown-linux-gnu-gcc -march=rv64gc -mabi=lp64d -O3 red.c -o > rv64gc.out > QEMU_CPU=rv64,vlen=128,v=true,vext_spec=v1.0,Zve32f=true,Zve64f=true > ./bin/qemu-riscv64 rv64gc.out 101 > QEMU_CPU=rv64,vlen=128,v=true,vext_spec=v1.0,Zve32f=true,Zve64f=true > ./bin/qemu-riscv64 rv64gcv.out 5 rv64gcv should match rv64gc and output 101. I'm not sure where it's getting "5" from. When you comment out the b++, the behavior is fixed (and both rv64gcv and rv64gc output 100): https://godbolt.org/z/chqGo6fj8 Changing other aspects of the program will also fix the behavior, it's just that commenting out b++ was the least-disruptive change on the assembly code that I could find. You can also set b to zero. I just have it at 100 to make it more obvious that the arg is getting overwritten, not just added/subtracted.