-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow StorageDead(s) of the target block could be copied to the branching block #93987
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
Closed
Changes from all commits
Commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -43,6 +43,13 @@ impl<'tcx> MirPass<'tcx> for ConstGoto { | |||||
let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto }; | ||||||
debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto); | ||||||
terminator.kind = new_goto; | ||||||
|
||||||
let bridge_bb_statement = &body.basic_blocks()[opt.bridge_bb].statements; | ||||||
if !bridge_bb_statement.is_empty() { | ||||||
let storagedeads = bridge_bb_statement.clone(); | ||||||
body.basic_blocks_mut()[opt.bb_with_goto].statements.extend(storagedeads); | ||||||
debug!("SUCCESS: extending terminator's basic block with briding basic block."); | ||||||
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
|
||||||
} | ||||||
} | ||||||
|
||||||
// if we applied optimizations, we potentially have some cfg to cleanup to | ||||||
|
@@ -67,12 +74,13 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { | |||||
// We found a constant being assigned to `place`. | ||||||
// Now check that the target of this Goto switches on this place. | ||||||
let target_bb = &self.body.basic_blocks()[target]; | ||||||
|
||||||
// FIXME(simonvandel): We are conservative here when we don't allow | ||||||
// any statements in the target basic block. | ||||||
// This could probably be relaxed to allow `StorageDead`s which could be | ||||||
// copied to the predecessor of this block. | ||||||
if !target_bb.statements.is_empty() { | ||||||
let target_bb_statements = &target_bb.statements; | ||||||
if !target_bb_statements.is_empty() | ||||||
&& !target_bb_statements.iter().all(|statement| match statement.kind { | ||||||
StatementKind::StorageDead(_) => true, | ||||||
_ => false, | ||||||
}) | ||||||
{ | ||||||
None? | ||||||
} | ||||||
|
||||||
|
@@ -86,6 +94,7 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { | |||||
let target_to_use_in_goto = targets.target_for_value(const_value); | ||||||
self.optimizations.push(OptimizationToApply { | ||||||
bb_with_goto: location.block, | ||||||
bridge_bb: target, | ||||||
target_to_use_in_goto, | ||||||
}); | ||||||
} | ||||||
|
@@ -99,6 +108,7 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { | |||||
|
||||||
struct OptimizationToApply { | ||||||
bb_with_goto: BasicBlock, | ||||||
bridge_bb: BasicBlock, | ||||||
target_to_use_in_goto: BasicBlock, | ||||||
} | ||||||
|
||||||
|
42 changes: 42 additions & 0 deletions
42
src/test/mir-opt/const_goto.match_nested_if.MatchBranchSimplification.diff
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
- // MIR for `match_nested_if` before MatchBranchSimplification | ||
+ // MIR for `match_nested_if` after MatchBranchSimplification | ||
|
||
fn match_nested_if() -> bool { | ||
let mut _0: bool; // return place in scope 0 at $DIR/const_goto.rs:16:25: 16:29 | ||
let _1: bool; // in scope 0 at $DIR/const_goto.rs:17:9: 17:12 | ||
let mut _2: bool; // in scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
+ let mut _3: bool; // in scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
scope 1 { | ||
debug val => _1; // in scope 1 at $DIR/const_goto.rs:17:9: 17:12 | ||
} | ||
|
||
bb0: { | ||
StorageLive(_1); // scope 0 at $DIR/const_goto.rs:17:9: 17:12 | ||
StorageLive(_2); // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
_2 = const true; // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
- switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
- } | ||
- | ||
- bb1: { | ||
+ StorageLive(_3); // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
+ _3 = move _2; // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
StorageDead(_2); // scope 0 at $DIR/const_goto.rs:18:51: 18:52 | ||
- _1 = const true; // scope 0 at $DIR/const_goto.rs:24:13: 24:17 | ||
- goto -> bb3; // scope 0 at $DIR/const_goto.rs:24:13: 24:17 | ||
- } | ||
- | ||
- bb2: { | ||
- StorageDead(_2); // scope 0 at $DIR/const_goto.rs:18:51: 18:52 | ||
- _1 = const false; // scope 0 at $DIR/const_goto.rs:26:14: 26:19 | ||
- goto -> bb3; // scope 0 at $DIR/const_goto.rs:26:14: 26:19 | ||
- } | ||
- | ||
- bb3: { | ||
+ _1 = Ne(_3, const false); // scope 0 at $DIR/const_goto.rs:26:14: 26:19 | ||
+ StorageDead(_3); // scope 0 at $DIR/const_goto.rs:18:24: 18:28 | ||
_0 = _1; // scope 1 at $DIR/const_goto.rs:28:5: 28:8 | ||
StorageDead(_1); // scope 0 at $DIR/const_goto.rs:29:1: 29:2 | ||
return; // scope 0 at $DIR/const_goto.rs:29:2: 29:2 | ||
} | ||
} | ||
|
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,24 @@ pub enum Foo { | |||||||
fn issue_77355_opt(num: Foo) -> u64 { | ||||||||
if matches!(num, Foo::B | Foo::C) { 23 } else { 42 } | ||||||||
} | ||||||||
|
||||||||
// EMIT_MIR const_goto.match_nested_if.MatchBranchSimplification.diff | ||||||||
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
|
||||||||
fn match_nested_if() -> bool { | ||||||||
let val = match () { | ||||||||
() if if if if true { true } else { false } { true } else { false } { | ||||||||
true | ||||||||
} else { | ||||||||
false | ||||||||
} => | ||||||||
{ | ||||||||
true | ||||||||
} | ||||||||
_ => false, | ||||||||
}; | ||||||||
val | ||||||||
} | ||||||||
|
||||||||
fn main() { | ||||||||
issue_77355_opt(Foo::A); | ||||||||
let _ = match_nested_if(); | ||||||||
} |
167 changes: 98 additions & 69 deletions
167
...test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff
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
Oops, something went wrong.
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.
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.