Skip to content

Regression in 1.99 (LLVM 23): loop vectorizer scalarizes a chunks_exact u16 compare on default x86-64 #166207

Open

Description

@wasade

Code

fn count(a: &[u16], b: &[u16]) -> usize {
    if a.len() != b.len() { return 0; }
    let mut lanes = [0u16; 32];
    let mut ai = a.chunks_exact(32);
    let mut bi = b.chunks_exact(32);
    for (ca, cb) in ai.by_ref().zip(bi.by_ref()) {
        for j in 0..32 {
            lanes[j] += (ca[j] != cb[j]) as u16;
        }
    }
    lanes.iter().map(|&x| x as usize).sum()
}

#[unsafe(no_mangle)]
pub fn row(a: &[u16], others: &[&[u16]]) -> usize {
    others.iter().map(|b| count(a, b)).sum()
}

Build: rustc --edition 2024 --crate-type=lib -C opt-level=3 --emit asm, target x86_64-unknown-linux-gnu, no target-cpu (SSE2).

I expected row to compile as it does on 1.98.0. There the 32-lane body uses whole-vector loads and pcmpeqw, and row is 107 instructions with no movzwl.

Instead, 1.99.0 and 1.100.0 vectorize the loop over chunks 8 wide. Each lane then needs ca[j] from a different chunk, so every u16 vector is built one element at a time. row grows to 2,198 instructions: 512 movzwl and 512 movd feeding punpcklwd.

row: instructions movzwl movd pcmpeqw
1.98.0 (LLVM 22.1.8) 107 0 0 4
1.99.0 (LLVM 23.1.1) 2,198 512 512 36

(Counted with objdump -d on a --emit obj build with -C codegen-units=1. A cdylib built with -C codegen-units=16 gives the same counts: 0 against 512 movzwl.)

The loop vectorizer's remarks

With -C debuginfo=1 -C remark=loop-vectorize, both remarks are on the chunks_exact iteration (split_at_checked):

1.98.0: core/src/slice/mod.rs:2157:12 loop-vectorize (missed): the cost-model indicates that vectorization is not beneficial
1.99.0: core/src/slice/mod.rs:2163:12 loop-vectorize (success): vectorized loop (vectorization width: 8, interleaved count: 1)

On 1.99, -C llvm-args=-vectorize-loops=false restores the 1.98 code. -C llvm-args=-vectorize-slp=false does not.

What it takes

  • A length check is required. if a.len() != b.len() { return 0; }, assert!, assert_eq! and let b = &b[..a.len()]; all trigger it. Without one, both versions are fine.
  • count must be a separate function (inlined). Writing its body directly into the loop in row avoids it.
  • by_ref() is required. With a plain a.chunks_exact(32).zip(b.chunks_exact(32)), both 1.98 and 1.99 produce the per-element code. That looks like an older, separate issue.
  • u16 only. 32, 16 and 8 lanes all reproduce. u32 is unaffected.

Version it worked on

1.98.0 (rustc 1.98.0 (88d9e12ae 2026-08-18), LLVM 22.1.8), and every earlier release checked back to 1.96.0.

Version with regression

rustc --version --verbose:

rustc 1.99.0 (b940084d7 2026-09-28)
binary: rustc
commit-hash: b940084d7eb6a299eb4bfeb8e34901bc051e7ac4
commit-date: 2026-09-28
host: x86_64-unknown-linux-gnu
release: 1.99.0
LLVM version: 23.1.1

It is still present in 1.100.0 (LLVM 23.1.5).

Bisection

cargo-bisect-rustc 0.6.12, run with a script that counts movzwl in row:

  • nightly-2026-08-05 (LLVM 22.1.8) is good, and nightly-2026-08-06 (LLVM 23.1.0) is bad.
  • Merge by merge: the regression is in 1b29cfd, the merge of Update to LLVM 23 #158734 (Update to LLVM 23). The merge before it, 7218ebe, is good.

Cause in LLVM

This is an upstream LLVM change; rustc's frontend isn't involved.

  • The same unoptimized IR from rustc 1.98 gives the good code through opt -O3 from LLVM 22.1.8, and the bad code through LLVM 23.1.1 or 23.1.6. Running -passes=loop-vectorize alone on t>
  • Bisected in LLVM (llvmorg-23-init to the 23.x branch point) to llvm/llvm-project@497a6d672263 ("Recommit "[VPlan] Only use isAddressSCEVForCost in legacy getAddressAccSCEV""). Its paren>
  • Why it flipped. The first lane's loads address the pointer induction variable directly, not through a GEP. Before that commit, those loads got no address SCEV, so X86's cost hook charged >
  • The resulting loop is 2.3-2.5x slower than plain scalar code. It also spills: 32 <8 x i16> accumulators don't fit in 16 XMM registers.
  • Still present on LLVM trunk (e7f64e2019be, 2026-11-19).

LLVM issue: llvm/llvm-project#244246

Impact

This is the real kernel of DartUniFrac (count_mismatches in crates/dartunifrac-core), reduced. Its default 16-bit distance runs about 12× slower when built with 1.99+: about 800 ns per pair against 66 ns, single-threaded, k = 2048. The results are identical; only the speed changes.

Measured on an AMD Ryzen 9 9950X3D, Linux 6.12, x86-64.

@rustbot label +regression-from-stable-to-stable -regression-untriaged +A-LLVM +I-slow +A-autovectorization +T-compiler

This issue was identified with the help of Claude.

cc @jianshu93

Activity

  1. added
    C-bug
    regression-untriaged
    on Nov 19, 2026
  2. added
    needs-triage
    I-prioritize
    A-autovectorization
    A-LLVM
    I-slow
    regression-from-stable-to-stable
    T-compiler
    and removed
    regression-untriaged
    on Nov 19, 2026
  3. saethlin commented on Nov 19, 2026

    @saethlin
    Member

    This is an upstream LLVM change; rustc's frontend isn't involved.

    So... Why did you file this here?

  4. wasade commented on Nov 19, 2026

    @wasade
    Author

    @saethlin, this was found through Rust so I thought it was relevant to the Rust developer community. I'm happy to close the issue if this is not useful.

  5. removed
    I-prioritize
    on Nov 22, 2026
  6. rustbot commented on Nov 22, 2026

    @rustbot
    Collaborator

    @wasade please avoid unnecessary @-mentions to LLMs, thanks

    Assigning P-medium (discussion on [Zulip](#t-compiler/prioritization/alerts > ✔ #166207 Regression in 1.99 (LLVM 23): loop vectorizer sca… @ 💬))

  7. added
    P-medium
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMA-autovectorizationC-bugI-slowP-mediumT-compilerneeds-triageregression-from-stable-to-stable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions