Implement the internal feature cfg_target_has_reliable_f16_f128

Support for `f16` and `f128` is varied across targets, backends, and
backend versions. Eventually we would like to reach a point where all
backends support these approximately equally, but until then we have to
work around some of these nuances of support being observable.

Introduce the `cfg_target_has_reliable_f16_f128` internal feature, which
provides the following new configuration gates:

* `cfg(target_has_reliable_f16)`
* `cfg(target_has_reliable_f16_math)`
* `cfg(target_has_reliable_f128)`
* `cfg(target_has_reliable_f128_math)`

`reliable_f16` and `reliable_f128` indicate that basic arithmetic for
the type works correctly. The `_math` versions indicate that anything
relying on `libm` works correctly, since sometimes this hits a separate
class of codegen bugs.

These options match configuration set by the build script at [1]. The
logic for LLVM support is duplicated as-is from the same script. There
are a few possible updates that will come as a follow up.

The config introduced here is not planned to ever become stable, it is
only intended to replace the build scripts for `std` tests and
`compiler-builtins` that don't have any way to configure based on the
codegen backend.

MCP: https://github.com/rust-lang/compiler-team/issues/866
Closes: https://github.com/rust-lang/compiler-team/issues/866

[1]: 555e1d0386/library/std/build.rs (L84-L186)
This commit is contained in:
Trevor Gross 2025-04-24 22:11:23 +00:00
parent 555e1d0386
commit 6ceeb0849e
20 changed files with 311 additions and 27 deletions

View file

@ -0,0 +1,8 @@
error: unexpected `--cfg target_has_reliable_f128` flag
|
= note: config `target_has_reliable_f128` is only supposed to be controlled by `--target`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
error: aborting due to 1 previous error

View file

@ -0,0 +1,8 @@
error: unexpected `--cfg target_has_reliable_f128_math` flag
|
= note: config `target_has_reliable_f128_math` is only supposed to be controlled by `--target`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
error: aborting due to 1 previous error

View file

@ -0,0 +1,8 @@
error: unexpected `--cfg target_has_reliable_f16` flag
|
= note: config `target_has_reliable_f16` is only supposed to be controlled by `--target`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
error: aborting due to 1 previous error

View file

@ -0,0 +1,8 @@
error: unexpected `--cfg target_has_reliable_f16_math` flag
|
= note: config `target_has_reliable_f16_math` is only supposed to be controlled by `--target`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
error: aborting due to 1 previous error

View file

@ -8,6 +8,7 @@
//@ revisions: target_thread_local_ relocation_model_
//@ revisions: fmt_debug_
//@ revisions: emscripten_wasm_eh_
//@ revisions: reliable_f16_ reliable_f16_math_ reliable_f128_ reliable_f128_math_
//@ [overflow_checks_]compile-flags: --cfg overflow_checks
//@ [debug_assertions_]compile-flags: --cfg debug_assertions
@ -35,6 +36,10 @@
//@ [relocation_model_]compile-flags: --cfg relocation_model="a"
//@ [fmt_debug_]compile-flags: --cfg fmt_debug="shallow"
//@ [emscripten_wasm_eh_]compile-flags: --cfg emscripten_wasm_eh
//@ [reliable_f16_]compile-flags: --cfg target_has_reliable_f16
//@ [reliable_f16_math_]compile-flags: --cfg target_has_reliable_f16_math
//@ [reliable_f128_]compile-flags: --cfg target_has_reliable_f128
//@ [reliable_f128_math_]compile-flags: --cfg target_has_reliable_f128_math
fn main() {}

View file

@ -0,0 +1,12 @@
//@ compile-flags: --check-cfg=cfg(target_has_reliable_f16,target_has_reliable_f16_math,target_has_reliable_f128,target_has_reliable_f128_math)
fn main() {
cfg!(target_has_reliable_f16);
//~^ ERROR `cfg(target_has_reliable_f16)` is experimental and subject to change
cfg!(target_has_reliable_f16_math);
//~^ ERROR `cfg(target_has_reliable_f16_math)` is experimental and subject to change
cfg!(target_has_reliable_f128);
//~^ ERROR `cfg(target_has_reliable_f128)` is experimental and subject to change
cfg!(target_has_reliable_f128_math);
//~^ ERROR `cfg(target_has_reliable_f128_math)` is experimental and subject to change
}

View file

@ -0,0 +1,39 @@
error[E0658]: `cfg(target_has_reliable_f16)` is experimental and subject to change
--> $DIR/feature-gate-cfg-target-has-reliable-f16-f128.rs:4:10
|
LL | cfg!(target_has_reliable_f16);
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: `cfg(target_has_reliable_f16_math)` is experimental and subject to change
--> $DIR/feature-gate-cfg-target-has-reliable-f16-f128.rs:6:10
|
LL | cfg!(target_has_reliable_f16_math);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: `cfg(target_has_reliable_f128)` is experimental and subject to change
--> $DIR/feature-gate-cfg-target-has-reliable-f16-f128.rs:8:10
|
LL | cfg!(target_has_reliable_f128);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: `cfg(target_has_reliable_f128_math)` is experimental and subject to change
--> $DIR/feature-gate-cfg-target-has-reliable-f16-f128.rs:10:10
|
LL | cfg!(target_has_reliable_f128_math);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(cfg_target_has_reliable_f16_f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -0,0 +1,31 @@
//@ run-pass
//@ compile-flags: --check-cfg=cfg(target_has_reliable_f16,target_has_reliable_f16_math,target_has_reliable_f128,target_has_reliable_f128_math)
// Verify that the feature gates and config work and are registered as known config
// options.
#![deny(unexpected_cfgs)]
#![feature(cfg_target_has_reliable_f16_f128)]
#[cfg(target_has_reliable_f16)]
pub fn has_f16() {}
#[cfg(target_has_reliable_f16_math)]
pub fn has_f16_math() {}
#[cfg(target_has_reliable_f128 )]
pub fn has_f128() {}
#[cfg(target_has_reliable_f128_math)]
pub fn has_f128_math() {}
fn main() {
if cfg!(target_arch = "aarch64") && cfg!(target_os = "linux") {
// Aarch64+Linux is one target that has support for all features, so use it to spot
// check that the compiler does indeed enable these gates.
assert!(cfg!(target_has_reliable_f16));
assert!(cfg!(target_has_reliable_f16_math));
assert!(cfg!(target_has_reliable_f128));
assert!(cfg!(target_has_reliable_f128_math));
}
}