-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Instruct LLVM that binary_search returns a valid index #81354
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
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
This seems like the wrong way of fixing the issue. |
I assume that the get unchecked doesn't emit a bounds check, but for some reason isn't propagating the information to outside the function, whereas the assume does - note that the test case checks for the lack of problems indexing with the returned usize. I do agree that it may be worth considering the assume being inlined into get unchecked instead. |
I didn't thought of that, I based this on the implementation of |
Well, in theory, LLVM could see that the access through get unchecked guarantees that the index is valid, but I imagine it would be quite difficult to demonstrate that. Cc @nagisa @cuviper @nikic - IIRC, in the past assume in common methods like get_unchecked has actually been rather unhelpful, but I don't recall if we've specifically tried it with get_unchecked. |
That's correct – in the past any attempts to add |
Aren't bound checks basically
Is this about adding |
Could the search be implemented by narrowing a slice and then doing a |
I don't think that LLVM really cares if it's derived from the original reference. The bound check tests for the index being less than the length, which is just another number for LLVM. |
This works as an alternative to assume. if base < ary.len() {
if cmp == Equal { Ok(base) } else { Err(base + (cmp == Less) as usize) }
} else {
unsafe { std::hint::unreachable_unchecked() }
} https://rust.godbolt.org/z/9o9oq8 Edit: nevermind, this also boils down to |
This seems related to what I see when using the newly introduced I have filled https://bugs.llvm.org/show_bug.cgi?id=48975 with the IR reduction and an analysis of why manually integrating the function (e.g. via a macro) helps sometimes. If solved, it may solve also this one as well as others like: rust/library/core/src/iter/adapters/fuse.rs Lines 43 to 53 in 0e63af5
|
63e95e7
to
2b3d8d3
Compare
Oops, I pushed a commit to the wrong branch |
☔ The latest upstream changes (presumably #74024) made this pull request unmergeable. Please resolve the merge conflicts. |
2b3d8d3
to
c9d04c2
Compare
Triage: @rustbot label: +S-waiting-on-review -S-waiting-on-author |
Going to re-assign to @nagisa who can better judge whether we should do this, but past experience hints to me that this is likely to hurt more than help in the common case. |
#80836 comes to mind as a counterexample to usefulness of this kind of optimisation. Basically, it really matters In this particular case what the implementation of the @bors r+ with that in mind, but happy to revert if anybody pinpoints this to cause any kind of regression in their code. |
📌 Commit c9d04c2 has been approved by |
@bors rollup=never |
☀️ Test successful - checks-actions |
This allows removing bound checks when the return value of
binary_search
is used to index into the slice it was call on. I also added a codegen test for this, not sure if it's the right thing to do (I didn't find anything on the dev guide), but it felt so.