Skip to content
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

Tracking Issue for bool::select_unpredictable #133962

Open
1 of 4 tasks
joboet opened this issue Dec 6, 2024 · 5 comments
Open
1 of 4 tasks

Tracking Issue for bool::select_unpredictable #133962

joboet opened this issue Dec 6, 2024 · 5 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@joboet
Copy link
Member

joboet commented Dec 6, 2024

Feature gate: #![feature(select_unpredictable)]

This is a tracking issue for bool::select_unpredictable, which selects between two values and hints to the optimizer that it should try to generate branchless code.

Public API

impl bool {
    /// Returns either `true_val` or `false_val` depending on the value of
    /// `condition`, with a hint to the compiler that `condition` is unlikely
    /// to be correctly predicted by a CPU’s branch predictor.
    pub fn select_unpredictable<T>(self, true_val: T, false_val: T) -> T;
}

Steps / History

Unresolved Questions

  • This should be const, right?

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@joboet joboet added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 6, 2024
@tgross35
Copy link
Contributor

tgross35 commented Dec 6, 2024

Reading through the ACP it seems like the justification for having this on bool, rather than hint or as an attribute, is that it it acts as a way to emulate ternaries in Rust for convenience/codestyle. Tying this to a low level optimization feature feels quite weird, as well as potentially confusing to users who see this in the API docs without a good equivalent.

If there is objection to hint::unpredictable(bool) then I think we should consider also adding @scottmcm's suggestion of a regular select method.

(sorry for the nonsensical email, accidentally submitted before I was done writing)

@leb-kuchen
Copy link

I would prefer a builtin branch_hint function as the first statement in a branch like in Zig.
BrachHInt
branch_hint()
This would a lot more flexible and not only work with a ternary statement.

@hanna-kruppe
Copy link
Contributor

For likely/unlikely, such intrinsic(s) are under discussion at rust-lang/libs-team#510. In contrast, the motivation for select_unpredictable is to reliably get branchless machine code. Expressing that by control flow at the source level can sabotage the intended effect, as discussed in the APC. So an equivalent of Zig's branch_hint(.unpredictable) wouldn't be a good replacement for select_unpredictable. If it's still useful for other reasons, then that should be an additional proposal laying out those reasons.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 4, 2025
…oss35

core: implement `bool::select_unpredictable`

Tracking issue: rust-lang#133962
ACP: rust-lang/libs-team#468
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 4, 2025
Rollup merge of rust-lang#133964 - joboet:select_unpredictable, r=tgross35

core: implement `bool::select_unpredictable`

Tracking issue: rust-lang#133962
ACP: rust-lang/libs-team#468
@Amanieu
Copy link
Member

Amanieu commented Jan 7, 2025

I'm personally inclined to move this to std::hint to make it clear that it is only an optimization hint and not something that is guaranteed (e.g. for cryptographic code). It would slightly depart from the precedent that all existing functions in std::hint are no-ops, but I don't think this is a significant concern.

@hanna-kruppe
Copy link
Contributor

This is a good point. It would also be inconsistent with a "non-unpredictable" select method (as discussed in the ACP) which has no reason at all to live in std::hint. But perhaps the important thing is just that both exist and not where they live and how they're invoked (cond.select(t, f) vs hint::select_unpredictable(cond, t, f)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants