Skip to content

Commit 7749591

Browse files
committed
Replace hash map with IndexVec for liveness data.
Utilize IndexVec::push to avoid redundant object creation.
1 parent a58b1ed commit 7749591

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/librustc_mir/transform/generator.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ struct TransformVisitor<'tcx> {
210210
remap: FxHashMap<Local, (Ty<'tcx>, VariantIdx, usize)>,
211211

212212
// A map from a suspension point in a block to the locals which have live storage at that point
213-
// FIXME(eddyb) This should use `IndexVec<BasicBlock, Option<_>>`.
214-
storage_liveness: FxHashMap<BasicBlock, liveness::LiveVarSet>,
213+
storage_liveness: IndexVec<BasicBlock, Option<liveness::LiveVarSet>>,
215214

216215
// A list of suspension points, generated during the transform
217216
suspension_points: Vec<SuspensionPoint<'tcx>>,
@@ -338,7 +337,7 @@ impl MutVisitor<'tcx> for TransformVisitor<'tcx> {
338337
resume,
339338
resume_arg,
340339
drop,
341-
storage_liveness: self.storage_liveness.get(&block).unwrap().clone(),
340+
storage_liveness: self.storage_liveness[block].clone().unwrap(),
342341
});
343342

344343
VariantIdx::new(state)
@@ -404,8 +403,7 @@ fn replace_local<'tcx>(
404403
is_block_tail: None,
405404
local_info: LocalInfo::Other,
406405
};
407-
let new_local = Local::new(body.local_decls.len());
408-
body.local_decls.push(new_decl);
406+
let new_local = body.local_decls.push(new_decl);
409407
body.local_decls.swap(local, new_local);
410408

411409
RenameLocalVisitor { from: local, to: new_local, tcx }.visit_body(body);
@@ -431,7 +429,7 @@ struct LivenessInfo {
431429

432430
/// For every suspending block, the locals which are storage-live across
433431
/// that suspension point.
434-
storage_liveness: FxHashMap<BasicBlock, liveness::LiveVarSet>,
432+
storage_liveness: IndexVec<BasicBlock, Option<liveness::LiveVarSet>>,
435433
}
436434

437435
fn locals_live_across_suspend_points(
@@ -472,7 +470,7 @@ fn locals_live_across_suspend_points(
472470
let mut liveness = liveness::liveness_of_locals(body);
473471
liveness::dump_mir(tcx, "generator_liveness", source, body_ref, &liveness);
474472

475-
let mut storage_liveness_map = FxHashMap::default();
473+
let mut storage_liveness_map = IndexVec::from_elem(None, body.basic_blocks());
476474
let mut live_locals_at_suspension_points = Vec::new();
477475

478476
for (block, data) in body.basic_blocks().iter_enumerated() {
@@ -502,7 +500,7 @@ fn locals_live_across_suspend_points(
502500

503501
// Store the storage liveness for later use so we can restore the state
504502
// after a suspension point
505-
storage_liveness_map.insert(block, storage_liveness);
503+
storage_liveness_map[block] = Some(storage_liveness);
506504

507505
requires_storage_cursor.seek_before(loc);
508506
let storage_required = requires_storage_cursor.get().clone();
@@ -690,7 +688,7 @@ fn compute_layout<'tcx>(
690688
) -> (
691689
FxHashMap<Local, (Ty<'tcx>, VariantIdx, usize)>,
692690
GeneratorLayout<'tcx>,
693-
FxHashMap<BasicBlock, liveness::LiveVarSet>,
691+
IndexVec<BasicBlock, Option<liveness::LiveVarSet>>,
694692
) {
695693
// Use a liveness analysis to compute locals which are live across a suspension point
696694
let LivenessInfo {
@@ -925,14 +923,12 @@ fn create_generator_drop_shim<'tcx>(
925923
}
926924

927925
fn insert_term_block<'tcx>(body: &mut Body<'tcx>, kind: TerminatorKind<'tcx>) -> BasicBlock {
928-
let term_block = BasicBlock::new(body.basic_blocks().len());
929926
let source_info = source_info(body);
930927
body.basic_blocks_mut().push(BasicBlockData {
931928
statements: Vec::new(),
932929
terminator: Some(Terminator { source_info, kind }),
933930
is_cleanup: false,
934-
});
935-
term_block
931+
})
936932
}
937933

938934
fn insert_panic_block<'tcx>(
@@ -1030,9 +1026,8 @@ fn create_generator_resume_function<'tcx>(
10301026

10311027
// Poison the generator when it unwinds
10321028
if can_unwind {
1033-
let poison_block = BasicBlock::new(body.basic_blocks().len());
10341029
let source_info = source_info(body);
1035-
body.basic_blocks_mut().push(BasicBlockData {
1030+
let poison_block = body.basic_blocks_mut().push(BasicBlockData {
10361031
statements: vec![transform.set_discr(VariantIdx::new(POISONED), source_info)],
10371032
terminator: Some(Terminator { source_info, kind: TerminatorKind::Resume }),
10381033
is_cleanup: true,
@@ -1105,21 +1100,19 @@ fn source_info(body: &Body<'_>) -> SourceInfo {
11051100
fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
11061101
let return_block = insert_term_block(body, TerminatorKind::Return);
11071102

1108-
// Create a block to destroy an unresumed generators. This can only destroy upvars.
1109-
let drop_clean = BasicBlock::new(body.basic_blocks().len());
11101103
let term = TerminatorKind::Drop {
11111104
location: Place::from(SELF_ARG),
11121105
target: return_block,
11131106
unwind: None,
11141107
};
11151108
let source_info = source_info(body);
1109+
1110+
// Create a block to destroy an unresumed generators. This can only destroy upvars.
11161111
body.basic_blocks_mut().push(BasicBlockData {
11171112
statements: Vec::new(),
11181113
terminator: Some(Terminator { source_info, kind: term }),
11191114
is_cleanup: false,
1120-
});
1121-
1122-
drop_clean
1115+
})
11231116
}
11241117

11251118
/// An operation that can be performed on a generator.
@@ -1151,7 +1144,6 @@ fn create_cases<'tcx>(
11511144
.filter_map(|point| {
11521145
// Find the target for this suspension point, if applicable
11531146
operation.target_block(point).map(|target| {
1154-
let block = BasicBlock::new(body.basic_blocks().len());
11551147
let mut statements = Vec::new();
11561148

11571149
// Create StorageLive instructions for locals with live storage
@@ -1186,7 +1178,7 @@ fn create_cases<'tcx>(
11861178
}
11871179

11881180
// Then jump to the real target
1189-
body.basic_blocks_mut().push(BasicBlockData {
1181+
let block = body.basic_blocks_mut().push(BasicBlockData {
11901182
statements,
11911183
terminator: Some(Terminator {
11921184
source_info,

0 commit comments

Comments
 (0)