Skip to content

Commit 54d707c

Browse files
Auto merge of #113382 - lqd:test-mcp510, r=<try>
[perf] test MCP510
2 parents 5ab6924 + b407ec1 commit 54d707c

File tree

7 files changed

+557
-22
lines changed

7 files changed

+557
-22
lines changed

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, HasLocalDecls, Loc
77
use rustc_middle::traits::query::DropckOutlivesResult;
88
use rustc_middle::ty::relate::Relate;
99
use rustc_middle::ty::{Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
10-
use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
10+
use rustc_mir_dataflow::impls::{FilteringMovePathIndexMapper, MaybeInitializedPlaces2};
1111
use rustc_mir_dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex};
1212
use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex};
13-
use rustc_mir_dataflow::{Analysis, ResultsCursor};
13+
use rustc_mir_dataflow::{Analysis, ResultsCursor, on_all_children_bits};
1414
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
1515
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1616
use rustc_trait_selection::traits::ObligationCtxt;
@@ -45,13 +45,29 @@ pub(super) fn trace<'tcx>(
4545
boring_locals: Vec<Local>,
4646
) {
4747
let local_use_map = &LocalUseMap::build(&relevant_live_locals, location_map, typeck.body);
48+
49+
let mut dense_mpis = smallvec::SmallVec::new();
50+
for &local in relevant_live_locals.iter() {
51+
let Some(mpi) = move_data.rev_lookup.find_local(local) else { unreachable!() };
52+
53+
// We only compute initializedness in drop-liveness on locals with drop points.
54+
if local_use_map.drops(local).next().is_none() {
55+
continue;
56+
}
57+
58+
on_all_children_bits(move_data, mpi, |child| {
59+
dense_mpis.push(child);
60+
});
61+
}
62+
4863
let cx = LivenessContext {
4964
typeck,
5065
flow_inits: None,
5166
location_map,
5267
local_use_map,
5368
move_data,
5469
drop_data: FxIndexMap::default(),
70+
dense_mpis,
5571
};
5672

5773
let mut results = LivenessResults::new(cx);
@@ -81,11 +97,15 @@ struct LivenessContext<'a, 'typeck, 'tcx> {
8197

8298
/// Results of dataflow tracking which variables (and paths) have been
8399
/// initialized. Computed lazily when needed by drop-liveness.
84-
flow_inits: Option<ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>>,
100+
flow_inits: Option<
101+
ResultsCursor<'a, 'tcx, MaybeInitializedPlaces2<'a, 'tcx, FilteringMovePathIndexMapper>>,
102+
>,
85103

86104
/// Index indicating where each variable is assigned, used, or
87105
/// dropped.
88106
local_use_map: &'a LocalUseMap,
107+
108+
dense_mpis: smallvec::SmallVec<[MovePathIndex; 1]>,
89109
}
90110

91111
struct DropData<'tcx> {
@@ -468,7 +488,10 @@ impl<'a, 'typeck, 'tcx> LivenessContext<'a, 'typeck, 'tcx> {
468488
///
469489
/// This happens as part of the drop-liveness computation: it's the only place checking for
470490
/// maybe-initializedness of `MovePathIndex`es.
471-
fn flow_inits(&mut self) -> &mut ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>> {
491+
fn flow_inits(
492+
&mut self,
493+
) -> &mut ResultsCursor<'a, 'tcx, MaybeInitializedPlaces2<'a, 'tcx, FilteringMovePathIndexMapper>>
494+
{
472495
self.flow_inits.get_or_insert_with(|| {
473496
let tcx = self.typeck.tcx();
474497
let body = self.typeck.body;
@@ -484,9 +507,85 @@ impl<'a, 'typeck, 'tcx> LivenessContext<'a, 'typeck, 'tcx> {
484507
// a much, much smaller domain: in our benchmarks, when it's not zero (the most likely
485508
// case), there are a few dozens compared to e.g. thousands or tens of thousands of
486509
// locals and move paths.
487-
let flow_inits = MaybeInitializedPlaces::new(tcx, body, self.move_data)
510+
// eprintln!(
511+
// "computing flow_inits: {} MPIs total, {} relevant MPIs",
512+
// self.move_data.move_paths.len(),
513+
// self.dense_mpis.len()
514+
// );
515+
516+
// FIXME: use the sparse vec + bitset metadata trick instead of a map!
517+
518+
// let map_timer = std::time::Instant::now();
519+
// let mut sparse_map = FxIndexMap::default();
520+
// for (idx, &dense_idx) in self.dense_mpis.iter().enumerate() {
521+
// let sparse_idx = SparseMovePathIndex::from_usize(idx);
522+
// sparse_map.insert(dense_idx, sparse_idx);
523+
// }
524+
// // for local in body.args_iter() {
525+
// // let Some(mpi) = self.move_data.rev_lookup.find_local(local) else { unreachable!() };
526+
// // self.dense_mpis.push(mpi);
527+
// // map.insert(mpi, self.dense_mpis.len());
528+
// // }
529+
530+
// let map_len = sparse_map.len();
531+
// let mapper = FilteringMovePathIndexMapper { sparse_map };
532+
// let map_elapsed = map_timer.elapsed();
533+
534+
// // let mapper = rustc_mir_dataflow::impls::NoOpMapper;
535+
536+
// let sparse_bitset_timer = std::time::Instant::now();
537+
538+
// // let mut sparse_bitset = Sparse32::new(self.dense_mpis.len());
539+
// // // FIXME: do this in asc order to keep idxes stable and not shuffle the vec inside
540+
// // for &dense_idx in self.dense_mpis.iter() {
541+
// // sparse_bitset.insert(dense_idx);
542+
// // }
543+
544+
// let sparse_bitset = Sparse32::new(&self.dense_mpis);
545+
546+
// // let sparse_bitset_elapsed = sparse_bitset_timer.elapsed();
547+
// // let sparse_bitset_len = sparse_bitset.dense.len();
548+
549+
// // let sparse_bitset_timer = std::time::Instant::now();
550+
551+
// let mut sparse_bitset = Sparse::new(
552+
// 1 + self.dense_mpis.iter().max().unwrap().as_usize(),
553+
// self.dense_mpis.len(),
554+
// );
555+
// // FIXME: do this in asc order to keep idxes stable and not shuffle the vec inside
556+
// for &dense_idx in self.dense_mpis.iter() {
557+
// sparse_bitset.insert(dense_idx);
558+
// }
559+
// // also: move this into the sparse ctor, so that prefixes can be computed there after inserting stuff
560+
561+
// sparse_bitset.compute_prefixes();
562+
563+
// // let sparse_bitset_elapsed = sparse_bitset_timer.elapsed();
564+
// // let sparse_bitset_len = sparse_bitset.sparse.len();
565+
566+
// let mapper = sparse_bitset;
567+
568+
// let timer = std::time::Instant::now();
569+
let flow_inits = MaybeInitializedPlaces2::new(tcx, body, self.move_data)
570+
.filter_move_paths(&self.dense_mpis)
571+
// .with_mapper(mapper)
488572
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
489573
.into_results_cursor(body);
574+
// let elapsed = timer.elapsed();
575+
576+
// use std::sync::OnceLock;
577+
// static PROFILE: OnceLock<bool> = OnceLock::new();
578+
// if *PROFILE.get_or_init(|| std::env::var("LETSGO1").is_ok()) {
579+
// eprintln!(
580+
// "flow_inits took {:?} ns, map of {} took: {} ns, sparse bitset of {} took {} ns, {:?}",
581+
// elapsed.as_nanos(),
582+
// map_len,
583+
// map_elapsed.as_nanos(),
584+
// sparse_bitset_len,
585+
// sparse_bitset_elapsed.as_nanos(),
586+
// body.span,
587+
// );
588+
// }
490589
flow_inits
491590
})
492591
}
@@ -502,13 +601,24 @@ impl<'tcx> LivenessContext<'_, '_, 'tcx> {
502601
/// the cursor to the desired location.
503602
fn initialized_at_curr_loc(&mut self, mpi: MovePathIndex) -> bool {
504603
let flow_inits = self.flow_inits();
604+
let analysis = flow_inits.analysis();
605+
let idx = analysis
606+
.map_index(mpi)
607+
.unwrap_or_else(|| unreachable!("dataflow is somehow missing MPI {mpi:?}"));
505608
let state = flow_inits.get();
506-
if state.contains(mpi) {
609+
if state.contains(idx) {
507610
return true;
508611
}
509612

510-
let move_paths = &flow_inits.analysis().move_data().move_paths;
511-
move_paths[mpi].find_descendant(move_paths, |mpi| state.contains(mpi)).is_some()
613+
let move_paths = &analysis.move_data().move_paths;
614+
move_paths[mpi]
615+
.find_descendant(move_paths, |mpi| {
616+
let idx = analysis
617+
.map_index(mpi)
618+
.unwrap_or_else(|| unreachable!("dataflow is somehow missing MPI {mpi:?}"));
619+
state.contains(idx)
620+
})
621+
.is_some()
512622
}
513623

514624
/// Returns `true` if the local variable (or some part of it) is initialized in

compiler/rustc_mir_dataflow/src/framework/fmt.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,17 @@ where
250250
write!(f, "{}", ctxt.move_data().move_paths[*self])
251251
}
252252
}
253+
254+
impl<C> DebugWithContext<C> for crate::move_paths::SparseMovePathIndex {}
255+
impl<C> DebugWithContext<C> for u32 {}
256+
257+
// impl<'tcx, C> DebugWithContext<C> for crate::move_paths::SparseMovePathIndex
258+
// where
259+
// C: crate::move_paths::HasMoveData<'tcx> +
260+
// C: crate::impls::MovePathIndexMapper
261+
// {
262+
// fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263+
// let
264+
// write!(f, "{}", ctxt.move_data().move_paths[*self])
265+
// }
266+
// }

compiler/rustc_mir_dataflow/src/impls/initialized.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ use crate::{
2121

2222
// Used by both `MaybeInitializedPlaces` and `MaybeUninitializedPlaces`.
2323
pub struct MaybePlacesSwitchIntData<'tcx> {
24-
enum_place: mir::Place<'tcx>,
24+
pub(super) enum_place: mir::Place<'tcx>,
2525
discriminants: Vec<(VariantIdx, Discr<'tcx>)>,
2626
index: usize,
2727
}
2828

2929
impl<'tcx> MaybePlacesSwitchIntData<'tcx> {
3030
/// Creates a `SmallVec` mapping each target in `targets` to its `VariantIdx`.
31-
fn variants(&mut self, targets: &mir::SwitchTargets) -> SmallVec<[VariantIdx; 4]> {
31+
pub(super) fn variants(&mut self, targets: &mir::SwitchTargets) -> SmallVec<[VariantIdx; 4]> {
3232
self.index = 0;
3333
targets.all_values().iter().map(|value| self.next_discr(value.get())).collect()
3434
}
3535

3636
// The discriminant order in the `SwitchInt` targets should match the order yielded by
3737
// `AdtDef::discriminants`. We rely on this to match each discriminant in the targets to its
3838
// corresponding variant in linear time.
39-
fn next_discr(&mut self, value: u128) -> VariantIdx {
39+
pub(super) fn next_discr(&mut self, value: u128) -> VariantIdx {
4040
// An out-of-bounds abort will occur if the discriminant ordering isn't as described above.
4141
loop {
4242
let (variant, discr) = self.discriminants[self.index];
@@ -49,7 +49,7 @@ impl<'tcx> MaybePlacesSwitchIntData<'tcx> {
4949
}
5050

5151
impl<'tcx> MaybePlacesSwitchIntData<'tcx> {
52-
fn new(
52+
pub(super) fn new(
5353
tcx: TyCtxt<'tcx>,
5454
body: &Body<'tcx>,
5555
block: mir::BasicBlock,

0 commit comments

Comments
 (0)