Skip to content

core::num::f16b Rust's 16bit Brain Float - #160859

Open
Jamesbarford wants to merge 20 commits into
rust-lang:mainfrom
Jamesbarford:feat/fb16-pt1
Open

core::num::f16b Rust's 16bit Brain Float#160859
Jamesbarford wants to merge 20 commits into
rust-lang:mainfrom
Jamesbarford:feat/fb16-pt1

Conversation

@Jamesbarford

@Jamesbarford Jamesbarford commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

View all comments

Implements the RFC: f16b type. Best reviewed commit by commit, happy to split into separate PRs if that is deemed easier to review. However the line count and surface area is, in my opinion, reasonably small.

Adds;

  • ABI plumbing for the f16b along with bfloat lang item to work with LLVM, GCC is explicitly unimplemented!(...)
  • f16b feature gate, page for f16b on libruscdoc and a struct bf16 in core::num
  • Tests
  • Treat f16b as a scalar primitive for scalable vectors

Issues;

@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_ir

cc @jdonszelmann, @JonathanBrouwer

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 10, 2026
@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 10, 2026
@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@jieyouxu

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned mati865 and unassigned jieyouxu Aug 10, 2026
Comment thread compiler/rustc_codegen_gcc/src/type_.rs Outdated
}

fn type_f16b(&self) -> Type<'gcc> {
bug!("f16b is not supported by the GCC codegen backend")

@antoyo antoyo Aug 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😄, I will aim to add it in a follow up PR 👍

@rustbot

rustbot commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

Comment thread compiler/rustc_ty_utils/src/layout.rs

@bjorn3 bjorn3 Aug 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the calling convention of other targets?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe only these files were changed because they have an exhaustive match on Float.

However, my version of abi-cafe found two interesting failures: GCC and Clang are inconsistent on aarch64 and armv7

// callee, compiled with GCC 12
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct Many1 {
    __bf16 f0;
} Many1;

void struct_in_1(Many1 arg0) {
    printf("%d", arg0.f0);
}
// caller, compiled with clang 23
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct Many1 {
    __bf16 f0;
} Many1;

void struct_in_1(Many1 arg0);

void do_test(void) {
    {
        Many1 arg0 = { .f0 = (((union { uint16_t bits; __bf16 value; }){ .bits = 49600 }).value) };

        printf("%d", arg0.f0);
        struct_in_1(arg0);

    }
}

hits

    func struct_in_1's values differed
      values (native-endian hex bytes):
        expect: C0 C1
        caller: C0 C1
        callee: 04 00
      the value was arg0.f0: rustarithmeticty(f16b)
      whose arg was arg0: Many1

The current Rust implementation matches clang, and is hence incompatible with GCC


armv7 with hardware floats also runs into incompatibilities

[target.armv7-unknown-linux-gnueabihf."f16::conv_c::repr_c::clang-nightly_calls_distro-gcc"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16::conv_c::repr_c::distro-gcc_calls_clang-nightly"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16b::conv_c::repr_c::clang-nightly_calls_distro-gcc"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16b::conv_c::repr_c::distro-gcc_calls_clang-nightly"]
busted = "check"

Finally, you can let this ICE on many targets, e.g. mips, powerpc, s390x, sparc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also (e.g. on loongarch64 https://godbolt.org/z/Y3hdhG4e6) emit a __truncsfbf2 libcall that is not provided (probably needs to be added to compiler-builtins).


I think the ICEs are probably a blocker? That needs a mechanism similar to has_reliable_f128.

@Jamesbarford Jamesbarford Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I added it because of the exhaustive match statement in mips64.rs and sparc64.rs I've removed it; 40d3f6e and put in a panic!(...).

With regard to has_reliable_f128, are you envisaging a has_reliable_f16b entry on TargetConfig?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With regard to has_reliable_f128, are you envisaging a has_reliable_f16b entry on TargetConfig?

Exactly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your take on those ABI mismatches? We should track that somewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly certain what mips64 should do.

It doesn't, at least to my knowledge, have hardware support. Given we aren't implementing scalar arithmetic and an f16b can only be created through a bit pattern or vendor intrinsics, I can't immediately see a practical application? Hence a panic! seems like a pragmatic choice for the time being.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comment on whether this is the right approach, but always use span_bug! or at least bug! where possible in the compiler, rather than panic. It gives much more useful info.

@rust-log-analyzer

This comment has been minimized.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe only these files were changed because they have an exhaustive match on Float.

However, my version of abi-cafe found two interesting failures: GCC and Clang are inconsistent on aarch64 and armv7

// callee, compiled with GCC 12
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct Many1 {
    __bf16 f0;
} Many1;

void struct_in_1(Many1 arg0) {
    printf("%d", arg0.f0);
}
// caller, compiled with clang 23
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

typedef struct Many1 {
    __bf16 f0;
} Many1;

void struct_in_1(Many1 arg0);

void do_test(void) {
    {
        Many1 arg0 = { .f0 = (((union { uint16_t bits; __bf16 value; }){ .bits = 49600 }).value) };

        printf("%d", arg0.f0);
        struct_in_1(arg0);

    }
}

hits

    func struct_in_1's values differed
      values (native-endian hex bytes):
        expect: C0 C1
        caller: C0 C1
        callee: 04 00
      the value was arg0.f0: rustarithmeticty(f16b)
      whose arg was arg0: Many1

The current Rust implementation matches clang, and is hence incompatible with GCC


armv7 with hardware floats also runs into incompatibilities

[target.armv7-unknown-linux-gnueabihf."f16::conv_c::repr_c::clang-nightly_calls_distro-gcc"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16::conv_c::repr_c::distro-gcc_calls_clang-nightly"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16b::conv_c::repr_c::clang-nightly_calls_distro-gcc"]
busted = "check"

[target.armv7-unknown-linux-gnueabihf."f16b::conv_c::repr_c::distro-gcc_calls_clang-nightly"]
busted = "check"

Finally, you can let this ICE on many targets, e.g. mips, powerpc, s390x, sparc

Comment thread library/core/src/num/f16b.rs Outdated
},
Primitive::Float(float) => match float {
Float::F16 | Float::F32 => "f32",
Float::F16 | Float::F16B | Float::F32 => "f32",

@folkertdev folkertdev Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that right? LLVM just crashes on bf16 right now, so it's probably at least untested?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be mistaken, however I don't think WASM supports bf16? I've made it panic!(...) for now; 40d3f6e

Comment thread library/core/src/num/f16b.rs Outdated
Comment thread library/core/src/num/f16b.rs
Comment thread tests/codegen-llvm/float/f16b.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` label Aug 12, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in cfg and check-cfg configuration

cc @Urgau

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@mati865

mati865 commented Aug 12, 2026

Copy link
Copy Markdown
Member

I'm not a good reviewer for this change. Can somebody here pick it up rather than blindly rerolling?

@folkertdev

Copy link
Copy Markdown
Contributor

r? me

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@folkertdev

Copy link
Copy Markdown
Contributor

re

##[error]error: internal compiler error: compiler/rustc_codegen_gcc/src/type_.rs:160:9: f16b is not supported by the GCC codegen backend

by delaying adding support you now exclude a bunch of tests from the GCC run that we later need to enable again. I believe hooking it up is actually kind of trivial, so that may be the better call?

Comment thread compiler/rustc_attr_ir/src/lang_items.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@davidtwco davidtwco mentioned this pull request Aug 19, 2026
11 tasks
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] thread_local test:false 0.201
   Compiling unicase v2.8.1
[RUSTC-TIMING] pulldown_cmark_escape test:false 0.064
   Compiling nu-ansi-term v0.50.3
error[E0004]: non-exhaustive patterns: `Primitive::Float(Float::F16B)` not covered
    --> compiler/rustc_abi/src/layout/ty.rs:321:19
     |
 321 |             match primitive {
     |                   ^^^^^^^^^ pattern `Primitive::Float(Float::F16B)` not covered
     |
note: `Primitive` defined here
    --> compiler/rustc_abi/src/lib.rs:1457:10
     |
1457 | pub enum Primitive {
     |          ^^^^^^^^^
...
1466 |     Float(Float),
     |     ----- not covered
     = note: the matched value is of type `Primitive`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
     |
 328 ~                 Primitive::Pointer(..) => None,
 329 ~                 Primitive::Float(Float::F16B) => todo!(),
     |

For more information about this error, try `rustc --explain E0004`.
[RUSTC-TIMING] rustc_abi test:false 0.555
error: could not compile `rustc_abi` (lib) due to 1 previous error

@folkertdev

Copy link
Copy Markdown
Contributor

re the CI error

error[E0004]: non-exhaustive patterns: `Primitive::Float(Float::F16B)` not covered
    --> compiler/rustc_abi/src/layout/ty.rs:321:19
     |
 321 |             match primitive {
     |                   ^^^^^^^^^ pattern `Primitive::Float(Float::F16B)` not covered
     |
note: `Primitive` defined here
    --> compiler/rustc_abi/src/lib.rs:1457:10
     |
1457 | pub enum Primitive {
     |          ^^^^^^^^^
...
1466 |     Float(Float),
     |     ----- not covered
     = note: the matched value is of type `Primitive`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
     |
 328 ~                 Primitive::Pointer(..) => None,
 329 ~                 Primitive::Float(Float::F16B) => todo!(),
     |

C compilers do not support _Complex bf16, so we shouldn't either. So that branch should return None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.