Skip to content

Cheaper dump_mir #105083

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

Closed
wants to merge 2 commits into from
Closed

Conversation

nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Nov 30, 2022

`dump_mir_for_pass` and `dump_mir_for_phase_change` do some allocations
even when `-Zdump-mir` isn't used, and the cost is enough to be
noticeable.

This commit rearranges things to avoid the cost.
- `dump_mir` now dumps MIR unconditionally.
- The new `maybe_dump_mir` functions dumps MIR if `-Zdump-mir` is set
  appropriately.
- `dump_enabled` is replaced with `pass_name_matches_dump_filters`,
  which just does the second part of the test.
- Most places use `maybe_dump_mir`, but `dump_mir_for_pass` and
  `dump_mir_for_phase_change` do their own tests to avoid the
  allocations in the common cases where no MIR dumping occurs.

This is not the most elegant code ever written, but hopefully it's good
enough. I also tried using closures for `pass_num` and `pass_name` but
that ended up being uglier than this approach.
It has a single call site.
@rustbot
Copy link
Collaborator

rustbot commented Nov 30, 2022

Failed to set assignee to JakobDegen: invalid assignee

Note: Only org members, users with write permissions, or people who have commented on the PR may be assigned.

@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 Nov 30, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 30, 2022

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@nnethercote
Copy link
Contributor Author

Best reviewed one commit at a time. Commit logs explain what's going on.

@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 Nov 30, 2022
@bors
Copy link
Collaborator

bors commented Nov 30, 2022

⌛ Trying commit 4e61953 with merge ba96534cdc0ce369c29afc119e717324c5c88c44...

@rust-lang rust-lang deleted a comment from rustbot Nov 30, 2022
Comment on lines -93 to -95
let Some(ref filters) = tcx.sess.opts.unstable_opts.dump_mir else {
return false;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

is it actually perf relevant to do this at every call site instead of inside the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is, as the perf CI results show.

There is a two-step check when deciding whether to dump MIR.

  • Is -Zdump-mir specified?
  • If so, does the MIR pass name match the -Zdump-mir value?

If both those conditions pass, then the MIR is dumped, which involves printing pass_name and pass_num.

For most of the dump_mir callsites pass_name and pass_num are simple scalars, which poses no problem.
But at at two of the dump_mir call sites (in dump_mir_for_phase_change and dump_mir_for_pass) pass_name and pass_num are constructed in a way that is moderately expensive, and this happens before the two-step check.

The PR changes things so that, at two of these call sites, pass_name is only constructed if the first part of the two-step check succeeds, and pass_num is only constructed if the second part of the two-step check succeeds.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried building an alternative design (#105121) that afaict should end up with similar perf improvements.

@bors
Copy link
Collaborator

bors commented Nov 30, 2022

☀️ Try build successful - checks-actions
Build commit: ba96534cdc0ce369c29afc119e717324c5c88c44 (ba96534cdc0ce369c29afc119e717324c5c88c44)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ba96534cdc0ce369c29afc119e717324c5c88c44): 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-review -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)
0.8% [0.7%, 0.9%] 2
Regressions ❌
(secondary)
2.3% [2.0%, 2.7%] 6
Improvements ✅
(primary)
-1.3% [-2.1%, -0.4%] 19
Improvements ✅
(secondary)
-1.3% [-2.0%, -0.6%] 13
All ❌✅ (primary) -1.1% [-2.1%, 0.9%] 21

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 30, 2022
@nnethercote
Copy link
Contributor Author

Perf results are good. I think the keccak and cranelift-codegen regressions are just noise, they've been noisy lately.

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 4, 2022
…nnethercote

Cheaper `dump_mir` take two

alternative to rust-lang#105083

r? `@nnethercote`
@nnethercote
Copy link
Contributor Author

Superseded by #105121.

@nnethercote nnethercote closed this Dec 4, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…nnethercote

Cheaper `dump_mir` take two

alternative to rust-lang#105083

r? `@nnethercote`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants