- Notifications
You must be signed in to change notification settings - Fork 13.9k
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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. | ||
/// | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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. | ||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.