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

Missed optimization with slice::get_unchecked followed by <slice as Index>::index at a lower index #116878

Closed
zachs18 opened this issue Oct 18, 2023 · 1 comment · Fixed by #116915
Labels
A-codegen Area: Code generation C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zachs18
Copy link
Contributor

zachs18 commented Oct 18, 2023

I tried this code:

pub unsafe fn cannot_elide_bounds_check(s: &[u8]) -> u8 {
    let a = *s.get_unchecked(1);
    a + s[0]
}
pub unsafe fn can_elide_bounds_check(s: &[u8]) -> u8 {
    let a = *s.get(1).unwrap_unchecked();
    a + s[0]
}

Compile with -Copt-level=3 on godbolt.org (godbolt link)

I expected to see this happen: Both functions should compile to approximately the same thing, with no bounds check on the s[0] since the optimizer should be able to see that the slice is at least of length 2 due to the previous unchecked indexing.

example::can_elide_bounds_check:
        movzx   eax, byte ptr [rdi]
        add     al, byte ptr [rdi + 1]
        ret

Instead, this happened: cannot_elide_bounds_check contains a bounds check (only can_elide_bounds_check does not contain a bounds check).

example::cannot_elide_bounds_check:
        test    rsi, rsi
        je      .LBB0_2
        movzx   eax, byte ptr [rdi]
        add     al, byte ptr [rdi + 1]
        ret
.LBB0_2:
        push    rax
        lea     rdx, [rip + .L__unnamed_1]
        xor     edi, edi
        xor     esi, esi
        call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
        ud2

Meta

rustc stable (1.73.0), nightly (2023-10-17), and 1.58.0 (when Option::unwrap_unchecked was stabilized) all have approximately the same codegen.
rustc --version --verbose:

rustc 1.75.0-nightly (09df6108c 2023-10-17)
binary: rustc
commit-hash: 09df6108c84fdec400043d99d9ee232336fd5a9f
commit-date: 2023-10-17
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

(no backtrace)

@rustbot label +C-optimization -C-bug

@zachs18 zachs18 added the C-bug Category: This is a bug. label Oct 18, 2023
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed C-bug Category: This is a bug. labels Oct 18, 2023
@workingjubilee workingjubilee added the A-codegen Area: Code generation label Oct 18, 2023
@the8472
Copy link
Member

the8472 commented Oct 18, 2023

unwrap_unchecked contains an assume intrinsic, get_unchecked doesn't. Might be worth adding it to the latter and see how that impacts perf, it's not guaranteed that it would actually improve things since we'd be emitting more IR and llvm would have to work harder.

Commonly it's the case that if one uses get_unchecked one does so for all accesses on the slice, so this seems like an unusual pattern.

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 18, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 19, 2023
Use `.get().unwrap()` in `[T]::get_unchecked`

Fixes rust-lang#116878
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 19, 2023
Use `.get().unwrap()` in `[T]::get_unchecked`

Fixes rust-lang#116878
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 20, 2023
Use `.get().unwrap()` in `[T]::get_unchecked`

Fixes rust-lang#116878
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 4, 2023
Add an assume that the index is inbounds to slice::get_unchecked

Fixes rust-lang#116878
@bors bors closed this as completed in 85a4bd8 Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants