-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Less cloning in early otherwise branch #77472
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
simonvandel
wants to merge
8
commits into
rust-lang:master
from
simonvandel:refactor-early-otherwise-branch
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e7811f9
Do not clone vec and bail early
simonvandel 7a8c277
less clone
simonvandel 62b6896
Remove duplicated check
simonvandel 4784a5c
reorder checks from (intuitively) cheapest to most expensive
simonvandel 586ba9d
Restructure data such that data about the first switch is not duplicated
simonvandel b4e144e
Move helper creation out of loop
simonvandel bd56b11
Use SwitchTargets instead of storing in Vec
simonvandel 1a8d28a
Use otherwise from switchTargets
simonvandel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -38,8 +38,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
.flat_map(|(bb_idx, bb)| { | ||||||
let switch = bb.terminator(); | ||||||
let helper = Helper { body, tcx }; | ||||||
let infos = helper.go(bb, switch)?; | ||||||
Some(OptimizationToApply { infos, basic_block_first_switch: bb_idx }) | ||||||
let info = helper.go(bb, switch)?; | ||||||
Some(OptimizationToApply { info, basic_block_first_switch: bb_idx }) | ||||||
}) | ||||||
.collect(); | ||||||
|
||||||
|
@@ -48,6 +48,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
for opt_to_apply in opts_to_apply { | ||||||
trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply); | ||||||
|
||||||
let first_switch_info = opt_to_apply.info.first_switch_info; | ||||||
let second_switch_info = &opt_to_apply.info.second_switch_infos[0]; | ||||||
let statements_before = | ||||||
body.basic_blocks()[opt_to_apply.basic_block_first_switch].statements.len(); | ||||||
let end_of_block_location = Location { | ||||||
|
@@ -58,8 +60,8 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
let mut patch = MirPatch::new(body); | ||||||
|
||||||
// create temp to store second discriminant in | ||||||
let discr_type = opt_to_apply.infos[0].second_switch_info.discr_ty; | ||||||
let discr_span = opt_to_apply.infos[0].second_switch_info.discr_source_info.span; | ||||||
let discr_type = second_switch_info.discr_ty; | ||||||
let discr_span = second_switch_info.discr_source_info.span; | ||||||
let second_discriminant_temp = patch.new_temp(discr_type, discr_span); | ||||||
|
||||||
patch.add_statement( | ||||||
|
@@ -68,8 +70,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
); | ||||||
|
||||||
// create assignment of discriminant | ||||||
let place_of_adt_to_get_discriminant_of = | ||||||
opt_to_apply.infos[0].second_switch_info.place_of_adt_discr_read; | ||||||
let place_of_adt_to_get_discriminant_of = second_switch_info.place_of_adt_discr_read; | ||||||
patch.add_assign( | ||||||
end_of_block_location, | ||||||
Place::from(second_discriminant_temp), | ||||||
|
@@ -83,8 +84,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
patch.add_statement(end_of_block_location, StatementKind::StorageLive(not_equal_temp)); | ||||||
|
||||||
// create NotEqual comparison between the two discriminants | ||||||
let first_descriminant_place = | ||||||
opt_to_apply.infos[0].first_switch_info.discr_used_in_switch; | ||||||
let first_descriminant_place = first_switch_info.discr_used_in_switch; | ||||||
let not_equal_rvalue = Rvalue::BinaryOp( | ||||||
not_equal, | ||||||
Operand::Copy(Place::from(second_discriminant_temp)), | ||||||
|
@@ -96,19 +96,17 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
); | ||||||
|
||||||
let new_targets = opt_to_apply | ||||||
.infos | ||||||
.info | ||||||
.second_switch_infos | ||||||
.iter() | ||||||
.flat_map(|x| x.second_switch_info.targets_with_values.iter()) | ||||||
.flat_map(|x| x.targets_with_values.iter()) | ||||||
.cloned(); | ||||||
|
||||||
let targets = SwitchTargets::new( | ||||||
new_targets, | ||||||
opt_to_apply.infos[0].first_switch_info.otherwise_bb, | ||||||
); | ||||||
let targets = SwitchTargets::new(new_targets, first_switch_info.otherwise_bb); | ||||||
|
||||||
// new block that jumps to the correct discriminant case. This block is switched to if the discriminants are equal | ||||||
let new_switch_data = BasicBlockData::new(Some(Terminator { | ||||||
source_info: opt_to_apply.infos[0].second_switch_info.discr_source_info, | ||||||
source_info: second_switch_info.discr_source_info, | ||||||
kind: TerminatorKind::SwitchInt { | ||||||
// the first and second discriminants are equal, so just pick one | ||||||
discr: Operand::Copy(first_descriminant_place), | ||||||
|
@@ -121,7 +119,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { | |||||
|
||||||
// switch on the NotEqual. If true, then jump to the `otherwise` case. | ||||||
// If false, then jump to a basic block that then jumps to the correct disciminant case | ||||||
let true_case = opt_to_apply.infos[0].first_switch_info.otherwise_bb; | ||||||
let true_case = first_switch_info.otherwise_bb; | ||||||
let false_case = new_switch_bb; | ||||||
patch.patch_terminator( | ||||||
opt_to_apply.basic_block_first_switch, | ||||||
|
@@ -189,7 +187,7 @@ struct SwitchDiscriminantInfo<'tcx> { | |||||
|
||||||
#[derive(Debug)] | ||||||
struct OptimizationToApply<'tcx> { | ||||||
infos: Vec<OptimizationInfo<'tcx>>, | ||||||
info: OptimizationInfo<'tcx>, | ||||||
/// Basic block of the original first switch | ||||||
basic_block_first_switch: BasicBlock, | ||||||
} | ||||||
|
@@ -198,36 +196,36 @@ struct OptimizationToApply<'tcx> { | |||||
struct OptimizationInfo<'tcx> { | ||||||
/// Info about the first switch and discriminant | ||||||
first_switch_info: SwitchDiscriminantInfo<'tcx>, | ||||||
/// Info about the second switch and discriminant | ||||||
second_switch_info: SwitchDiscriminantInfo<'tcx>, | ||||||
/// Info about all swtiches that are successors of the first switch | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
second_switch_infos: Vec<SwitchDiscriminantInfo<'tcx>>, | ||||||
} | ||||||
|
||||||
impl<'a, 'tcx> Helper<'a, 'tcx> { | ||||||
pub fn go( | ||||||
&self, | ||||||
bb: &BasicBlockData<'tcx>, | ||||||
switch: &Terminator<'tcx>, | ||||||
) -> Option<Vec<OptimizationInfo<'tcx>>> { | ||||||
) -> Option<OptimizationInfo<'tcx>> { | ||||||
// try to find the statement that defines the discriminant that is used for the switch | ||||||
let discr = self.find_switch_discriminant_info(bb, switch)?; | ||||||
|
||||||
// go through each target, finding a discriminant read, and a switch | ||||||
let mut results = vec![]; | ||||||
let mut second_switch_infos = vec![]; | ||||||
|
||||||
for (value, target) in discr.targets_with_values.iter() { | ||||||
let info = self.find_discriminant_switch_pairing(&discr, *target, *value)?; | ||||||
results.push(info); | ||||||
second_switch_infos.push(info); | ||||||
} | ||||||
|
||||||
Some(results) | ||||||
Some(OptimizationInfo { first_switch_info: discr, second_switch_infos }) | ||||||
} | ||||||
|
||||||
fn find_discriminant_switch_pairing( | ||||||
&self, | ||||||
discr_info: &SwitchDiscriminantInfo<'tcx>, | ||||||
target: BasicBlock, | ||||||
value: u128, | ||||||
) -> Option<OptimizationInfo<'tcx>> { | ||||||
) -> Option<SwitchDiscriminantInfo<'tcx>> { | ||||||
let bb = &self.body.basic_blocks()[target]; | ||||||
// find switch | ||||||
let terminator = bb.terminator(); | ||||||
|
@@ -273,10 +271,7 @@ impl<'a, 'tcx> Helper<'a, 'tcx> { | |||||
// if we reach this point, the optimization applies, and we should be able to optimize this case | ||||||
// store the info that is needed to apply the optimization | ||||||
|
||||||
Some(OptimizationInfo { | ||||||
first_switch_info: discr_info.clone(), | ||||||
second_switch_info: this_bb_discr_info, | ||||||
}) | ||||||
Some(this_bb_discr_info) | ||||||
} else { | ||||||
None | ||||||
} | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.