Skip to content

iter::zip changes vectorization behaviour even if value is unused #143016

@BloodStainedCrow

Description

@BloodStainedCrow

I tried this code:

godbolt

#[inline(never)]
pub fn zipped(
    slice_of_refs: &mut [&mut bool],
    unused_slice: &mut [u8],
) {
        for (unused, write_destination) in unused_slice
        .iter_mut()
        .zip(slice_of_refs) {
            **write_destination = true;
        }
}


#[inline(never)]
pub fn not_zipped(
    slice_of_refs: &mut [&mut bool],
    unused_slice: &mut [u8],
) {
    for write_destination in slice_of_refs {
        **write_destination = true;
    }
}

I expected to see this happen: They compile to (approximately) the same assembly (Either both vectorized or both scalar).

Instead, this happened: Only the code which zips (but does not use) another slice is vectorized. (Caveat: so far I could only observe this with -target-cpu=alderlake set, and I am unsure whether vectorized or scalar operations would be considered the 'correct' result here)

Meta

rustc --version --verbose:

Full compiler version
rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1
Internal compiler ID: r1870

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-autovectorizationArea: Autovectorization, which can impact perf or code sizeA-iteratorsArea: IteratorsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-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