Skip to content

Missed bounds check elimination #127553

Open

Description

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].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions