Skip to content

Commit

Permalink
Remove use of feature(integer_atomics)
Browse files Browse the repository at this point in the history
Fixes subtle behavior differences between stable and nightly.
  • Loading branch information
taiki-e committed Jun 17, 2022
1 parent b55eed8 commit 61fe79f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 105 deletions.
38 changes: 10 additions & 28 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ const LATEST_STABLE: Version =
// Probe if unstable features can be compiled with the expected API.
// This prevents accidental depends on them if the upstream changes its API.
// This is the same approach used in autocfg, anyhow, etc.
// for aarch64 and x86_64 macos
const PROBE_ATOMIC_128: &str = r#"
#![no_std]
#![feature(integer_atomics)]
fn _probe() {
let v = core::sync::atomic::AtomicU128::new(0_u128);
let _: u128 = v.swap(1_u128, core::sync::atomic::Ordering::Relaxed);
}
"#;
// for x86_64
const PROBE_CMPXCHG16B: &str = r#"
#![no_std]
Expand Down Expand Up @@ -172,11 +163,8 @@ fn main() {
println!("cargo:rustc-cfg=portable_atomic_armv5te");
}

let aarch64 = target.starts_with("aarch64");
let x86_64 = target.starts_with("x86_64");

// aarch64 macos always support lse and lse2 because it is armv8.6: /~https://github.com/rust-lang/rust/blob/1.61.0/compiler/rustc_target/src/spec/aarch64_apple_darwin.rs#L5
if aarch64 && (version.minor >= 59 || version.nightly) {
if target.starts_with("aarch64") && (version.minor >= 59 || version.nightly) {
// aarch64_target_feature stabilized in Rust 1.61.
if has_target_feature("lse", target == "aarch64-apple-darwin", &version, Some(61), true) {
println!("cargo:rustc-cfg=portable_atomic_target_feature=\"lse\"");
Expand All @@ -189,7 +177,8 @@ fn main() {
}

// cmpxchg16b is available via asm (1.59+) or stdsimd (nightly).
let may_use_cmpxchg16b = x86_64 && (version.minor >= 59 || version.nightly);
let may_use_cmpxchg16b =
target.starts_with("x86_64") && (version.minor >= 59 || version.nightly);
let mut has_cmpxchg16b = false;
if may_use_cmpxchg16b {
// x86_64 macos always support cmpxchg16b: /~https://github.com/rust-lang/rust/blob/1.61.0/compiler/rustc_target/src/spec/x86_64_apple_darwin.rs#L7
Expand All @@ -209,20 +198,13 @@ fn main() {
println!("cargo:rustc-cfg=sanitize_thread");
}

if aarch64 || x86_64 {
if HAS_ATOMIC_128.contains(&&*target)
&& probe(PROBE_ATOMIC_128, &target).unwrap_or(false)
{
println!("cargo:rustc-cfg=portable_atomic_core_atomic_128");
} else if may_use_cmpxchg16b
&& (has_cmpxchg16b
|| cfg!(feature = "fallback") && cfg!(feature = "outline-atomics"))
&& probe(PROBE_CMPXCHG16B, &target).unwrap_or(false)
{
println!("cargo:rustc-cfg=portable_atomic_cmpxchg16b_stdsimd");
if cfg!(feature = "fallback") && cfg!(feature = "outline-atomics") {
println!("cargo:rustc-cfg=portable_atomic_cmpxchg16b_dynamic");
}
if may_use_cmpxchg16b
&& (has_cmpxchg16b || cfg!(feature = "fallback") && cfg!(feature = "outline-atomics"))
&& probe(PROBE_CMPXCHG16B, &target).unwrap_or(false)
{
println!("cargo:rustc-cfg=portable_atomic_cmpxchg16b_stdsimd");
if cfg!(feature = "fallback") && cfg!(feature = "outline-atomics") {
println!("cargo:rustc-cfg=portable_atomic_cmpxchg16b_dynamic");
}
} else if target.starts_with("s390x")
&& probe(PROBE_ATOMIC_INTRINSICS, &target).unwrap_or(false)
Expand Down
26 changes: 0 additions & 26 deletions no_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,3 @@ const NO_ATOMIC: &[&str] = &[
"riscv32im-unknown-none-elf",
"riscv32imc-unknown-none-elf",
];

const HAS_ATOMIC_128: &[&str] = &[
"aarch64-apple-darwin",
"aarch64-apple-ios",
"aarch64-apple-ios-macabi",
"aarch64-apple-ios-sim",
"aarch64-apple-tvos",
"aarch64-apple-watchos-sim",
"aarch64-fuchsia",
"aarch64-kmc-solid_asp3",
"aarch64-linux-android",
"aarch64-unknown-freebsd",
"aarch64-unknown-hermit",
"aarch64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu_ilp32",
"aarch64-unknown-linux-musl",
"aarch64-unknown-netbsd",
"aarch64-unknown-none",
"aarch64-unknown-none-softfloat",
"aarch64-unknown-openbsd",
"aarch64-unknown-redox",
"aarch64-wrs-vxworks",
"aarch64_be-unknown-linux-gnu",
"aarch64_be-unknown-linux-gnu_ilp32",
"x86_64-apple-darwin",
];
2 changes: 0 additions & 2 deletions src/imp/core_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ atomic!(AtomicI32, AtomicU32);
cfg(any(target_has_atomic = "64", target_pointer_width = "64")) // cfg(target_has_atomic_load_store = "64")
)]
atomic!(AtomicI64, AtomicU64);
#[cfg(portable_atomic_core_atomic_128)]
atomic!(AtomicI128, AtomicU128);
22 changes: 2 additions & 20 deletions src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
#[cfg(not(portable_atomic_no_atomic_load_store))]
mod core_atomic;

#[cfg(any(test, not(portable_atomic_core_atomic_128)))]
#[cfg(any(not(portable_atomic_no_asm), portable_atomic_nightly))]
#[cfg(target_arch = "aarch64")]
#[path = "atomic128/aarch64.rs"]
mod aarch64;

#[cfg(any(test, not(portable_atomic_core_atomic_128)))]
#[cfg(any(not(portable_atomic_no_asm), portable_atomic_nightly))]
#[cfg(any(
target_feature = "cmpxchg16b",
Expand Down Expand Up @@ -51,15 +49,9 @@ mod riscv;
#[cfg(any(
test,
not(any(
portable_atomic_core_atomic_128,
portable_atomic_s390x_atomic_128,
all(any(not(portable_atomic_no_asm), portable_atomic_nightly), target_arch = "aarch64"),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
target_arch = "aarch64"
),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
any(target_feature = "cmpxchg16b", portable_atomic_target_feature = "cmpxchg16b"),
target_arch = "x86_64",
Expand Down Expand Up @@ -206,18 +198,14 @@ pub(crate) use self::fallback::{AtomicI64, AtomicU64};
pub(crate) use self::interrupt::{AtomicI64, AtomicU64};

// Atomic{I,U}128
#[cfg(portable_atomic_core_atomic_128)]
pub(crate) use self::core_atomic::{AtomicI128, AtomicU128};
// aarch64 stable
#[cfg(all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
target_arch = "aarch64"
))]
pub(crate) use self::aarch64::{AtomicI128, AtomicU128};
// no core Atomic{I,U}128 & has cmpxchg16b => use cmpxchg16b
#[cfg(all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
any(
target_feature = "cmpxchg16b",
Expand All @@ -238,15 +226,9 @@ pub(crate) use self::s390x::{AtomicI128, AtomicU128};
// no core Atomic{I,U}128 & has CAS => use lock-base fallback
#[cfg(feature = "fallback")]
#[cfg(not(any(
portable_atomic_core_atomic_128,
portable_atomic_s390x_atomic_128,
all(any(not(portable_atomic_no_asm), portable_atomic_nightly), target_arch = "aarch64"),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
target_arch = "aarch64"
),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
any(
target_feature = "cmpxchg16b",
Expand Down
20 changes: 2 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ On x86_64, when the `outline-atomics` optional feature is not enabled and `cmpxc
clippy::type_complexity
)]
// 128-bit atomic
#![cfg_attr(
any(all(test, portable_atomic_nightly), portable_atomic_core_atomic_128),
feature(integer_atomics)
)]
#![cfg_attr(
all(
target_arch = "x86_64",
Expand Down Expand Up @@ -2036,15 +2032,9 @@ atomic_int!(AtomicU64, u64, 8);
#[cfg_attr(
not(feature = "fallback"),
cfg(any(
portable_atomic_core_atomic_128,
portable_atomic_s390x_atomic_128,
all(any(not(portable_atomic_no_asm), portable_atomic_nightly), target_arch = "aarch64"),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
target_arch = "aarch64"
),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
any(
target_feature = "cmpxchg16b",
Expand Down Expand Up @@ -2072,15 +2062,9 @@ atomic_int!(AtomicI128, i128, 16);
#[cfg_attr(
not(feature = "fallback"),
cfg(any(
portable_atomic_core_atomic_128,
portable_atomic_s390x_atomic_128,
all(any(not(portable_atomic_no_asm), portable_atomic_nightly), target_arch = "aarch64"),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
target_arch = "aarch64"
),
all(
not(portable_atomic_core_atomic_128),
any(not(portable_atomic_no_asm), portable_atomic_nightly),
any(
target_feature = "cmpxchg16b",
Expand Down
12 changes: 1 addition & 11 deletions tools/no_atomic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ file="no_atomic.rs"
no_atomic_cas=()
no_atomic_64=()
no_atomic=()
has_atomic_128=()
for target in $(rustc --print target-list); do
target_spec=$(rustc --print target-spec-json -Z unstable-options --target "${target}")
res=$(jq <<<"${target_spec}" -r 'select(."atomic-cas" == false)')
Expand All @@ -32,8 +31,7 @@ for target in $(rustc --print target-list); do
no_atomic_64+=("${target}")
no_atomic+=("${target}")
;;
64) ;;
128) has_atomic_128+=("${target}") ;;
64 | 128) ;;
# There is no `"max-atomic-width" == 16` or `"max-atomic-width" == 8` targets.
*) echo "${target}" && exit 1 ;;
esac
Expand Down Expand Up @@ -66,12 +64,4 @@ for target in "${no_atomic[@]}"; do
done
cat >>"${file}" <<EOF
];
const HAS_ATOMIC_128: &[&str] = &[
EOF
for target in "${has_atomic_128[@]}"; do
echo " \"${target}\"," >>"${file}"
done
cat >>"${file}" <<EOF
];
EOF

0 comments on commit 61fe79f

Please sign in to comment.