[Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V.

2023-04-09 Thread gccriscvuser at proton dot me via 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#,
 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

[Bug c/109456] `-ffixed` is ignored for `a` registers on RISC-V.

2023-04-09 Thread gccriscvuser at proton dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #1 from gccriscvuser at proton dot me ---
Corrected URLs from original 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

[Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.

2023-05-01 Thread gccriscvuser at proton dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #6 from gccriscvuser at proton dot me ---
I'm willing to agree that the generator is emitting correct code, but in that
case, there should be an error- or fatal-level diagnostic indicating that
`-ffixed` is not supported for `a4` (solution 3 above).

It would be even better if the diagnostic were issued only if there is at least
one function that takes so many parameters that `a4` is needed.  The diagnostic
message could point at the function(s) themselves to assist the programmer in
correcting the issue.  If none of the functions use `a4` as a parameter, then
no diagnostic is needed.

[Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.

2023-05-01 Thread gccriscvuser at proton dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #7 from gccriscvuser at proton dot me ---
To be clear, when I said "generator is emitting correct code", I mean with
respect to the ABI specification.

However, this is an actual bug, not a request for enhancement, because the
emitted code is incorrect with respect to the command-line option.  The
programmer has a reasonable expectation that, if no diagnostic is issued, the
`-ffixed` command-line option is respected.

Thanks

[Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.

2023-06-09 Thread gccriscvuser at proton dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #8 from gccriscvuser at proton dot me ---
Thoughts?

[Bug target/109456] `-ffixed-reg` cannot prevent using `reg` for ABI-mandated roles (argument register etc) and the behavior should be documented more clearly

2023-06-23 Thread gccriscvuser at proton dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #12 from gccriscvuser at proton dot me ---
Updating the documentation is good, but there should also be an error
diagnostic, right?