From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5CD6D3858D33; Sun, 9 Apr 2023 15:25:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5CD6D3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681053955; bh=nVE7lnI0R8TB5jKZIoTJ3Oa4mL3AB57yBLq4DgMC2T4=; h=From:To:Subject:Date:From; b=vfYRnfzhFMXkpzsdS+pabQhcmUBdlgEwiDVoyQcbyWJ+o6TRv+W0fwLU0IzhzYvFY IJV8DybW3dOy4vNaqhk3tnRPfBLlNkoyT9l9cg3rruAF4SCUwSY9LZVljX8QWskgHL +OyoaGIkqWxAf+M8bUxsQxx+gRb8waBgEqf2skLU= From: "gccriscvuser at proton dot me" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/109456] New: `-ffixed` is ignored for `a` registers on RISC-V. Date: Sun, 09 Apr 2023 15:25:54 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.2.1 X-Bugzilla-Keywords: inline-asm, wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: gccriscvuser at proton dot me X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109456 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=3D54819&action=3Dedit Reduced testcase to a small file that doesn't include any other file =3D=3D=3D 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 =3D 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`. =3D=3D=3D 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. =3D=3D=3D 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.ffizeda= 4.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=