Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIR-opt] matches!(x, Some(_)) should simplify to Eq(Discriminant, Const) #131219

Open
scottmcm opened this issue Oct 3, 2024 · 0 comments · May be fixed by #131244
Open

[MIR-opt] matches!(x, Some(_)) should simplify to Eq(Discriminant, Const) #131219

scottmcm opened this issue Oct 3, 2024 · 0 comments · May be fixed by #131244
Assignees
Labels
A-mir-opt Area: MIR optimizations C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Oct 3, 2024

Repro: https://rust.godbolt.org/z/4dvWdqcn5

Today, this check

pub fn my_is_eq<T>(x: &std::cmp::Ordering) -> bool { matches!(x, std::cmp::Ordering::Equal)}

simplifies as expected down to just

    bb0: {
        _2 = discriminant((*_1));
        _0 = Eq(copy _2, const 0_i8);
        return;
    }

Checking for Some in an Option, however,

pub fn my_is_some<T>(x: &Option<T>) -> bool { matches!(x, Some(_)) }

doesn't and ends up taking a whole five basic blocks

    bb0: {
        _2 = discriminant((*_1));
        switchInt(move _2) -> [1: bb2, 0: bb1, otherwise: bb4];
    }

    bb1: {
        _0 = const false;
        goto -> bb3;
    }

    bb2: {
        _0 = const true;
        goto -> bb3;
    }

    bb3: {
        return;
    }

    bb4: {
        unreachable;
    }

That's wasteful, and shouldn't happen.

@scottmcm scottmcm added the A-mir-opt Area: MIR optimizations label Oct 3, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 3, 2024
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 3, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2024
…, r=<try>

Consider empty-unreachable otherwise branches in MatchBranchSimplification

Fixes rust-lang#131219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants