Skip to content

Commit b12c3af

Browse files
committed
x86_64: Do not use outline-atomics on target where CPUID is not available
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.
1 parent bdcf02c commit b12c3af

File tree

8 files changed

+24
-8
lines changed

8 files changed

+24
-8
lines changed

src/imp/atomic128/detect/x86_64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#![cfg_attr(
44
any(
5-
portable_atomic_no_outline_atomics,
65
not(target_feature = "sse"),
6+
portable_atomic_no_outline_atomics,
77
target_env = "sgx",
88
miri,
99
portable_atomic_sanitize_thread,
@@ -69,7 +69,7 @@ fn _detect(info: &mut CpuInfo) {
6969
{
7070
info.set(CpuInfo::HAS_CMPXCHG16B);
7171
}
72-
// sgx doesn't support `cpuid`: https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/core_arch/src/x86/cpuid.rs#L102-L105
72+
// SGX doesn't support CPUID: https://github.com/rust-lang/stdarch/blob/a0c30f3e3c75adcd6ee7efc94014ebcead61c507/crates/core_arch/src/x86/cpuid.rs#L102-L105
7373
#[cfg(not(any(target_env = "sgx", miri)))]
7474
{
7575
use core::arch::x86_64::_xgetbv;
@@ -137,8 +137,8 @@ mod tests {
137137
}
138138

139139
#[test]
140-
// Miri doesn't support inline assembly
141-
// sgx doesn't support `cpuid`
140+
// SGX doesn't support CPUID.
141+
// Miri doesn't support inline assembly.
142142
#[cfg_attr(any(target_env = "sgx", miri), ignore)]
143143
fn test_cpuid() {
144144
assert_eq!(std::is_x86_feature_detected!("cmpxchg16b"), has_cmpxchg16b());

src/imp/atomic128/x86_64.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,12 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
273273

274274
// Do not use vector registers on targets such as x86_64-unknown-none unless SSE is explicitly enabled.
275275
// https://doc.rust-lang.org/nightly/rustc/platform-support/x86_64-unknown-none.html
276+
// SGX doesn't support CPUID.
276277
// Miri and Sanitizer do not support inline assembly.
277278
#[cfg(any(
278-
portable_atomic_no_outline_atomics,
279279
not(target_feature = "sse"),
280+
portable_atomic_no_outline_atomics,
281+
target_env = "sgx",
280282
miri,
281283
portable_atomic_sanitize_thread,
282284
))]
@@ -285,8 +287,9 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
285287
_atomic_load_cmpxchg16b(src, order)
286288
}
287289
#[cfg(not(any(
288-
portable_atomic_no_outline_atomics,
289290
not(target_feature = "sse"),
291+
portable_atomic_no_outline_atomics,
292+
target_env = "sgx",
290293
miri,
291294
portable_atomic_sanitize_thread,
292295
)))]
@@ -316,10 +319,12 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
316319

317320
// Do not use vector registers on targets such as x86_64-unknown-none unless SSE is explicitly enabled.
318321
// https://doc.rust-lang.org/nightly/rustc/platform-support/x86_64-unknown-none.html
322+
// SGX doesn't support CPUID.
319323
// Miri and Sanitizer do not support inline assembly.
320324
#[cfg(any(
321-
portable_atomic_no_outline_atomics,
322325
not(target_feature = "sse"),
326+
portable_atomic_no_outline_atomics,
327+
target_env = "sgx",
323328
miri,
324329
portable_atomic_sanitize_thread,
325330
))]
@@ -328,8 +333,9 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
328333
_atomic_store_cmpxchg16b(dst, val, order);
329334
}
330335
#[cfg(not(any(
331-
portable_atomic_no_outline_atomics,
332336
not(target_feature = "sse"),
337+
portable_atomic_no_outline_atomics,
338+
target_env = "sgx",
333339
miri,
334340
portable_atomic_sanitize_thread,
335341
)))]

src/imp/fallback/imp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
target_arch = "x86_64",
44
portable_atomic_cmpxchg16b_target_feature,
55
not(portable_atomic_no_outline_atomics),
6+
not(target_env = "sgx"),
67
),
78
allow(dead_code)
89
)]

src/imp/fallback/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub(crate) use seq_lock::imp::{AtomicI64, AtomicU64};
8686
target_arch = "x86_64",
8787
portable_atomic_cmpxchg16b_target_feature,
8888
not(portable_atomic_no_outline_atomics),
89+
not(target_env = "sgx"),
8990
),
9091
),
9192
allow(unused_imports)

src/imp/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod aarch64;
4343
feature = "fallback",
4444
portable_atomic_cmpxchg16b_target_feature,
4545
not(portable_atomic_no_outline_atomics),
46+
not(target_env = "sgx"),
4647
),
4748
))]
4849
#[cfg(target_arch = "x86_64")]
@@ -323,6 +324,7 @@ pub(crate) use self::aarch64::{AtomicI128, AtomicU128};
323324
feature = "fallback",
324325
portable_atomic_cmpxchg16b_target_feature,
325326
not(portable_atomic_no_outline_atomics),
327+
not(target_env = "sgx"),
326328
),
327329
),
328330
target_arch = "x86_64",
@@ -353,6 +355,7 @@ pub(crate) use self::s390x::{AtomicI128, AtomicU128};
353355
feature = "fallback",
354356
portable_atomic_cmpxchg16b_target_feature,
355357
not(portable_atomic_no_outline_atomics),
358+
not(target_env = "sgx"),
356359
),
357360
),
358361
target_arch = "x86_64",

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ See also [the `atomic128` module's readme](https://github.com/taiki-e/portable-a
233233
target_arch = "x86_64",
234234
portable_atomic_unstable_cmpxchg16b_target_feature,
235235
not(portable_atomic_no_outline_atomics),
236+
not(target_env = "sgx"),
236237
feature = "fallback",
237238
),
238239
feature(cmpxchg16b_target_feature)
@@ -4597,6 +4598,7 @@ atomic_int!(AtomicU64, u64, 8);
45974598
feature = "fallback",
45984599
portable_atomic_cmpxchg16b_target_feature,
45994600
not(portable_atomic_no_outline_atomics),
4601+
not(target_env = "sgx"),
46004602
),
46014603
),
46024604
target_arch = "x86_64",
@@ -4649,6 +4651,7 @@ atomic_int!(AtomicI128, i128, 16);
46494651
feature = "fallback",
46504652
portable_atomic_cmpxchg16b_target_feature,
46514653
not(portable_atomic_no_outline_atomics),
4654+
not(target_env = "sgx"),
46524655
),
46534656
),
46544657
target_arch = "x86_64",

src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ fn test_is_lock_free() {
167167
feature = "fallback",
168168
portable_atomic_cmpxchg16b_target_feature,
169169
not(portable_atomic_no_outline_atomics),
170+
not(target_env = "sgx"),
170171
)) && std::is_x86_feature_detected!("cmpxchg16b");
171172
assert_eq!(AtomicI128::is_lock_free(), has_cmpxchg16b);
172173
assert_eq!(AtomicU128::is_lock_free(), has_cmpxchg16b);

src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ macro_rules! serde_impls {
141141
// Adapted from https://github.com/BurntSushi/memchr/blob/2.4.1/src/memchr/x86/mod.rs#L9-L71.
142142
#[allow(unused_macros)]
143143
#[cfg(not(portable_atomic_no_outline_atomics))]
144+
#[cfg(any(target_arch = "aarch64", all(target_arch = "x86_64", not(target_env = "sgx"))))]
144145
macro_rules! ifunc {
145146
// if the functions are unsafe, this macro is also unsafe.
146147
(unsafe fn($($arg_pat:ident: $arg_ty:ty),*) $(-> $ret_ty:ty)? { $($if_block:tt)* }) => {{

0 commit comments

Comments
 (0)