Skip to content

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

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

Jamesbarford wants to merge 5 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 👍

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.

Actually, as @folkertdev pointed out, it was a doddle. So I've included the implementation in the 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
Comment thread compiler/rustc_target/src/callconv/mips64.rs
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_target/src/callconv/mips64.rs
Comment thread library/core/src/num/f16b.rs Outdated
Comment thread compiler/rustc_codegen_ssa/src/mir/naked_asm.rs Outdated
Comment thread library/core/src/num/f16b.rs Outdated
Comment thread library/core/src/num/bfloat.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.

asukaminato0721 pushed a commit to asukaminato0721/rust-analyzer that referenced this pull request Sep 2, 2026
…lkertdev

Make sin, cos, exp, exp2, log, log2, log10 generic

Rebased and smaller version of rust-lang/rust#153934

Following `fabs`, make the `sin`, `cos`, `exp`, `exp2`, `log`, `log2` and `log10` intrinsics generic over the float type, rather than having four variants per float type.

The first two commits are purely stylistic:
- reorganised Cranelift code to make following changes simpler
- moved a misplaced comment in `compiler/rustc_codegen_llvm/src/intrinsic.rs` that caused `x fmt` to give up

The last commit actually makes them generic! Most code is a bit simpler, ~~and this will also hopefully simplify adding support for these intrinsics for the future [`bf16` type](rust-lang/rust#160859 :)

Unfortunately both GCC and Cranelift backend changes are a bit churny. Their code is a bit, opaque, to put it kindly, and I didn't want to refactor those here.

r? @folkertdev
cc @RalfJung

@folkertdev folkertdev left a comment

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.

some nits

I diagnosed the arm/aarch64 issue to be about what types are valid in a homogeneous float aggregate. Not much we can do about that here.

View changes since this review

Comment thread compiler/rustc_target/src/callconv/mips64.rs Outdated
Comment thread compiler/rustc_target/src/callconv/sparc64.rs Outdated
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Sep 6, 2026
…lkertdev

Make sin, cos, exp, exp2, log, log2, log10 generic

Rebased and smaller version of rust-lang/rust#153934

Following `fabs`, make the `sin`, `cos`, `exp`, `exp2`, `log`, `log2` and `log10` intrinsics generic over the float type, rather than having four variants per float type.

The first two commits are purely stylistic:
- reorganised Cranelift code to make following changes simpler
- moved a misplaced comment in `compiler/rustc_codegen_llvm/src/intrinsic.rs` that caused `x fmt` to give up

The last commit actually makes them generic! Most code is a bit simpler, ~~and this will also hopefully simplify adding support for these intrinsics for the future [`bf16` type](rust-lang/rust#160859 :)

Unfortunately both GCC and Cranelift backend changes are a bit churny. Their code is a bit, opaque, to put it kindly, and I didn't want to refactor those here.

r? @folkertdev
cc @RalfJung
@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

pull Bot pushed a commit to Stars1233/rustc_codegen_gcc that referenced this pull request Sep 8, 2026
…lkertdev

Make sin, cos, exp, exp2, log, log2, log10 generic

Rebased and smaller version of rust-lang/rust#153934

Following `fabs`, make the `sin`, `cos`, `exp`, `exp2`, `log`, `log2` and `log10` intrinsics generic over the float type, rather than having four variants per float type.

The first two commits are purely stylistic:
- reorganised Cranelift code to make following changes simpler
- moved a misplaced comment in `compiler/rustc_codegen_llvm/src/intrinsic.rs` that caused `x fmt` to give up

The last commit actually makes them generic! Most code is a bit simpler, ~~and this will also hopefully simplify adding support for these intrinsics for the future [`bf16` type](rust-lang/rust#160859 :)

Unfortunately both GCC and Cranelift backend changes are a bit churny. Their code is a bit, opaque, to put it kindly, and I didn't want to refactor those here.

r? @folkertdev
cc @RalfJung
@rust-bors

This comment has been minimized.

Comment thread tests/assembly-llvm/f16b.rs Outdated
use minicore::From;
use minicore::num::f16b;

// CHECK-LABEL: {{^"?[#_]?identity_f16b"?:}}

@folkertdev folkertdev Sep 13, 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.

what is this regex for? just CHECK-LABEL: identity_f16b should suffice?

View changes since the review

@Jamesbarford Jamesbarford Sep 14, 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.

Done a9b2c63

@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment thread compiler/rustc_abi/src/layout/ty.rs
@Jamesbarford

Copy link
Copy Markdown
Contributor Author

@bors try jobs=test-various,aarch64-apple-,-gnu-nopt-,x86_64-mingw-,aarch64-msvc-*,arm-android

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
`core::num::f16b` Rust's 16bit Brain Float


try-job: test-various
try-job: aarch64-apple-*
try-job: *-gnu-nopt-*
try-job: x86_64-mingw-*
try-job: aarch64-msvc-*
try-job: arm-android
@rust-bors

rust-bors Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 8f68b95 failed: CI. Failed job:

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling diff v0.1.13
   Compiling citool v0.1.0 (/home/runner/work/rust/rust/src/ci/citool)
    Finished `dev` profile [unoptimized] target(s) in 25.45s
     Running `target/debug/citool calculate-job-matrix`
Run type: TryJob { job_patterns: Some(["test-various", "aarch64-apple-*", "*-gnu-nopt-*", "x86_64-mingw-*", "aarch64-msvc-*", "arm-android"]), nolimit: false }
Error: Failed to calculate job matrix

Caused by:
    Patterns `aarch64-apple-*, x86_64-mingw-*, aarch64-msvc-*, arm-android` did not match any auto jobs
##[error]Process completed with exit code 1.
Post job cleanup.

@Jamesbarford

Copy link
Copy Markdown
Contributor Author

I believe some of the jobs were renamed

@bors try jobs=test-various,test-aarch64-apple-,-gnu-nopt-,test-x86_64-mingw-,test-aarch64-msvc-*,test-arm-android

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
`core::num::f16b` Rust's 16bit Brain Float


try-job: test-various
try-job: test-aarch64-apple-*
try-job: *-gnu-nopt-*
try-job: test-x86_64-mingw-*
try-job: test-aarch64-msvc-*
try-job: test-arm-android
@rust-bors

rust-bors Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2027cd7 (2027cd7f37bc10f8ed9010e7e13772d9306d7d42)
Base parent: c26ce70 (c26ce708de5d14682647895d2f3caf38f70b5aa6)

@folkertdev folkertdev left a comment

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.

Some final nits, but otherwise I think this looks good

View changes since this review

Comment thread compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Comment on lines +34 to +35
// No `f16b` type
Float::F16B => unreachable!("`f16b` unsupported on mips64"),

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.

Suggested change
// No `f16b` type
Float::F16B => unreachable!("`f16b` unsupported on mips64"),
Float::F16B => unreachable!("`f16b` unsupported on mips64"),

nit but the panic message is clear enough I think

@tgross35 tgross35 left a comment

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.

Few small requests but overall LGTM

View changes since this review

// This is similar to <https://github.com/llvm/llvm-project/issues/94434>, however
// does not work until LLVM 23 on Windows.
(Arch::Arm64EC, _) => major >= 23,
(Arch::AArch64, _) | (Arch::X86_64, _) | (Arch::RiscV64, _) | (Arch::LoongArch64, _) => {

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.

Simplification nit

Suggested change
(Arch::AArch64, _) | (Arch::X86_64, _) | (Arch::RiscV64, _) | (Arch::LoongArch64, _) => {
(Arch::AArch64 | Arch::X86_64 | Arch::RiscV64 | Arch::LoongArch64, _) => {

begin_panic,
bench,
bevy_ecs,
bfloat,

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.

Is this actually used anymore or can it be deleted?

Comment on lines +50 to +54
// RISCV64: fmv.x.w a0, fa0
// RISCV64-NEXT: lui a1, 1048560
// RISCV64-NEXT: or a0, a0, a1
// RISCV64-NEXT: fmv.w.x fa0, a0
// RISCV64-NEXT: ret

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.

It seems to be setting the high bits to 0xffff here, which is kind of weird. I assume the ABI permits garbage in the upper bits (looking at what it does for f16b_to_bits) rather than requiring 0xffff.... for returns but apparently not passing? Seems worth an LLVM optimization issue if there isn't one.

Cc @beetrees I'm sure you have a better idea about this.

Comment on lines +109 to +112
#[inline]
const fn widen(value: f16b) -> f32 {
f32::from_bits((value.to_bits() as u32) << 16)
}

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.

Are we using this because LLVM doesn't support this operation? Or is it supported and we will use an intrinsic in the future?

Worth a comment in any case.

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` A-tidy Area: The tidy tool S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.