Skip to content

Commit 4ed3dc1

Browse files
committed
only benchmark forward single-pass dataflow
1 parent 0a04b31 commit 4ed3dc1

File tree

1 file changed

+77
-54
lines changed
  • compiler/rustc_borrowck/src

1 file changed

+77
-54
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
// ignore-tidy-filelength
44
// tidy-alphabetical-start
5+
#![allow(dead_code)]
56
#![allow(internal_features)]
67
#![doc(rust_logo)]
78
#![feature(assert_matches)]
@@ -24,12 +25,16 @@ use std::ops::{ControlFlow, Deref};
2425
use borrow_set::LocalsStateAtExit;
2526
use root_cx::BorrowCheckRootCtxt;
2627
use rustc_abi::FieldIdx;
28+
#[cfg(test)]
29+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2730
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2831
use rustc_data_structures::graph::dominators::Dominators;
2932
use rustc_errors::LintDiagnostic;
3033
use rustc_hir as hir;
3134
use rustc_hir::CRATE_HIR_ID;
3235
use rustc_hir::def_id::LocalDefId;
36+
#[cfg(test)]
37+
use rustc_index::bit_set::DenseBitSet;
3338
use rustc_index::bit_set::MixedBitSet;
3439
use rustc_index::{IndexSlice, IndexVec};
3540
use rustc_infer::infer::{
@@ -45,8 +50,7 @@ use rustc_mir_dataflow::impls::{EverInitializedPlaces, MaybeUninitializedPlaces}
4550
use rustc_mir_dataflow::move_paths::{
4651
InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,
4752
};
48-
use rustc_mir_dataflow::{Analysis, ResultsVisitor};
49-
// use rustc_mir_dataflow::{Analysis, Results, ResultsVisitor, visit_results};
53+
use rustc_mir_dataflow::{Analysis, Results, ResultsVisitor, visit_results};
5054
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
5155
use rustc_span::{ErrorGuaranteed, Span, Symbol};
5256
use smallvec::SmallVec;
@@ -605,64 +609,83 @@ fn do_mir_borrowck<'tcx>(
605609
// );
606610
// }
607611

608-
// if body.basic_blocks.is_cfg_cyclic() {
609-
if body.basic_blocks.thir_had_loops.unwrap_or_else(|| body.basic_blocks.is_cfg_cyclic()) {
610-
// let (mut flow_analysis, flow_entry_states) =
611-
// get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
612-
// visit_results(
613-
// body,
614-
// traversal::reverse_postorder(body).map(|(bb, _)| bb),
615-
// &mut flow_analysis,
616-
// &flow_entry_states,
617-
// &mut mbcx,
618-
// );
619-
620-
// let sccs = body.basic_blocks.sccs();
621-
// let mut single_block = 0; let mut single_successor = 0; let mut single_predecessor = 0;
622-
// for &scc in &sccs.queue {
623-
// if sccs.sccs[scc as usize].len() == 1 {
624-
// single_block += 1;
625-
// }
626-
627-
// for block in sccs.sccs[scc as usize].iter().copied() {
628-
// if body[block].terminator().successors().count() == 1 {
629-
// single_successor += 1;
630-
// }
631-
632-
// if body.basic_blocks.predecessors()[block].len() == 1 {
633-
// single_predecessor += 1;
634-
// }
635-
// }
636-
// }
637-
638-
// eprintln!(
639-
// "CFG, {} blocks, SCCs: {}, single-block SCCs: {}, single-successor blocks: {}, single-predecessor blocks: {}, {:?}",
640-
// body.basic_blocks.len(),
641-
// sccs.component_count,
642-
// single_block,
643-
// single_successor,
644-
// single_predecessor,
645-
// body.span,
646-
// );
647-
648-
let borrows = Borrows::new(tcx, body, &regioncx, &borrow_set);
649-
let uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data);
650-
let ever_inits = EverInitializedPlaces::new(body, &move_data);
651-
compute_cyclic_dataflow(body, borrows, uninits, ever_inits, &mut mbcx);
652-
653-
// let (_, flow_entry_states) =
654-
// get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
655-
// compute_cyclic_dataflow(body, borrows, uninits, ever_inits, &mut mbcx, &flow_entry_states);
612+
if body.basic_blocks.thir_had_loops != Some(false) {
613+
// Cyclic
614+
let (mut flow_analysis, flow_entry_states) =
615+
get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
616+
visit_results(
617+
body,
618+
traversal::reverse_postorder(body).map(|(bb, _)| bb),
619+
&mut flow_analysis,
620+
&flow_entry_states,
621+
&mut mbcx,
622+
);
656623
} else {
657-
// compute_dataflow(tcx, body, &move_data, &borrow_set, &regioncx, &mut mbcx);
658-
624+
// Acyclic
659625
let borrows = Borrows::new(tcx, body, &regioncx, &borrow_set);
660626
let uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data);
661627
let ever_inits = EverInitializedPlaces::new(body, &move_data);
662628
let mut analysis = Borrowck { borrows, uninits, ever_inits };
663629
compute_rpo_dataflow(body, &mut analysis, &mut mbcx);
664630
}
665631

632+
// if body.basic_blocks.thir_had_loops.unwrap_or_else(|| body.basic_blocks.is_cfg_cyclic()) {
633+
// // let (mut flow_analysis, flow_entry_states) =
634+
// // get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
635+
// // visit_results(
636+
// // body,
637+
// // traversal::reverse_postorder(body).map(|(bb, _)| bb),
638+
// // &mut flow_analysis,
639+
// // &flow_entry_states,
640+
// // &mut mbcx,
641+
// // );
642+
643+
// // let sccs = body.basic_blocks.sccs();
644+
// // let mut single_block = 0; let mut single_successor = 0; let mut single_predecessor = 0;
645+
// // for &scc in &sccs.queue {
646+
// // if sccs.sccs[scc as usize].len() == 1 {
647+
// // single_block += 1;
648+
// // }
649+
650+
// // for block in sccs.sccs[scc as usize].iter().copied() {
651+
// // if body[block].terminator().successors().count() == 1 {
652+
// // single_successor += 1;
653+
// // }
654+
655+
// // if body.basic_blocks.predecessors()[block].len() == 1 {
656+
// // single_predecessor += 1;
657+
// // }
658+
// // }
659+
// // }
660+
661+
// // eprintln!(
662+
// // "CFG, {} blocks, SCCs: {}, single-block SCCs: {}, single-successor blocks: {}, single-predecessor blocks: {}, {:?}",
663+
// // body.basic_blocks.len(),
664+
// // sccs.component_count,
665+
// // single_block,
666+
// // single_successor,
667+
// // single_predecessor,
668+
// // body.span,
669+
// // );
670+
671+
// let borrows = Borrows::new(tcx, body, &regioncx, &borrow_set);
672+
// let uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data);
673+
// let ever_inits = EverInitializedPlaces::new(body, &move_data);
674+
// compute_cyclic_dataflow(body, borrows, uninits, ever_inits, &mut mbcx);
675+
676+
// // let (_, flow_entry_states) =
677+
// // get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
678+
// // compute_cyclic_dataflow(body, borrows, uninits, ever_inits, &mut mbcx, &flow_entry_states);
679+
// } else {
680+
// // compute_dataflow(tcx, body, &move_data, &borrow_set, &regioncx, &mut mbcx);
681+
682+
// let borrows = Borrows::new(tcx, body, &regioncx, &borrow_set);
683+
// let uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data);
684+
// let ever_inits = EverInitializedPlaces::new(body, &move_data);
685+
// let mut analysis = Borrowck { borrows, uninits, ever_inits };
686+
// compute_rpo_dataflow(body, &mut analysis, &mut mbcx);
687+
// }
688+
666689
mbcx.report_move_errors();
667690

668691
// For each non-user used mutable variable, check if it's been assigned from
@@ -695,7 +718,7 @@ fn do_mir_borrowck<'tcx>(
695718
};
696719

697720
#[cfg(test)]
698-
if body.basic_blocks.len() > 5000 {
721+
if body.basic_blocks.len() > 5000 && false {
699722
eprintln!("borrow stats, locals: {}, loans: {}", body.local_decls.len(), borrow_set.len());
700723
eprintln!("nuutila duration: {} ns", mbcx.duration);
701724
eprintln!("predecessor duration: {} ns", mbcx.duration2);
@@ -2529,6 +2552,7 @@ fn do_mir_borrowck<'tcx>(
25292552
result
25302553
}
25312554

2555+
#[cfg(test)]
25322556
fn compute_cyclic_dataflow<'mir, 'tcx>(
25332557
body: &Body<'tcx>,
25342558
borrows: Borrows<'mir, 'tcx>,
@@ -3802,7 +3826,6 @@ fn compute_dataflow<'a, 'tcx>(
38023826
}
38033827
}
38043828

3805-
#[cfg(test)]
38063829
fn get_flow_results<'a, 'tcx>(
38073830
tcx: TyCtxt<'tcx>,
38083831
body: &'a Body<'tcx>,

0 commit comments

Comments
 (0)