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] trivial matches aren't optimized out at mir-opt-level=1 #66855

Closed
scottmcm opened this issue Nov 28, 2019 · 6 comments · Fixed by #69756
Closed

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

scottmcm opened this issue Nov 28, 2019 · 6 comments · Fixed by #69756
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. I-compiletime Issue: Problems and improvements with respect to compile times. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
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, ...

@scottmcm scottmcm added the A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html label Nov 28, 2019
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. I-compiletime Issue: Problems and improvements with respect to compile times. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 28, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Nov 29, 2019

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

@nagisa nagisa added the requires-nightly This issue requires a nightly compiler in some way. label Nov 29, 2019
@matthewjasper
Copy link
Contributor

matthewjasper commented Nov 29, 2019

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

@oli-obk
Copy link
Contributor

oli-obk commented Dec 1, 2019

@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
Copy link
Contributor

Centril commented Dec 1, 2019

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

@bjorn3
Copy link
Member

bjorn3 commented Dec 1, 2019

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

@matthewjasper
Copy link
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.

bors added a commit that referenced this issue Mar 13, 2020
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1

I also added test cases to make sure the optimization can fire on all of
these cases:

```rust
fn case_1(o: Option<u8>) -> Option<u8> {
  match o {
    Some(u) => Some(u),
    None => None,
  }
}

fn case2(r: Result<u8, i32>) -> Result<u8, i32> {
  match r {
    Ok(u) => Ok(u),
    Err(i) => Err(i),
  }
}

fn case3(r: Result<u8, i32>) -> Result<u8, i32> {
  let u = r?;
  Ok(u)
}

```

Without MIR inlining, this still does not completely optimize away the
`?` operator because the `Try::into_result()`, `From::from()` and
`Try::from_error()` calls still exist. This does move us a bit closer to
that goal though because:

- We can now run the pass on mir-opt-level=1

- We no longer depend on the copy propagation pass running which is
  unlikely to stabilize anytime soon.

Fixes #66855
bors added a commit that referenced this issue Mar 13, 2020
Modify SimplifyArmIdentity so it can trigger on mir-opt-level=1

I also added test cases to make sure the optimization can fire on all of
these cases:

```rust
fn case_1(o: Option<u8>) -> Option<u8> {
  match o {
    Some(u) => Some(u),
    None => None,
  }
}

fn case2(r: Result<u8, i32>) -> Result<u8, i32> {
  match r {
    Ok(u) => Ok(u),
    Err(i) => Err(i),
  }
}

fn case3(r: Result<u8, i32>) -> Result<u8, i32> {
  let u = r?;
  Ok(u)
}

```

Without MIR inlining, this still does not completely optimize away the
`?` operator because the `Try::into_result()`, `From::from()` and
`Try::from_error()` calls still exist. This does move us a bit closer to
that goal though because:

- We can now run the pass on mir-opt-level=1

- We no longer depend on the copy propagation pass running which is
  unlikely to stabilize anytime soon.

Fixes #66855
@jonas-schievink jonas-schievink added the A-mir-opt Area: MIR optimizations label Mar 23, 2020
@bors bors closed this as completed in 7c34d8d May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. I-compiletime Issue: Problems and improvements with respect to compile times. requires-nightly This issue requires a nightly compiler in some way. 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.

7 participants