rust-lang / rust-lang/rust

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

Open
#143,016 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-autovectorization A-iterators C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the zipped and not_zipped functions from the issue in Godbolt with rustc 1.87.0, -C opt-level=3, and -C target-cpu=alderlake, then compare their generated assembly. No repository file or test is named; done means identifying why the unused zipped value changes vectorization and adding a focused regression test or fix in the relevant compiler area.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.