Skip to content

Commit 2208981

Browse files
committed
make LiveBorrows Clone
1 parent 0b39afe commit 2208981

File tree

1 file changed

+56
-55
lines changed

1 file changed

+56
-55
lines changed

compiler/rustc_mir_dataflow/src/impls/live_borrows.rs

+56-55
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowDependencies<'a, 'tcx> {
778778
Rvalue::Ref(_, borrow_kind, borrowed_place) => {
779779
let mutbl = match borrow_kind {
780780
BorrowKind::Mut { .. } => Mutability::Mut,
781-
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
782-
Mutability::Not
783-
}
781+
BorrowKind::Shallow | BorrowKind::Shared => Mutability::Not,
784782
};
785783

786784
self.handle_rvalue_ref_or_ptr(borrowed_place, mutbl)
@@ -923,8 +921,8 @@ where
923921
MaybeBorrowedLocals,
924922
&'a Results<'tcx, MaybeBorrowedLocals>,
925923
>,
924+
dep_graph: BorrowDepGraph,
926925
) -> Self {
927-
let dep_graph = &borrows_analysis_results.analysis.borrow_deps.dep_graph;
928926
let borrowed_local_to_locals_to_keep_alive = Self::get_locals_to_keep_alive_map(dep_graph);
929927
Self {
930928
borrows_analysis_results,
@@ -938,14 +936,14 @@ where
938936
/// Uses the dependency graph to find all locals that we need to keep live for a given
939937
/// `Node` (or more specically the `Local` corresponding to that `Node`).
940938
#[instrument(skip(dep_graph), level = "debug")]
941-
fn get_locals_to_keep_alive_map<'b>(
942-
dep_graph: &'b BorrowDepGraph,
939+
fn get_locals_to_keep_alive_map(
940+
dep_graph: BorrowDepGraph,
943941
) -> FxHashMap<Local, FxHashSet<Local>> {
944942
let mut borrows_to_locals: FxHashMap<Local, FxHashSet<Local>> = Default::default();
945943
let mut memoization_map: FxHashMap<NodeIndex, FxHashSet<Local>> = Default::default();
946944

947945
// create SCCs for dependency graph and map each local to its SCC.
948-
let sccs: Sccs<NodeIndex, NodeIndex> = Sccs::new(dep_graph);
946+
let sccs: Sccs<NodeIndex, NodeIndex> = Sccs::new(&dep_graph);
949947

950948
// Contains the Locals to keep alive for each scc.
951949
let mut scc_to_locals_to_keep_alive: FxHashMap<NodeIndex, FxHashSet<Local>> =
@@ -1096,11 +1094,28 @@ pub fn get_borrowed_locals_results<'a, 'mir, 'tcx>(
10961094
}
10971095
}
10981096

1099-
let live_borrows = LiveBorrows::new(body, tcx, borrow_deps);
1100-
let results =
1097+
let reborrows_map = borrow_deps.reborrows_map;
1098+
let local_to_nodekind = borrow_deps
1099+
.dep_graph
1100+
.all_nodes()
1101+
.iter()
1102+
.map(|node| {
1103+
let nodekind = node.data;
1104+
let local = nodekind.get_local();
1105+
1106+
(local, nodekind)
1107+
})
1108+
.collect::<FxHashMap<Local, NodeKind>>();
1109+
1110+
let live_borrows = LiveBorrows::new(body, tcx, local_to_nodekind, reborrows_map);
1111+
let live_borrows_results =
11011112
live_borrows.into_engine(tcx, body).pass_name("borrowed_locals").iterate_to_fixpoint();
11021113

1103-
BorrowedLocalsResults::new(results, maybe_borrowed_locals_cursor)
1114+
BorrowedLocalsResults::new(
1115+
live_borrows_results,
1116+
maybe_borrowed_locals_cursor,
1117+
borrow_deps.dep_graph,
1118+
)
11041119
}
11051120

11061121
/// The `ResultsCursor` equivalent for the borrowed locals analysis. Since this analysis doesn't
@@ -1178,19 +1193,22 @@ impl<'a, 'mir, 'tcx> BorrowedLocalsResultsCursor<'a, 'mir, 'tcx> {
11781193
/// Performs a liveness analysis for borrows and raw pointers. This analysis also tracks `Local`s
11791194
/// corresponding to `Node`s of kind `NodeKind::LocalWithRefs`, as these could potentially refer to
11801195
/// or include references, pointers or exposed pointers.
1196+
#[derive(Clone)]
11811197
pub struct LiveBorrows<'mir, 'tcx> {
11821198
body: &'mir Body<'tcx>,
11831199
tcx: TyCtxt<'tcx>,
1184-
borrow_deps: BorrowDependencies<'mir, 'tcx>,
1200+
local_to_nodekind: FxHashMap<Local, NodeKind>,
1201+
reborrows_map: FxHashMap<Local, Local>,
11851202
}
11861203

11871204
impl<'mir, 'tcx> LiveBorrows<'mir, 'tcx> {
11881205
fn new(
11891206
body: &'mir Body<'tcx>,
11901207
tcx: TyCtxt<'tcx>,
1191-
borrow_deps: BorrowDependencies<'mir, 'tcx>,
1208+
local_to_nodekind: FxHashMap<Local, NodeKind>,
1209+
reborrows_map: FxHashMap<Local, Local>,
11921210
) -> Self {
1193-
LiveBorrows { body, tcx, borrow_deps }
1211+
LiveBorrows { body, tcx, local_to_nodekind, reborrows_map }
11941212
}
11951213

11961214
fn transfer_function<'b, T>(
@@ -1201,11 +1219,18 @@ impl<'mir, 'tcx> LiveBorrows<'mir, 'tcx> {
12011219
body: self.body,
12021220
tcx: self.tcx,
12031221
_trans: trans,
1204-
borrow_deps: &self.borrow_deps,
1222+
local_to_nodekind: &self.local_to_nodekind,
1223+
reborrows_map: &self.reborrows_map,
12051224
}
12061225
}
12071226
}
12081227

1228+
impl<'mir, 'tcx> crate::CloneAnalysis for LiveBorrows<'mir, 'tcx> {
1229+
fn clone_analysis(&self) -> Self {
1230+
self.clone()
1231+
}
1232+
}
1233+
12091234
impl<'a, 'tcx> AnalysisDomain<'tcx> for LiveBorrows<'a, 'tcx> {
12101235
type Domain = BitSet<Local>;
12111236
type Direction = Backward;
@@ -1258,7 +1283,8 @@ struct TransferFunction<'a, 'b, 'c, 'tcx, T> {
12581283
body: &'a Body<'tcx>,
12591284
tcx: TyCtxt<'tcx>,
12601285
_trans: &'b mut T,
1261-
borrow_deps: &'c BorrowDependencies<'a, 'tcx>,
1286+
local_to_nodekind: &'c FxHashMap<Local, NodeKind>,
1287+
reborrows_map: &'c FxHashMap<Local, Local>,
12621288
}
12631289

12641290
impl<'a, 'b, 'c, 'tcx, T> TransferFunction<'a, 'b, 'c, 'tcx, T>
@@ -1267,21 +1293,21 @@ where
12671293
{
12681294
fn gen(&mut self, local: Local) {
12691295
debug!("gen {:?}", local);
1270-
if let Some(reborrowed_local) = self.borrow_deps.reborrows_map.get(&local) {
1296+
if let Some(reborrowed_local) = self.reborrows_map.get(&local) {
12711297
self._trans.gen(*reborrowed_local);
12721298
} else {
1273-
if self.borrow_deps.dep_graph.locals_to_node_indexes.get(&local).is_some() {
1299+
if self.local_to_nodekind.get(&local).is_some() {
12741300
self._trans.gen(local)
12751301
}
12761302
}
12771303
}
12781304

12791305
fn kill(&mut self, local: Local) {
12801306
debug!("killing {:?}", local);
1281-
if let Some(reborrowed_local) = self.borrow_deps.reborrows_map.get(&local) {
1307+
if let Some(reborrowed_local) = self.reborrows_map.get(&local) {
12821308
self._trans.kill(*reborrowed_local);
12831309
} else {
1284-
if self.borrow_deps.dep_graph.locals_to_node_indexes.get(&local).is_some() {
1310+
if self.local_to_nodekind.get(&local).is_some() {
12851311
self._trans.kill(local)
12861312
}
12871313
}
@@ -1312,16 +1338,10 @@ where
13121338
self.visit_rvalue(&assign.1, location);
13131339
}
13141340
_ => {
1315-
if let Some(node_idx) = self
1316-
.borrow_deps
1317-
.dep_graph
1318-
.locals_to_node_indexes
1319-
.get(&lhs_place.local)
1341+
if let Some(NodeKind::LocalWithRefs(_)) =
1342+
self.local_to_nodekind.get(&lhs_place.local)
13201343
{
1321-
let node = self.borrow_deps.dep_graph.node(*node_idx);
1322-
if let NodeKind::LocalWithRefs(_) = node.data {
1323-
self.kill(lhs_place.local);
1324-
}
1344+
self.kill(lhs_place.local);
13251345
}
13261346
self.super_assign(&assign.0, &assign.1, location);
13271347
}
@@ -1357,13 +1377,8 @@ where
13571377
self.gen(local);
13581378
}
13591379
_ => {
1360-
if let Some(node_idx) =
1361-
self.borrow_deps.dep_graph.locals_to_node_indexes.get(&local)
1362-
{
1363-
let node = self.borrow_deps.dep_graph.node(*node_idx);
1364-
if matches!(node.data, NodeKind::LocalWithRefs(_)) {
1365-
self.gen(local);
1366-
}
1380+
if let Some(NodeKind::LocalWithRefs(_)) = self.local_to_nodekind.get(&local) {
1381+
self.gen(local);
13671382
}
13681383
}
13691384
}
@@ -1392,20 +1407,13 @@ where
13921407
for arg in args {
13931408
match arg {
13941409
Operand::Copy(place) | Operand::Move(place) => {
1395-
if let Some(node_idx) = self
1396-
.borrow_deps
1397-
.dep_graph
1398-
.locals_to_node_indexes
1399-
.get(&place.local)
1410+
if let Some(NodeKind::LocalWithRefs(_)) =
1411+
self.local_to_nodekind.get(&place.local)
14001412
{
1401-
let node = self.borrow_deps.dep_graph.node(*node_idx);
1402-
14031413
// these are `Local`s that contain references/pointers or are raw pointers
14041414
// that were assigned to raw pointers, which were cast to usize. Since the
14051415
// function call is free to use these in any form, we need to gen them here.
1406-
if let NodeKind::LocalWithRefs(_) = node.data {
1407-
self.gen(place.local);
1408-
}
1416+
self.gen(place.local);
14091417
} else {
14101418
self.super_operand(arg, location)
14111419
}
@@ -1424,20 +1432,13 @@ where
14241432

14251433
match value {
14261434
Operand::Copy(place) | Operand::Move(place) => {
1427-
if let Some(node_idx) = self
1428-
.borrow_deps
1429-
.dep_graph
1430-
.locals_to_node_indexes
1431-
.get(&place.local)
1435+
if let Some(NodeKind::LocalWithRefs(_)) =
1436+
self.local_to_nodekind.get(&place.local)
14321437
{
1433-
let node = self.borrow_deps.dep_graph.node(*node_idx);
1434-
14351438
// these are `Local`s that contain references/pointers or are raw pointers
14361439
// that were assigned to raw pointers, which were cast to usize. Since the
14371440
// function call is free to use these in any form, we need to gen them here.
1438-
if let NodeKind::LocalWithRefs(_) = node.data {
1439-
self.gen(place.local);
1440-
}
1441+
self.gen(place.local);
14411442
} else {
14421443
self.super_operand(value, location)
14431444
}

0 commit comments

Comments
 (0)