Skip to content

Commit a705ccb

Browse files
committed
MIR dominators: computed on-demand and cached
Cached inside MIR body - motivated by potential reuse opportunity when code generating different instantiations of the same generic code.
1 parent eab621d commit a705ccb

File tree

5 files changed

+12
-32
lines changed

5 files changed

+12
-32
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 2 deletions
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.body.basic_blocks.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.body.basic_blocks.dominators()) {
23092309
back_edge_stack.push(predecessor)
23102310
} else {
23112311
stack.push(predecessor);

compiler/rustc_borrowck/src/invalidation.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![deny(rustc::untranslatable_diagnostic)]
22
#![deny(rustc::diagnostic_outside_of_impl)]
3-
use rustc_data_structures::graph::dominators::Dominators;
43
use rustc_middle::mir::visit::Visitor;
5-
use rustc_middle::mir::{self, BasicBlock, Body, Location, NonDivergingIntrinsic, Place, Rvalue};
4+
use rustc_middle::mir::{self, Body, Location, NonDivergingIntrinsic, Place, Rvalue};
65
use rustc_middle::mir::{BorrowKind, Mutability, Operand};
76
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
87
use rustc_middle::mir::{Statement, StatementKind};
@@ -28,15 +27,8 @@ pub(super) fn generate_invalidates<'tcx>(
2827

2928
if let Some(all_facts) = all_facts {
3029
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
31-
let dominators = body.basic_blocks.dominators();
32-
let mut ig = InvalidationGenerator {
33-
all_facts,
34-
borrow_set,
35-
tcx,
36-
location_table,
37-
body: &body,
38-
dominators,
39-
};
30+
let mut ig =
31+
InvalidationGenerator { all_facts, borrow_set, tcx, location_table, body: &body };
4032
ig.visit_body(body);
4133
}
4234
}
@@ -46,7 +38,6 @@ struct InvalidationGenerator<'cx, 'tcx> {
4638
all_facts: &'cx mut AllFacts,
4739
location_table: &'cx LocationTable,
4840
body: &'cx Body<'tcx>,
49-
dominators: Dominators<BasicBlock>,
5041
borrow_set: &'cx BorrowSet<'tcx>,
5142
}
5243

@@ -389,7 +380,7 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
389380

390381
(Read(_), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
391382
// Reading from mere reservations of mutable-borrows is OK.
392-
if !is_active(&this.dominators, borrow, location) {
383+
if !is_active(this.body.basic_blocks.dominators(), borrow, location) {
393384
// If the borrow isn't active yet, reads don't invalidate it
394385
assert!(allow_two_phase_borrow(borrow.kind));
395386
return Control::Continue;

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(let_chains)]
66
#![feature(min_specialization)]
77
#![feature(never_type)]
8-
#![feature(once_cell)]
98
#![feature(rustc_attrs)]
109
#![feature(stmt_expr_attributes)]
1110
#![feature(trusted_step)]
@@ -18,7 +17,6 @@ extern crate rustc_middle;
1817
extern crate tracing;
1918

2019
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
21-
use rustc_data_structures::graph::dominators::Dominators;
2220
use rustc_data_structures::vec_map::VecMap;
2321
use rustc_errors::{Diagnostic, DiagnosticBuilder};
2422
use rustc_hir as hir;
@@ -30,7 +28,7 @@ use rustc_middle::mir::{
3028
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
3129
Place, PlaceElem, PlaceRef, VarDebugInfoContents,
3230
};
33-
use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
31+
use rustc_middle::mir::{AggregateKind, BorrowCheckResult, BorrowKind};
3432
use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
3533
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
3634
use rustc_middle::ty::query::Providers;
@@ -40,7 +38,6 @@ use rustc_span::{Span, Symbol};
4038

4139
use either::Either;
4240
use smallvec::SmallVec;
43-
use std::cell::OnceCell;
4441
use std::cell::RefCell;
4542
use std::collections::BTreeMap;
4643
use std::rc::Rc;
@@ -335,7 +332,6 @@ fn do_mir_borrowck<'tcx>(
335332
used_mut: Default::default(),
336333
used_mut_upvars: SmallVec::new(),
337334
borrow_set: Rc::clone(&borrow_set),
338-
dominators: Default::default(),
339335
upvars: Vec::new(),
340336
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
341337
region_names: RefCell::default(),
@@ -364,7 +360,6 @@ fn do_mir_borrowck<'tcx>(
364360
used_mut: Default::default(),
365361
used_mut_upvars: SmallVec::new(),
366362
borrow_set: Rc::clone(&borrow_set),
367-
dominators: Default::default(),
368363
upvars,
369364
local_names,
370365
region_names: RefCell::default(),
@@ -533,9 +528,6 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
533528
/// The set of borrows extracted from the MIR
534529
borrow_set: Rc<BorrowSet<'tcx>>,
535530

536-
/// Dominators for MIR
537-
dominators: OnceCell<Dominators<BasicBlock>>,
538-
539531
/// Information about upvars not necessarily preserved in types or MIR
540532
upvars: Vec<Upvar<'tcx>>,
541533

@@ -1051,7 +1043,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10511043

10521044
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
10531045
// Reading from mere reservations of mutable-borrows is OK.
1054-
if !is_active(this.dominators(), borrow, location) {
1046+
if !is_active(this.body.basic_blocks.dominators(), borrow, location) {
10551047
assert!(allow_two_phase_borrow(borrow.kind));
10561048
return Control::Continue;
10571049
}
@@ -2219,10 +2211,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22192211
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
22202212
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
22212213
}
2222-
2223-
fn dominators(&self) -> &Dominators<BasicBlock> {
2224-
self.dominators.get_or_init(|| self.body.basic_blocks.dominators())
2225-
}
22262214
}
22272215

22282216
mod error {

compiler/rustc_codegen_ssa/src/mir/analyze.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ enum LocalKind {
6969

7070
struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
7171
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
72-
dominators: Dominators<mir::BasicBlock>,
72+
dominators: &'mir Dominators<mir::BasicBlock>,
7373
locals: IndexVec<mir::Local, LocalKind>,
7474
}
7575

compiler/rustc_middle/src/mir/basic_blocks.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[Option<u1
2323

2424
#[derive(Clone, Default, Debug)]
2525
struct Cache {
26+
dominators: OnceCell<Dominators<BasicBlock>>,
2627
predecessors: OnceCell<Predecessors>,
2728
switch_sources: OnceCell<SwitchSources>,
2829
is_cyclic: OnceCell<bool>,
@@ -42,8 +43,8 @@ impl<'tcx> BasicBlocks<'tcx> {
4243
}
4344

4445
#[inline]
45-
pub fn dominators(&self) -> Dominators<BasicBlock> {
46-
dominators(&self)
46+
pub fn dominators(&self) -> &Dominators<BasicBlock> {
47+
self.cache.dominators.get_or_init(|| dominators(self))
4748
}
4849

4950
/// Returns predecessors for each basic block.

0 commit comments

Comments
 (0)