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

Rework definition of MIR phases to more closely reflect semantic concerns #99102

Merged
merged 2 commits into from
Aug 31, 2022

Conversation

JakobDegen
Copy link
Contributor

@JakobDegen JakobDegen commented Jul 10, 2022

Implements most of rust-lang/compiler-team#522 .

I tried my best to restrict this PR to the "core" parts of the MCP. In other words, this includes just enough changes to make the new definition of MirPhase make sense. That means there are a couple of FIXMEs lying around. Depending on what reviewers prefer, I can either fix them in this PR or send follow up PRs. There are also a couple other refactorings of the rustc_mir_transform/src/lib.rs file that I want to do in follow ups that I didn't leave explicit FIXMEs for.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 10, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 10, 2022

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-highfive
Copy link
Collaborator

r? @oli-obk

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 10, 2022
@rust-log-analyzer

This comment has been minimized.

@JakobDegen
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

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

bors commented Jul 10, 2022

⌛ Trying commit 0f43aec8691e29c5f9a1ae3bc97a15379b3c43a7 with merge e760531213653adf44edf5e5e9fdf64bcd9459b6...

@bors
Copy link
Contributor

bors commented Jul 10, 2022

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

@rust-timer
Copy link
Collaborator

Queued e760531213653adf44edf5e5e9fdf64bcd9459b6 with parent 17355a3, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e760531213653adf44edf5e5e9fdf64bcd9459b6): comparison url.

Instruction count

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

Max RSS (memory usage)

Results
  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: 🎉 relevant improvement found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
-2.8% -2.8% 1
All 😿🎉 (primary) N/A N/A 0

Cycles

Results
  • Primary benchmarks: 😿 relevant regressions found
  • Secondary benchmarks: 😿 relevant regression found
mean1 max count2
Regressions 😿
(primary)
2.2% 2.3% 2
Regressions 😿
(secondary)
2.4% 2.4% 1
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) 2.2% 2.3% 2

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

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.

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

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 10, 2022
@JakobDegen JakobDegen marked this pull request as ready for review July 30, 2022 07:48
@JakobDegen
Copy link
Contributor Author

I've updated the main comment

@rust-log-analyzer

This comment has been minimized.

@JakobDegen
Copy link
Contributor Author

Attached a commit to the end that simplifies some of the run_passes logic, since I'm touching that code already. Also going to update the title to not be blatantly wrong :)

@JakobDegen JakobDegen changed the title Reorder generator lowering to be closer to drop elaboration Rework definition of MIR phases to more closely reflect semantic concerns Aug 8, 2022
@Jaic1
Copy link
Contributor

Jaic1 commented Aug 11, 2022

To sum up, the approximate mapping between the old MirPhase and the new one is:

pub enum MirPhase {
    Built,              // MirPhase::Built, 
    Const,              //
    ConstsPromoted,     // AnalysisPhase::Initial
    Derefered,          // AnalysisPhase::PostCleanup
    DropsLowered,       //
    GeneratorsLowered,  // (`StateTransform` run before `Deaggregator` in the change)
    Deaggregated,       // RuntimePhase::Initial
                        // RuntimePhase::PostCleanup
    Optimized,          // RuntimePhase::Optimized
}

Hopefully this can help people better viewing the code changes.

@bors
Copy link
Contributor

bors commented Aug 22, 2022

☔ The latest upstream changes (presumably #99908) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f07d6e8): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

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

mean1 range count2
Regressions ❌
(primary)
0.7% [0.3%, 1.7%] 33
Regressions ❌
(secondary)
1.4% [0.5%, 2.0%] 11
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.5%] 4
All ❌✅ (primary) 0.7% [0.3%, 1.7%] 33

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.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.8% [3.5%, 4.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.4% [-3.6%, -3.0%] 3
All ❌✅ (primary) - - 0

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.

mean1 range count2
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
4.7% [2.3%, 7.5%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.8% [-4.8%, -4.8%] 1
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 1

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@rustbot rustbot added the perf-regression Performance regression. label Aug 31, 2022
@nnethercote
Copy link
Contributor

@JakobDegen The original CI perf run was perf-neutral, but the one from the merge was clearly regressive. Could the "commit to the end that simplifies some of the run_passes logic" be the cause?

@JakobDegen
Copy link
Contributor Author

JakobDegen commented Aug 31, 2022

Running valgrind does point at that as being the cause. I'm somewhat surprised by this though. First of all I didn't expect this code to be that hot, but it's also not clear to me why this version is so much slower. I'm mostly just shuffling some computations around. I'll keep investigating

Comment on lines +85 to +87
/// - Const prop lints: The lint pass which reports eg `200_u8 + 200_u8` as an error is run as a
/// part of analysis to runtime MIR lowering. This means that transformations which may supress
/// such errors may not run on analysis MIR.
Copy link
Member

Choose a reason for hiding this comment

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

That's not really a semantic difference, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same program before has a different meaning before and after the pass, since before the pass the inclusion of certain patterns will imply that a diagnostic is emitted. "Semantic" might be slightly too strict a term to describe the inclusion of an extra diagnostic though

Copy link
Member

Choose a reason for hiding this comment

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

I understood "semantic" to refer to the operational semantics of the language. I would likely to strictly separate that from "soft" concerns such as lints.

@rylev
Copy link
Member

rylev commented Sep 6, 2022

@JakobDegen any progress on looking into the performance regression?

@JakobDegen
Copy link
Contributor Author

Didn't find anything interesting locally. Best thing I'd know to do is revert that commit and try putting back whatever parts I can that don't cause a perf regression. Not sure whether I'll have a chance to do that within the next few weeks though

@oli-obk
Copy link
Contributor

oli-obk commented Sep 8, 2022

cachegrind on the many-assoc-items-Check-Full regression:

 30,126,434  ???:rustc_mir_transform::pass_manager::run_passes_inner
-19,408,301  ???:rustc_mir_transform::pass_manager::run_passes
 14,114,457  ???:rustc_mir_transform::run_analysis_to_runtime_passes
  7,139,438  ???:do_rallocx
  6,644,000  ???:_rjem_je_arena_ralloc
  5,183,772  ???:<rustc_const_eval::transform::validate::TypeChecker as rustc_middle::mir::visit::Visitor>::visit_terminator
 -5,084,401  ???:rustc_mir_transform::mir_const
 -4,624,444  ???:<rustc_const_eval::transform::validate::Validator as rustc_middle::mir::MirPass>::run_pass
  4,594,756  ???:_rjem_je_arena_ralloc_no_move
  4,175,374  ???:<rustc_middle::mir::syntax::MirPhase>::phase_index
  3,136,649  ???:<rustc_mir_transform::elaborate_box_derefs::ElaborateBoxDerefs as rustc_middle::mir::MirPass>::run_pass
  3,116,468  ???:<rustc_middle::mir::patch::MirPatch>::apply
 -3,036,621  ???:rustc_mir_transform::mir_drops_elaborated_and_const_checked
  2,959,873  /build/glibc-sMfBJT/glibc-2.31/string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
  2,896,704  ???:<rustc_mir_dataflow::framework::engine::Engine<rustc_mir_dataflow::impls::MaybeInitializedPlaces>>::new_gen_kill
 -2,716,736  ???:<rustc_data_structures::graph::iterate::TriColorDepthFirstSearch<rustc_middle::mir::basic_blocks::BasicBlocks>>::run_from_start::<rustc_data_structures::graph::iterate::CycleDetector>
 -2,687,033  ???:rustc_mir_transform::mir_promoted
  2,397,315  library/core/src/fmt/mod.rs:<&mut W as core::fmt::Write>::write_str
  1,698,059  ???:<rustc_middle::mir::patch::MirPatch>::new
  1,598,240  ???:rustc_middle::mir::pretty::dump_enabled
  1,518,290  library/core/src/fmt/builders.rs:core::fmt::builders::DebugTuple::field
  1,488,365  library/core/src/slice/iter/macros.rs:core::slice::memchr::memrchr
  1,368,834  ???:<alloc::rc::Rc<alloc::vec::Vec<rustc_ast::tokenstream::TokenTree>> as core::ops::drop::Drop>::drop
  1,348,461  library/alloc/src/raw_vec.rs:alloc::raw_vec::RawVec<T,A>::reserve::do_reserve_and_handle
  1,248,575  ???:realloc
  1,218,633  library/alloc/src/vec/mod.rs:<&mut W as core::fmt::Write>::write_str
  1,118,740  library/core/src/fmt/mod.rs:core::fmt::Formatter::debug_tuple_field1_finish
  1,046,913  ???:free
 -1,039,005  ???:<alloc::vec::Vec<rustc_ast::tokenstream::TokenTree> as core::ops::drop::Drop>::drop
  1,038,852  ???:<rustc_target::asm::nvptx::NvptxInlineAsmRegClass>::suggest_class
  1,028,762  ???:<alloc::vec::Vec<rustc_middle::mir::BasicBlockData> as core::clone::Clone>::clone

Even comparing that with the diff shows no obvious avenues for improvement.

@oli-obk oli-obk mentioned this pull request Sep 8, 2022
if validate {
validate_body(tcx, body, format!("after pass {}", pass.name()));
validate_body(tcx, body, format!("after pass {}", name));
Copy link
Member

Choose a reason for hiding this comment

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

If new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized)), doesn't this now run validation each time around the loop, rather than just once at the end?

if validate {
validate_body(tcx, body, format!("after pass {}", pass.name()));
validate_body(tcx, body, format!("after pass {}", name));
Copy link
Member

Choose a reason for hiding this comment

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

If new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized)), doesn't this now run validation each time around the loop, rather than just once at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized)) is only ever true exactly once (on the appropriate PhaseChange pass)

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, I was not aware of this non-local invariant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's not ideal, my suggestion below would make this much more obvious as a side-effect

let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
trace!(?overridden_passes);

if validate {
validate_body(tcx, body, format!("start of phase transition from {:?}", start_phase));
Copy link
Member

Choose a reason for hiding this comment

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

So we are no longer doing pre-validation to ensure we have sensible input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There isn't really a "pre" step - the input to all passes is the output to another. And the first pass that runs after Mir building is a nop (although I suppose it might be principled to insert an explicit invocation of the validator immediately after Mir building)

Copy link
Member

Choose a reason for hiding this comment

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

That is relying on a non-local invariant though, so if it is ever broken it could be hard to debug. The previous check was set up to ensure that it is definitely this pass that is to blame, no matter how much things changed elsewhere in the compiler, possibly by someone who is not aware of this invariant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh hmm, had not considered that angle. Yeah, that sounds reasonable to me. Will re-add this next time I touch this logic (probably fairly soon)

let new_phase = pass.phase_change();
let dump_enabled = (is_enabled && pass.is_mir_dump_enabled()) || new_phase.is_some();
let validate = (validate && is_enabled)
|| new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized));
Copy link
Member

@RalfJung RalfJung Sep 8, 2022

Choose a reason for hiding this comment

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

Honestly with all the is_enabled being manually applied everywherre, I would say this logic is now more complicated and harder to understand than it was before. E.g. this will validate even after disabled passes if new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized)).

@RalfJung
Copy link
Member

RalfJung commented Sep 8, 2022

Best thing I'd know to do is revert that commit

I'm in favor of that, IMO it made the logic more complicated than it was before. (I very much hope it can be simplified, but I don't think this commit achieves that.)

@JakobDegen
Copy link
Contributor Author

E.g. this will validate even after disabled passes if new_phase == Some(MirPhase::Runtime(RuntimePhase::Optimized)).

Yes, that's actually intentional - the fact that the old pass manager didn't do this was a bug imo.

I made a mistake not properly communicating what was going on here, but I think the change to the pass manager is actually correct. It fixes its behavior around disabled passes, and I think it's just more upfront about the complexity of the logic here.

I sympathize with your disliking the complexity, but I don't think reverting is the right way to go (unless perf says it is). The right thing to do here to reduce the complexity I think would be to get rid of MirPass::phase_change and instead have the pass manager accept a &[MirChange] with

enum MirChange {
    PhaseChange(MirPhase),
    Pass(&dyn MirPass),
}

Having the phase change be a part of the pass is I think weird anyway (it's not really a property of the pass). This would get rid of that, we can replace all the existing uses of the PhaseChange passes with the proper variant, and the logic here never has to consider the possibility of having both things happen at the same time.

@RalfJung
Copy link
Member

RalfJung commented Sep 8, 2022

I made a mistake not properly communicating what was going on here, but I think the change to the pass manager is actually correct. It fixes its behavior around disabled passes, and I think it's just more upfront about the complexity of the logic here.

Given there's barely any comments in the code, I would not agree that it is being upfront about anything. ;)

It is possible that I misunderstood the intent of the old and/or the new pass managed. Having more than a single line of comment for a function that is both important and subtle would probably help with that. :)

@JakobDegen JakobDegen deleted the reorder-generators branch September 25, 2022 20:44
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 25, 2022
…on, r=oli-obk

Split phase change from `MirPass`

The main goal here is to simplify the pass manager logic. `MirPass` no longer contains the `phase_change` method, and `run_passes` instead accepts an `Option<PhaseChange>`. The hope is that this addresses the comments (and maybe perf regression) from rust-lang#99102 .

r? `@oli-obk` cc `@RalfJung`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 5, 2023
Remove duplicated elaborate box derefs pass

The pass runs earlier as a part of `run_runtime_lowering_passes`.

The duplicate was added in rust-lang#99102.
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…on, r=oli-obk

Split phase change from `MirPass`

The main goal here is to simplify the pass manager logic. `MirPass` no longer contains the `phase_change` method, and `run_passes` instead accepts an `Option<PhaseChange>`. The hope is that this addresses the comments (and maybe perf regression) from rust-lang#99102 .

r? `@oli-obk` cc `@RalfJung`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet