diff options
| author | Folkert de Vries <folkert@folkertdev.nl> | 2025-08-19 21:12:53 +0200 |
|---|---|---|
| committer | Folkert de Vries <folkert@folkertdev.nl> | 2025-08-19 21:17:49 +0200 |
| commit | 51df7aabbe3140ccca1f63617d7cfdb883509239 (patch) | |
| tree | 7fe8037dbe292e1776ffe01e0a55996765225558 | |
| parent | 16ad385579cebb6f7d53367c552661b6b51a4a02 (diff) | |
| download | rust-51df7aabbe3140ccca1f63617d7cfdb883509239.tar.gz rust-51df7aabbe3140ccca1f63617d7cfdb883509239.zip | |
add a fallback implementation for the `prefetch_*` intrinsics
The fallback is to just ignore the arguments. That is a valid implementation because this intrinsic is just a hint. I also added `miri::intrinsic_fallback_is_spec` annotation, so that miri now supports these operations. A prefetch intrinsic call is valid on any pointer.
| -rw-r--r-- | library/core/src/intrinsics/mod.rs | 39 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/prefetch.rs | 23 |
2 files changed, 54 insertions, 8 deletions
diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index dd838d494bc..346c7825b74 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -261,7 +261,7 @@ pub unsafe fn atomic_fence<const ORD: AtomicOrdering>(); pub unsafe fn atomic_singlethreadfence<const ORD: AtomicOrdering>(); /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction -/// if supported; otherwise, it is a no-op. +/// for the given address if supported; otherwise, it is a no-op. /// Prefetches have no effect on the behavior of the program but can change its performance /// characteristics. /// @@ -271,9 +271,15 @@ pub unsafe fn atomic_singlethreadfence<const ORD: AtomicOrdering>(); /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic] #[rustc_nounwind] -pub unsafe fn prefetch_read_data<T>(data: *const T, locality: i32); +#[miri::intrinsic_fallback_is_spec] +pub unsafe fn prefetch_read_data<T>(data: *const T, locality: i32) { + // This operation is a no-op, unless it is overridden by the backend. + let _ = data; + let _ = locality; +} + /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction -/// if supported; otherwise, it is a no-op. +/// for the given address if supported; otherwise, it is a no-op. /// Prefetches have no effect on the behavior of the program but can change its performance /// characteristics. /// @@ -283,9 +289,15 @@ pub unsafe fn prefetch_read_data<T>(data: *const T, locality: i32); /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic] #[rustc_nounwind] -pub unsafe fn prefetch_write_data<T>(data: *const T, locality: i32); +#[miri::intrinsic_fallback_is_spec] +pub unsafe fn prefetch_write_data<T>(data: *const T, locality: i32) { + // This operation is a no-op, unless it is overridden by the backend. + let _ = data; + let _ = locality; +} + /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction -/// if supported; otherwise, it is a no-op. +/// for the given address if supported; otherwise, it is a no-op. /// Prefetches have no effect on the behavior of the program but can change its performance /// characteristics. /// @@ -295,9 +307,15 @@ pub unsafe fn prefetch_write_data<T>(data: *const T, locality: i32); /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic] #[rustc_nounwind] -pub unsafe fn prefetch_read_instruction<T>(data: *const T, locality: i32); +#[miri::intrinsic_fallback_is_spec] +pub unsafe fn prefetch_read_instruction<T>(data: *const T, locality: i32) { + // This operation is a no-op, unless it is overridden by the backend. + let _ = data; + let _ = locality; +} + /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction -/// if supported; otherwise, it is a no-op. +/// for the given address if supported; otherwise, it is a no-op. /// Prefetches have no effect on the behavior of the program but can change its performance /// characteristics. /// @@ -307,7 +325,12 @@ pub unsafe fn prefetch_read_instruction<T>(data: *const T, locality: i32); /// This intrinsic does not have a stable counterpart. #[rustc_intrinsic] #[rustc_nounwind] -pub unsafe fn prefetch_write_instruction<T>(data: *const T, locality: i32); +#[miri::intrinsic_fallback_is_spec] +pub unsafe fn prefetch_write_instruction<T>(data: *const T, locality: i32) { + // This operation is a no-op, unless it is overridden by the backend. + let _ = data; + let _ = locality; +} /// Executes a breakpoint trap, for inspection by a debugger. /// diff --git a/src/tools/miri/tests/pass/prefetch.rs b/src/tools/miri/tests/pass/prefetch.rs new file mode 100644 index 00000000000..09352695989 --- /dev/null +++ b/src/tools/miri/tests/pass/prefetch.rs @@ -0,0 +1,23 @@ +#![feature(core_intrinsics)] + +fn main() { + static X: [u8; 8] = [0; 8]; + + unsafe { + ::std::intrinsics::prefetch_read_data(::std::ptr::null::<u8>(), 1); + ::std::intrinsics::prefetch_read_data(::std::ptr::dangling::<u8>(), 2); + ::std::intrinsics::prefetch_read_data(X.as_ptr(), 3); + + ::std::intrinsics::prefetch_write_data(::std::ptr::null::<u8>(), 1); + ::std::intrinsics::prefetch_write_data(::std::ptr::dangling::<u8>(), 2); + ::std::intrinsics::prefetch_write_data(X.as_ptr(), 3); + + ::std::intrinsics::prefetch_read_instruction(::std::ptr::null::<u8>(), 1); + ::std::intrinsics::prefetch_read_instruction(::std::ptr::dangling::<u8>(), 2); + ::std::intrinsics::prefetch_read_instruction(X.as_ptr(), 3); + + ::std::intrinsics::prefetch_write_instruction(::std::ptr::null::<u8>(), 1); + ::std::intrinsics::prefetch_write_instruction(::std::ptr::dangling::<u8>(), 2); + ::std::intrinsics::prefetch_write_instruction(X.as_ptr(), 3); + } +} |
