Skip to content

Commit dff167f

Browse files
authored
Rollup merge of rust-lang#147858 - yotamofek:pr/mir/coroutine-layout-opt, r=cjgillot
Micro-optimization attempt in coroutine layout computation In `compute_layout`, there were a bunch of collections (`IndexVec`s) that were being created by `push`ing in a loop, instead of a, hopefully, more performant usage of iterator combinators. [Second commit](rust-lang@6f682c2) is just a small cleanup. I'd love a perf run to see if this shows up in benchmarks.
2 parents 0a9570f + a67c615 commit dff167f

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@
5252
5353
mod by_move_body;
5454
mod drop;
55-
use std::{iter, ops};
55+
use std::ops;
5656

5757
pub(super) use by_move_body::coroutine_by_move_body_def_id;
5858
use drop::{
5959
cleanup_async_drops, create_coroutine_drop_shim, create_coroutine_drop_shim_async,
6060
create_coroutine_drop_shim_proxy_async, elaborate_coroutine_drops, expand_async_drops,
6161
has_expandable_async_drops, insert_clean_drop,
6262
};
63+
use itertools::izip;
6364
use rustc_abi::{FieldIdx, VariantIdx};
6465
use rustc_data_structures::fx::FxHashSet;
6566
use rustc_errors::pluralize;
@@ -752,53 +753,53 @@ fn locals_live_across_suspend_points<'tcx>(
752753
let mut live_locals_at_any_suspension_point = DenseBitSet::new_empty(body.local_decls.len());
753754

754755
for (block, data) in body.basic_blocks.iter_enumerated() {
755-
if let TerminatorKind::Yield { .. } = data.terminator().kind {
756-
let loc = Location { block, statement_index: data.statements.len() };
757-
758-
liveness.seek_to_block_end(block);
759-
let mut live_locals = liveness.get().clone();
760-
761-
if !movable {
762-
// The `liveness` variable contains the liveness of MIR locals ignoring borrows.
763-
// This is correct for movable coroutines since borrows cannot live across
764-
// suspension points. However for immovable coroutines we need to account for
765-
// borrows, so we conservatively assume that all borrowed locals are live until
766-
// we find a StorageDead statement referencing the locals.
767-
// To do this we just union our `liveness` result with `borrowed_locals`, which
768-
// contains all the locals which has been borrowed before this suspension point.
769-
// If a borrow is converted to a raw reference, we must also assume that it lives
770-
// forever. Note that the final liveness is still bounded by the storage liveness
771-
// of the local, which happens using the `intersect` operation below.
772-
borrowed_locals_cursor2.seek_before_primary_effect(loc);
773-
live_locals.union(borrowed_locals_cursor2.get());
774-
}
756+
let TerminatorKind::Yield { .. } = data.terminator().kind else { continue };
757+
758+
let loc = Location { block, statement_index: data.statements.len() };
759+
760+
liveness.seek_to_block_end(block);
761+
let mut live_locals = liveness.get().clone();
762+
763+
if !movable {
764+
// The `liveness` variable contains the liveness of MIR locals ignoring borrows.
765+
// This is correct for movable coroutines since borrows cannot live across
766+
// suspension points. However for immovable coroutines we need to account for
767+
// borrows, so we conservatively assume that all borrowed locals are live until
768+
// we find a StorageDead statement referencing the locals.
769+
// To do this we just union our `liveness` result with `borrowed_locals`, which
770+
// contains all the locals which has been borrowed before this suspension point.
771+
// If a borrow is converted to a raw reference, we must also assume that it lives
772+
// forever. Note that the final liveness is still bounded by the storage liveness
773+
// of the local, which happens using the `intersect` operation below.
774+
borrowed_locals_cursor2.seek_before_primary_effect(loc);
775+
live_locals.union(borrowed_locals_cursor2.get());
776+
}
775777

776-
// Store the storage liveness for later use so we can restore the state
777-
// after a suspension point
778-
storage_live.seek_before_primary_effect(loc);
779-
storage_liveness_map[block] = Some(storage_live.get().clone());
778+
// Store the storage liveness for later use so we can restore the state
779+
// after a suspension point
780+
storage_live.seek_before_primary_effect(loc);
781+
storage_liveness_map[block] = Some(storage_live.get().clone());
780782

781-
// Locals live are live at this point only if they are used across
782-
// suspension points (the `liveness` variable)
783-
// and their storage is required (the `storage_required` variable)
784-
requires_storage_cursor.seek_before_primary_effect(loc);
785-
live_locals.intersect(requires_storage_cursor.get());
783+
// Locals live are live at this point only if they are used across
784+
// suspension points (the `liveness` variable)
785+
// and their storage is required (the `storage_required` variable)
786+
requires_storage_cursor.seek_before_primary_effect(loc);
787+
live_locals.intersect(requires_storage_cursor.get());
786788

787-
// The coroutine argument is ignored.
788-
live_locals.remove(SELF_ARG);
789+
// The coroutine argument is ignored.
790+
live_locals.remove(SELF_ARG);
789791

790-
debug!("loc = {:?}, live_locals = {:?}", loc, live_locals);
792+
debug!(?loc, ?live_locals);
791793

792-
// Add the locals live at this suspension point to the set of locals which live across
793-
// any suspension points
794-
live_locals_at_any_suspension_point.union(&live_locals);
794+
// Add the locals live at this suspension point to the set of locals which live across
795+
// any suspension points
796+
live_locals_at_any_suspension_point.union(&live_locals);
795797

796-
live_locals_at_suspension_points.push(live_locals);
797-
source_info_at_suspension_points.push(data.terminator().source_info);
798-
}
798+
live_locals_at_suspension_points.push(live_locals);
799+
source_info_at_suspension_points.push(data.terminator().source_info);
799800
}
800801

801-
debug!("live_locals_anywhere = {:?}", live_locals_at_any_suspension_point);
802+
debug!(?live_locals_at_any_suspension_point);
802803
let saved_locals = CoroutineSavedLocals(live_locals_at_any_suspension_point);
803804

804805
// Renumber our liveness_map bitsets to include only the locals we are
@@ -999,8 +1000,8 @@ fn compute_layout<'tcx>(
9991000
} = liveness;
10001001

10011002
// Gather live local types and their indices.
1002-
let mut locals = IndexVec::<CoroutineSavedLocal, _>::new();
1003-
let mut tys = IndexVec::<CoroutineSavedLocal, _>::new();
1003+
let mut locals = IndexVec::<CoroutineSavedLocal, _>::with_capacity(saved_locals.domain_size());
1004+
let mut tys = IndexVec::<CoroutineSavedLocal, _>::with_capacity(saved_locals.domain_size());
10041005
for (saved_local, local) in saved_locals.iter_enumerated() {
10051006
debug!("coroutine saved local {:?} => {:?}", saved_local, local);
10061007

@@ -1034,38 +1035,39 @@ fn compute_layout<'tcx>(
10341035
// In debuginfo, these will correspond to the beginning (UNRESUMED) or end
10351036
// (RETURNED, POISONED) of the function.
10361037
let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span;
1037-
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [
1038+
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = IndexVec::with_capacity(
1039+
CoroutineArgs::RESERVED_VARIANTS + live_locals_at_suspension_points.len(),
1040+
);
1041+
variant_source_info.extend([
10381042
SourceInfo::outermost(body_span.shrink_to_lo()),
10391043
SourceInfo::outermost(body_span.shrink_to_hi()),
10401044
SourceInfo::outermost(body_span.shrink_to_hi()),
1041-
]
1042-
.iter()
1043-
.copied()
1044-
.collect();
1045+
]);
10451046

10461047
// Build the coroutine variant field list.
10471048
// Create a map from local indices to coroutine struct indices.
1048-
let mut variant_fields: IndexVec<VariantIdx, IndexVec<FieldIdx, CoroutineSavedLocal>> =
1049-
iter::repeat(IndexVec::new()).take(CoroutineArgs::RESERVED_VARIANTS).collect();
1049+
let mut variant_fields: IndexVec<VariantIdx, _> = IndexVec::from_elem_n(
1050+
IndexVec::new(),
1051+
CoroutineArgs::RESERVED_VARIANTS + live_locals_at_suspension_points.len(),
1052+
);
10501053
let mut remap = IndexVec::from_elem_n(None, saved_locals.domain_size());
1051-
for (suspension_point_idx, live_locals) in live_locals_at_suspension_points.iter().enumerate() {
1052-
let variant_index =
1053-
VariantIdx::from(CoroutineArgs::RESERVED_VARIANTS + suspension_point_idx);
1054-
let mut fields = IndexVec::new();
1055-
for (idx, saved_local) in live_locals.iter().enumerate() {
1056-
fields.push(saved_local);
1054+
for (live_locals, &source_info_at_suspension_point, (variant_index, fields)) in izip!(
1055+
&live_locals_at_suspension_points,
1056+
&source_info_at_suspension_points,
1057+
variant_fields.iter_enumerated_mut().skip(CoroutineArgs::RESERVED_VARIANTS)
1058+
) {
1059+
*fields = live_locals.iter().collect();
1060+
for (idx, &saved_local) in fields.iter_enumerated() {
10571061
// Note that if a field is included in multiple variants, we will
10581062
// just use the first one here. That's fine; fields do not move
10591063
// around inside coroutines, so it doesn't matter which variant
10601064
// index we access them by.
1061-
let idx = FieldIdx::from_usize(idx);
10621065
remap[locals[saved_local]] = Some((tys[saved_local].ty, variant_index, idx));
10631066
}
1064-
variant_fields.push(fields);
1065-
variant_source_info.push(source_info_at_suspension_points[suspension_point_idx]);
1067+
variant_source_info.push(source_info_at_suspension_point);
10661068
}
1067-
debug!("coroutine variant_fields = {:?}", variant_fields);
1068-
debug!("coroutine storage_conflicts = {:#?}", storage_conflicts);
1069+
debug!(?variant_fields);
1070+
debug!(?storage_conflicts);
10691071

10701072
let mut field_names = IndexVec::from_elem(None, &tys);
10711073
for var in &body.var_debug_info {

0 commit comments

Comments
 (0)