Skip to content

Branch elimination not performed on dead store / failure to introduce a spurious store #114886

Open
@SUPERCILEX

Description

@SUPERCILEX

I tried this code:

pub fn bar(count: &mut usize, n: usize) {
    for i in 0..n {
        *count = count.wrapping_add(1);
    }
}

I expected to see this happen:

example::bar:
        add     qword ptr [rdi], rsi
        ret

Instead, this happened:

example::bar:
        test    rsi, rsi
        je      .LBB1_2
        add     qword ptr [rdi], rsi
.LBB1_2:
        ret

The missed branch is going to be much more expansive than throwing something in a store buffer. Maybe you could argue the compiler thinks n=0 is going to be common, but I find that unlikely.

I'm pretty sure Rust's ownership system allows inserting stores that didn't exist because an &mut can't be used on another thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions