Skip to content

[MIR-opt] trivial matches aren't optimized out at mir-opt-level=1 #66855

Closed
@scottmcm

Description

@scottmcm
Member

I got excited by and went to try out SimplifyArmIdentity+SimplifyBranchSame from #66282, but was surprised that the first thing I attempted didn't actually get optimized away as I'd expected.

pub fn demo(x: Result<u32, i32>) -> Result<u32, i32> {
    match x {
        Ok(v) => Ok(v),
        Err(e) => Err(e),
    }
}

Repro: https://rust.godbolt.org/z/bxFAsP

It does, however, optimize away in MIR with -Z mir-opt-level=2.

(Note that this is the simple case without ?, where there are no function calls involved so inlining should be irrelevant.)

Seems like there's a gap here? cc @Centril @oli-obk @wesleywiser

Some guesses from the discord conversation where centril asked me to open this issue: the Storage{Live|Dead} presence, extra copies because [ui]32, const prop hiding something, ...

Activity

added
A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
on Nov 28, 2019
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
I-compiletimeIssue: Problems and improvements with respect to compile times.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 28, 2019
oli-obk

oli-obk commented on Nov 29, 2019

@oli-obk
Contributor

You can run with -Zdump-mir locally with each -Zmir-opt-level and diff the resulting directories full of mir dumps. Then you'll see where stuff starts to diverge, which should pinpoint where we need to change something

matthewjasper

matthewjasper commented on Nov 29, 2019

@matthewjasper
Contributor

I'm surprised that this was merged without explicitly requiring -Zmir-opt-level=2.

oli-obk

oli-obk commented on Dec 1, 2019

@oli-obk
Contributor

@matthewjasper well... we don't have any policy about this afaik. Should we establish one? I like level 1 because it gives us good testing (as the issues have shown), but I also see that exposing new optimizations directly will likely cause problems again in the future

Centril

Centril commented on Dec 1, 2019

@Centril
Contributor

Rather than using mir-opt-level=2, we could limit new optimizations to nightly compilers instead?

bjorn3

bjorn3 commented on Dec 1, 2019

@bjorn3
Member

Or maybe use -Zmir-opt-level=2 as default for nightly.

matthewjasper

matthewjasper commented on Dec 1, 2019

@matthewjasper
Contributor

Given that bugs were found in this pass even with it requiring a Z-flag, I'm not really concerned about testing, at least for a few weeks. Having nightly not mirror stable in something with such a poor track record as newly added mir optimisations sounds terrible.

added 3 commits that reference this issue on Mar 13, 2020

Auto merge of #69756 - wesleywiser:simplify_try, r=<try>

8793905

Auto merge of #69756 - wesleywiser:simplify_try, r=<try>

d00f109
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-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-compiletimeIssue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @oli-obk@nagisa@Centril@jonas-schievink@bjorn3

      Issue actions

        [MIR-opt] trivial matches aren't optimized out at mir-opt-level=1 · Issue #66855 · rust-lang/rust