Skip to content

Commit

Permalink
x86_64: Do not enable outline-atomics on target where CPUID is not av…
Browse files Browse the repository at this point in the history
…ailable

SGX doesn't support CPUID. Currently, on this target, if the cmpxchg16b
feature is not enabled at compile time, detect::has_cmpxchg16b will
always return false.

So, enabling outline-atomics on this target does not make sense since
the fallback implementation is always used.
  • Loading branch information
taiki-e committed Mar 1, 2023
1 parent bdcf02c commit 5e38444
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/imp/atomic128/detect/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#![cfg_attr(
any(
portable_atomic_no_outline_atomics,
not(target_feature = "sse"),
portable_atomic_no_outline_atomics,
target_env = "sgx",
miri,
portable_atomic_sanitize_thread,
Expand Down Expand Up @@ -69,7 +69,7 @@ fn _detect(info: &mut CpuInfo) {
{
info.set(CpuInfo::HAS_CMPXCHG16B);
}
// sgx doesn't support `cpuid`: https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/core_arch/src/x86/cpuid.rs#L102-L105
// SGX doesn't support CPUID: https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/core_arch/src/x86/cpuid.rs#L102-L105
#[cfg(not(any(target_env = "sgx", miri)))]
{
use core::arch::x86_64::_xgetbv;
Expand Down Expand Up @@ -137,8 +137,8 @@ mod tests {
}

#[test]
// Miri doesn't support inline assembly
// sgx doesn't support `cpuid`
// SGX doesn't support CPUID.
// Miri doesn't support inline assembly.
#[cfg_attr(any(target_env = "sgx", miri), ignore)]
fn test_cpuid() {
assert_eq!(std::is_x86_feature_detected!("cmpxchg16b"), has_cmpxchg16b());
Expand Down
14 changes: 10 additions & 4 deletions src/imp/atomic128/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,12 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {

// Do not use vector registers on targets such as x86_64-unknown-none unless SSE is explicitly enabled.
// https://doc.rust-lang.org/nightly/rustc/platform-support/x86_64-unknown-none.html
// SGX doesn't support CPUID.
// Miri and Sanitizer do not support inline assembly.
#[cfg(any(
portable_atomic_no_outline_atomics,
not(target_feature = "sse"),
portable_atomic_no_outline_atomics,
target_env = "sgx",
miri,
portable_atomic_sanitize_thread,
))]
Expand All @@ -285,8 +287,9 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
_atomic_load_cmpxchg16b(src, order)
}
#[cfg(not(any(
portable_atomic_no_outline_atomics,
not(target_feature = "sse"),
portable_atomic_no_outline_atomics,
target_env = "sgx",
miri,
portable_atomic_sanitize_thread,
)))]
Expand Down Expand Up @@ -316,10 +319,12 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {

// Do not use vector registers on targets such as x86_64-unknown-none unless SSE is explicitly enabled.
// https://doc.rust-lang.org/nightly/rustc/platform-support/x86_64-unknown-none.html
// SGX doesn't support CPUID.
// Miri and Sanitizer do not support inline assembly.
#[cfg(any(
portable_atomic_no_outline_atomics,
not(target_feature = "sse"),
portable_atomic_no_outline_atomics,
target_env = "sgx",
miri,
portable_atomic_sanitize_thread,
))]
Expand All @@ -328,8 +333,9 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
_atomic_store_cmpxchg16b(dst, val, order);
}
#[cfg(not(any(
portable_atomic_no_outline_atomics,
not(target_feature = "sse"),
portable_atomic_no_outline_atomics,
target_env = "sgx",
miri,
portable_atomic_sanitize_thread,
)))]
Expand Down
1 change: 1 addition & 0 deletions src/imp/fallback/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
target_arch = "x86_64",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
allow(dead_code)
)]
Expand Down
1 change: 1 addition & 0 deletions src/imp/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) use seq_lock::imp::{AtomicI64, AtomicU64};
target_arch = "x86_64",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
),
allow(unused_imports)
Expand Down
3 changes: 3 additions & 0 deletions src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod aarch64;
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
))]
#[cfg(target_arch = "x86_64")]
Expand Down Expand Up @@ -323,6 +324,7 @@ pub(crate) use self::aarch64::{AtomicI128, AtomicU128};
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
),
target_arch = "x86_64",
Expand Down Expand Up @@ -353,6 +355,7 @@ pub(crate) use self::s390x::{AtomicI128, AtomicU128};
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
),
target_arch = "x86_64",
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ See also [the `atomic128` module's readme](https://github.com/taiki-e/portable-a
target_arch = "x86_64",
portable_atomic_unstable_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
feature = "fallback",
),
feature(cmpxchg16b_target_feature)
Expand Down Expand Up @@ -4597,6 +4598,7 @@ atomic_int!(AtomicU64, u64, 8);
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
),
target_arch = "x86_64",
Expand Down Expand Up @@ -4649,6 +4651,7 @@ atomic_int!(AtomicI128, i128, 16);
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
),
),
target_arch = "x86_64",
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fn test_is_lock_free() {
feature = "fallback",
portable_atomic_cmpxchg16b_target_feature,
not(portable_atomic_no_outline_atomics),
not(target_env = "sgx"),
)) && std::is_x86_feature_detected!("cmpxchg16b");
assert_eq!(AtomicI128::is_lock_free(), has_cmpxchg16b);
assert_eq!(AtomicU128::is_lock_free(), has_cmpxchg16b);
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ macro_rules! serde_impls {
// Adapted from https://github.com/BurntSushi/memchr/blob/2.4.1/src/memchr/x86/mod.rs#L9-L71.
#[allow(unused_macros)]
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(target_arch = "aarch64", all(target_arch = "x86_64", not(target_env = "sgx"))))]
macro_rules! ifunc {
// if the functions are unsafe, this macro is also unsafe.
(unsafe fn($($arg_pat:ident: $arg_ty:ty),*) $(-> $ret_ty:ty)? { $($if_block:tt)* }) => {{
Expand Down

0 comments on commit 5e38444

Please sign in to comment.