riscv-collab / riscv-gnu-toolchain

GNU toolchain for RISC-V, including GCC
Other
3.31k stars 1.12k forks source link

Is bfloat16 supported in gcc or clang #1491

Open fanghuaqi opened 1 month ago

fanghuaqi commented 1 month ago

Hello, there

The RISC-V bfloat16 ISA, available at https://github.com/riscv/riscv-bfloat16, is already in version 1.0. I noticed that the BF16 format has been ratified for both scalar and vector extensions. I would like to know if gcc or clang currently support this format. If not, are there any patches being developed to add support for it?

Thanks

TommyMurphyTM1234 commented 1 month ago

The RISC-V bfloat16 ISA, available at https://github.com/riscv/riscv-bfloat16, is already in version 1.0.

This seems to suggest that it's ready for ratification but not actually ratified yet?

It's not listed on the RISC-V International ratified specs page:

I don't see it mentioned in any GCC release notes:

There does seem to be mention of it on the GCC mailing lists - maybe it's work in progress on the/a development branch?

I don't know much about LLVM/Clang but it's mentioned here:

But I don't see it mentioned in the LLVM 17 release notes:

Might be worth asking on the relevant GCC and LLVM/Clang mailing lists?

TommyMurphyTM1234 commented 1 month ago

Any update @fanghuaqi? Did my posts/links help to clarify this at all? Have you posted to ask about this in the upstream projects?

fanghuaqi commented 1 month ago

Any update @fanghuaqi? Did my posts/links help to clarify this at all? Have you posted to ask about this in the upstream projects?

Hi @TommyMurphyTM1234 , I didn't posted to ask in upstream project, I thought this repo will be watched by many people, so just asked here, I will search the patches in https://patchwork.sourceware.org/project/gcc/list/?q=risc-v later

TommyMurphyTM1234 commented 3 weeks ago

For what it's worth, the RISC-V BF16 extensions were ratified in June 2024:

And the GCC documentation suggests that it's supported:

But, confusingly, I don't see it mentioned in the release notes for GCC 12.4, which is the only GCC release post dating ratification.

I suspect that this means that (work in progress?) support is only available via the GCC master sources for now and you would have to build the toolchain with those using configure ... --with-gcc-src=...?

TommyMurphyTM1234 commented 2 days ago

Any update @fanghuaqi?

fanghuaqi commented 2 days ago

I see https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=666f167bec09d1234e6496c86b566fe1a71f61f0 RISC-V: Add vector type of BFloat16 format commit is added into gcc master, we are looking to see whether it matched our requirements.

TommyMurphyTM1234 commented 2 days ago

I see https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=666f167bec09d1234e6496c86b566fe1a71f61f0 RISC-V: Add vector type of BFloat16 format commit is added into gcc master, we are looking to see whether it matched our requirements.

What specific instruction(s) are you trying to use and how (e.g. inline assembler, compiler inference etc.)? Can you provide a small self-contained code snippet?

fanghuaqi commented 2 days ago

What specific instruction(s) are you trying to use and how (e.g. inline assembler, compiler inference etc.)? Can you provide a small self-contained code snippet?

We just want to use compiler intrinsic API for bfloat16 just reusing the existing RVV instructions, but we have a CSR register to control whether the RVV instruction is doing float16 or bfloat16 calculation.