-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[MIR] Generalize the depth first search logic and use it in all of SimplifyCfg. #30239
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -21,6 +21,31 @@ pub fn update_basic_block_ids(mir: &mut Mir, replacements: &[BasicBlock]) { | |||
} | |||
} | |||
|
|||
/// Run a function on every reachable basic block, then delete any unreachable blocks. | |||
/// The function given should not add or remove and blocks from the control flow graph. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/and/any/
Typo fixed. |
@@ -126,7 +105,6 @@ impl<'tcx> MirPass<'tcx> for SimplifyCfg { | |||
while changed { | |||
changed = self.simplify_branches(mir); | |||
changed |= self.remove_goto_chains(mir); | |||
self.remove_dead_blocks(mir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh. I'm not sure if it is better to compress the blocks twice for each round. Seems like it might be slower overall?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it seems like this might take longer to reach a fixed point, since in some cases (e.g. simplify_branches) you may now have to do one step to modify the terminator and another round to remove the block, and then a final round, whereas before I think you would have finished in two?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR it may be time to start thinking about some compile-time measurements here :)
@gereeter thoughts on this comment of mine:
|
I don't completely understand what you are worried about - the only difference execution-wise with this change is the dead blocks are now removed twice per round, not a single time. This should only improve convergence by making any optimization opportunities more obvious earlier. |
Man, coming back to this, I am not sure what I was worried about either when it comes to convergence. However, I AM concerned that adjusting basic blocks has a kind of fixed cost and it would be cheaper to do it once per round once we know the full set of things to be removed. I guess it's hard to know without measuring. Anybody on @rust-lang/compiler have an opinion? @dotdash? |
I can't see how this would improve convergence time. Unreachable blocks are unreachable, whether or not they're removed from the MIR, so they aren't going to be visited during the depth-first search either way. The only reason I can see for removing the blocks each iteration before was because it was iterating over all blocks, reachable or otherwise, so removing them meant they weren't looked at unnecessarily. Since this code also changes the sub-passes to do a preorder traversal, there is no advantage to removing unreachable blocks before we hit a fixpoint any more. Therefore, I think we should keep the |
@Aatch At this point, you are right that removing the blocks in the middle has very little effect. Once #30238 merges, however, the predecessor map can be messed up by dead blocks, slowing convergence if they aren't pruned before producing the map. I'm not sure if that's a good enough reason to do it here, though. |
☔ The latest upstream changes (presumably #30481) made this pull request unmergeable. Please resolve the merge conflicts. |
(ping) Does this just need a rebase? Comments to address? (trying to help clear out the queue) |
Yeah, I'm not really sure. I remain mildly uncomfortable with this change, but I've kind of forgotten exactly why. I guess I still am just not sure that this is an improvement. Given that we did not land #30238, as @Aatch suggested an alternative approach, I think we ought to close this one too (since the two seem to be linked). Closing therefore. @gereeter let me know if I'm mis-remembering things, and thanks for the PR in any case. |
[MIR] Generalize the depth first search logic and use it in all of SimplifyCfg.