-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Replacement of #114390: Add new intrinsic is_var_statically_known
and optimize pow for powers of two
#119911
Merged
+632
−152
Merged
Replacement of #114390: Add new intrinsic is_var_statically_known
and optimize pow for powers of two
#119911
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -2511,6 +2511,66 @@ extern "rust-intrinsic" { | |
where | ||
G: FnOnce<ARG, Output = RET>, | ||
F: FnOnce<ARG, Output = RET>; | ||
|
||
/// Returns whether the argument's value is statically known at | ||
/// compile-time. | ||
/// | ||
/// This is useful when there is a way of writing the code that will | ||
/// be *faster* when some variables have known values, but *slower* | ||
/// in the general case: an `if is_val_statically_known(var)` can be used | ||
/// to select between these two variants. The `if` will be optimized away | ||
/// and only the desired branch remains. | ||
/// | ||
/// Formally speaking, this function non-deterministically returns `true` | ||
/// or `false`, and the caller has to ensure sound behavior for both cases. | ||
/// In other words, the following code has *Undefined Behavior*: | ||
/// | ||
/// ``` | ||
/// #![feature(is_val_statically_known)] | ||
/// #![feature(core_intrinsics)] | ||
/// # #![allow(internal_features)] | ||
/// use std::hint::unreachable_unchecked; | ||
/// use std::intrinsics::is_val_statically_known; | ||
/// | ||
/// unsafe { | ||
/// if !is_val_statically_known(0) { unreachable_unchecked(); } | ||
/// } | ||
/// ``` | ||
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. Tests that have UB should be marked as I'll file a PR. 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. Filed #120366 |
||
/// | ||
/// This also means that the following code's behavior is unspecified; it | ||
/// may panic, or it may not: | ||
/// | ||
/// ```no_run | ||
/// #![feature(is_val_statically_known)] | ||
/// #![feature(core_intrinsics)] | ||
/// # #![allow(internal_features)] | ||
/// use std::intrinsics::is_val_statically_known; | ||
/// | ||
/// unsafe { | ||
/// assert_eq!(is_val_statically_known(0), is_val_statically_known(0)); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Unsafe code may not rely on `is_val_statically_known` returning any | ||
/// particular value, ever. However, the compiler will generally make it | ||
/// return `true` only if the value of the argument is actually known. | ||
/// | ||
/// When calling this in a `const fn`, both paths must be semantically | ||
/// equivalent, that is, the result of the `true` branch and the `false` | ||
/// branch must return the same value and have the same side-effects *no | ||
/// matter what*. | ||
#[rustc_const_unstable(feature = "is_val_statically_known", issue = "none")] | ||
#[rustc_nounwind] | ||
#[cfg(not(bootstrap))] | ||
pub fn is_val_statically_known<T: Copy>(arg: T) -> bool; | ||
} | ||
|
||
// FIXME: Seems using `unstable` here completely ignores `rustc_allow_const_fn_unstable` | ||
// and thus compiling stage0 core doesn't work. | ||
#[rustc_const_stable(feature = "is_val_statically_known", since = "0.0.0")] | ||
#[cfg(bootstrap)] | ||
pub const unsafe fn is_val_statically_known<T: Copy>(_arg: T) -> bool { | ||
false | ||
} | ||
|
||
// Some functions are defined here because they accidentally got made | ||
|
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this file btw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GuillaumeGomez I don't know what you are asking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe he wanted to know why you changed this file, but it seems like you only added stuff and reshuffled things, so that looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
__builtin_constant_p
. I decided to put__builtin_constant_p
with the other non-arithmetic__builtin*
s. Since__builtin_expect_with_probability
was at the very end, I figured it was new and not sorted yet. I decided to put it between__builtin_expect
and__builtin_constant_p
. If there was a reason it was at the end I could easily move it back.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems good to me as you did.