Skip to content

Function no longer auto-vectorizes in 1.87.0 #142519

Open
@fintelia

Description

@fintelia
Contributor

Code

I tried this code:

use std::convert::TryInto;

pub fn mul3(previous: &[u8], current: &mut [u8]) {
    let mut c_bpp = [0; 4];
    for (chunk, b_bpp) in current.chunks_exact_mut(4).zip(previous.chunks_exact(4))
    {
        let new_chunk = [
            chunk[0].wrapping_add(c_bpp[0]),
            chunk[1].wrapping_add(c_bpp[1]),
            chunk[2].wrapping_add(c_bpp[2]),
            chunk[3].wrapping_add(c_bpp[3]),
        ];
        *TryInto::<&mut [u8; 4]>::try_into(chunk).unwrap() = new_chunk;
        c_bpp = b_bpp.try_into().unwrap();
    }
}

I expected to see this happen: Function runs quickly thanks to auto-vectorization.

Instead, this happened: Function is 60% slower than before, because it now doesn't get vectorized

Godbolt comparison link: https://godbolt.org/z/8EhWdYc13

Version it worked on

It most recently worked on: rustc 1.86.0 (which uses LLVM version 19.1.7)

Version with regression

rustc --version --verbose:

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

Other context

This is an attempted minimization of image-rs/image-png#598.

Activity

added
C-bugCategory: This is a bug.
regression-untriagedUntriaged performance or correctness regression.
on Jun 14, 2025
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Jun 14, 2025
added
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
I-slowIssue: Problems and improvements with respect to performance of generated code.
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
and removed
C-bugCategory: This is a bug.
on Jun 14, 2025
moxian

moxian commented on Jun 15, 2025

@moxian
Contributor

Can confirm this bisects to llvm update (#135763)

@rustbot label: -regression-untriaged +regression-from-stable-to-stable

added
regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
and removed
regression-untriagedUntriaged performance or correctness regression.
on Jun 15, 2025
ds84182

ds84182 commented on Jun 15, 2025

@ds84182

Codegen isnt the exact same but it does autovectorize the loop if you use a while loop & pattern matching to zip the slices. https://godbolt.org/z/5P7x8rK53

nikic

nikic commented on Jun 15, 2025

@nikic
Contributor

The pre-SLP IR seems to be about the same for both versions, but somehow I can't reproduce the difference when running just SLP: https://llvm.godbolt.org/z/jE5vxjqMa

nikic

nikic commented on Jun 15, 2025

@nikic
Contributor

Duh, I just forgot to specify the target triple. This shows the difference in behavior: https://llvm.godbolt.org/z/PP1Pja45v

5 remaining items

Loading
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-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nikic@ds84182@fintelia@apiraino@moxian

        Issue actions

          Function no longer auto-vectorizes in 1.87.0 · Issue #142519 · rust-lang/rust