Skip to content

Commit d4bff25

Browse files
committed
Rename DepGraphQuery to RetainedDepGraph
1 parent fbd6934 commit d4bff25

5 files changed

Lines changed: 85 additions & 78 deletions

File tree

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
4545
use rustc_hir::intravisit::{self, Visitor};
4646
use rustc_middle::bug;
4747
use rustc_middle::dep_graph::{
48-
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
48+
DepKind, DepNode, DepNodeFilter, EdgeFilter, RetainedDepGraph, dep_kinds,
4949
};
5050
use rustc_middle::hir::nested_filter;
5151
use rustc_middle::ty::TyCtxt;
@@ -59,7 +59,7 @@ use crate::errors;
5959
pub(crate) fn assert_dep_graph(tcx: TyCtxt<'_>) {
6060
tcx.dep_graph.with_ignore(|| {
6161
if tcx.sess.opts.unstable_opts.dump_dep_graph {
62-
tcx.dep_graph.with_query(dump_graph);
62+
tcx.dep_graph.with_retained_dep_graph(dump_graph);
6363
}
6464

6565
if !tcx.sess.opts.unstable_opts.query_dep_graph {
@@ -186,7 +186,7 @@ fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_wou
186186
}
187187
return;
188188
}
189-
tcx.dep_graph.with_query(|query| {
189+
tcx.dep_graph.with_retained_dep_graph(|query| {
190190
for &(_, source_def_id, ref source_dep_node) in if_this_changed {
191191
let dependents = query.transitive_predecessors(source_dep_node);
192192
for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
@@ -204,21 +204,21 @@ fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_wou
204204
});
205205
}
206206

207-
fn dump_graph(query: &DepGraphQuery) {
207+
fn dump_graph(graph: &RetainedDepGraph) {
208208
let path: String = env::var("RUST_DEP_GRAPH").unwrap_or_else(|_| "dep_graph".to_string());
209209

210210
let nodes = match env::var("RUST_DEP_GRAPH_FILTER") {
211211
Ok(string) => {
212212
// Expect one of: "-> target", "source -> target", or "source ->".
213213
let edge_filter =
214214
EdgeFilter::new(&string).unwrap_or_else(|e| bug!("invalid filter: {}", e));
215-
let sources = node_set(query, &edge_filter.source);
216-
let targets = node_set(query, &edge_filter.target);
217-
filter_nodes(query, &sources, &targets)
215+
let sources = node_set(graph, &edge_filter.source);
216+
let targets = node_set(graph, &edge_filter.target);
217+
filter_nodes(graph, &sources, &targets)
218218
}
219-
Err(_) => query.nodes().into_iter().map(|n| n.kind).collect(),
219+
Err(_) => graph.nodes().into_iter().map(|n| n.kind).collect(),
220220
};
221-
let edges = filter_edges(query, &nodes);
221+
let edges = filter_edges(graph, &nodes);
222222

223223
{
224224
// dump a .txt file with just the edges:
@@ -281,51 +281,51 @@ impl<'a> dot::Labeller<'a> for GraphvizDepGraph {
281281
// Given an optional filter like `"x,y,z"`, returns either `None` (no
282282
// filter) or the set of nodes whose labels contain all of those
283283
// substrings.
284-
fn node_set<'q>(
285-
query: &'q DepGraphQuery,
284+
fn node_set<'g>(
285+
graph: &'g RetainedDepGraph,
286286
filter: &DepNodeFilter,
287-
) -> Option<FxIndexSet<&'q DepNode>> {
287+
) -> Option<FxIndexSet<&'g DepNode>> {
288288
debug!("node_set(filter={:?})", filter);
289289

290290
if filter.accepts_all() {
291291
return None;
292292
}
293293

294-
Some(query.nodes().into_iter().filter(|n| filter.test(n)).collect())
294+
Some(graph.nodes().into_iter().filter(|n| filter.test(n)).collect())
295295
}
296296

297-
fn filter_nodes<'q>(
298-
query: &'q DepGraphQuery,
299-
sources: &Option<FxIndexSet<&'q DepNode>>,
300-
targets: &Option<FxIndexSet<&'q DepNode>>,
297+
fn filter_nodes<'g>(
298+
graph: &'g RetainedDepGraph,
299+
sources: &Option<FxIndexSet<&'g DepNode>>,
300+
targets: &Option<FxIndexSet<&'g DepNode>>,
301301
) -> FxIndexSet<DepKind> {
302302
if let Some(sources) = sources {
303303
if let Some(targets) = targets {
304-
walk_between(query, sources, targets)
304+
walk_between(graph, sources, targets)
305305
} else {
306-
walk_nodes(query, sources, OUTGOING)
306+
walk_nodes(graph, sources, OUTGOING)
307307
}
308308
} else if let Some(targets) = targets {
309-
walk_nodes(query, targets, INCOMING)
309+
walk_nodes(graph, targets, INCOMING)
310310
} else {
311-
query.nodes().into_iter().map(|n| n.kind).collect()
311+
graph.nodes().into_iter().map(|n| n.kind).collect()
312312
}
313313
}
314314

315-
fn walk_nodes<'q>(
316-
query: &'q DepGraphQuery,
317-
starts: &FxIndexSet<&'q DepNode>,
315+
fn walk_nodes<'g>(
316+
graph: &'g RetainedDepGraph,
317+
starts: &FxIndexSet<&'g DepNode>,
318318
direction: Direction,
319319
) -> FxIndexSet<DepKind> {
320320
let mut set = FxIndexSet::default();
321321
for &start in starts {
322322
debug!("walk_nodes: start={:?} outgoing?={:?}", start, direction == OUTGOING);
323323
if set.insert(start.kind) {
324-
let mut stack = vec![query.indices[start]];
324+
let mut stack = vec![graph.indices[start]];
325325
while let Some(index) = stack.pop() {
326-
for (_, edge) in query.graph.adjacent_edges(index, direction) {
326+
for (_, edge) in graph.inner.adjacent_edges(index, direction) {
327327
let neighbor_index = edge.source_or_target(direction);
328-
let neighbor = query.graph.node_data(neighbor_index);
328+
let neighbor = graph.inner.node_data(neighbor_index);
329329
if set.insert(neighbor.kind) {
330330
stack.push(neighbor_index);
331331
}
@@ -336,10 +336,10 @@ fn walk_nodes<'q>(
336336
set
337337
}
338338

339-
fn walk_between<'q>(
340-
query: &'q DepGraphQuery,
341-
sources: &FxIndexSet<&'q DepNode>,
342-
targets: &FxIndexSet<&'q DepNode>,
339+
fn walk_between<'g>(
340+
graph: &'g RetainedDepGraph,
341+
sources: &FxIndexSet<&'g DepNode>,
342+
targets: &FxIndexSet<&'g DepNode>,
343343
) -> FxIndexSet<DepKind> {
344344
// This is a bit tricky. We want to include a node only if it is:
345345
// (a) reachable from a source and (b) will reach a target. And we
@@ -354,27 +354,27 @@ fn walk_between<'q>(
354354
Excluded,
355355
}
356356

357-
let mut node_states = vec![State::Undecided; query.graph.len_nodes()];
357+
let mut node_states = vec![State::Undecided; graph.inner.len_nodes()];
358358

359359
for &target in targets {
360-
node_states[query.indices[target].0] = State::Included;
360+
node_states[graph.indices[target].0] = State::Included;
361361
}
362362

363-
for source in sources.iter().map(|&n| query.indices[n]) {
364-
recurse(query, &mut node_states, source);
363+
for source in sources.iter().map(|&n| graph.indices[n]) {
364+
recurse(graph, &mut node_states, source);
365365
}
366366

367-
return query
367+
return graph
368368
.nodes()
369369
.into_iter()
370370
.filter(|&n| {
371-
let index = query.indices[n];
371+
let index = graph.indices[n];
372372
node_states[index.0] == State::Included
373373
})
374374
.map(|n| n.kind)
375375
.collect();
376376

377-
fn recurse(query: &DepGraphQuery, node_states: &mut [State], node: NodeIndex) -> bool {
377+
fn recurse(graph: &RetainedDepGraph, node_states: &mut [State], node: NodeIndex) -> bool {
378378
match node_states[node.0] {
379379
// known to reach a target
380380
State::Included => return true,
@@ -390,8 +390,8 @@ fn walk_between<'q>(
390390

391391
node_states[node.0] = State::Deciding;
392392

393-
for neighbor_index in query.graph.successor_nodes(node) {
394-
if recurse(query, node_states, neighbor_index) {
393+
for neighbor_index in graph.inner.successor_nodes(node) {
394+
if recurse(graph, node_states, neighbor_index) {
395395
node_states[node.0] = State::Included;
396396
}
397397
}
@@ -407,8 +407,8 @@ fn walk_between<'q>(
407407
}
408408
}
409409

410-
fn filter_edges(query: &DepGraphQuery, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
411-
let uniq: FxIndexSet<_> = query
410+
fn filter_edges(graph: &RetainedDepGraph, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
411+
let uniq: FxIndexSet<_> = graph
412412
.edges()
413413
.into_iter()
414414
.map(|(s, t)| (s.kind, t.kind))

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use tracing::{debug, instrument};
2121
#[cfg(debug_assertions)]
2222
use {super::debug::EdgeFilter, std::env};
2323

24-
use super::query::DepGraphQuery;
24+
use super::retained::RetainedDepGraph;
2525
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
2626
use super::{DepKind, DepNode, WorkProductId, read_deps, with_deps};
2727
use crate::dep_graph::edges::EdgesVec;
@@ -191,9 +191,9 @@ impl DepGraph {
191191
self.data.is_some()
192192
}
193193

194-
pub fn with_query(&self, f: impl Fn(&DepGraphQuery)) {
194+
pub fn with_retained_dep_graph(&self, f: impl Fn(&RetainedDepGraph)) {
195195
if let Some(data) = &self.data {
196-
data.current.encoder.with_query(f)
196+
data.current.encoder.with_retained_dep_graph(f)
197197
}
198198
}
199199

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use self::graph::{
1111
WorkProductMap, hash_result,
1212
};
1313
use self::graph::{MarkFrame, print_markframe_trace};
14-
pub use self::query::DepGraphQuery;
14+
pub use self::retained::RetainedDepGraph;
1515
pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
1616
pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
1717
use crate::ty::print::with_reduced_queries;
@@ -22,7 +22,7 @@ pub mod dep_node;
2222
mod dep_node_key;
2323
mod edges;
2424
mod graph;
25-
mod query;
25+
mod retained;
2626
mod serialized;
2727

2828
/// Describes the contents of the fingerprint generated by a given query.

compiler/rustc_middle/src/dep_graph/query.rs renamed to compiler/rustc_middle/src/dep_graph/retained.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,60 @@ use rustc_index::IndexVec;
44

55
use super::{DepNode, DepNodeIndex};
66

7-
pub struct DepGraphQuery {
8-
pub graph: LinkedGraph<DepNode, ()>,
7+
/// An in-memory copy of the current session's query dependency graph, which
8+
/// is only enabled when `-Zquery-dep-graph` is set (for debugging/testing).
9+
///
10+
/// Normally, dependencies recorded during the current session are written to
11+
/// disk and then forgotten, to avoid wasting memory on information that is
12+
/// not needed when the compiler is working correctly.
13+
pub struct RetainedDepGraph {
14+
pub inner: LinkedGraph<DepNode, ()>,
915
pub indices: FxHashMap<DepNode, NodeIndex>,
1016
pub dep_index_to_index: IndexVec<DepNodeIndex, Option<NodeIndex>>,
1117
}
1218

13-
impl DepGraphQuery {
14-
pub fn new(prev_node_count: usize) -> DepGraphQuery {
19+
impl RetainedDepGraph {
20+
pub fn new(prev_node_count: usize) -> Self {
1521
let node_count = prev_node_count + prev_node_count / 4;
1622
let edge_count = 6 * node_count;
1723

18-
let graph = LinkedGraph::with_capacity(node_count, edge_count);
24+
let inner = LinkedGraph::with_capacity(node_count, edge_count);
1925
let indices = FxHashMap::default();
2026
let dep_index_to_index = IndexVec::new();
2127

22-
DepGraphQuery { graph, indices, dep_index_to_index }
28+
Self { inner, indices, dep_index_to_index }
2329
}
2430

2531
pub fn push(&mut self, index: DepNodeIndex, node: DepNode, edges: &[DepNodeIndex]) {
26-
let source = self.graph.add_node(node);
32+
let source = self.inner.add_node(node);
2733
self.dep_index_to_index.insert(index, source);
2834
self.indices.insert(node, source);
2935

3036
for &target in edges.iter() {
3137
// We may miss the edges that are pushed while the `DepGraphQuery` is being accessed.
3238
// Skip them to issues.
3339
if let Some(&Some(target)) = self.dep_index_to_index.get(target) {
34-
self.graph.add_edge(source, target, ());
40+
self.inner.add_edge(source, target, ());
3541
}
3642
}
3743
}
3844

3945
pub fn nodes(&self) -> Vec<&DepNode> {
40-
self.graph.all_nodes().iter().map(|n| &n.data).collect()
46+
self.inner.all_nodes().iter().map(|n| &n.data).collect()
4147
}
4248

4349
pub fn edges(&self) -> Vec<(&DepNode, &DepNode)> {
44-
self.graph
50+
self.inner
4551
.all_edges()
4652
.iter()
4753
.map(|edge| (edge.source(), edge.target()))
48-
.map(|(s, t)| (self.graph.node_data(s), self.graph.node_data(t)))
54+
.map(|(s, t)| (self.inner.node_data(s), self.inner.node_data(t)))
4955
.collect()
5056
}
5157

5258
fn reachable_nodes(&self, node: &DepNode, direction: Direction) -> Vec<&DepNode> {
5359
if let Some(&index) = self.indices.get(node) {
54-
self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect()
60+
self.inner.depth_traverse(index, direction).map(|s| self.inner.node_data(s)).collect()
5561
} else {
5662
vec![]
5763
}

0 commit comments

Comments
 (0)