Skip to content

fp8 and fp16 instructions #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amithmath opened this issue Nov 19, 2021 · 25 comments
Closed

fp8 and fp16 instructions #993

amithmath opened this issue Nov 19, 2021 · 25 comments

Comments

@amithmath
Copy link

Hello Jim and All,

I am wondering, how to add fp8 and fp16 data type and custom instructions to RISC-V32 elf? I think one has to add fp8 and fp16 in the file: https://github.com/riscv-collab/riscv-gcc/blob/5964b5cd72721186ea2195a7be8d40cfe6554023/gcc/c-family/c-common.c after line 482 but I am not getting how to generate instructions for hard FPU?

Thanks,
-Amit

@TommyMurphyTM1234
Copy link
Collaborator

@amithmath
Copy link
Author

Thanks Tommy. Let me study all links.

@jim-wilson
Copy link
Collaborator

jim-wilson commented Nov 19, 2021 via email

@amithmath
Copy link
Author

Thanks Jim.

@TommyMurphyTM1234
Copy link
Collaborator

No update/activity in almost a year. Anybody know if this is still an issue, if it can be closed, or if it needs to remain open as an enhancement issue to add further FP precision support?

@mgrang
Copy link

mgrang commented Mar 21, 2023

I think this is still an issue. I built riscv-gnu-toolchain from master using configure options: ./configure --prefix=~/llvm-mtia/install/riscv-gnu-toolchain --with-abi=lp64f --with-cmodel=medany. Fp16 still seems to be undefined:

> cat a.cpp
void f(Fp16 x);

riscv-gnu-toolchain/bin/riscv64-unknown-elf-g++ -march=rv64gc_zfh a.cpp -c

a.cpp:1:8: error: 'Fp16' was not declared in this scope
    1 | void f(Fp16 x);

I get the same errors with __fp16 and _Float16.

Tagging @jim-wilson for attention. This is a blocker for us in using riscv-gnu-toolchain for our RISC-V baremetal builds.

@amithmath
Copy link
Author

is risc-v zfh extension ratified?

@cmuellner
Copy link
Collaborator

is risc-v zfh extension ratified?

Zfh was ratified in November 2021:
https://wiki.riscv.org/display/HOME/Ratified+Extensions

@amithmath
Copy link
Author

Thanks. Does risc-v gcc,g++ and gfortran compilers support fp8/fp16? If yes, please point me which risc-v gnu tool chain

@TommyMurphyTM1234
Copy link
Collaborator

Zfh support was added in GCC 13 (can't determine which minor version added it though because of the way that GCC only publishes consolidated release notes/change logs covering all minor versions of a major version):

riscv-gnu-toolchain still uses GCC 13.2:

The update to GCC 14 is still pending:

If riscv-gnu-toolchain with GCC 13.2 doesn't have it then try building the toolchain with later out of tree GCC sources from upstream using configure ... --with-gcc-src=... . You may also need a later Binutils, standard library, etc.

@TommyMurphyTM1234
Copy link
Collaborator

Zfh support was added in GCC 13 (can't determine which minor version added it though because of the way that GCC only publishes consolidated release notes/change logs covering all minor versions of a major version):

Curiously I can't find any mention of Zfh in any of the 13.x minor release change logs:

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Aug 18, 2024

The latest CI build (using GCC 13.2.0 + Binutils 2.43):

doesn't complain when -march includes the zfh extension but it does fails when Fp16 is used as in this post:

wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
tar xzvf riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
cd riscv/bin

cat test.c
void f(Fp16 x);

./riscv64-unknown-elf-gcc -march=rv64gc_zfh test.c -c
test.c:1:8: error: unknown type name 'Fp16'
    1 | void f(Fp16 x);
      |        ^~~~

(Same with the Linux rather than the bare-metal/Newlib variant of that toolchain).

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Aug 18, 2024

The latest CI build (using GCC 13.2.0 + Binutils 2.43):

doesn't complain when -march includes the zfh extension but it does fails when Fp16 is used as in this post:

wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
tar xzvf https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
cd riscv/bin

cat test.c
void f(Fp16 x);

./riscv64-unknown-elf-gcc -march=rv64gc_zfh test.c -c
test.c:1:8: error: unknown type name 'Fp16'
    1 | void f(Fp16 x);
      |        ^~~~

(Same with the Linux rather than the bare-metal/Newlib variant of that toolchain).

Oh - looks like _Float16 is the correct type to use.

cat test.c
void f(_Float16 x);

./riscv64-unknown-linux-gnu-gcc  -march=rv64gc_zfh test.c -c

file test.o
test.o: ELF 64-bit LSB relocatable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), not stripped

_Float8 doesn't work though - and maybe it's not supposed to?

cat test.c
void f(_Float8 x);

./riscv64-unknown-linux-gnu-gcc  -march=rv64gc_zfh test.c -c
test.c:1:8: error: unknown type name '_Float8'; did you mean '_Float128'?
    1 | void f(_Float8 x);
      |        ^~~~~~~
      |        _Float128

Ultimately, it looks like the latest riscv-gnu-toolchain using GCC 13.2.0 and Binutils 2.43 already supports the Zfh extension and related types?

@amithmath
Copy link
Author

I am trying to build riscv-gnu-tool-chain with this repo: https://github.com/riscv-collab/riscv-gnu-toolchain to test __Float16
./configure --prefix=/opt/riscv --disable-linux --with-arch=rv32gc --with-abi=ilp32

make -j$(nproc) all but I am getting the error: cc/gcc/config/riscv/genrvv-type-indexer.cc:118:30: error: ‘log2’ is not a member of ‘std and gcc/gcc/config/riscv/genrvv-type-indexer.cc:120:30: error: ‘log2’ is not a member of ‘std’

@TommyMurphyTM1234
Copy link
Collaborator

Why don't you just use the pre-built CI toolchain binaries since they already seem to support Zfh?

I am trying to build riscv-gnu-tool-chain with this repo: https://github.com/riscv-collab/riscv-gnu-toolchain to test __Float16 ./configure --prefix=/opt/riscv --disable-linux --with-arch=rv32gc --with-abi=ilp32

Why --with-abi=ilp32 and not, say, --with-abi=ilp32d which arguably better matches the default architecture of rv32gc aka rv32imafdc?

make -j$(nproc) all but I am getting the error: cc/gcc/config/riscv/genrvv-type-indexer.cc:118:30: error: ‘log2’ is not a member of ‘std and gcc/gcc/config/riscv/genrvv-type-indexer.cc:120:30: error: ‘log2’ is not a member of ‘std’

Details of your clone. configure and make commands and a build log?
Better to do a non parallel build without any -j$(nproc) option when tryng to investigate build problems.

@amithmath
Copy link
Author

I guess you meant pre-built CI toolchain binaries from:
wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
tar xzvf https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
cd riscv/bin ?
I did try riscv/bin/riscv64-unknown-gcc -march=rv64gc_zfh test.c -c
I am getting following error:
/lib64/libc.so.6: version `GLIBC_2.32' not found (required by riscv/bin/riscv64-unknown-elf-gcc)

If not, please point me pre-built CI toolchain binaries

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Aug 18, 2024

I already linked to a CI build above:

I am getting following error:
/lib64/libc.so.6: version `GLIBC_2.32' not found (required by riscv/bin/riscv64-unknown-elf-gcc)

You're probably using the wrong one for your Linux/GLIBC version.

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Aug 18, 2024

To find the version of GLIBC that your Linux distro uses:

ldd --version
ldd (Ubuntu GLIBC 2.35-0ubuntu3.8) 2.35
Copyright (C) 2022 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.
Written by Roland McGrath and Ulrich Drepper.

The CI builds are for Ubuntu 20.04 and 22.04 but should work on other distros with a GLIBC greater than or equal to the one that the toolchain was built to expect:

wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-20.04-gcc-nightly-2024.08.06-nightly.tar.gz
tar xzvf riscv64-elf-ubuntu-20.04-gcc-nightly-2024.08.06-nightly.tar.gz
mv riscv riscv-20.04

ldd -v riscv-20.04/bin/riscv64-unknown-elf-gcc
        linux-vdso.so.1 (0x00007ffcc4df5000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5f5c50b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5f5c73f000)

        Version information:
        riscv-20.04/bin/riscv64-unknown-elf-gcc:
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                libc.so.6 (GLIBC_2.9) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.7) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.14) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.3) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.4) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.11) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.3.4) => /lib/x86_64-linux-gnu/libc.so.6
        /lib/x86_64-linux-gnu/libc.so.6:
                ld-linux-x86-64.so.2 (GLIBC_2.2.5) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2

wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2024.08.06/riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
tar xzvf riscv64-elf-ubuntu-22.04-gcc-nightly-2024.08.06-nightly.tar.gz
mv riscv riscv-22.04

ldd -v riscv-22.04/bin/riscv64-unknown-elf-gcc
        linux-vdso.so.1 (0x00007ffc443f6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fee7ab5b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fee7ad8f000)

        Version information:
        riscv-22.04/bin/riscv64-unknown-elf-gcc:
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                libc.so.6 (GLIBC_2.14) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.32) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.3) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.33) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.7) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.11) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.34) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.3.4) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.9) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.2.5) => /lib/x86_64-linux-gnu/libc.so.6
                libc.so.6 (GLIBC_2.4) => /lib/x86_64-linux-gnu/libc.so.6
        /lib/x86_64-linux-gnu/libc.so.6:
                ld-linux-x86-64.so.2 (GLIBC_2.2.5) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
                ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2

So, the 20.04 build needs GLIBC >= 2.14 while the 22.04 build needs GLIBC >= 2.33.

If GLIBC or other dependencies are a problem for you then maybe something like the xPack binary tools might be a better option as they are built to be as self-contained as possible and the only external dependedency is a minimum GLIBC version (currently 2.27 or later):

Note that the xPack toolchain uses a different prefix string to riscv-gnu-toolchain:

@amithmath
Copy link
Author

I tried to compile with this: riscv32-elf-ubuntu-20.04-gcc-nightly-2024.08.06-nightly.tar.gz like: riscv/bin/riscv32-unknown-elf-gcc -march=rv32gc_zfh test.c -c

I am getting following error:
riscv/bin/../libexec/gcc/riscv32-unknown-elf/13.2.0/cc1: error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory

When I tried to install libmpfr.so.6 in CentOS 7, I am getting following error:

error: Failed dependencies:
rpmlib(PayloadIsZstd) <= 5.4.18-1 is needed by mpfr-4.1.0-7.el9.x86_64

Bur there is no rpmlib(PayloadIsZstd) 5.4.18-1 for CentOS 7

@TommyMurphyTM1234
Copy link
Collaborator

I tried to compile with this: riscv32-elf-ubuntu-20.04-gcc-nightly-2024.08.06-nightly.tar.gz like: riscv/bin/riscv32-unknown-elf-gcc -march=rv32gc_zfh test.c -c

I am getting following error: riscv/bin/../libexec/gcc/riscv32-unknown-elf/13.2.0/cc1: error while loading shared libraries: libmpfr.so.6: cannot open shared object file: No such file or directory

When I tried to install libmpfr.so.6 in CentOS 7, I am getting following error:

error: Failed dependencies: rpmlib(PayloadIsZstd) <= 5.4.18-1 is needed by mpfr-4.1.0-7.el9.x86_64

Bur there is no rpmlib(PayloadIsZstd) 5.4.18-1 for CentOS 7

These are general dependency issues and not riscv-gnu-toolchain specific issues.
You'll need to figure them out yourself or use a more up to date Linux distro and/or compile the toolchain from scratch according to your needs or use the xPack pre-built tools.

Actually, just checking the latest xPack release multilibs and I'm not sure that they include Zfh support:

I would suggest that you do the following on an up to date Linux distro:

git clone https://github.com/riscv-collab/riscv-gnu-toolchain issue-993
cd issue-993
./configure --prefix=`pwd`/installed-tools --with-arch=rv32gc_zfh --with-abi=ilp32d
make 2>&1 | tee build.log

assuming that the selected arch/abi are what you need to test with - adjust them if necessary.

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Aug 18, 2024

I am trying to build riscv-gnu-tool-chain with this repo: https://github.com/riscv-collab/riscv-gnu-toolchain to test __Float16
./configure --prefix=/opt/riscv --disable-linux --with-arch=rv32gc --with-abi=ilp32

make -j$(nproc) all but I am getting the error: cc/gcc/config/riscv/genrvv-type-indexer.cc:118:30: error: ‘log2’ is not a member of ‘std and gcc/gcc/config/riscv/genrvv-type-indexer.cc:120:30: error: ‘log2’ is not a member of ‘std’

I did a build as follows (on Ubuntu 22.04.4 on WSL2) and it worked fine:

git clone https://github.com/riscv-collab/riscv-gnu-toolchain issue-993
cd issue-993
./configure --prefix=`pwd`/installed-tools --with-arch=rv32gc --with-abi=ilp32 --disable-gdb
make 2>&1 | tee build.log

...

./installed-tools/bin/riscv32-unknown-elf-gcc -v
Using built-in specs.
COLLECT_GCC=./installed-tools/bin/riscv32-unknown-elf-gcc
COLLECT_LTO_WRAPPER=/home/user/Downloads/issue-993/installed-tools/libexec/gcc/riscv32-unknown-elf/13.2.0/lto-wrapper
Target: riscv32-unknown-elf
Configured with: /home/user/Downloads/issue-993/gcc/configure --target=riscv32-unknown-elf --prefix=/home/user/Downloads/issue-993/installed-tools --disable-shared --disable-threads --enable-languages=c,c++ --with-pkgversion= --with-system-zlib --enable-tls --with-newlib --with-sysroot=/home/user/Downloads/issue-993/installed-tools/riscv32-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=.././gcc --disable-multilib --with-abi=ilp32 --with-arch=rv32gc --with-tune=rocket --with-isa-spec=20191213 'CFLAGS_FOR_TARGET=-Os    -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-Os    -mcmodel=medlow'
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 13.2.0 ()

@amithmath
Copy link
Author

It seems I have issue on linux-distros...but does riscv32-unknown-elf-gcc supports _Float8?

@TommyMurphyTM1234
Copy link
Collaborator

It seems I have issue on linux-distros...but does riscv32-unknown-elf-gcc supports _Float8?

Did you read my earlier post?

_Float8 doesn't seem to be a GCC float type.

@amithmath
Copy link
Author

Thanks got it.

@TommyMurphyTM1234
Copy link
Collaborator

Closing this since the original issue is moot now that Zfh extension has been ratified and support implemented in the toolchain and subsequent questions have been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants