Skip to content

Commit b18aacd

Browse files
committed
Add a separate function for a non-temporal read prefetch
1 parent 206dca8 commit b18aacd

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

library/core/src/hint.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,6 @@ pub fn select_unpredictable<T>(condition: bool, true_val: T, false_val: T) -> T
834834
#[non_exhaustive]
835835
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
836836
pub enum Locality {
837-
/// Data is unlikely to be reused soon.
838-
///
839-
/// Typically bypasses the caches so they are not polluted.
840-
NonTemporal = 0,
841837
/// Data is expected to be reused eventually.
842838
///
843839
/// Typically prefetches into L3 cache (if the CPU supports it).
@@ -865,6 +861,7 @@ pub enum Locality {
865861
/// # Examples
866862
///
867863
/// ```
864+
/// #![feature(hint_prefetch)]
868865
/// use std::hint::{Locality, prefetch_read_data};
869866
/// use std::mem::size_of_val;
870867
///
@@ -880,15 +877,28 @@ pub enum Locality {
880877
#[unstable(feature = "hint_prefetch", issue = "146941")]
881878
pub const fn prefetch_read_data<T>(ptr: *const T, locality: Locality) {
882879
match locality {
883-
Locality::NonTemporal => {
884-
intrinsics::prefetch_read_data::<T, { Locality::NonTemporal as i32 }>(ptr)
885-
}
886880
Locality::L3 => intrinsics::prefetch_read_data::<T, { Locality::L3 as i32 }>(ptr),
887881
Locality::L2 => intrinsics::prefetch_read_data::<T, { Locality::L2 as i32 }>(ptr),
888882
Locality::L1 => intrinsics::prefetch_read_data::<T, { Locality::L1 as i32 }>(ptr),
889883
}
890884
}
891885

886+
/// Prefetch the cache line containing `ptr` for a single future read, but attempt to avoid
887+
/// polluting the cache.
888+
///
889+
/// A strategically placed prefetch can reduce cache miss latency if the data is accessed
890+
/// soon after, but may also increase bandwidth usage or evict other cache lines.
891+
///
892+
/// A prefetch is a *hint*, and may be ignored on certain targets or by the hardware.
893+
///
894+
/// Passing a dangling or invalid pointer is permitted: the memory will not
895+
/// actually be dereferenced, and no faults are raised.
896+
#[inline(always)]
897+
#[unstable(feature = "hint_prefetch", issue = "146941")]
898+
pub const fn prefetch_read_data_non_temporal<T>(ptr: *const T) {
899+
intrinsics::prefetch_read_data::<T, 0>(ptr)
900+
}
901+
892902
/// Prefetch the cache line containing `ptr` for a future write.
893903
///
894904
/// A strategically placed prefetch can reduce cache miss latency if the data is accessed
@@ -898,12 +908,10 @@ pub const fn prefetch_read_data<T>(ptr: *const T, locality: Locality) {
898908
///
899909
/// Passing a dangling or invalid pointer is permitted: the memory will not
900910
/// actually be dereferenced, and no faults are raised.
911+
#[inline(always)]
901912
#[unstable(feature = "hint_prefetch", issue = "146941")]
902913
pub const fn prefetch_write_data<T>(ptr: *mut T, locality: Locality) {
903914
match locality {
904-
Locality::NonTemporal => {
905-
intrinsics::prefetch_write_data::<T, { Locality::NonTemporal as i32 }>(ptr)
906-
}
907915
Locality::L3 => intrinsics::prefetch_write_data::<T, { Locality::L3 as i32 }>(ptr),
908916
Locality::L2 => intrinsics::prefetch_write_data::<T, { Locality::L2 as i32 }>(ptr),
909917
Locality::L1 => intrinsics::prefetch_write_data::<T, { Locality::L1 as i32 }>(ptr),
@@ -919,12 +927,10 @@ pub const fn prefetch_write_data<T>(ptr: *mut T, locality: Locality) {
919927
///
920928
/// Passing a dangling or invalid pointer is permitted: the memory will not
921929
/// actually be dereferenced, and no faults are raised.
930+
#[inline(always)]
922931
#[unstable(feature = "hint_prefetch", issue = "146941")]
923932
pub const fn prefetch_read_instruction<T>(ptr: *const T, locality: Locality) {
924933
match locality {
925-
Locality::NonTemporal => {
926-
intrinsics::prefetch_read_instruction::<T, { Locality::NonTemporal as i32 }>(ptr)
927-
}
928934
Locality::L3 => intrinsics::prefetch_read_instruction::<T, { Locality::L3 as i32 }>(ptr),
929935
Locality::L2 => intrinsics::prefetch_read_instruction::<T, { Locality::L2 as i32 }>(ptr),
930936
Locality::L1 => intrinsics::prefetch_read_instruction::<T, { Locality::L1 as i32 }>(ptr),

0 commit comments

Comments
 (0)