-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add select_unpredictable
to force LLVM to use CMOV
#128250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1010,6 +1010,34 @@ pub const fn unlikely(b: bool) -> bool { | |
b | ||
} | ||
|
||
/// Returns either `true_val` or `false_val` depending on condition `b` with a | ||
/// hint to the compiler that this condition is unlikely to be correctly | ||
/// predicted by a CPU's branch predictor (e.g. a binary search). | ||
/// | ||
/// This is otherwise functionally equivalent to `if b { true_val } else { false_val }`. | ||
/// | ||
/// Note that, unlike most intrinsics, this is safe to call; | ||
/// it does not require an `unsafe` block. | ||
/// Therefore, implementations must not require the user to uphold | ||
/// any safety invariants. | ||
/// | ||
/// This intrinsic does not have a stable counterpart. | ||
#[cfg(not(bootstrap))] | ||
#[unstable(feature = "core_intrinsics", issue = "none")] | ||
#[rustc_intrinsic] | ||
#[rustc_nounwind] | ||
#[miri::intrinsic_fallback_is_spec] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ping @rust-lang/miri when using this attribute, so that we can have a look at the spec. :) |
||
#[inline] | ||
pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T { | ||
if b { true_val } else { false_val } | ||
} | ||
|
||
#[cfg(bootstrap)] | ||
#[inline] | ||
pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T { | ||
if b { true_val } else { false_val } | ||
} | ||
|
||
extern "rust-intrinsic" { | ||
/// Executes a breakpoint trap, for inspection by a debugger. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||||
//@ compile-flags: -O | ||||||||||
|
||||||||||
#![feature(core_intrinsics)] | ||||||||||
#![crate_type = "lib"] | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
pub fn test_int(p: bool, a: u64, b: u64) -> u64 { | ||||||||||
// CHECK-LABEL: define{{.*}} @test_int | ||||||||||
// CHECK: select i1 %p, i64 %a, i64 %b, !unpredictable | ||||||||||
core::intrinsics::select_unpredictable(p, a, b) | ||||||||||
} | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
pub fn test_pair(p: bool, a: (u64, u64), b: (u64, u64)) -> (u64, u64) { | ||||||||||
// CHECK-LABEL: define{{.*}} @test_pair | ||||||||||
// CHECK: select i1 %p, {{.*}}, !unpredictable | ||||||||||
core::intrinsics::select_unpredictable(p, a, b) | ||||||||||
} | ||||||||||
|
||||||||||
struct Large { | ||||||||||
e: [u64; 100], | ||||||||||
} | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
pub fn test_struct(p: bool, a: Large, b: Large) -> Large { | ||||||||||
// CHECK-LABEL: define{{.*}} @test_struct | ||||||||||
// CHECK: select i1 %p, {{.*}}, !unpredictable | ||||||||||
core::intrinsics::select_unpredictable(p, a, b) | ||||||||||
} | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
pub fn test_zst(p: bool, a: (), b: ()) -> () { | ||||||||||
// CHECK-LABEL: define{{.*}} @test_zst | ||||||||||
core::intrinsics::select_unpredictable(p, a, b) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe?
Suggested change
Otherwise, I don't know what we are supposed to be testing. |
||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.