rust-lang / rust-lang/rust

Failed to vectorize the for_each iteration with min function

Open
#123,837 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM C-optimization I-slow
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#[inline]
fn min_update(dst: &mut u32, lhs: u32){
    if *dst > lhs{
        *dst = lhs;
    }
}

#[inline]
fn min(mut dst: u32, lhs: u32) -> u32 {
    min_update(&mut dst, lhs);
    dst
}

pub fn vectorize_fail(lhs: &[u32], dst: &mut u32)  {
    lhs.iter().for_each(|&x|{
        min_update(dst, x);
    });

}

pub fn vectorize(lhs: &[u32], dst: &mut u32){
    lhs.iter().for_each(|&x|{
        *dst = min(*dst, x);
    });
}

I expected to see this happen: The vectorize_fail function will get vectorized

Instead, this happened: The auto-vectorization is not performed, see godbolt for details

Meta

rustc --version --verbose:

1.77.0
Backtrace

<backtrace>

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 difference between vectorize_fail and vectorize using the Rust snippet and the linked Godbolt example. Compare the generated output for both functions and investigate why the min_update form is not auto-vectorized. Done means the failing form is vectorized consistently with the equivalent min form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
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.