Skip to content

Unnecessary loop unrolling to handle tail when tail length has a smaller known size #130795

Open
@okaneco

Description

@okaneco

In the following code, the first while loop should process 8 bytes at a time and exit early if an invalid byte is found.

The remaining bytes should be known to be bytes.len() % 8, but the auto-vectorization unrolls to test again for 32 bytes and 8 bytes at a time
https://rust.godbolt.org/z/z8hnb9PGY

pub const fn is_ascii(bytes: &[u8]) -> bool {
    const N1: usize = 8;
    let mut i = 0;
    while i + N1 <= bytes.len() {
        let chunk_end = i + N1;
        let mut count = 0;
        while i < chunk_end {
            count += (bytes[i] <= 127) as u8;
            i += 1;
        }

        if count != N1 as u8 {
            return false;
        }
    }

    // Process the remaining `bytes.len() % N` bytes.
    let mut is_ascii = true;
    while i < bytes.len() {
        is_ascii &= bytes[i] <= 127;
        i += 1;
    }
    is_ascii
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions