Skip to content

Commit

Permalink
Merge pull request rust-lang#342 from tgross35/adjust-features
Browse files Browse the repository at this point in the history
Adjust how intrinsics are enabled
  • Loading branch information
tgross35 authored Oct 28, 2024
2 parents 2063e36 + da5144f commit 86a9a75
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ default = []

# This tells the compiler to assume that a Nightly toolchain is being used and
# that it should activate any useful Nightly things accordingly.
unstable = []
unstable = ["unstable-intrinsics"]

# Enable calls to functions in `core::intrinsics`
unstable-intrinsics = []

# Used to prevent using any intrinsics or arch-specific code.
#
# HACK: this is a negative feature which is generally a bad idea in Cargo, but
# we need it to be able to forbid other features when this crate is used in
# Rust dependencies. Setting this overrides all features that may enable
# hard float operations.
force-soft-floats = []

[workspace]
Expand Down
15 changes: 14 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rustc-check-cfg=cfg(assert_no_panic)");
println!("cargo:rustc-check-cfg=cfg(feature, values(\"unstable\"))");

println!("cargo:rustc-check-cfg=cfg(feature, values(\"checked\"))");

Expand All @@ -14,4 +13,18 @@ fn main() {
println!("cargo:rustc-cfg=assert_no_panic");
}
}

configure_intrinsics();
}

/// Simplify the feature logic for enabling intrinsics so code only needs to use
/// `cfg(intrinsics_enabled)`.
fn configure_intrinsics() {
println!("cargo:rustc-check-cfg=cfg(intrinsics_enabled)");

// Disabled by default; `unstable-intrinsics` enables again; `force-soft-floats` overrides
// to disable.
if cfg!(feature = "unstable-intrinsics") && !cfg!(feature = "force-soft-floats") {
println!("cargo:rustc-cfg=intrinsics_enabled");
}
}
10 changes: 7 additions & 3 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ if [ "$(uname -a)" = "Linux" ]; then
extra_flags="$extra_flags --features libm-test/test-musl-serialized"
fi

# Make sure we can build with overriding features. We test the indibidual
# features it controls separately.
cargo check --features "force-soft-floats"

if [ "${BUILD_ONLY:-}" = "1" ]; then
cmd="cargo build --target $target --package libm"
$cmd
$cmd --features 'unstable'
$cmd --features "unstable-intrinsics"

echo "can't run tests on $target"
else
Expand All @@ -60,6 +64,6 @@ else
$cmd --release

# unstable with a feature
$cmd --features 'unstable'
$cmd --release --features 'unstable'
$cmd --features "unstable-intrinsics"
$cmd --release --features "unstable-intrinsics"
fi
8 changes: 8 additions & 0 deletions crates/compiler-builtins-smoke-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ test = false
bench = false

[features]
# Duplicated from libm's Cargo.toml
unstable = []
unstable-intrinsics = []
checked = []
force-soft-floats = []

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = [
"cfg(assert_no_panic)",
"cfg(intrinsics_enabled)",
] }
3 changes: 0 additions & 3 deletions crates/compiler-builtins-smoke-test/build.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! libm in pure Rust
#![no_std]
#![cfg_attr(feature = "unstable", allow(internal_features))]
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
#![cfg_attr(intrinsics_enabled, allow(internal_features))]
#![cfg_attr(intrinsics_enabled, feature(core_intrinsics))]
#![allow(clippy::assign_op_pattern)]
#![allow(clippy::deprecated_cfg_attr)]
#![allow(clippy::eq_op)]
Expand Down
6 changes: 3 additions & 3 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ macro_rules! i {
// the time of this writing this is only used in a few places, and once
// rust-lang/rust#72751 is fixed then this macro will no longer be necessary and
// the native `/` operator can be used and panics won't be codegen'd.
#[cfg(any(debug_assertions, not(feature = "unstable")))]
#[cfg(any(debug_assertions, not(intrinsics_enabled)))]
macro_rules! div {
($a:expr, $b:expr) => {
$a / $b
};
}

#[cfg(all(not(debug_assertions), feature = "unstable"))]
#[cfg(all(not(debug_assertions), intrinsics_enabled))]
macro_rules! div {
($a:expr, $b:expr) => {
unsafe { core::intrinsics::unchecked_div($a, $b) }
Expand All @@ -76,7 +76,7 @@ macro_rules! div {

macro_rules! llvm_intrinsically_optimized {
(#[cfg($($clause:tt)*)] $e:expr) => {
#[cfg(all(feature = "unstable", not(feature = "force-soft-floats"), $($clause)*))]
#[cfg(all(intrinsics_enabled, not(feature = "force-soft-floats"), $($clause)*))]
{
if true { // thwart the dead code lint
$e
Expand Down

0 comments on commit 86a9a75

Please sign in to comment.