https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456
Bug ID: 109456
Summary: `-ffixed` is ignored for `a` registers on RISC-V.
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Keywords: inline-asm, wrong-code
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: gccriscvuser at proton dot me
Target Milestone: ---
Created attachment 54819
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54819&action=edit
Reduced testcase to a small file that doesn't include any other file
=== Problem Description
Register `a4` is normally used for passing parameters in a call.
https:github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc
As such, it would be problematic to use `a4` for a global as shown here.
https:gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html
However, if this code is compiled with `-ffixed-a4`, then `a4` should be
reserved for this global register variable.
https:gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
If it were reserved as the documentation specifies, then the value of
`global_a4` should remain `999` across the call to `alpha` below. However, it
is changes to `4`, which demonstrates the bug.
The bug can also be observed by inspecting the assembly language emitted by
GCC.
#: global_a4 = 999;
li a4,999 # global_a4,
#: alpha(0, 1, 2, 3, 4);
li a4,4#,
li a3,3#,
li a2,2#,
li a1,1#,
li a0,0#,
callalpha #
#: return global_a4 < 100;
sext.w a5,a4 # global_a4.0_1, global_a4
mv a3,a5 # tmp78, global_a4.0_1
li a5,99 # tmp79,
sgtua5,a3,a5# tmp80, tmp78, tmp79
Note that the call uses `a4` as a parameter with `li a4,4` even though the GCC
command line has `-ffixed-a4`. This is the essence of the bug. The
consequence is that the data in `a4` is corrupted when it is used for
`global_a4` on `sext.w a5,a4`.
=== Proposed Solutions
Three reasonable solutions, starting with the most preferred, are:
1. Use register `a5` for `param_a4` since `a4` is not available,
2. Pass `param_a4` on the stack, or
3. Issue an error diagnostic.
=== Testing
The issue has been identified with the following GCC versions. The command
line used for each is included along with the version comment from the top of
the assembly-language output.
riscv64-linux-gnu-gcc (GCC) 12.2.1 20220819 (Red Hat Cross 12.2.1-2)
riscv64-linux-gnu-gcc -ffixed-a4 -O0 -dA -fverbose-asm -S -o gccbug.ffizeda4.s
gccbug.ffizeda4.c
GNU C17 (GCC) version 12.2.1 20220819 (Red Hat Cross 12.2.1-2)
(riscv64-linux-gnu)
compiled by GNU C version 12.2.1 20221121 (Red Hat 12.2.1-4), GMP version
6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version none
riscv64-linux-gnu-gcc-10 (Debian 10.2.1-6) 10.2.1 20210110
riscv64-linux-gnu-gcc-10 -ffixed-a4 -O0 -dA -fverbose-asm -S -o
gccbug.ffizeda4.s gccbug.ffizeda4.c
GNU C17 (Debian 10.2.1-6) version 10.2.1 20210110 (riscv64-linux-gnu)
compiled by GNU C version 10.2.1 20210110, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.0, isl version isl-0.23-GMP
riscv64-unknown-elf-gcc GCC 8.3.0 (Debian)
riscv64-unknown-elf-gcc -ffixed-a4 -O0 -dA -fverbose-asm -S -o
gccbug.ffizeda4.s gccbug.ffizeda4.c
GNU C17 () version 8.3.0 (riscv64-unknown-elf)
compiled by GNU C version 9.2.1 20190909, GMP version 6.1.2, MPFR version
4.0.2, MPC version 1.1.0, isl version none
riscv64-elf-gcc (GCC) 8.2.0
riscv64-elf-gcc -ffixed-a4 -O0 -dA -fverbose-asm -S -o gccbug.ffizeda4.s
gccbug.ffizeda4.c
GNU C17 (GCC) version 8.2.0 (riscv64-elf)
compiled by GNU C version 4.4.7 20120313 (Red Hat 4.4.7-17), GMP version 6.1.2,
MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.18-GMP