Skip to content

Commit

Permalink
aarch64: Support outline atomics on old nightly
Browse files Browse the repository at this point in the history
Similar to 3f5d42f.
  • Loading branch information
taiki-e committed Feb 10, 2023
1 parent cbda326 commit a5b7722
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
17 changes: 13 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ fn main() {
}
println!("cargo:rustc-cfg=portable_atomic_no_asm");
}
// aarch64_target_feature stabilized in Rust 1.61 (nightly-2022-03-16): https://github.com/rust-lang/rust/pull/90621
if !version.probe(61, 2022, 3, 15) {
println!("cargo:rustc-cfg=portable_atomic_no_aarch64_target_feature");
}
// https://github.com/rust-lang/rust/pull/98383 merged in Rust 1.64 (nightly-2022-07-19).
if !version.probe(64, 2022, 7, 18) {
println!("cargo:rustc-cfg=portable_atomic_no_stronger_failure_ordering");
Expand Down Expand Up @@ -176,6 +172,19 @@ fn main() {
}
}
"aarch64" => {
// aarch64_target_feature stabilized in Rust 1.61 (nightly-2022-03-16): https://github.com/rust-lang/rust/pull/90621
if !version.probe(61, 2022, 3, 15) {
if version.nightly && is_allowed_feature("aarch64_target_feature") {
// The part of this feature we use has not been changed since 1.27
// (https://github.com/rust-lang/rust/commit/1217d70465edb2079880347fea4baaac56895f51)
// until it was stabilized in nightly-2022-03-16, so it can be safely enabled in
// nightly, which is older than nightly-2022-03-16.
println!("cargo:rustc-cfg=portable_atomic_unstable_aarch64_target_feature");
} else {
println!("cargo:rustc-cfg=portable_atomic_no_aarch64_target_feature");
}
}

// aarch64 macos always support FEAT_LSE and FEAT_LSE2 because it is armv8.6: https://github.com/rust-lang/rust/blob/1.67.0/compiler/rustc_target/src/spec/aarch64_apple_darwin.rs#L7
let is_macos = target_os == "macos";
// aarch64_target_feature stabilized in Rust 1.61.
Expand Down
7 changes: 5 additions & 2 deletions src/imp/atomic128/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,12 @@ unsafe fn atomic_compare_exchange(
#[cfg(any(
target_feature = "lse",
portable_atomic_target_feature = "lse",
not(portable_atomic_no_aarch64_target_feature),
all(not(portable_atomic_no_aarch64_target_feature), not(portable_atomic_no_outline_atomics)),
))]
#[cfg_attr(not(portable_atomic_no_aarch64_target_feature), target_feature(enable = "lse"))]
#[cfg_attr(
not(any(target_feature = "lse", portable_atomic_target_feature = "lse",)),
target_feature(enable = "lse")
)]
#[inline]
unsafe fn _atomic_compare_exchange_casp(
dst: *mut u128,
Expand Down
5 changes: 4 additions & 1 deletion src/imp/atomic128/detect/aarch64_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub(crate) fn has_lse() -> bool {
#[allow(unreachable_code)]
{
#[cfg(all(
not(portable_atomic_no_aarch64_target_feature),
not(any(
portable_atomic_no_aarch64_target_feature,
portable_atomic_unstable_aarch64_target_feature
)),
// https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/std_detect/src/detect/mod.rs
// It is fine to use std for targets that we know can be linked to std.
// Note: std may not be available on tier 3 such as aarch64 FreeBSD/OpenBSD.
Expand Down
20 changes: 16 additions & 4 deletions src/imp/atomic128/detect/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ mod tests_aarch64_common {
#[cfg(any(
target_feature = "lse",
portable_atomic_target_feature = "lse",
not(portable_atomic_no_aarch64_target_feature),
all(
not(portable_atomic_no_aarch64_target_feature),
not(portable_atomic_no_outline_atomics)
),
))]
unsafe {
use core::{cell::UnsafeCell, sync::atomic::Ordering};
Expand All @@ -265,7 +268,10 @@ mod tests_aarch64_common {
}
} else {
assert!(!detect().test(CpuInfo::HAS_LSE));
#[cfg(not(portable_atomic_no_aarch64_target_feature))]
#[cfg(not(any(
portable_atomic_no_aarch64_target_feature,
portable_atomic_unstable_aarch64_target_feature
)))]
{
assert!(!std::arch::is_aarch64_feature_detected!("lse"));
}
Expand All @@ -281,7 +287,10 @@ mod tests_aarch64_common {
}
} else {
assert!(!detect().test(CpuInfo::HAS_LSE2));
// #[cfg(not(portable_atomic_no_aarch64_target_feature))]
// #[cfg(not(any(
// portable_atomic_no_aarch64_target_feature,
// portable_atomic_unstable_aarch64_target_feature
// )))]
// {
// assert!(!std::arch::is_aarch64_feature_detected!("lse2"));
// }
Expand All @@ -295,7 +304,10 @@ mod tests_aarch64_common {
assert!(detect().test(CpuInfo::HAS_LSE128));
} else {
assert!(!detect().test(CpuInfo::HAS_LSE128));
// #[cfg(not(portable_atomic_no_aarch64_target_feature))]
// #[cfg(not(any(
// portable_atomic_no_aarch64_target_feature,
// portable_atomic_unstable_aarch64_target_feature
// )))]
// {
// assert!(!std::arch::is_aarch64_feature_detected!("lse128"));
// }
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,19 @@ See also [the `atomic128` module's readme](https://github.com/taiki-e/portable-a
// These features are already stabilized or have already been removed from compilers,
// and can safely be enabled for old nightly as long as version detection works.
// - cfg(target_has_atomic)
// - #[target_feature(enable = "lse")] on AArch64
// - asm! on ARM, AArch64, RISC-V, x86_64
// - llvm_asm! on AVR (tier 3) and MSP430 (tier 3)
// - #[instruction_set] on non-Linux pre-v6 ARM (tier 3)
#![cfg_attr(portable_atomic_unstable_cfg_target_has_atomic, feature(cfg_target_has_atomic))]
#![cfg_attr(
all(
target_arch = "aarch64",
portable_atomic_unstable_aarch64_target_feature,
not(portable_atomic_no_outline_atomics),
),
feature(aarch64_target_feature)
)]
#![cfg_attr(
all(
portable_atomic_unstable_asm,
Expand Down

0 comments on commit a5b7722

Please sign in to comment.