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 bounds check elimination #127553

Open
ericlagergren opened this issue Jul 10, 2024 · 6 comments
Open

Missed bounds check elimination #127553

ericlagergren opened this issue Jul 10, 2024 · 6 comments
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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

@ericlagergren
Copy link

On Rust 1.79.0.

pub const fn missing_bce(v: &[u32]) -> u32 {
    if v.is_empty() {
        return 0;
    }
    let mut sum = 0;
    let mut i = v.len() - 1;
    while i > 0 {
        sum += v[i];
        i -= 1;
    }
    sum
}

https://godbolt.org/z/xxjKd5anW

The compiler should be able to elide the bounds check for v[i] since i is always in [0, v.len()-1].

@ericlagergren ericlagergren added the C-bug Category: This is a bug. label Jul 10, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 10, 2024
@tgross35
Copy link
Contributor

@rustbot label +A-codegen +C-optimization +T-compiler +I-slow -needs-triage

@rustbot rustbot added A-codegen Area: Code generation C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 11, 2024
@tgross35
Copy link
Contributor

tgross35 commented Jul 11, 2024

From a quick glance at the IR it looks like the bounds checks exist for both in MIR but get removed before the IR, so maybe mir opts gets one but not the other? Cc @rust-lang/wg-mir-opt if somebody wants to take a closer look.

@DianQK
Copy link
Member

DianQK commented Jul 11, 2024

The bounds checks of has_bce are eliminated in LLVM InstCombine: https://godbolt.org/z/38EaT1n73. IIUC, mir-opt currently does not perform similar optimizations (e.g. CorrelatedValuePropagationPass). Or rather, some range analysis optimizations.

@rustbot label +A-LLVM

@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jul 11, 2024
@nikic nikic changed the title Missed BCE Missed bounds check elimination Jul 11, 2024
@enqueueDequeue
Copy link

May I point that the code shown is summing upto index 0 but not index 0, which I think is the reason why there's a bound check.

@ericlagergren
Copy link
Author

May I point that the code shown is summing upto index 0 but not index 0, which I think is the reason why there's a bound check.

Why would that cause a bounds check?

It should be trivial for the compiler to prove that 0 < i < s.len() and elide the bounds checks.

@enqueueDequeue
Copy link

I'm saying that a break after the index update is causing a bound check

// This code is equivalent to the first snippet
// And also causes a bound check instruction to be present in the assembly
loop {
    sum += v[i];
    i -= 1;
    if i == 0 {
        break;
    }
}

where as, a break before the index update is not causing a bound check

loop {
    sum += v[i];
    if i == 0 {
        break;
    }
    i -= 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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

No branches or pull requests

5 participants