Skip to content

Slice bounds check not elided in trivial case when using a local varaible outside of a branch #134466

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

Closed
ChrisDenton opened this issue Dec 18, 2024 · 2 comments · Fixed by #139129
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChrisDenton
Copy link
Member

ChrisDenton commented Dec 18, 2024

I tried this code:

fn last_four(s: &[u8]) -> &[u8] {
    let start = if s.len() <= 4 {
        0
    } else {
        s.len() - 4
    };
    &s[start..]
}

Which produces the following assembly on x86_64:

last_four:
        mov     rax, rdi
        lea     rcx, [rsi - 4]
        xor     edi, edi
        cmp     rsi, 5
        cmovae  rdi, rcx
        mov     rdx, rsi
        sub     rdx, rdi
        jb      .LBB0_2
        add     rax, rdi
        ret
.LBB0_2:
        push    rax
        lea     rdx, [rip + .L__unnamed_1]
        call    qword ptr [rip + core::slice::index::slice_start_index_len_fail::hda7fe0c134770be5@GOTPCREL]

.L__unnamed_2:
        .ascii  "/app/example.rs"

.L__unnamed_1:
        .quad   .L__unnamed_2
        .asciz  "\017\000\000\000\000\000\000\000\b\000\000\000\007\000\000"

The branch with the call to slice_start_index_len_fail should be unreachable.

Indeed, I can remove it by moving the slice operation inside the if statement:

fn last_four(s: &[u8]) -> &[u8] {
    if s.len() <= 4 {
        &s[0..]
    } else {
        &s[s.len() - 4..]
    }
}

Which gives:

last_four:
        cmp     rsi, 5
        lea     rax, [rdi + rsi - 4]
        mov     edx, 4
        cmovb   rdx, rsi
        cmovb   rax, rdi
        ret

Which is what I expected.

@ChrisDenton ChrisDenton added the C-bug Category: This is a bug. label Dec 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 18, 2024
@saethlin saethlin added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. I-slow Issue: Problems and improvements with respect to performance of generated code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 18, 2024
@nikic
Copy link
Contributor

nikic commented Jan 3, 2025

Godbolt: https://rust.godbolt.org/z/1od19xaaa

I believe this is already fixed in LLVM 20 by improvements to usub.sat recognition: https://llvm.godbolt.org/z/sMbdWfoEr

@nikic nikic added llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes and removed C-bug Category: This is a bug. labels Jan 3, 2025
@nikic
Copy link
Contributor

nikic commented Feb 20, 2025

Fixed by LLVM 20 upgrade.

@nikic nikic added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes labels Feb 20, 2025
Zalathar added a commit to Zalathar/rust that referenced this issue Apr 1, 2025
…-check-optimization, r=fee1-dead

Add tests for slice bounds check optimization

Closes rust-lang#134466
Zalathar added a commit to Zalathar/rust that referenced this issue Apr 1, 2025
…-check-optimization, r=fee1-dead

Add tests for slice bounds check optimization

Closes rust-lang#134466
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 1, 2025
…-check-optimization, r=fee1-dead

Add tests for slice bounds check optimization

Closes rust-lang#134466
@bors bors closed this as completed in a04b0e3 Apr 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 2, 2025
Rollup merge of rust-lang#139129 - reez12g:add-tests-for-slice-bounds-check-optimization, r=fee1-dead

Add tests for slice bounds check optimization

Closes rust-lang#134466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code. 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.

4 participants