-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Smaller and more correct generator codegen #69814
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
425e7e5
Don't insert panic when generator can not return
jonas-schievink 5ac41a1
Don't insert panic when generator can not unwind
jonas-schievink 46aeef6
Poison generators when any terminator unwinds
jonas-schievink 8d9f633
Change index for SwitchInt case
jonas-schievink 2cbccce
Add test for unnecessary panic branches
jonas-schievink cb58de8
Apply suggestions from code review
jonas-schievink 38fa378
Swap inserts to keep the original ordering
jonas-schievink 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 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 |
---|---|---|
|
@@ -991,18 +991,101 @@ fn insert_panic_block<'tcx>( | |
assert_block | ||
} | ||
|
||
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool { | ||
// Returning from a function with an uninhabited return type is undefined behavior. | ||
if body.return_ty().conservative_is_privately_uninhabited(tcx) { | ||
return false; | ||
} | ||
|
||
// If there's a return terminator the function may return. | ||
for block in body.basic_blocks() { | ||
if let TerminatorKind::Return = block.terminator().kind { | ||
return true; | ||
} | ||
} | ||
|
||
// Otherwise the function can't return. | ||
false | ||
} | ||
|
||
fn can_unwind<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> bool { | ||
// Nothing can unwind when landing pads are off. | ||
if tcx.sess.no_landing_pads() { | ||
return false; | ||
} | ||
|
||
// Unwinds can only start at certain terminators. | ||
for block in body.basic_blocks() { | ||
match block.terminator().kind { | ||
// These never unwind. | ||
TerminatorKind::Goto { .. } | ||
| TerminatorKind::SwitchInt { .. } | ||
| TerminatorKind::Abort | ||
| TerminatorKind::Return | ||
| TerminatorKind::Unreachable | ||
| TerminatorKind::GeneratorDrop | ||
| TerminatorKind::FalseEdges { .. } | ||
| TerminatorKind::FalseUnwind { .. } => {} | ||
|
||
// Resume will *continue* unwinding, but if there's no other unwinding terminator it | ||
// will never be reached. | ||
TerminatorKind::Resume => {} | ||
|
||
TerminatorKind::Yield { .. } => { | ||
unreachable!("`can_unwind` called before generator transform") | ||
} | ||
|
||
// These may unwind. | ||
TerminatorKind::Drop { .. } | ||
| TerminatorKind::DropAndReplace { .. } | ||
| TerminatorKind::Call { .. } | ||
| TerminatorKind::Assert { .. } => return true, | ||
} | ||
} | ||
|
||
// If we didn't find an unwinding terminator, the function cannot unwind. | ||
false | ||
} | ||
|
||
fn create_generator_resume_function<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
transform: TransformVisitor<'tcx>, | ||
def_id: DefId, | ||
source: MirSource<'tcx>, | ||
body: &mut BodyAndCache<'tcx>, | ||
can_return: bool, | ||
) { | ||
let can_unwind = can_unwind(tcx, body); | ||
|
||
// Poison the generator when it unwinds | ||
for block in body.basic_blocks_mut() { | ||
let source_info = block.terminator().source_info; | ||
if let &TerminatorKind::Resume = &block.terminator().kind { | ||
block.statements.push(transform.set_discr(VariantIdx::new(POISONED), source_info)); | ||
if can_unwind { | ||
let poison_block = BasicBlock::new(body.basic_blocks().len()); | ||
let source_info = source_info(body); | ||
body.basic_blocks_mut().push(BasicBlockData { | ||
statements: vec![transform.set_discr(VariantIdx::new(POISONED), source_info)], | ||
terminator: Some(Terminator { source_info, kind: TerminatorKind::Resume }), | ||
is_cleanup: true, | ||
}); | ||
|
||
for (idx, block) in body.basic_blocks_mut().iter_enumerated_mut() { | ||
let source_info = block.terminator().source_info; | ||
|
||
if let TerminatorKind::Resume = block.terminator().kind { | ||
// An existing `Resume` terminator is redirected to jump to our dedicated | ||
// "poisoning block" above. | ||
if idx != poison_block { | ||
*block.terminator_mut() = Terminator { | ||
source_info, | ||
kind: TerminatorKind::Goto { target: poison_block }, | ||
}; | ||
} | ||
} else if !block.is_cleanup { | ||
// Any terminators that *can* unwind but don't have an unwind target set are also | ||
// pointed at our poisoning block (unless they're part of the cleanup path). | ||
if let Some(unwind @ None) = block.terminator_mut().unwind_mut() { | ||
*unwind = Some(poison_block); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1015,8 +1098,20 @@ fn create_generator_resume_function<'tcx>( | |
|
||
// Panic when resumed on the returned or poisoned state | ||
let generator_kind = body.generator_kind.unwrap(); | ||
cases.insert(1, (RETURNED, insert_panic_block(tcx, body, ResumedAfterReturn(generator_kind)))); | ||
cases.insert(2, (POISONED, insert_panic_block(tcx, body, ResumedAfterPanic(generator_kind)))); | ||
|
||
if can_unwind { | ||
cases.insert( | ||
1, | ||
(POISONED, insert_panic_block(tcx, body, ResumedAfterPanic(generator_kind))), | ||
); | ||
} | ||
|
||
if can_return { | ||
cases.insert( | ||
1, | ||
(RETURNED, insert_panic_block(tcx, body, ResumedAfterReturn(generator_kind))), | ||
); | ||
} | ||
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. Maybe swap the order of |
||
|
||
insert_switch(body, cases, &transform, TerminatorKind::Unreachable); | ||
|
||
|
@@ -1200,6 +1295,8 @@ impl<'tcx> MirPass<'tcx> for StateTransform { | |
let (remap, layout, storage_liveness) = | ||
compute_layout(tcx, source, &upvars, interior, movable, body); | ||
|
||
let can_return = can_return(tcx, body); | ||
|
||
// Run the transformation which converts Places from Local to generator struct | ||
// accesses for locals in `remap`. | ||
// It also rewrites `return x` and `yield y` as writing a new generator state and returning | ||
|
@@ -1243,6 +1340,6 @@ impl<'tcx> MirPass<'tcx> for StateTransform { | |
body.generator_drop = Some(box drop_shim); | ||
|
||
// Create the Generator::resume function | ||
create_generator_resume_function(tcx, transform, def_id, source, body); | ||
create_generator_resume_function(tcx, transform, def_id, source, body, can_return); | ||
} | ||
} |
This file contains 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,34 @@ | ||
//! Tests that generators that cannot return or unwind don't have unnecessary | ||
//! panic branches. | ||
|
||
// compile-flags: -Zno-landing-pads | ||
|
||
#![feature(generators, generator_trait)] | ||
|
||
struct HasDrop; | ||
|
||
impl Drop for HasDrop { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn callee() {} | ||
|
||
fn main() { | ||
let _gen = |_x: u8| { | ||
let _d = HasDrop; | ||
loop { | ||
yield; | ||
callee(); | ||
} | ||
}; | ||
} | ||
|
||
// END RUST SOURCE | ||
|
||
// START rustc.main-{{closure}}.generator_resume.0.mir | ||
// bb0: { | ||
// ... | ||
// switchInt(move _11) -> [0u32: bb1, 3u32: bb5, otherwise: bb6]; | ||
// } | ||
// ... | ||
// END rustc.main-{{closure}}.generator_resume.0.mir |
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.
If
can_unwind
andcan_return
, this will overwrite thePOISONED
case.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.
insert
on aVec
can't overwrite.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.
It should be moved to index 2 though, right?
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.
Ah, missed that reply
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.
I thought
cases
was anFxHashMap
.