Bug 104853 - [RISC-V] -march=rv64g not including extension Zifencei
Summary: [RISC-V] -march=rv64g not including extension Zifencei
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 11.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-03-09 05:16 UTC by rvalue
Modified: 2022-05-24 16:13 UTC (History)
4 users (show)

See Also:
Host:
Target: riscv
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-03-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rvalue 2022-03-09 05:16:28 UTC
I'm trying to compile some code including inline assembly instruction `fence.i`. According to RISC-V ISA spec, G is defined as abbreviation for IMAFDZicsrZifencei, which includes instruction `fence.i`. But I get assembler message indicating that `fence.i` is unrecognized.

I report this to GCC instead of binutils because when I use `gcc -S` and then use `as` manually, it works fine.

Also, it works fine before I upgrade to GCC 11.2.0

Minimum recurrence:

$ gcc --version
gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat > test.c
int main(){
    asm volatile("fence.i" ::: "memory");
}
$ gcc -c test.c -march=rv64g
test.c: Assembler messages:
test.c:2: Error: unrecognized opcode `fence.i'

Manually use `as`:

$ gcc -S test.c -march=rv64g -o test.s
$ as -march=rv64g test.s -o test.o
$ objdump -d test.o

test.o:     file format elf64-littleriscv


Disassembly of section .text:

0000000000000000 <main>:
   0:   ff010113                addi    sp,sp,-16
   4:   00813423                sd      s0,8(sp)
   8:   01010413                addi    s0,sp,16
   c:   0000100f                fence.i
  10:   00000793                li      a5,0
  14:   00078513                mv      a0,a5
  18:   00813403                ld      s0,8(sp)
  1c:   01010113                addi    sp,sp,16
  20:   00008067                ret
Comment 1 rvalue 2022-03-09 05:22:43 UTC
It do works with `-march=rv64g_zifencei`, but I don't think it's the right way to work.
Comment 2 Kito Cheng 2022-03-09 07:13:00 UTC
Do you mind give few more version info for binutils and configuration info for gcc?

You can obtain those info by following two commands:
$ gcc -v
$ as --version

Thanks
Comment 3 rvalue 2022-03-09 07:19:31 UTC
(In reply to Kito Cheng from comment #2)
> Do you mind give few more version info for binutils and configuration info
> for gcc?
> 
> You can obtain those info by following two commands:
> $ gcc -v
> $ as --version
> 
> Thanks

Sure.

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --disable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1 gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (GCC)
$ as --version
GNU assembler (GNU Binutils) 2.38
Copyright (C) 2022 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `riscv64-unknown-linux-gnu'.
Comment 4 Kito Cheng 2022-03-09 07:53:00 UTC
Thanks your info, that cause by the default ISA spec version bump issue, binutils 2.38 and GCC 11.* using different default ISA spec cause this issue, I've push a patch to GCC 11 branch [1] for this issue, could you try this patch? thanks.


See more detail for the full story about ISA spec:

https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/aE1ZeHHCYf4

[1] https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=9871d39f752bc9c114ed694662a519d04896f491
Comment 5 rvalue 2022-03-12 17:30:16 UTC
(In reply to Kito Cheng from comment #4)
> Thanks your info, that cause by the default ISA spec version bump issue,
> binutils 2.38 and GCC 11.* using different default ISA spec cause this
> issue, I've push a patch to GCC 11 branch [1] for this issue, could you try
> this patch? thanks.
> 
> 
> See more detail for the full story about ISA spec:
> 
> https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/aE1ZeHHCYf4
> 
> [1]
> https://gcc.gnu.org/git/?p=gcc.git;a=commit;
> h=9871d39f752bc9c114ed694662a519d04896f491

I've tried this patch just now.
Unfortunately, the gcc rebuilt reports the same error as before.
Comment 6 rvalue 2022-03-12 19:20:11 UTC
I've got some verbose output from gcc, and it seems that a duplicate `-march` is passed to `as`

$ gcc -c test.c -march=rv64g --verbose
Using built-in specs.
COLLECT_GCC=/usr/sbin/gcc
Target: riscv64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --disable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1 gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (GCC)
COLLECT_GCC_OPTIONS='-c' '-march=rv64g' '-v' '-mabi=lp64d' '-march=rv64imafd'
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/cc1 -quiet -v test.c -quiet -dumpbase test.c -dumpbase-ext .c -march=rv64g -mabi=lp64d -march=rv64imafd -version -o /tmp/ccCK3sIZ.s
GNU C17 (GCC) version 11.2.0 (riscv64-unknown-linux-gnu)
        compiled by GNU C version 11.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/../../../../riscv64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/include
 /usr/local/include
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/include-fixed
 /usr/include
End of search list.
GNU C17 (GCC) version 11.2.0 (riscv64-unknown-linux-gnu)
        compiled by GNU C version 11.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 42e88359d8058cfa8524073dbb277472
COLLECT_GCC_OPTIONS='-c' '-march=rv64g' '-v' '-mabi=lp64d' '-march=rv64imafd'
 as -v --traditional-format -fpic -march=rv64g -march=rv64imafd -mabi=lp64d -o test.o /tmp/ccCK3sIZ.s
GNU assembler version 2.38 (riscv64-unknown-linux-gnu) using BFD version (GNU Binutils) 2.38
test.c: Assembler messages:
test.c:2: Error: unrecognized opcode `fence.i'
Comment 7 rvalue 2022-03-12 19:44:34 UTC
Well, I've found something that could be problematic here:

https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/riscv/arch-canonicalize;h=49a6204b9cb64cb0e375c6003c423bf115a0a8a6;hb=HEAD#l60

It's never updated in ISA spec version bumping.
Comment 8 Kito Cheng 2022-03-30 09:06:11 UTC
Hi rvalue:

I pushed couple fixes to gcc 11 release branch, mostly back from trunk and a fix for setting right isa spec value: g:f41871dfdbd9d0c3368c0bc32d879fd5485e7abb

I expect that could fix that issue, could you try again with top of gcc 11 branch?

Thanks!
Comment 9 rvalue 2022-03-30 22:51:23 UTC
Hi Kito:

Thank you for your follow-up patch! Confirmed that these patches do work. The test program compiles successfully with no `-misa-spec` flag or `-misa-spec=2.2` flag.

But when I set `-misa-spec=20190608` or `-misa-spec=20191213`, it still fails. I think the file mentioned in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104853#c7 is still buggy. Is there any future plan about this problem?
Comment 10 Kito Cheng 2022-03-31 00:57:51 UTC
I plan to fix that in next few day for trunk and backport to GCC 11.
Comment 11 CVS Commits 2022-04-11 14:57:05 UTC
The master branch has been updated by Kito Cheng <kito@gcc.gnu.org>:

https://gcc.gnu.org/g:4132f6ba9583e128a00d55961ae8c8e7245b2223

commit r12-8084-g4132f6ba9583e128a00d55961ae8c8e7245b2223
Author: Kito Cheng <kito.cheng@sifive.com>
Date:   Mon Apr 11 16:29:34 2022 +0800

    RISC-V: Support -misa-spec for arch-canonicalize and multilib-generator. [PR104853]
    
    We migrate the default ISA spec version from 2.2 to 20191213, but those scripts
    aren't updated at the same time, this patch is making both scripts support
    different ISA spec versions.
    
    gcc/ChangeLog:
    
            PR target/104853
            * config.gcc: Pass -misa-spec to arch-canonicalize and
            multilib-generator.
            * config/riscv/arch-canonicalize: Adding -misa-spec option.
            (SUPPORTED_ISA_SPEC): New.
            (arch_canonicalize): New argument `isa_spec`.
            Handle multiple ISA spec versions.
            * config/riscv/multilib-generator: Adding -misa-spec option.
Comment 12 CVS Commits 2022-04-11 16:03:00 UTC
The releases/gcc-11 branch has been updated by Kito Cheng <kito@gcc.gnu.org>:

https://gcc.gnu.org/g:fa21fcfb67587837c1704703a710496999393c1d

commit r11-9809-gfa21fcfb67587837c1704703a710496999393c1d
Author: Kito Cheng <kito.cheng@sifive.com>
Date:   Mon Apr 11 16:29:34 2022 +0800

    RISC-V: Support -misa-spec for arch-canonicalize and multilib-generator. [PR104853]
    
    We migrate the default ISA spec version from 2.2 to 20191213, but those scripts
    aren't updated at the same time, this patch is making both scripts support
    different ISA spec versions.
    
    gcc/ChangeLog:
    
            PR target/104853
            * config.gcc: Pass -misa-spec to arch-canonicalize and
            multilib-generator.
            * config/riscv/arch-canonicalize: Adding -misa-spec option.
            (SUPPORTED_ISA_SPEC): New.
            (arch_canonicalize): New argument `isa_spec`.
            Handle multiple ISA spec versions.
            * config/riscv/multilib-generator: Adding -misa-spec option.
    
    (cherry picked from commit 4132f6ba9583e128a00d55961ae8c8e7245b2223)
Comment 13 Kito Cheng 2022-04-11 16:05:26 UTC
Hi rvalue:

Pushed the fix to trunk and GCC 11 branch for fixing both arch-canonicalize and multilib-generator script.

Tested GCC 11/trunk with --with-isa-spec=2.2/20191213.

Could you try that to make sure that work to you?
Comment 14 John Doe 2022-04-14 20:40:49 UTC
Shouldn't it also be necessary to re-generate the existing configurations files (e.g. gcc/config/riscv/t-elf-multilib) with the modified multilib-generator script? Maybe it also makes sense to add _zicsr / _zifence multilib targets to those for MULTILIB_REUSE while at it?
Comment 15 rvalue 2022-04-15 18:38:27 UTC
Hi, Kito:

I've tried these patches and found that it doesn't work with isa-spec 20190608/20191213.

Release 11.2.0 with following patches applied: 9871d39f752b, f41871dfdbd9, f049717d8d50, 330aff0a9f7b, fa21fcfb6758

Verbose output:

$ cat > test.c
int main(){
    asm volatile("fence.i" ::: "memory");
}
$ gcc test.c -o test -v -march=rv64gc -misa-spec=20191213
Using built-in specs.
COLLECT_GCC=/usr/sbin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --disable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --enable-link-serialization=1 gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (GCC)
COLLECT_GCC_OPTIONS='-o' 'test' '-v' '-march=rv64gc' '-misa-spec=20191213' '-mabi=lp64d' '-march=rv64imafdc'
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/cc1 -quiet -v test.c -quiet -dumpbase test.c -dumpbase-ext .c -march=rv64gc -misa-spec=20191213 -mabi=lp64d -march=rv64imafdc -version -o /tmp/ccQTUxnc.s
GNU C17 (GCC) version 11.2.0 (riscv64-unknown-linux-gnu)
        compiled by GNU C version 11.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/../../../../riscv64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/include
 /usr/local/include
 /usr/lib/gcc/riscv64-unknown-linux-gnu/11.2.0/include-fixed
 /usr/include
End of search list.
GNU C17 (GCC) version 11.2.0 (riscv64-unknown-linux-gnu)
        compiled by GNU C version 11.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 7a7a4553dc5ab0b4f8fc26761070a0ed
COLLECT_GCC_OPTIONS='-o' 'test' '-v' '-march=rv64gc' '-misa-spec=20191213' '-mabi=lp64d' '-march=rv64imafdc'
 as -v --traditional-format -fpic -march=rv64gc -march=rv64imafdc -mabi=lp64d -misa-spec=20191213 -o /tmp/cc2my6Or.o /tmp/ccQTUxnc.s
GNU assembler version 2.38 (riscv64-unknown-linux-gnu) using BFD version (GNU Binutils) 2.38
test.c: Assembler messages:
test.c:2: Error: unrecognized opcode `fence.i'