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

Fortify and re-enable NRVO MIR opt #116531

Closed
wants to merge 3 commits into from
Closed

Conversation

cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Oct 8, 2023

Fixes #111005
Fixes #110902

This PR re-enables NRVO opt that had been disabled in #111007

To look for RVO opportunities, we walk the MIR backwards from return terminators. In addition to the former implementation, we look at all the traversed statements and terminators for writes. If a local is written-to or moved-from, we discard it from the RVO candidates (assigned_locals bitset). If we see an indirect write or move, we discard all borrowed locals from candidates.

cc @JakobDegen

@rustbot
Copy link
Collaborator

rustbot commented Oct 8, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 8, 2023
@cjgillot cjgillot added A-mir-opt Area: MIR optimizations A-mir-opt-nrvo Fixed by the Named Return Value Opt. (NRVO) labels Oct 8, 2023
@rustbot
Copy link
Collaborator

rustbot commented Oct 8, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@cjgillot
Copy link
Contributor Author

cjgillot commented Oct 8, 2023

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 8, 2023
@bors
Copy link
Contributor

bors commented Oct 8, 2023

⌛ Trying commit 75d82bd with merge 549ef8c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 8, 2023
Fortify and re-enable RNVO MIR opt

Fixes rust-lang#111005
Fixes rust-lang#110902

This PR re-enables NRVO opt that had been disabled in rust-lang#111007

To look for RVO opportunities, we walk the MIR backwards from `return` terminators. In addition to the former implementation, we look at all the traversed statements and terminators for writes. If a local is written-to or moved-from, we discard it from the RVO candidates (`assigned_locals` bitset). If we see an indirect write or move, we discard all borrowed locals from candidates.

cc `@JakobDegen`
@bors
Copy link
Contributor

bors commented Oct 8, 2023

☀️ Try build successful - checks-actions
Build commit: 549ef8c (549ef8c3a56f8dd29dad5eee971226d19b7088e9)

1 similar comment
@bors
Copy link
Contributor

bors commented Oct 8, 2023

☀️ Try build successful - checks-actions
Build commit: 549ef8c (549ef8c3a56f8dd29dad5eee971226d19b7088e9)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (549ef8c): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
5.2% [0.6%, 9.2%] 6
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.7%, -0.2%] 156
Improvements ✅
(secondary)
-0.4% [-0.6%, -0.2%] 53
All ❌✅ (primary) -0.1% [-0.7%, 9.2%] 162

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.9% [0.0%, 3.1%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-7.0% [-10.7%, -2.8%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.9% [-10.7%, 3.1%] 7

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
5.7% [1.0%, 10.3%] 6
Regressions ❌
(secondary)
2.4% [2.2%, 2.6%] 4
Improvements ✅
(primary)
-1.8% [-1.8%, -1.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.6% [-1.8%, 10.3%] 7

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.4%] 17
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-1.4%, -0.0%] 39
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) -0.1% [-1.4%, 0.4%] 56

Bootstrap: 624.124s -> 624.52s (0.06%)
Artifact size: 270.68 MiB -> 270.53 MiB (-0.06%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 8, 2023
@RalfJung
Copy link
Member

RalfJung commented Oct 10, 2023

Will this ever lead to MIR that takes a reference to the return place? E.g. this

fn foo(f: fn(&i16), k: i16) -> i16 {
    let mut x = k;
    x += 2;
    f(&x);
    x
}

could replace all x by RET. Will this pass do that?

EDIT: looks like it will

Comment on lines 152 to 165
if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
&& lhs.as_local() == Some(RETURN_PLACE)
&& let Rvalue::Use(rhs) = rhs
&& let Some(rhs) = rhs.place()
&& let Some(rhs) = rhs.as_local()
&& !assigned_locals.contains(rhs)
{
return Some(rhs);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#![allow(internal_features, unused_assignments)]
#![feature(custom_mir, core_intrinsics)]
extern crate core;
use core::intrinsics::mir::*;

#[custom_mir(dialect = "runtime", phase = "initial")]
pub fn f() -> u32 {
    mir!({
        let x = 1;
        RET = 2;
        RET = x;
        Return()
    })
}
fn main() {
    assert_eq!(f(), 1);
}
$ rustc +stage1 a.rs -Zmir-opt-level=0 && ./a
$ rustc +stage1 a.rs -Zmir-opt-level=0 -Zmir-enable-passes=+RenameReturnPlace && ./a
thread 'main' panicked at a.rs:16:5:
assertion `left == right` failed
  left: 2
 right: 1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that's even worse. This does not appear solvable without reimplementing DestProp here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach seems plausible if limited to a MIR that contains a single occurrence of a return place - as a destination of an assignment.

@RalfJung
Copy link
Member

Fortify and re-enable RNVO MIR opt

Should this say NRVO?

@cjgillot cjgillot changed the title Fortify and re-enable RNVO MIR opt Fortify and re-enable NRVO MIR opt Oct 10, 2023
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 A-mir-opt-nrvo Fixed by the Named Return Value Opt. (NRVO) perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NRVO miropt is unsound ICE: left: MutatingUse(Call), right: NonUse(VarDebugInfo)
7 participants