1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This is the implementation of the pass which transforms generators into state machines.
13 //! MIR generation for generators creates a function which has a self argument which
14 //! passes by value. This argument is effectively a generator type which only contains upvars and
15 //! is only used for this argument inside the MIR for the generator.
16 //! It is passed by value to enable upvars to be moved out of it. Drop elaboration runs on that
17 //! MIR before this pass and creates drop flags for MIR locals.
18 //! It will also drop the generator argument (which only consists of upvars) if any of the upvars
19 //! are moved out of. This pass elaborates the drops of upvars / generator argument in the case
20 //! that none of the upvars were moved out of. This is because we cannot have any drops of this
21 //! generator in the MIR, since it is used to create the drop glue for the generator. We'd get
22 //! infinite recursion otherwise.
24 //! This pass creates the implementation for the Generator::resume function and the drop shim
25 //! for the generator based on the MIR input. It converts the generator argument from Self to
26 //! &mut Self adding derefs in the MIR as needed. It computes the final layout of the generator
27 //! struct which looks like this:
28 //! First upvars are stored
29 //! It is followed by the generator state field.
30 //! Then finally the MIR locals which are live across a suspension point are stored.
32 //! struct Generator {
38 //! This pass computes the meaning of the state field and the MIR locals which are live
39 //! across a suspension point. There are however two hardcoded generator states:
40 //! 0 - Generator have not been resumed yet
41 //! 1 - Generator has been poisoned
43 //! It also rewrites `return x` and `yield y` as setting a new generator state and returning
44 //! GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
45 //! MIR locals which are live across a suspension point are moved to the generator struct
46 //! with references to them being updated with references to the generator struct.
48 //! The pass creates two functions which have a switch on the generator state giving
49 //! the action to take.
51 //! One of them is the implementation of Generator::resume.
52 //! For generators which have already returned it panics.
53 //! For generators with state 0 (unresumed) it starts the execution of the generator.
54 //! For generators with state 1 (poisoned) it panics.
55 //! Otherwise it continues the execution from the last suspension point.
57 //! The other function is the drop glue for the generator.
58 //! For generators which have already returned it does nothing.
59 //! For generators with state 0 (unresumed) it drops the upvars of the generator.
60 //! For generators with state 1 (poisoned) it does nothing.
61 //! Otherwise it drops all the values in scope at the last suspension point.
64 use rustc::hir::def_id::DefId;
65 use rustc::middle::const_val::ConstVal;
67 use rustc::mir::transform::{MirPass, MirSource};
68 use rustc::mir::visit::{LvalueContext, MutVisitor};
69 use rustc::ty::{self, TyCtxt, AdtDef, Ty, GeneratorInterior};
70 use rustc::ty::subst::{Kind, Substs};
73 use rustc_const_math::ConstInt;
74 use rustc_data_structures::indexed_vec::Idx;
75 use std::collections::HashMap;
79 use transform::simplify;
80 use transform::no_landing_pads::no_landing_pads;
82 pub struct StateTransform;
84 struct RenameLocalVisitor {
89 impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor {
90 fn visit_local(&mut self,
92 _: LvalueContext<'tcx>,
94 if *local == self.from {
100 struct DerefArgVisitor;
102 impl<'tcx> MutVisitor<'tcx> for DerefArgVisitor {
103 fn visit_local(&mut self,
105 _: LvalueContext<'tcx>,
107 assert_ne!(*local, self_arg());
110 fn visit_lvalue(&mut self,
111 lvalue: &mut Lvalue<'tcx>,
112 context: LvalueContext<'tcx>,
113 location: Location) {
114 if *lvalue == Lvalue::Local(self_arg()) {
115 *lvalue = Lvalue::Projection(Box::new(Projection {
116 base: lvalue.clone(),
117 elem: ProjectionElem::Deref,
120 self.super_lvalue(lvalue, context, location);
125 fn self_arg() -> Local {
129 struct TransformVisitor<'a, 'tcx: 'a> {
130 tcx: TyCtxt<'a, 'tcx, 'tcx>,
131 state_adt_ref: &'tcx AdtDef,
132 state_substs: &'tcx Substs<'tcx>,
134 // The index of the generator state in the generator struct
137 // Mapping from Local to (type of local, generator struct index)
138 remap: HashMap<Local, (Ty<'tcx>, usize)>,
140 // The number of generator states. 0 is unresumed, 1 is poisoned. So this is initialized to 2
141 bb_target_count: u32,
143 // Map from a (which block to resume execution at, which block to use to drop the generator)
144 // to a generator state
145 bb_targets: HashMap<(BasicBlock, Option<BasicBlock>), u32>,
147 // The original RETURN_POINTER local
148 new_ret_local: Local,
150 // The block to resume execution when for Return
151 return_block: BasicBlock,
154 impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
155 // Make a GeneratorState rvalue
156 fn make_state(&self, idx: usize, val: Operand<'tcx>) -> Rvalue<'tcx> {
157 let adt = AggregateKind::Adt(self.state_adt_ref, idx, self.state_substs, None);
158 Rvalue::Aggregate(box adt, vec![val])
161 // Create a Lvalue referencing a generator struct field
162 fn make_field(&self, idx: usize, ty: Ty<'tcx>) -> Lvalue<'tcx> {
163 let base = Lvalue::Local(self_arg());
164 let field = Projection {
166 elem: ProjectionElem::Field(Field::new(idx), ty),
168 Lvalue::Projection(Box::new(field))
171 // Create a statement which changes the generator state
172 fn set_state(&self, state_disc: u32, source_info: SourceInfo) -> Statement<'tcx> {
173 let state = self.make_field(self.state_field, self.tcx.types.u32);
174 let val = Operand::Constant(box Constant {
175 span: source_info.span,
176 ty: self.tcx.types.u32,
177 literal: Literal::Value {
178 value: self.tcx.mk_const(ty::Const {
179 val: ConstVal::Integral(ConstInt::U32(state_disc)),
180 ty: self.tcx.types.u32
186 kind: StatementKind::Assign(state, Rvalue::Use(val)),
191 impl<'a, 'tcx> MutVisitor<'tcx> for TransformVisitor<'a, 'tcx> {
192 fn visit_local(&mut self,
194 _: LvalueContext<'tcx>,
196 assert_eq!(self.remap.get(local), None);
199 fn visit_lvalue(&mut self,
200 lvalue: &mut Lvalue<'tcx>,
201 context: LvalueContext<'tcx>,
202 location: Location) {
203 if let Lvalue::Local(l) = *lvalue {
204 // Replace an Local in the remap with a generator struct access
205 if let Some(&(ty, idx)) = self.remap.get(&l) {
206 *lvalue = self.make_field(idx, ty);
209 self.super_lvalue(lvalue, context, location);
213 fn visit_basic_block_data(&mut self,
215 data: &mut BasicBlockData<'tcx>) {
216 // Remove StorageLive and StorageDead statements for remapped locals
217 data.retain_statements(|s| {
219 StatementKind::StorageLive(l) | StatementKind::StorageDead(l) => {
220 !self.remap.contains_key(&l)
226 let ret_val = match data.terminator().kind {
227 TerminatorKind::Return => Some((1,
229 Operand::Consume(Lvalue::Local(self.new_ret_local)),
231 TerminatorKind::Yield { ref value, resume, drop } => Some((0,
238 if let Some((state_idx, resume, v, drop)) = ret_val {
240 let bb_targets = &mut self.bb_targets;
241 let bb_target = &mut self.bb_target_count;
242 *bb_targets.entry((resume, drop)).or_insert_with(|| {
243 let target = *bb_target;
244 *bb_target = target.checked_add(1).unwrap();
248 let source_info = data.terminator().source_info;
249 data.statements.push(self.set_state(bb_idx, source_info));
250 data.statements.push(Statement {
252 kind: StatementKind::Assign(Lvalue::Local(RETURN_POINTER),
253 self.make_state(state_idx, v)),
255 data.terminator.as_mut().unwrap().kind = TerminatorKind::Return;
258 self.super_basic_block_data(block, data);
262 fn make_generator_state_argument_indirect<'a, 'tcx>(
263 tcx: TyCtxt<'a, 'tcx, 'tcx>,
265 mir: &mut Mir<'tcx>) {
266 let gen_ty = mir.local_decls.raw[1].ty;
268 let region = ty::ReFree(ty::FreeRegion {
270 bound_region: ty::BoundRegion::BrEnv,
273 let region = tcx.mk_region(region);
275 let ref_gen_ty = tcx.mk_ref(region, ty::TypeAndMut {
277 mutbl: hir::MutMutable
280 // Replace the by value generator argument
281 mir.local_decls.raw[1].ty = ref_gen_ty;
283 // Add a deref to accesses of the generator state
284 DerefArgVisitor.visit_mir(mir);
287 fn replace_result_variable<'tcx>(ret_ty: Ty<'tcx>,
288 mir: &mut Mir<'tcx>) -> Local {
289 let source_info = SourceInfo {
291 scope: ARGUMENT_VISIBILITY_SCOPE,
294 let new_ret = LocalDecl {
295 mutability: Mutability::Mut,
300 is_user_variable: false,
302 let new_ret_local = Local::new(mir.local_decls.len());
303 mir.local_decls.push(new_ret);
304 mir.local_decls.swap(0, new_ret_local.index());
307 from: RETURN_POINTER,
314 fn locals_live_across_suspend_points<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
316 source: MirSource) -> liveness::LocalSet {
317 let mut set = liveness::LocalSet::new_empty(mir.local_decls.len());
318 let result = liveness::liveness_of_locals(mir);
319 liveness::dump_mir(tcx, "generator_liveness", source, mir, &result);
321 for (block, data) in mir.basic_blocks().iter_enumerated() {
322 if let TerminatorKind::Yield { .. } = data.terminator().kind {
323 set.union(&result.outs[block]);
327 // The generator argument is ignored
328 set.remove(&self_arg());
333 fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
335 interior: GeneratorInterior<'tcx>,
337 -> (HashMap<Local, (Ty<'tcx>, usize)>, GeneratorLayout<'tcx>)
339 // Use a liveness analysis to compute locals which are live across a suspension point
340 let live_locals = locals_live_across_suspend_points(tcx, mir, source);
342 // Erase regions from the types passed in from typeck so we can compare them with
344 let allowed = tcx.erase_regions(&interior.as_slice());
346 for (local, decl) in mir.local_decls.iter_enumerated() {
347 // Ignore locals which are internal or not live
348 if !live_locals.contains(&local) || decl.internal {
352 // Sanity check that typeck knows about the type of locals which are
353 // live across a suspension point
354 if !allowed.contains(&decl.ty) {
356 "Broken MIR: generator contains type {} in MIR, \
357 but typeck only knows about {}",
363 let upvar_len = mir.upvar_decls.len();
364 let dummy_local = LocalDecl::new_internal(tcx.mk_nil(), mir.span);
366 // Gather live locals and their indices replacing values in mir.local_decls with a dummy
367 // to avoid changing local indices
368 let live_decls = live_locals.iter().map(|local| {
369 let var = mem::replace(&mut mir.local_decls[local], dummy_local.clone());
373 // Create a map from local indices to generator struct indices.
374 // These are offset by (upvar_len + 1) because of fields which comes before locals.
375 // We also create a vector of the LocalDecls of these locals.
376 let (remap, vars) = live_decls.enumerate().map(|(idx, (local, var))| {
377 ((local, (var.ty, upvar_len + 1 + idx)), var)
380 let layout = GeneratorLayout {
387 fn insert_entry_point<'tcx>(mir: &mut Mir<'tcx>,
388 block: BasicBlockData<'tcx>) {
389 mir.basic_blocks_mut().raw.insert(0, block);
391 let blocks = mir.basic_blocks_mut().iter_mut();
393 for target in blocks.flat_map(|b| b.terminator_mut().successors_mut()) {
394 *target = BasicBlock::new(target.index() + 1);
398 fn elaborate_generator_drops<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
400 mir: &mut Mir<'tcx>) {
401 use util::elaborate_drops::{elaborate_drop, Unwind};
402 use util::patch::MirPatch;
403 use shim::DropShimElaborator;
405 // Note that `elaborate_drops` only drops the upvars of a generator, and
406 // this is ok because `open_drop` can only be reached within that own
407 // generator's resume function.
409 let param_env = tcx.param_env(def_id);
410 let gen = self_arg();
412 for block in mir.basic_blocks().indices() {
413 let (target, unwind, source_info) = match mir.basic_blocks()[block].terminator() {
416 kind: TerminatorKind::Drop {
417 location: Lvalue::Local(local),
421 } if local == gen => (target, unwind, source_info),
424 let unwind = if let Some(unwind) = unwind {
430 let mut elaborator = DropShimElaborator {
432 patch: MirPatch::new(mir),
451 fn create_generator_drop_shim<'a, 'tcx>(
452 tcx: TyCtxt<'a, 'tcx, 'tcx>,
453 transform: &TransformVisitor<'a, 'tcx>,
458 drop_clean: BasicBlock) -> Mir<'tcx> {
459 let mut mir = mir.clone();
461 let source_info = SourceInfo {
463 scope: ARGUMENT_VISIBILITY_SCOPE,
466 let return_block = BasicBlock::new(mir.basic_blocks().len());
467 mir.basic_blocks_mut().push(BasicBlockData {
468 statements: Vec::new(),
469 terminator: Some(Terminator {
471 kind: TerminatorKind::Return,
476 let mut cases: Vec<_> = transform.bb_targets.iter().filter_map(|(&(_, u), &s)| {
480 cases.insert(0, (0, drop_clean));
482 // The poisoned state 1 falls through to the default case which is just to return
484 let switch = TerminatorKind::SwitchInt {
485 discr: Operand::Consume(transform.make_field(transform.state_field, tcx.types.u32)),
486 switch_ty: tcx.types.u32,
487 values: Cow::from(cases.iter().map(|&(i, _)| {
489 }).collect::<Vec<_>>()),
490 targets: cases.iter().map(|&(_, d)| d).chain(once(return_block)).collect(),
493 insert_entry_point(&mut mir, BasicBlockData {
494 statements: Vec::new(),
495 terminator: Some(Terminator {
502 for block in mir.basic_blocks_mut() {
503 let kind = &mut block.terminator_mut().kind;
504 if let TerminatorKind::GeneratorDrop = *kind {
505 *kind = TerminatorKind::Return;
509 // Replace the return variable
510 let source_info = SourceInfo {
512 scope: ARGUMENT_VISIBILITY_SCOPE,
515 mir.return_ty = tcx.mk_nil();
516 mir.local_decls[RETURN_POINTER] = LocalDecl {
517 mutability: Mutability::Mut,
522 is_user_variable: false,
525 make_generator_state_argument_indirect(tcx, def_id, &mut mir);
527 // Change the generator argument from &mut to *mut
528 mir.local_decls[self_arg()] = LocalDecl {
529 mutability: Mutability::Mut,
530 ty: tcx.mk_ptr(ty::TypeAndMut {
532 mutbl: hir::Mutability::MutMutable,
537 is_user_variable: false,
540 no_landing_pads(tcx, &mut mir);
542 // Make sure we remove dead blocks to remove
543 // unrelated code from the resume part of the function
544 simplify::remove_dead_blocks(&mut mir);
546 dump_mir(tcx, None, "generator_drop", &0, source, &mut mir);
551 fn insert_panic_on_resume_after_return<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
552 mir: &mut Mir<'tcx>) {
553 let assert_block = BasicBlock::new(mir.basic_blocks().len());
554 let term = TerminatorKind::Assert {
555 cond: Operand::Constant(box Constant {
558 literal: Literal::Value {
559 value: tcx.mk_const(ty::Const {
560 val: ConstVal::Bool(false),
566 msg: AssertMessage::GeneratorResumedAfterReturn,
567 target: assert_block,
571 let source_info = SourceInfo {
573 scope: ARGUMENT_VISIBILITY_SCOPE,
576 mir.basic_blocks_mut().push(BasicBlockData {
577 statements: Vec::new(),
578 terminator: Some(Terminator {
586 fn create_generator_resume_function<'a, 'tcx>(
587 tcx: TyCtxt<'a, 'tcx, 'tcx>,
588 mut transform: TransformVisitor<'a, 'tcx>,
591 mir: &mut Mir<'tcx>) {
592 // Poison the generator when it unwinds
593 for block in mir.basic_blocks_mut() {
594 let source_info = block.terminator().source_info;
595 if let &TerminatorKind::Resume = &block.terminator().kind {
596 block.statements.push(transform.set_state(1, source_info));
600 let source_info = SourceInfo {
602 scope: ARGUMENT_VISIBILITY_SCOPE,
605 let poisoned_block = BasicBlock::new(mir.basic_blocks().len());
607 let term = TerminatorKind::Assert {
608 cond: Operand::Constant(box Constant {
611 literal: Literal::Value {
612 value: tcx.mk_const(ty::Const {
613 val: ConstVal::Bool(false),
619 msg: AssertMessage::GeneratorResumedAfterPanic,
620 target: transform.return_block,
624 mir.basic_blocks_mut().push(BasicBlockData {
625 statements: Vec::new(),
626 terminator: Some(Terminator {
633 transform.bb_targets.insert((poisoned_block, None), 1);
635 let switch = TerminatorKind::SwitchInt {
636 discr: Operand::Consume(transform.make_field(transform.state_field, tcx.types.u32)),
637 switch_ty: tcx.types.u32,
638 values: Cow::from(transform.bb_targets.values().map(|&i| {
640 }).collect::<Vec<_>>()),
641 targets: transform.bb_targets.keys()
643 .chain(once(transform.return_block))
647 insert_entry_point(mir, BasicBlockData {
648 statements: Vec::new(),
649 terminator: Some(Terminator {
656 make_generator_state_argument_indirect(tcx, def_id, mir);
658 no_landing_pads(tcx, mir);
660 // Make sure we remove dead blocks to remove
661 // unrelated code from the drop part of the function
662 simplify::remove_dead_blocks(mir);
664 dump_mir(tcx, None, "generator_resume", &0, source, mir);
667 fn insert_clean_drop<'a, 'tcx>(mir: &mut Mir<'tcx>) -> BasicBlock {
668 let source_info = SourceInfo {
670 scope: ARGUMENT_VISIBILITY_SCOPE,
673 let return_block = BasicBlock::new(mir.basic_blocks().len());
674 mir.basic_blocks_mut().push(BasicBlockData {
675 statements: Vec::new(),
676 terminator: Some(Terminator {
678 kind: TerminatorKind::Return,
683 // Create a block to destroy an unresumed generators. This can only destroy upvars.
684 let drop_clean = BasicBlock::new(mir.basic_blocks().len());
685 let term = TerminatorKind::Drop {
686 location: Lvalue::Local(self_arg()),
687 target: return_block,
690 mir.basic_blocks_mut().push(BasicBlockData {
691 statements: Vec::new(),
692 terminator: Some(Terminator {
702 impl MirPass for StateTransform {
703 fn run_pass<'a, 'tcx>(&self,
704 tcx: TyCtxt<'a, 'tcx, 'tcx>,
706 mir: &mut Mir<'tcx>) {
707 let yield_ty = if let Some(yield_ty) = mir.yield_ty {
710 // This only applies to generators
714 assert!(mir.generator_drop.is_none());
716 let node_id = source.item_id();
717 let def_id = tcx.hir.local_def_id(source.item_id());
718 let hir_id = tcx.hir.node_to_hir_id(node_id);
720 // Get the interior types which typeck computed
721 let interior = *tcx.typeck_tables_of(def_id).generator_interiors().get(hir_id).unwrap();
723 // The first argument is the generator type passed by value
724 let gen_ty = mir.local_decls.raw[1].ty;
726 // Compute GeneratorState<yield_ty, return_ty>
727 let state_did = tcx.lang_items().gen_state().unwrap();
728 let state_adt_ref = tcx.adt_def(state_did);
729 let state_substs = tcx.mk_substs([Kind::from(yield_ty),
730 Kind::from(mir.return_ty)].iter());
731 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
733 // We rename RETURN_POINTER which has type mir.return_ty to new_ret_local
734 // RETURN_POINTER then is a fresh unused local with type ret_ty.
735 let new_ret_local = replace_result_variable(ret_ty, mir);
737 // Extract locals which are live across suspension point into `layout`
738 // `remap` gives a mapping from local indices onto generator struct indices
739 let (remap, layout) = compute_layout(tcx, source, interior, mir);
741 let state_field = mir.upvar_decls.len();
743 let mut bb_targets = HashMap::new();
745 // If we jump to the entry point, we should go to the initial 0 generator state.
746 // FIXME: Could this result in the need for destruction for state 0?
747 bb_targets.insert((BasicBlock::new(0), None), 0);
749 // Run the transformation which converts Lvalues from Local to generator struct
750 // accesses for locals in `remap`.
751 // It also rewrites `return x` and `yield y` as writing a new generator state and returning
752 // GeneratorState::Complete(x) and GeneratorState::Yielded(y) respectively.
753 let mut transform = TransformVisitor {
763 // For returns we will resume execution at the next added basic block.
764 // This happens in `insert_panic_on_resume_after_return`
765 return_block: BasicBlock::new(mir.basic_blocks().len()),
767 transform.visit_mir(mir);
769 // Update our MIR struct to reflect the changed we've made
770 mir.return_ty = ret_ty;
773 mir.spread_arg = None;
774 mir.generator_layout = Some(layout);
776 // Panic if we resumed after returning
777 insert_panic_on_resume_after_return(tcx, mir);
779 // Insert `drop(generator_struct)` which is used to drop upvars for generators in
780 // the unresumed (0) state.
781 // This is expanded to a drop ladder in `elaborate_generator_drops`.
782 let drop_clean = insert_clean_drop(mir);
784 dump_mir(tcx, None, "generator_pre-elab", &0, source, mir);
786 // Expand `drop(generator_struct)` to a drop ladder which destroys upvars.
787 // If any upvars are moved out of, drop elaboration will handle upvar destruction.
788 // However we need to also elaborate the code generated by `insert_clean_drop`.
789 elaborate_generator_drops(tcx, def_id, mir);
791 dump_mir(tcx, None, "generator_post-transform", &0, source, mir);
793 // Create a copy of our MIR and use it to create the drop shim for the generator
794 let drop_shim = create_generator_drop_shim(tcx,
802 mir.generator_drop = Some(box drop_shim);
804 // Create the Generator::resume function
805 create_generator_resume_function(tcx, transform, def_id, source, mir);