Skip to content

flat_map fails to autovectorize internal iterators #104914

Open
@james7132

Description

#[no_mangle]
fn iter(query: &mut [&mut [f32]]) {
    for segment in query {
        for value in segment.iter_mut() {
            *value += 1.0;
        }
    }
}

#[no_mangle]
fn flat_map(query: &mut [&mut [f32]]) {
    let iter = query.iter_mut().flat_map(|segment| segment.iter_mut());
    for value in iter {
        *value += 1.0;
    }
}

I expected to see this happen: Both versions of the function to autovectorize.

Instead, this happened: only the normal for-loop version autovectorizes. See https://godbolt.org/z/caTqoq3x3.

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0

Also happens on nightly and for other "flattened" iterators. For example, Bevy's QueryIter and Query::for_each show this exact API split due to this performance difference.

There seems to be #87411 that was targeting something similar.

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.I-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