Skip to content

Failed optimization: missing range/enumerate bijection suppresses length fact propagation #114242

Description

@SUPERCILEX
pub fn score_round_opt(candidates: &[bool]) {
    u32::try_from(candidates.len()).unwrap();

    for (i, _) in candidates.iter().enumerate() {
        u32::try_from(i).unwrap();
        std::hint::black_box(42);
    }
}

pub fn score_round_fail(candidates: &[bool]) {
    u32::try_from(candidates.len()).unwrap();

    for i in 0..candidates.len() {
        u32::try_from(i).unwrap();
        std::hint::black_box(42);
    }
}

https://rust.godbolt.org/z/8a6jd8acG

The unwrap in the loop fails to get optimized out when using 0..candidates.len() even though this is equivalent to enumerate.

Metadata

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 suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions