Skip to content

Codegen forgets array size when used with iterators #63552

Closed
@orlp

Description

@orlp

Consider these three functions:

pub fn in_range1(x: [usize; 3], m: usize) -> bool {
    (x[0] < m) && (x[1] < m) && (x[2] < m)
}

pub fn in_range2(x: [usize; 3], m: usize) -> bool {
    for k in &x {
        if *k >= m {
            return false;
        }
    }

    true
}

pub fn in_range3(x: [usize; 3], m: usize) -> bool {
    x.iter().cloned().all(|k| k < m)
}

I'd expect all of them to generate roughly the same code since our array x has a fixed compile-time size. With rustc 1.27.1 it does, but after that it seems there was a regression that causes the compiler to forget the size of the array and generate generic code rather than code specific for the array size when an iterator is used.

Particularly damning is that the compiler generates an unrolled loop which is dead code as our array isn't large enough to ever qualify for unrolling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-iteratorsArea: IteratorsC-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-llvmWorking group: LLVM backend code generationregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions