Skip to content

Commit 7fe6857

Browse files
committed
Lazy dominator tree construction in borrowck
Motivated by the observation that sometimes constructed dominator tree was never queried.
1 parent 159ba8a commit 7fe6857

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21932193
let mut back_edge_stack = Vec::new();
21942194

21952195
predecessor_locations(self.body, location).for_each(|predecessor| {
2196-
if location.dominates(predecessor, &self.dominators) {
2196+
if location.dominates(predecessor, self.dominators()) {
21972197
back_edge_stack.push(predecessor)
21982198
} else {
21992199
stack.push(predecessor);
@@ -2305,7 +2305,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
23052305

23062306
let mut has_predecessor = false;
23072307
predecessor_locations(self.body, location).for_each(|predecessor| {
2308-
if location.dominates(predecessor, &self.dominators) {
2308+
if location.dominates(predecessor, self.dominators()) {
23092309
back_edge_stack.push(predecessor)
23102310
} else {
23112311
stack.push(predecessor);

compiler/rustc_borrowck/src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(let_chains)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]
8+
#![feature(once_cell)]
89
#![feature(rustc_attrs)]
910
#![feature(stmt_expr_attributes)]
1011
#![feature(trusted_step)]
@@ -39,6 +40,7 @@ use rustc_span::{Span, Symbol};
3940

4041
use either::Either;
4142
use smallvec::SmallVec;
43+
use std::cell::OnceCell;
4244
use std::cell::RefCell;
4345
use std::collections::BTreeMap;
4446
use std::rc::Rc;
@@ -333,7 +335,7 @@ fn do_mir_borrowck<'tcx>(
333335
used_mut: Default::default(),
334336
used_mut_upvars: SmallVec::new(),
335337
borrow_set: Rc::clone(&borrow_set),
336-
dominators: Dominators::dummy(), // not used
338+
dominators: Default::default(),
337339
upvars: Vec::new(),
338340
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
339341
region_names: RefCell::default(),
@@ -346,8 +348,6 @@ fn do_mir_borrowck<'tcx>(
346348
};
347349
}
348350

349-
let dominators = body.basic_blocks.dominators();
350-
351351
let mut mbcx = MirBorrowckCtxt {
352352
infcx,
353353
param_env,
@@ -364,7 +364,7 @@ fn do_mir_borrowck<'tcx>(
364364
used_mut: Default::default(),
365365
used_mut_upvars: SmallVec::new(),
366366
borrow_set: Rc::clone(&borrow_set),
367-
dominators,
367+
dominators: Default::default(),
368368
upvars,
369369
local_names,
370370
region_names: RefCell::default(),
@@ -534,7 +534,7 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
534534
borrow_set: Rc<BorrowSet<'tcx>>,
535535

536536
/// Dominators for MIR
537-
dominators: Dominators<BasicBlock>,
537+
dominators: OnceCell<Dominators<BasicBlock>>,
538538

539539
/// Information about upvars not necessarily preserved in types or MIR
540540
upvars: Vec<Upvar<'tcx>>,
@@ -1051,7 +1051,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10511051

10521052
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
10531053
// Reading from mere reservations of mutable-borrows is OK.
1054-
if !is_active(&this.dominators, borrow, location) {
1054+
if !is_active(this.dominators(), borrow, location) {
10551055
assert!(allow_two_phase_borrow(borrow.kind));
10561056
return Control::Continue;
10571057
}
@@ -2219,6 +2219,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22192219
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
22202220
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
22212221
}
2222+
2223+
fn dominators(&self) -> &Dominators<BasicBlock> {
2224+
self.dominators.get_or_init(|| self.body.basic_blocks.dominators())
2225+
}
22222226
}
22232227

22242228
mod error {

compiler/rustc_data_structures/src/graph/dominators/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ pub struct Dominators<N: Idx> {
268268
}
269269

270270
impl<Node: Idx> Dominators<Node> {
271-
pub fn dummy() -> Self {
272-
Self { post_order_rank: IndexVec::new(), immediate_dominators: IndexVec::new() }
273-
}
274-
275271
pub fn is_reachable(&self, node: Node) -> bool {
276272
self.immediate_dominators[node].is_some()
277273
}

0 commit comments

Comments
 (0)