Skip to content

Commit bf321ec

Browse files
authored
Rollup merge of #106856 - vadorovsky:fix-atomic-annotations, r=joshtriplett
core: Support variety of atomic widths in width-agnostic functions Before this change, the following functions and macros were annotated with `#[cfg(target_has_atomic = "8")]` or `#[cfg(target_has_atomic_load_store = "8")]`: * `atomic_int` * `strongest_failure_ordering` * `atomic_swap` * `atomic_add` * `atomic_sub` * `atomic_compare_exchange` * `atomic_compare_exchange_weak` * `atomic_and` * `atomic_nand` * `atomic_or` * `atomic_xor` * `atomic_max` * `atomic_min` * `atomic_umax` * `atomic_umin` However, none of those functions and macros actually depend on 8-bit width and they are needed for all atomic widths (16-bit, 32-bit, 64-bit etc.). Some targets might not support 8-bit atomics (i.e. BPF, if we would enable atomic CAS for it). This change fixes that by removing the `"8"` argument from annotations, which results in accepting the whole variety of widths. Fixes #106845 Fixes #106795 Signed-off-by: Michal Rostecki <[email protected]>
2 parents 4b4aeae + 474ea87 commit bf321ec

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

compiler/rustc_session/src/config.rs

+8
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
945945
if sess.target.has_thread_local {
946946
ret.insert((sym::target_thread_local, None));
947947
}
948+
let mut has_atomic = false;
948949
for (i, align) in [
949950
(8, layout.i8_align.abi),
950951
(16, layout.i16_align.abi),
@@ -953,6 +954,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
953954
(128, layout.i128_align.abi),
954955
] {
955956
if i >= min_atomic_width && i <= max_atomic_width {
957+
has_atomic = true;
956958
let mut insert_atomic = |s, align: Align| {
957959
ret.insert((sym::target_has_atomic_load_store, Some(Symbol::intern(s))));
958960
if atomic_cas {
@@ -969,6 +971,12 @@ fn default_configuration(sess: &Session) -> CrateConfig {
969971
}
970972
}
971973
}
974+
if sess.is_nightly_build() && has_atomic {
975+
ret.insert((sym::target_has_atomic_load_store, None));
976+
if atomic_cas {
977+
ret.insert((sym::target_has_atomic, None));
978+
}
979+
}
972980

973981
let panic_strategy = sess.panic_strategy();
974982
ret.insert((sym::panic, Some(panic_strategy.desc_symbol())));

library/core/src/sync/atomic.rs

+30-15
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,8 @@ macro_rules! if_not_8_bit {
18611861
($_:ident, $($tt:tt)*) => { $($tt)* };
18621862
}
18631863

1864-
#[cfg(target_has_atomic_load_store = "8")]
1864+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic_load_store))]
1865+
#[cfg_attr(bootstrap, cfg(target_has_atomic_load_store = "8"))]
18651866
macro_rules! atomic_int {
18661867
($cfg_cas:meta,
18671868
$cfg_align:meta,
@@ -2988,7 +2989,8 @@ atomic_int_ptr_sized! {
29882989
}
29892990

29902991
#[inline]
2991-
#[cfg(target_has_atomic = "8")]
2992+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
2993+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
29922994
fn strongest_failure_ordering(order: Ordering) -> Ordering {
29932995
match order {
29942996
Release => Relaxed,
@@ -3030,7 +3032,8 @@ unsafe fn atomic_load<T: Copy>(dst: *const T, order: Ordering) -> T {
30303032
}
30313033

30323034
#[inline]
3033-
#[cfg(target_has_atomic = "8")]
3035+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3036+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
30343037
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
30353038
unsafe fn atomic_swap<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30363039
// SAFETY: the caller must uphold the safety contract for `atomic_swap`.
@@ -3047,7 +3050,8 @@ unsafe fn atomic_swap<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30473050

30483051
/// Returns the previous value (like __sync_fetch_and_add).
30493052
#[inline]
3050-
#[cfg(target_has_atomic = "8")]
3053+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3054+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
30513055
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
30523056
unsafe fn atomic_add<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30533057
// SAFETY: the caller must uphold the safety contract for `atomic_add`.
@@ -3064,7 +3068,8 @@ unsafe fn atomic_add<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30643068

30653069
/// Returns the previous value (like __sync_fetch_and_sub).
30663070
#[inline]
3067-
#[cfg(target_has_atomic = "8")]
3071+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3072+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
30683073
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
30693074
unsafe fn atomic_sub<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30703075
// SAFETY: the caller must uphold the safety contract for `atomic_sub`.
@@ -3080,7 +3085,8 @@ unsafe fn atomic_sub<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
30803085
}
30813086

30823087
#[inline]
3083-
#[cfg(target_has_atomic = "8")]
3088+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3089+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
30843090
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
30853091
unsafe fn atomic_compare_exchange<T: Copy>(
30863092
dst: *mut T,
@@ -3115,7 +3121,8 @@ unsafe fn atomic_compare_exchange<T: Copy>(
31153121
}
31163122

31173123
#[inline]
3118-
#[cfg(target_has_atomic = "8")]
3124+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3125+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
31193126
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
31203127
unsafe fn atomic_compare_exchange_weak<T: Copy>(
31213128
dst: *mut T,
@@ -3150,7 +3157,8 @@ unsafe fn atomic_compare_exchange_weak<T: Copy>(
31503157
}
31513158

31523159
#[inline]
3153-
#[cfg(target_has_atomic = "8")]
3160+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3161+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
31543162
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
31553163
unsafe fn atomic_and<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31563164
// SAFETY: the caller must uphold the safety contract for `atomic_and`
@@ -3166,7 +3174,8 @@ unsafe fn atomic_and<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31663174
}
31673175

31683176
#[inline]
3169-
#[cfg(target_has_atomic = "8")]
3177+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3178+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
31703179
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
31713180
unsafe fn atomic_nand<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31723181
// SAFETY: the caller must uphold the safety contract for `atomic_nand`
@@ -3182,7 +3191,8 @@ unsafe fn atomic_nand<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31823191
}
31833192

31843193
#[inline]
3185-
#[cfg(target_has_atomic = "8")]
3194+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3195+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
31863196
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
31873197
unsafe fn atomic_or<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31883198
// SAFETY: the caller must uphold the safety contract for `atomic_or`
@@ -3198,7 +3208,8 @@ unsafe fn atomic_or<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
31983208
}
31993209

32003210
#[inline]
3201-
#[cfg(target_has_atomic = "8")]
3211+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3212+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
32023213
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
32033214
unsafe fn atomic_xor<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32043215
// SAFETY: the caller must uphold the safety contract for `atomic_xor`
@@ -3215,7 +3226,8 @@ unsafe fn atomic_xor<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32153226

32163227
/// returns the max value (signed comparison)
32173228
#[inline]
3218-
#[cfg(target_has_atomic = "8")]
3229+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3230+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
32193231
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
32203232
unsafe fn atomic_max<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32213233
// SAFETY: the caller must uphold the safety contract for `atomic_max`
@@ -3232,7 +3244,8 @@ unsafe fn atomic_max<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32323244

32333245
/// returns the min value (signed comparison)
32343246
#[inline]
3235-
#[cfg(target_has_atomic = "8")]
3247+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3248+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
32363249
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
32373250
unsafe fn atomic_min<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32383251
// SAFETY: the caller must uphold the safety contract for `atomic_min`
@@ -3249,7 +3262,8 @@ unsafe fn atomic_min<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32493262

32503263
/// returns the max value (unsigned comparison)
32513264
#[inline]
3252-
#[cfg(target_has_atomic = "8")]
3265+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3266+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
32533267
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
32543268
unsafe fn atomic_umax<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32553269
// SAFETY: the caller must uphold the safety contract for `atomic_umax`
@@ -3266,7 +3280,8 @@ unsafe fn atomic_umax<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32663280

32673281
/// returns the min value (unsigned comparison)
32683282
#[inline]
3269-
#[cfg(target_has_atomic = "8")]
3283+
#[cfg_attr(not(bootstrap), cfg(target_has_atomic))]
3284+
#[cfg_attr(bootstrap, cfg(target_has_atomic = "8"))]
32703285
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
32713286
unsafe fn atomic_umin<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
32723287
// SAFETY: the caller must uphold the safety contract for `atomic_umin`

0 commit comments

Comments
 (0)