Skip to content

Commit 0a04b31

Browse files
committed
reduce acyclic check cost
1 parent 907949d commit 0a04b31

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,20 @@ fn do_mir_borrowck<'tcx>(
593593
// );
594594
// }
595595

596-
if body.basic_blocks.is_cfg_cyclic() {
596+
// Loops in THIR don't always have backedges (e.g. a loop that breaks immediately), so the MIR
597+
// can still be acyclic even if the THIR had loops. However, if the MIR is acyclic, it must come
598+
// from loops.
599+
// if body.basic_blocks.is_cfg_cyclic() {
600+
// assert_eq!(
601+
// Some(true),
602+
// body.basic_blocks.thir_had_loops,
603+
// "mismatch between THIR loop and cyclic MIR {:?}",
604+
// body.span,
605+
// );
606+
// }
607+
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()) {
597610
// let (mut flow_analysis, flow_entry_states) =
598611
// get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
599612
// visit_results(

compiler/rustc_middle/src/mir/basic_blocks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::mir::{BasicBlock, BasicBlockData, START_BLOCK};
1515
pub struct BasicBlocks<'tcx> {
1616
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
1717
cache: Cache,
18+
pub thir_had_loops: Option<bool>,
1819
}
1920

2021
// Typically 95%+ of basic blocks have 4 or fewer predecessors.
@@ -529,7 +530,7 @@ impl<T: Idx> VecQueue<T> {
529530
impl<'tcx> BasicBlocks<'tcx> {
530531
#[inline]
531532
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
532-
BasicBlocks { basic_blocks, cache: Cache::default() }
533+
BasicBlocks { basic_blocks, cache: Cache::default(), thir_had_loops: None }
533534
}
534535

535536
/// Returns true if control-flow graph contains a cycle reachable from the `START_BLOCK`.

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,6 @@ fn construct_fn<'tcx>(
533533
builder.build_drop_trees();
534534
return_block.unit()
535535
});
536-
537536
let mut body = builder.finish();
538537

539538
body.spread_arg = if abi == ExternAbi::RustCall {
@@ -803,6 +802,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
803802
None,
804803
);
805804
body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
805+
body.basic_blocks.thir_had_loops = Some(self.thir.exprs.iter().any(|e| {
806+
matches!(e.kind, thir::ExprKind::Loop { .. } | thir::ExprKind::LoopMatch { .. })
807+
}));
806808

807809
for (index, block) in body.basic_blocks.iter().enumerate() {
808810
if block.terminator.is_none() {

0 commit comments

Comments
 (0)