rust-lang / rust-lang/rust

std::simd generates a lot of cpu instructions on opt-level=z

Open
#117,926 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM C-optimization
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(portable_simd)]

use std::simd::prelude::*;

pub fn bigger_diffs(a: [u64; 64], other: [u64; 64]) -> u128 {
    let first = u64x64::from_array(a);
    let second = u64x64::from_array(other);
    let (a, b) = (first.saturating_sub(second), second.saturating_sub(first));
    (a ^ b).reduce_sum() as u128
}

I expected to see this happen:
Under opt-level=z on default x86_64 target I expect the compiler to generate a loop with some jumps

Instead, this happened:
It generated more than 1200 instructions, seemingly unrolling some kind of a loop

Meta

rustc --version --verbose:

rustc 1.76.0-nightly (ba7c7a301 2023-11-13)
binary: rustc
commit-hash: ba7c7a301984967c8c13adb580ef9b86ba706a83
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.4
Backtrace

RUST_BACKTRACE=1 cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
$ ./target/debug/simd_unroll
0

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

Start by compiling the provided portable_simd example for the default x86_64 target with opt-level=z and inspect the generated instructions. Compare the result with the expected loop containing jumps; done means the compiler no longer emits the reported 1200-plus instructions for this case.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.