rust-lang / rust-lang/rust

failed deinterleaving autovectorization

Open
#146,336 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

fn good(input_buffer: &mut [f32], buf: &[f32]) {
    let (l, r) = input_buffer.split_at_mut(input_buffer.len() / 2);

    buf.chunks_exact(2)
        .zip(l.iter_mut().zip(r).map(<[_; _]>::from))
        .for_each(|(buf, sample)| {
            *sample[0] = buf[0];
            *sample[1] = buf[1];
        });
}

fn bad(input_buffer: &mut [f32], buf: &[f32]) {
    let (l, r) = input_buffer.split_at_mut(input_buffer.len() / 2);

    buf.iter()
        .zip(l.iter_mut().zip(r).flat_map(<[_; _]>::from))
        .for_each(|(buf, sample)| *sample = *buf);
}

I expected to see this happen: both versions compile to roughly the same code and are roughly the same speed.

Instead, this happened: good is successfully autovectorized, while bad isn't. I'm fairly sure there's no behavioral difference between the two, so the codegen difference seems odd.

Godbolt: https://godbolt.org/z/fohWsjEdr

Meta

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

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 good and bad functions from the issue with rustc 1.89.0, starting from the linked Godbolt example and comparing their generated code and vectorization. Done means the deinterleaved bad form receives equivalent autovectorization, or the code-generation difference is characterized and covered by an appropriate compiler test.

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.