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

* [Bug c/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  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 ` gccriscvuser at proton dot me
  2023-04-09 15:37 ` [Bug middle-end/109456] " pinskia at gcc dot gnu.org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-04-09 15:33 UTC (permalink / raw)
  To: 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

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

* [Bug middle-end/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  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 ` pinskia at gcc dot gnu.org
  2023-04-09 15:43 ` pinskia at gcc dot gnu.org
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-09 15:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> or in some other fixed role

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

* [Bug middle-end/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  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
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-09 15:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Seems like using a global register is not really recommended, especially one
that is used for argument passing.

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (2 preceding siblings ...)
  2023-04-09 15:43 ` pinskia at gcc dot gnu.org
@ 2023-04-09 15:45 ` pinskia at gcc dot gnu.org
  2023-04-10 11:42 ` xry111 at gcc dot gnu.org
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-09 15:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|middle-end                  |target
           Keywords|inline-asm                  |
             Target|                            |riscv

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Anyways this is a target issue when setting up arguments.

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (3 preceding siblings ...)
  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
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-04-10 11:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #5 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Is this a bug?

-ffixed-reg
Treat the register named reg as a fixed register; generated code should never
refer to it (except perhaps as a stack pointer, frame pointer or in some other
fixed role).

To me "the ABI mandates to pass an argument in this register" means "some other
fixed role".

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (4 preceding siblings ...)
  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
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-05-01 18:23 UTC (permalink / raw)
  To: 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.

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (5 preceding siblings ...)
  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
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-05-01 20:19 UTC (permalink / raw)
  To: 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

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (6 preceding siblings ...)
  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
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-06-09 17:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

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

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (7 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-11 10:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #9 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
The easiest way is considering it a documentation issue and do:

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 0870f7aff93..c39880349d5 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -18635,8 +18635,8 @@ more efficient than other code generation strategies.
 @opindex ffixed
 @item -ffixed-@var{reg}
 Treat the register named @var{reg} as a fixed register; generated code
-should never refer to it (except perhaps as a stack pointer, frame
-pointer or in some other fixed role).
+should never refer to it (except as a stack pointer, frame
+pointer, argument register or in some other fixed role).

 @var{reg} must be the name of a register.  The register names accepted
 are machine-specific and are defined in the @code{REGISTER_NAMES}

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

* [Bug target/109456] `-ffixed` is ignored for `a` registers on RISC-V.
  2023-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (8 preceding siblings ...)
  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
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: schwab@linux-m68k.org @ 2023-06-11 10:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #10 from Andreas Schwab <schwab@linux-m68k.org> ---
Or "other ABI-mandated fixed roles".  This also includes return value
registers.

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

* [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-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (9 preceding siblings ...)
  2023-06-11 10:32 ` schwab@linux-m68k.org
@ 2023-06-12  9:08 ` xry111 at gcc dot gnu.org
  2023-06-23 17:39 ` gccriscvuser at proton dot me
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-12  9:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|`-ffixed` is ignored for    |`-ffixed-reg` cannot
                   |`a` registers on RISC-V.    |prevent using `reg` for
                   |                            |ABI-mandated roles
                   |                            |(argument register etc) and
                   |                            |the behavior should be
                   |                            |documented more clearly

--- Comment #11 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Not a RISCV-specific issue.

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

* [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-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (10 preceding siblings ...)
  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
  13 siblings, 0 replies; 15+ messages in thread
From: gccriscvuser at proton dot me @ 2023-06-23 17:39 UTC (permalink / raw)
  To: 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?

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

* [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-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (11 preceding siblings ...)
  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
  13 siblings, 0 replies; 15+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-24  9:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #13 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to gccriscvuser from comment #12)
> Updating the documentation is good, but there should also be an error
> diagnostic, right?

It would be a backward-incompatible change.  IMO it's perfectly legal to
disable a register for RA with -ffixed- but still allowing it for fixed roles. 
And I bet there are existing uses of this feature in the wild.

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

* [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-04-09 15:25 [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V gccriscvuser at proton dot me
                   ` (12 preceding siblings ...)
  2023-06-24  9:50 ` xry111 at gcc dot gnu.org
@ 2023-06-24 10:04 ` xry111 at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-24 10:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109456

--- Comment #14 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Andreas Schwab from comment #10)
> Or "other ABI-mandated fixed roles".  This also includes return value
> registers.

Hmm, even "ABI-mandated fixed roles" is not enough.  For example, or RISC-V we
have

extern int foo(char *);

int test()
{
    char x[12345678];
    return foo(x);
}

Then t0 is used by the function prologue, and -ffixed-t0 cannot stop it
(because the use of t0 is hard coded in the target hook).

^ 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).