Skip to content

Commit

Permalink
refactor to match precomputer from PR 111759
Browse files Browse the repository at this point in the history
  • Loading branch information
lqd committed Oct 4, 2023
1 parent 773fe75 commit 93477da
Showing 1 changed file with 42 additions and 48 deletions.
90 changes: 42 additions & 48 deletions compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
// - placeholders or free regions themselves,
// - or also transitively outlive a free region.
//
// That is to say, if there are member constraints here, the loan escapes the
// function and cannot go out of scope. We can early return.
// That is to say, if there are member constraints here, the loan escapes the function
// and cannot go out of scope. We can early return.
//
// 2. Via regions that are live at all points: placeholders and free regions.
//
Expand All @@ -364,36 +364,32 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
return;
}

// 3. Via outlives successors, which we want to record and traverse: we add them
// to the worklist stack
// 3. Via outlives successors, which we want to record and traverse: we add them to the
// worklist stack
for &succ_scc in sccs.successors(scc) {
if self.reachability.insert(succ_scc) {
self.reachability_stack.push(succ_scc);
}
}
}

// We visit one BB at a time. The complication is that we may start in the
// middle of the first BB visited (the one containing `location`), in which
// case we may have to later on process the first part of that BB if there
// is a path back to its start.
// We visit one BB at a time. The complication is that we may start in the middle of the
// first BB visited (the one containing `issued_location`), in which case we may have to
// later on process the first part of that BB if there is a path back to its start.

// For visited BBs, we record the index of the first statement processed.
// (In fully processed BBs this index is 0.) Note also that we add BBs to
// `visited` once they are added to `stack`, before they are actually
// processed, because this avoids the need to look them up again on
// completion.
// For visited BBs, we record the index of the first statement processed. (In fully
// processed BBs this index is 0.) Note also that we add BBs to `visited` once they are
// added to `stack`, before they are actually processed, because this avoids the need to
// look them up again on completion.
self.visited.insert(issued_location.block);

let first_block = issued_location.block;
let mut first_lo = issued_location.statement_index;
let first_hi = self.body[issued_location.block].statements.len();

self.visit_stack.push(StackEntry { bb: issued_location.block, lo: first_lo, hi: first_hi });

while let Some(StackEntry { bb, lo, hi }) = self.visit_stack.pop() {
// If we process the first part of the first basic block (i.e. we encounter that block
// for the second time), we no longer have to visit its successors again.
let mut finished_early = bb == issued_location.block && hi != first_hi;
'preorder: while let Some(StackEntry { bb, lo, hi }) = self.visit_stack.pop() {
for i in lo..=hi {
let location = Location { block: bb, statement_index: i };

Expand All @@ -419,44 +415,42 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
}
}

// If no live region is reachable from the issuing region, then the loan is
// killed at this point, and goes out of scope.
// If no live region is reachable from the issuing region, then the loan is killed
// at this point, and goes out of scope. We also can skip visiting successor
// locations.
if !issuing_region_can_reach_live_regions {
debug!("loan {:?} gets killed at {:?}", loan_idx, location);
self.loans_out_of_scope_at_location.entry(location).or_default().push(loan_idx);
finished_early = true;
break;
continue 'preorder;
}
}

if !finished_early {
// Add successor BBs to the work list, if necessary.
let bb_data = &self.body[bb];
debug_assert!(hi == bb_data.statements.len());
for succ_bb in bb_data.terminator().successors() {
if !self.visited.insert(succ_bb) {
if succ_bb == issued_location.block && first_lo > 0 {
// `succ_bb` has been seen before. If it wasn't fully processed, add its
// first part to `visit_stack` for processing.
self.visit_stack.push(StackEntry {
bb: succ_bb,
lo: 0,
hi: first_lo - 1,
});

// And update this entry with 0, to represent the whole BB being
// processed.
first_lo = 0;
}
} else {
// `succ_bb` hasn't been seen before. Add it to `visit_stack` for
// processing.
self.visit_stack.push(StackEntry {
bb: succ_bb,
lo: 0,
hi: self.body[succ_bb].statements.len(),
});
// If we process the first part of the first basic block (i.e. we encounter that block
// for the second time), we no longer have to visit its successors again.
if bb == first_block && hi != first_hi {
continue;
}

// Add successor BBs to the work list, if necessary.
let bb_data = &self.body[bb];
debug_assert!(hi == bb_data.statements.len());
for succ_bb in bb_data.terminator().successors() {
if !self.visited.insert(succ_bb) {
if succ_bb == first_block && first_lo > 0 {
// `succ_bb` has been seen before. If it wasn't fully processed, add its
// first part to the stack for processing.
self.visit_stack.push(StackEntry { bb: succ_bb, lo: 0, hi: first_lo - 1 });

// And update this entry with 0, to represent the whole BB being processed.
first_lo = 0;
}
} else {
// `succ_bb` hasn't been seen before. Add it to the stack for processing.
self.visit_stack.push(StackEntry {
bb: succ_bb,
lo: 0,
hi: self.body[succ_bb].statements.len(),
});
}
}
}
Expand Down

0 comments on commit 93477da

Please sign in to comment.