Skip to content

Commit 93691b4

Browse files
committed
Support Yield in ops.
1 parent 746456d commit 93691b4

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
4646
return;
4747
}
4848

49+
// Avoid computing layout inside coroutines, since their `optimized_mir` is used for layout
50+
// computation, which can create a cycle.
51+
if body.coroutine.is_some() {
52+
return;
53+
}
54+
4955
// We want to have a somewhat linear runtime w.r.t. the number of statements/terminators.
5056
// Let's call this number `n`. Dataflow analysis has `O(h*n)` transfer function
5157
// applications, where `h` is the height of the lattice. Because the height of our lattice
@@ -241,9 +247,8 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
241247
TerminatorKind::Drop { place, .. } => {
242248
state.flood_with(place.as_ref(), &self.map, FlatSet::<Scalar>::BOTTOM);
243249
}
244-
TerminatorKind::Yield { .. } => {
245-
// They would have an effect, but are not allowed in this phase.
246-
bug!("encountered disallowed terminator");
250+
TerminatorKind::Yield { resume_arg, .. } => {
251+
state.flood_with(resume_arg.as_ref(), &self.map, FlatSet::<Scalar>::BOTTOM);
247252
}
248253
TerminatorKind::SwitchInt { discr, targets } => {
249254
return self.handle_switch_int(discr, targets, state);

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ impl WriteInfo {
639639
self.add_operand(&arg.node);
640640
}
641641
}
642+
TerminatorKind::Yield { resume_arg, value, .. } => {
643+
self.add_place(*resume_arg);
644+
self.add_operand(value);
645+
}
642646
TerminatorKind::InlineAsm { operands, .. } => {
643647
for asm_operand in operands {
644648
match asm_operand {
@@ -669,14 +673,12 @@ impl WriteInfo {
669673
| TerminatorKind::UnwindResume
670674
| TerminatorKind::UnwindTerminate(_)
671675
| TerminatorKind::Return
676+
| TerminatorKind::CoroutineDrop
672677
| TerminatorKind::Unreachable { .. } => (),
673678
TerminatorKind::Drop { .. } => {
674679
// `Drop`s create a `&mut` and so are not considered
675680
}
676-
TerminatorKind::Yield { .. }
677-
| TerminatorKind::CoroutineDrop
678-
| TerminatorKind::FalseEdge { .. }
679-
| TerminatorKind::FalseUnwind { .. } => {
681+
TerminatorKind::FalseEdge { .. } | TerminatorKind::FalseUnwind { .. } => {
680682
bug!("{:?} not found in this MIR phase", terminator)
681683
}
682684
}

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,14 +1767,18 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
17671767
}
17681768

17691769
fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, location: Location) {
1770-
if let Terminator { kind: TerminatorKind::Call { destination, .. }, .. } = terminator {
1771-
if let Some(local) = destination.as_local()
1772-
&& self.ssa.is_ssa(local)
1773-
{
1774-
let ty = self.local_decls[local].ty;
1775-
let opaque = self.new_opaque(ty);
1776-
self.assign(local, opaque);
1777-
}
1770+
let destination = match terminator.kind {
1771+
TerminatorKind::Call { destination, .. } => Some(destination),
1772+
TerminatorKind::Yield { resume_arg, .. } => Some(resume_arg),
1773+
_ => None,
1774+
};
1775+
if let Some(destination) = destination
1776+
&& let Some(local) = destination.as_local()
1777+
&& self.ssa.is_ssa(local)
1778+
{
1779+
let ty = self.local_decls[local].ty;
1780+
let opaque = self.new_opaque(ty);
1781+
self.assign(local, opaque);
17781782
}
17791783
// Function calls and ASM may invalidate (nested) derefs. We must handle them carefully.
17801784
// Currently, only preserving derefs for trivial terminators like SwitchInt and Goto.

compiler/rustc_mir_transform/src/jump_threading.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,9 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
612612
| TerminatorKind::Unreachable
613613
| TerminatorKind::CoroutineDrop => bug!("{term:?} has no terminators"),
614614
// Disallowed during optimizations.
615-
TerminatorKind::FalseEdge { .. }
616-
| TerminatorKind::FalseUnwind { .. }
617-
| TerminatorKind::Yield { .. } => bug!("{term:?} invalid"),
615+
TerminatorKind::FalseEdge { .. } | TerminatorKind::FalseUnwind { .. } => {
616+
bug!("{term:?} invalid")
617+
}
618618
// Cannot reason about inline asm.
619619
TerminatorKind::InlineAsm { .. } => return,
620620
// `SwitchInt` is handled specially.
@@ -623,6 +623,7 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
623623
TerminatorKind::Goto { .. } => None,
624624
// Flood the overwritten place, and progress through.
625625
TerminatorKind::Drop { place: destination, .. }
626+
| TerminatorKind::Yield { resume_arg: destination, .. }
626627
| TerminatorKind::Call { destination, .. } => Some(destination),
627628
// Ignore, as this can be a no-op at codegen time.
628629
TerminatorKind::Assert { .. } => None,

0 commit comments

Comments
 (0)