public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V.
@ 2023-04-09 15:25 gccriscvuser at proton dot me
  2023-04-09 15:33 ` [Bug c/109456] " gccriscvuser at proton dot me
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-04-09 15:25 UTC (permalink / raw)
  To: gcc-bugs

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            #,
     call    alpha           #

#:   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,
     sgtu    a5,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

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-06-24 10:04 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
2023-04-09 15:33 ` [Bug c/109456] " gccriscvuser at proton dot me
2023-04-09 15:37 ` [Bug middle-end/109456] " pinskia at gcc dot gnu.org
2023-04-09 15:43 ` pinskia at gcc dot gnu.org
2023-04-09 15:45 ` [Bug target/109456] " pinskia at gcc dot gnu.org
2023-04-10 11:42 ` xry111 at gcc dot gnu.org
2023-05-01 18:23 ` gccriscvuser at proton dot me
2023-05-01 20:19 ` gccriscvuser at proton dot me
2023-06-09 17:12 ` gccriscvuser at proton dot me
2023-06-11 10:23 ` xry111 at gcc dot gnu.org
2023-06-11 10:32 ` schwab@linux-m68k.org
2023-06-12  9:08 ` [Bug target/109456] `-ffixed-reg` cannot prevent using `reg` for ABI-mandated roles (argument register etc) and the behavior should be documented more clearly xry111 at gcc dot gnu.org
2023-06-23 17:39 ` gccriscvuser at proton dot me
2023-06-24  9:50 ` xry111 at gcc dot gnu.org
2023-06-24 10:04 ` xry111 at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).