Auto merge of #106976 - tmiasko:borrowck-lazy-dominators, r=cjgillot
Lazy dominator tree construction in borrowck Motivated by the observation that sometimes constructed dominator tree was never queried.
This commit is contained in:
commit
21f6839352
@ -2193,7 +2193,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
let mut back_edge_stack = Vec::new();
|
let mut back_edge_stack = Vec::new();
|
||||||
|
|
||||||
predecessor_locations(self.body, location).for_each(|predecessor| {
|
predecessor_locations(self.body, location).for_each(|predecessor| {
|
||||||
if location.dominates(predecessor, &self.dominators) {
|
if location.dominates(predecessor, self.dominators()) {
|
||||||
back_edge_stack.push(predecessor)
|
back_edge_stack.push(predecessor)
|
||||||
} else {
|
} else {
|
||||||
stack.push(predecessor);
|
stack.push(predecessor);
|
||||||
@ -2305,7 +2305,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
|
|
||||||
let mut has_predecessor = false;
|
let mut has_predecessor = false;
|
||||||
predecessor_locations(self.body, location).for_each(|predecessor| {
|
predecessor_locations(self.body, location).for_each(|predecessor| {
|
||||||
if location.dominates(predecessor, &self.dominators) {
|
if location.dominates(predecessor, self.dominators()) {
|
||||||
back_edge_stack.push(predecessor)
|
back_edge_stack.push(predecessor)
|
||||||
} else {
|
} else {
|
||||||
stack.push(predecessor);
|
stack.push(predecessor);
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
#![feature(min_specialization)]
|
#![feature(min_specialization)]
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
|
#![feature(once_cell)]
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
#![feature(stmt_expr_attributes)]
|
#![feature(stmt_expr_attributes)]
|
||||||
#![feature(trusted_step)]
|
#![feature(trusted_step)]
|
||||||
@ -39,6 +40,7 @@ use rustc_span::{Span, Symbol};
|
|||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
use std::cell::OnceCell;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@ -333,7 +335,7 @@ fn do_mir_borrowck<'tcx>(
|
|||||||
used_mut: Default::default(),
|
used_mut: Default::default(),
|
||||||
used_mut_upvars: SmallVec::new(),
|
used_mut_upvars: SmallVec::new(),
|
||||||
borrow_set: Rc::clone(&borrow_set),
|
borrow_set: Rc::clone(&borrow_set),
|
||||||
dominators: Dominators::dummy(), // not used
|
dominators: Default::default(),
|
||||||
upvars: Vec::new(),
|
upvars: Vec::new(),
|
||||||
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
|
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
|
||||||
region_names: RefCell::default(),
|
region_names: RefCell::default(),
|
||||||
@ -346,8 +348,6 @@ fn do_mir_borrowck<'tcx>(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let dominators = body.basic_blocks.dominators();
|
|
||||||
|
|
||||||
let mut mbcx = MirBorrowckCtxt {
|
let mut mbcx = MirBorrowckCtxt {
|
||||||
infcx,
|
infcx,
|
||||||
param_env,
|
param_env,
|
||||||
@ -364,7 +364,7 @@ fn do_mir_borrowck<'tcx>(
|
|||||||
used_mut: Default::default(),
|
used_mut: Default::default(),
|
||||||
used_mut_upvars: SmallVec::new(),
|
used_mut_upvars: SmallVec::new(),
|
||||||
borrow_set: Rc::clone(&borrow_set),
|
borrow_set: Rc::clone(&borrow_set),
|
||||||
dominators,
|
dominators: Default::default(),
|
||||||
upvars,
|
upvars,
|
||||||
local_names,
|
local_names,
|
||||||
region_names: RefCell::default(),
|
region_names: RefCell::default(),
|
||||||
@ -534,7 +534,7 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
borrow_set: Rc<BorrowSet<'tcx>>,
|
borrow_set: Rc<BorrowSet<'tcx>>,
|
||||||
|
|
||||||
/// Dominators for MIR
|
/// Dominators for MIR
|
||||||
dominators: Dominators<BasicBlock>,
|
dominators: OnceCell<Dominators<BasicBlock>>,
|
||||||
|
|
||||||
/// Information about upvars not necessarily preserved in types or MIR
|
/// Information about upvars not necessarily preserved in types or MIR
|
||||||
upvars: Vec<Upvar<'tcx>>,
|
upvars: Vec<Upvar<'tcx>>,
|
||||||
@ -1051,7 +1051,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
|
|
||||||
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
|
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
|
||||||
// Reading from mere reservations of mutable-borrows is OK.
|
// Reading from mere reservations of mutable-borrows is OK.
|
||||||
if !is_active(&this.dominators, borrow, location) {
|
if !is_active(this.dominators(), borrow, location) {
|
||||||
assert!(allow_two_phase_borrow(borrow.kind));
|
assert!(allow_two_phase_borrow(borrow.kind));
|
||||||
return Control::Continue;
|
return Control::Continue;
|
||||||
}
|
}
|
||||||
@ -2219,6 +2219,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
|
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
|
||||||
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
|
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dominators(&self) -> &Dominators<BasicBlock> {
|
||||||
|
self.dominators.get_or_init(|| self.body.basic_blocks.dominators())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod error {
|
mod error {
|
||||||
|
@ -271,10 +271,6 @@ pub struct Dominators<N: Idx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Node: Idx> Dominators<Node> {
|
impl<Node: Idx> Dominators<Node> {
|
||||||
pub fn dummy() -> Self {
|
|
||||||
Self { post_order_rank: IndexVec::new(), immediate_dominators: IndexVec::new() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_reachable(&self, node: Node) -> bool {
|
pub fn is_reachable(&self, node: Node) -> bool {
|
||||||
self.immediate_dominators[node].is_some()
|
self.immediate_dominators[node].is_some()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user