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] Generalize the depth first search logic and use it in all of SimplifyCfg. #30239

Closed
wants to merge 3 commits into from

Conversation

gereeter
Copy link
Contributor

@gereeter gereeter commented Dec 6, 2015

[MIR] Generalize the depth first search logic and use it in all of SimplifyCfg.

@rust-highfive
Copy link
Collaborator

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

Choose a reason for hiding this comment

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

s/and/any/

@gereeter
Copy link
Contributor Author

gereeter commented Dec 6, 2015

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);
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

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 :)

@pnkfelix pnkfelix changed the title [MIR] Generalize the depth first search logic and use it in all of Si… [MIR] Generalize the depth first search logic and use it in all of SimplifyCfg. Dec 15, 2015
@nikomatsakis
Copy link
Contributor

@gereeter thoughts on this comment of mine:

...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?

@gereeter
Copy link
Contributor Author

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.

@nikomatsakis
Copy link
Contributor

@gereeter

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?

@Aatch
Copy link
Contributor

Aatch commented Dec 22, 2015

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 remove_dead_blocks function and run it just before returning.

@gereeter
Copy link
Contributor Author

@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.

@bors
Copy link
Contributor

bors commented Jan 6, 2016

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

@alexcrichton
Copy link
Member

(ping)

Does this just need a rebase? Comments to address?

(trying to help clear out the queue)

@nikomatsakis
Copy link
Contributor

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.

@gereeter
Copy link
Contributor Author

Sorry for not replying earlier. This definitely should be changed to take into account @Aatch's suggestions, and this should probably be combined with #30238. Unfortunately, I've been busy and haven't gotten around to improving this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants