core::num::f16b Rust's 16bit Brain Float - #160859
Conversation
|
Some changes occurred in compiler/rustc_attr_ir cc @jdonszelmann, @JonathanBrouwer This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a, @makai410
|
|
r? @jieyouxu rustbot has assigned @jieyouxu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
@rustbot reroll |
| } | ||
|
|
||
| fn type_f16b(&self) -> Type<'gcc> { | ||
| bug!("f16b is not supported by the GCC codegen backend") |
There was a problem hiding this comment.
I believe GCC actually supports this type: https://github.com/rust-lang/gccjit.rs/blob/master/src/context.rs#L1482
There was a problem hiding this comment.
Thanks 😄, I will aim to add it in a follow up PR 👍
There was a problem hiding this comment.
Actually, as @folkertdev pointed out, it was a doddle. So I've included the implementation in the PR 👍
bf10f8a to
01c5c1b
Compare
|
cc @bjorn3 |
There was a problem hiding this comment.
What is the calling convention of other targets?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
With regard to
has_reliable_f128, are you envisaging ahas_reliable_f16bentry onTargetConfig?
Exactly
There was a problem hiding this comment.
What's your take on those ABI mismatches? We should track that somewhere.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We can't use those custom macros in rustc_target because they are defined in rustc_middle, and that crate depends on rustc_target.
There was a problem hiding this comment.
It doesn't seem possible to use in compiler/rustc_target, however elsewhere I have strived to use it when available.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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
53d3660 to
40d3f6e
Compare
This comment has been minimized.
This comment has been minimized.
da620d0 to
0ed984f
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Some changes occurred in cfg and check-cfg configuration cc @Urgau
cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
|
I'm not a good reviewer for this change. Can somebody here pick it up rather than blindly rerolling? |
|
r? me |
|
Guess some jobs got renamed... @bors try jobs=test-various,aarch64-apple-,-gnu-nopt-,x86_64-mingw-,aarch64-msvc-*,arm-android |
This comment has been minimized.
This comment has been minimized.
`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
This comment has been minimized.
This comment has been minimized.
This comment was marked as outdated.
This comment was marked as outdated.
|
Oh fun I hit https://triage.rust-lang.org/gha-logs/rust-lang/rust/99996805056 before, the issue is some symbol being bumped and now being 3 digits instead of 2, causing formatting changes. |
This comment has been minimized.
This comment has been minimized.
|
@bors try jobs=test-various,aarch64-apple-,-gnu-nopt-,x86_64-mingw-,aarch64-msvc-*,arm-android |
This comment has been minimized.
This comment has been minimized.
`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
This comment has been minimized.
This comment has been minimized.
|
💔 Test for f6d6327 failed: CI. Failed jobs:
|
…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
This comment has been minimized.
This comment has been minimized.
update tests and remove major >= 21 check
… in the documentation
c7fcc75 to
39a366b
Compare
|
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. |
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;
f16balong withbfloatlang item to work with LLVM, GCC is explicitlyunimplemented!(...)f16bfeature gate, page forf16bon libruscdoc and astruct bf16incore::numf16bas a scalar primitive for scalable vectorsIssues;