Skip to content

Incrementing &mut Option<u32> generates verbose instructions 1.94.0 -> 1.95.0 #157337

@JL-Money

Description

@JL-Money

Code

[Godbolt Link] This is a follow-up to #149970 which, at the time of this writing, has not been resolved yet.

Source:

pub fn f(x: &mut Option<u32>) {
    let mut y = *x;
    y = y.map(|a| a + 1);
    *x = y;
}
// OR
pub fn f(x: &mut Option<u32>) {
    *x = x.map(|a| a + 1);
}

1.94.0's more optimally generated assembly:

f:
        inc     dword ptr [rdi + 4]
        ret

1.95.0's less optimally generated assembly:

f:
        mov     eax, dword ptr [rdi + 4]
        inc     eax
        mov     dword ptr [rdi + 4], eax
        ret

Author's Insights

Within the optimization pipeline, I noticed that the LLVM IR was the same up until the X86 DAG->DAG Instruction Selection (x86-isel) step. The Compiler Explorer link contains a pane to see this.
Although not shown in Godbolt, the regression also exists for x86_32 (i686). See it with Overrides -> Target architecture -> i686-unknown-linux-gnu.

Compilation Info

Rust Edition: 2024
Flags: -C opt-level=3

@rustbot modify labels: +regression-from-stable-to-stable +T-compiler +C-optimization +A-codegen +A-LLVM +E-needs-bisection +S-has-mcve +O-x86_64 +O-x86_32 -C-bug -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcI-slowIssue: Problems and improvements with respect to performance of generated code.O-x86_32Target: x86 processors, 32 bit (like i686-*) (also known as IA-32, i386, i586, i686)O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)P-lowLow priorityS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions