Skip to content

Commit 2628b10

Browse files
committed
Use atomic volatile relaxed intrinsic in the implementation of volatile.
1 parent fd1d1b5 commit 2628b10

File tree

2 files changed

+77
-47
lines changed

2 files changed

+77
-47
lines changed

library/core/src/intrinsics/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,26 @@ pub unsafe fn unaligned_volatile_load<T>(src: *const T) -> T;
18411841
#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_store"]
18421842
pub unsafe fn unaligned_volatile_store<T>(dst: *mut T, val: T);
18431843

1844+
/// Performs a volatile load from the `dst` pointer.
1845+
/// This pointer is required to be aligned and supported for
1846+
/// lock-free atomic operations.
1847+
///
1848+
/// It also creates a relaxed atomic ordering at this place.
1849+
#[rustc_intrinsic]
1850+
#[rustc_nounwind]
1851+
#[cfg(not(bootstrap))]
1852+
pub unsafe fn volatile_load_atomic_relaxed<T>(src: *const T) -> T;
1853+
1854+
/// Performs a volatile store to the `dst` pointer.
1855+
/// This pointer is required to be aligned, and the value supported
1856+
/// for lock-free atomic operations.
1857+
///
1858+
/// This also creates a relaxed atomic ordering on at this place.
1859+
#[rustc_intrinsic]
1860+
#[rustc_nounwind]
1861+
#[cfg(not(bootstrap))]
1862+
pub unsafe fn volatile_store_atomic_relaxed<T>(dst: *mut T, val: T);
1863+
18441864
/// Returns the square root of an `f16`
18451865
///
18461866
/// The stabilized version of this intrinsic is

library/core/src/ptr/volatile.rs

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::mem::SizedTypeProperties;
2-
use crate::intrinsics;
3-
use crate::macros::cfg;
4-
use crate::sync::atomic::{AtomicU8, AtomicU16, AtomicU32, Atomicu64};
2+
#[cfg(not(bootstrap))]
3+
use crate::sync::atomic::{AtomicU8, AtomicU16, AtomicU32, AtomicU64};
4+
use crate::{cfg, intrinsics};
55

66
/// Performs a volatile read of the value from `src` without moving it. This
77
/// leaves the memory in `src` unchanged.
@@ -80,29 +80,34 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
8080
is_zst: bool = T::IS_ZST,
8181
) => crate::ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
8282
);
83-
match size_of<T>() {
84-
1 => if cfg!(target_has_atomic_load_store = "8") && align_of::<T>() == align_of::<AtomicU8>() {
85-
intrinsics::atomic_load_relaxed(src)
86-
} else {
87-
intrinsics::volatile_load(dst, val)
88-
}
89-
2 => if cfg!(target_has_atomic_load_store = "16") && align_of::<T>() == align_of::<AtomicU16>() {
90-
intrinsics::atomic_load_relaxed(src)
91-
} else {
92-
intrinsics::volatile_load(dst, val)
93-
}
94-
4 => if cfg!(target_has_atomic_load_store = "32") && align_of::<T>() == align_of::<AtomicU32>() {
95-
intrinsics::atomic_load_relaxed(src)
96-
} else {
97-
intrinsics::volatile_load(dst, val)
98-
}
99-
8 => if cfg!(target_has_atomic_load_store = "64") && align_of::<T>() == align_of::<AtomicU64>() {
100-
intrinsics::atomic_load_relaxed(src)
101-
} else {
102-
intrinsics::volatile_load(dst, val)
103-
}
104-
_ => intrinsics::volatile_load(dst, val)
83+
84+
// TODO: Guard patterns
85+
#[cfg(not(bootstrap))]
86+
match size_of::<T>() {
87+
1 if cfg!(target_has_atomic_load_store = "8")
88+
&& align_of::<T>() == align_of::<AtomicU8>() =>
89+
{
90+
intrinsics::volatile_load_atomic_relaxed(src)
91+
}
92+
2 if cfg!(target_has_atomic_load_store = "16")
93+
&& align_of::<T>() == align_of::<AtomicU16>() =>
94+
{
95+
intrinsics::volatile_load_atomic_relaxed(src)
96+
}
97+
4 if cfg!(target_has_atomic_load_store = "32")
98+
&& align_of::<T>() == align_of::<AtomicU32>() =>
99+
{
100+
intrinsics::volatile_load_atomic_relaxed(src)
101+
}
102+
8 if cfg!(target_has_atomic_load_store = "64")
103+
&& align_of::<T>() == align_of::<AtomicU64>() =>
104+
{
105+
intrinsics::volatile_load_atomic_relaxed(src)
106+
}
107+
_ => intrinsics::volatile_load(src),
105108
}
109+
#[cfg(bootstrap)]
110+
intrinsics::volatile_load(src)
106111
}
107112
}
108113

@@ -182,28 +187,33 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
182187
is_zst: bool = T::IS_ZST,
183188
) => crate::ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
184189
);
185-
match size_of<T>() {
186-
1 => if cfg!(target_has_atomic_load_store = "8") && align_of::<T>() == align_of::<AtomicU8>() {
187-
intrinsics::atomic_store_relaxed(dst, val)
188-
} else {
189-
intrinsics::volatile_store(dst, val)
190-
}
191-
2 => if cfg!(target_has_atomic_load_store = "16") && align_of::<T>() == align_of::<AtomicU16>() {
192-
intrinsics::atomic_store_relaxed(dst, val)
193-
} else {
194-
intrinsics::volatile_store(dst, val)
195-
}
196-
4 => if cfg!(target_has_atomic_load_store = "32") && align_of::<T>() == align_of::<AtomicU32>() {
197-
intrinsics::atomic_store_relaxed(dst, val)
198-
} else {
199-
intrinsics::volatile_store(dst, val)
200-
}
201-
8 => if cfg!(target_has_atomic_load_store = "64") && align_of::<T>() == align_of::<AtomicU64>() {
202-
intrinsics::atomic_store_relaxed(dst, val)
203-
} else {
204-
intrinsics::volatile_store(dst, val)
205-
}
206-
_ => intrinsics::volatile_store(dst, val)
190+
191+
// TODO: Guard patterns
192+
#[cfg(not(bootstrap))]
193+
match size_of::<T>() {
194+
1 if cfg!(target_has_atomic_load_store = "8")
195+
&& align_of::<T>() == align_of::<AtomicU8>() =>
196+
{
197+
intrinsics::volatile_store_atomic_relaxed(dst, src)
198+
}
199+
2 if cfg!(target_has_atomic_load_store = "16")
200+
&& align_of::<T>() == align_of::<AtomicU16>() =>
201+
{
202+
intrinsics::volatile_store_atomic_relaxed(dst, src)
203+
}
204+
4 if cfg!(target_has_atomic_load_store = "32")
205+
&& align_of::<T>() == align_of::<AtomicU32>() =>
206+
{
207+
intrinsics::volatile_store_atomic_relaxed(dst, src)
208+
}
209+
8 if cfg!(target_has_atomic_load_store = "64")
210+
&& align_of::<T>() == align_of::<AtomicU64>() =>
211+
{
212+
intrinsics::volatile_store_atomic_relaxed(dst, src)
213+
}
214+
_ => intrinsics::volatile_store(dst, src),
207215
}
216+
#[cfg(bootstrap)]
217+
intrinsics::volatile_store(dst, src)
208218
}
209219
}

0 commit comments

Comments
 (0)