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

Optimize switch sources representation and usage #96838

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ impl<'tcx> Body<'tcx> {
self.predecessor_cache.compute(&self.basic_blocks)
}

/// `body.switch_sources()[target][switch]` returns a list of switch values
/// that lead to a `target` block from a `switch` block.
#[inline]
pub fn switch_sources(&self) -> &SwitchSources {
self.switch_source_cache.compute(&self.basic_blocks)
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_mir_dataflow/src/framework/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ impl Direction for Backward {

mir::TerminatorKind::SwitchInt { targets: _, ref discr, switch_ty: _ } => {
let mut applier = BackwardSwitchIntEdgeEffectsApplier {
body,
pred,
exit_state,
values: &body.switch_sources()[bb][pred],
bb,
propagate: &mut propagate,
effects_applied: false,
Expand Down Expand Up @@ -305,9 +305,9 @@ impl Direction for Backward {
}

struct BackwardSwitchIntEdgeEffectsApplier<'a, D, F> {
body: &'a mir::Body<'a>,
pred: BasicBlock,
exit_state: &'a mut D,
values: &'a [Option<u128>],
bb: BasicBlock,
propagate: &'a mut F,

Expand All @@ -322,7 +322,8 @@ where
fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) {
assert!(!self.effects_applied);

let targets = self.values.iter().map(|&value| SwitchIntTarget { value, target: self.bb });
let values = &self.body.switch_sources()[self.bb][self.pred];
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't expect this to affect much, but since we're only interested in self.pred, it could be cheaper to just look at the terminator of basic block with id self.pred and iterate over all targets filtering for ones for self.bb. Caching this map does not seem very useful if "computing" it for specific blocks is just a lookup and an iteration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Considering that this is the only user of switch_sources, I'm not sure it is carrying its weight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the discussion on the original pull request #95120, switch sources were introduced to avoid quadratic time complexity associated with the implementation strategy you are just suggesting.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess if we have a switch with lots of targets... 🤷 added to my todo to do some experiments in the future

let targets = values.iter().map(|&value| SwitchIntTarget { value, target: self.bb });

let mut tmp = None;
for target in targets {
Expand Down