Skip to content

Bounds check optimizations are not applied if the accessed slice len is smaller than the upfront checked slice len #69101

Closed
@Matthias247

Description

@Matthias247

When using a loop and checking exact bounds upfront, vectorized instructions are used:

pub fn xorl(a: &[u8], b: &[u8], c: &mut [u8]) {
    let (a_bound, b_bound, c_bound) = (&a[..1024], &b[..1024], &mut c[..1024]);
    for i in 0..1024 {
        c[i] = a[i] ^ b[i];
    }
}

Godbolt

However when the upfront checks actually tests for a bigger slice size, the same optimizations are not applied:

pub fn xorl(a: &[u8], b: &[u8], c: &mut [u8]) {
    let (a_bound, b_bound, c_bound) = (&a[..2048], &b[..2048], &mut c[..2048]);
    for i in 0..1024 {
        c[i] = a[i] ^ b[i];
    }
}

Godbolt

Since slices of size 2048 are always also at least of size 1024, I would expect the generated code to be equivalent.

Meta

rustc --version --verbose:

Happens on nightly as well as 1.40 - as seen in Godbolt

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-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