Skip to content

Commit eb5f275

Browse files
Rollup merge of #152844 - Zalathar:retain-dep-graph, r=JonathanBrouwer
Rename `DepGraphQuery` to `RetainedDepGraph` This is a revised subset of #152836 that only performs an internal renaming, and does not touch the `-Zquery-dep-graph` flag. The new name and comments for `RetainedDepGraph` should hopefully do a better job of communicating that it is not used in normal compiler operation, even in incremental mode.
2 parents 29871ea + 19e0c62 commit eb5f275

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
@@ -44,7 +44,7 @@ use rustc_hir::attrs::AttributeKind;
4444
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
4545
use rustc_hir::intravisit::{self, Visitor};
4646
use rustc_middle::bug;
47-
use rustc_middle::dep_graph::{DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter};
47+
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeFilter, EdgeFilter, RetainedDepGraph};
4848
use rustc_middle::hir::nested_filter;
4949
use rustc_middle::ty::TyCtxt;
5050
use rustc_span::{Span, Symbol, sym};
@@ -57,7 +57,7 @@ use crate::errors;
5757
pub(crate) fn assert_dep_graph(tcx: TyCtxt<'_>) {
5858
tcx.dep_graph.with_ignore(|| {
5959
if tcx.sess.opts.unstable_opts.dump_dep_graph {
60-
tcx.dep_graph.with_query(dump_graph);
60+
tcx.dep_graph.with_retained_dep_graph(dump_graph);
6161
}
6262

6363
if !tcx.sess.opts.unstable_opts.query_dep_graph {
@@ -184,7 +184,7 @@ fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_wou
184184
}
185185
return;
186186
}
187-
tcx.dep_graph.with_query(|query| {
187+
tcx.dep_graph.with_retained_dep_graph(|query| {
188188
for &(_, source_def_id, ref source_dep_node) in if_this_changed {
189189
let dependents = query.transitive_predecessors(source_dep_node);
190190
for &(target_span, ref target_pass, _, ref target_dep_node) in then_this_would_need {
@@ -202,21 +202,21 @@ fn check_paths<'tcx>(tcx: TyCtxt<'tcx>, if_this_changed: &Sources, then_this_wou
202202
});
203203
}
204204

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

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

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

288288
if filter.accepts_all() {
289289
return None;
290290
}
291291

292-
Some(query.nodes().into_iter().filter(|n| filter.test(n)).collect())
292+
Some(graph.nodes().into_iter().filter(|n| filter.test(n)).collect())
293293
}
294294

295-
fn filter_nodes<'q>(
296-
query: &'q DepGraphQuery,
297-
sources: &Option<FxIndexSet<&'q DepNode>>,
298-
targets: &Option<FxIndexSet<&'q DepNode>>,
295+
fn filter_nodes<'g>(
296+
graph: &'g RetainedDepGraph,
297+
sources: &Option<FxIndexSet<&'g DepNode>>,
298+
targets: &Option<FxIndexSet<&'g DepNode>>,
299299
) -> FxIndexSet<DepKind> {
300300
if let Some(sources) = sources {
301301
if let Some(targets) = targets {
302-
walk_between(query, sources, targets)
302+
walk_between(graph, sources, targets)
303303
} else {
304-
walk_nodes(query, sources, OUTGOING)
304+
walk_nodes(graph, sources, OUTGOING)
305305
}
306306
} else if let Some(targets) = targets {
307-
walk_nodes(query, targets, INCOMING)
307+
walk_nodes(graph, targets, INCOMING)
308308
} else {
309-
query.nodes().into_iter().map(|n| n.kind).collect()
309+
graph.nodes().into_iter().map(|n| n.kind).collect()
310310
}
311311
}
312312

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

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

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

357357
for &target in targets {
358-
node_states[query.indices[target].0] = State::Included;
358+
node_states[graph.indices[target].0] = State::Included;
359359
}
360360

361-
for source in sources.iter().map(|&n| query.indices[n]) {
362-
recurse(query, &mut node_states, source);
361+
for source in sources.iter().map(|&n| graph.indices[n]) {
362+
recurse(graph, &mut node_states, source);
363363
}
364364

365-
return query
365+
return graph
366366
.nodes()
367367
.into_iter()
368368
.filter(|&n| {
369-
let index = query.indices[n];
369+
let index = graph.indices[n];
370370
node_states[index.0] == State::Included
371371
})
372372
.map(|n| n.kind)
373373
.collect();
374374

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

389389
node_states[node.0] = State::Deciding;
390390

391-
for neighbor_index in query.graph.successor_nodes(node) {
392-
if recurse(query, node_states, neighbor_index) {
391+
for neighbor_index in graph.inner.successor_nodes(node) {
392+
if recurse(graph, node_states, neighbor_index) {
393393
node_states[node.0] = State::Included;
394394
}
395395
}
@@ -405,8 +405,8 @@ fn walk_between<'q>(
405405
}
406406
}
407407

408-
fn filter_edges(query: &DepGraphQuery, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
409-
let uniq: FxIndexSet<_> = query
408+
fn filter_edges(graph: &RetainedDepGraph, nodes: &FxIndexSet<DepKind>) -> Vec<(DepKind, DepKind)> {
409+
let uniq: FxIndexSet<_> = graph
410410
.edges()
411411
.into_iter()
412412
.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
@@ -10,7 +10,7 @@ pub use self::graph::{
1010
WorkProductMap, hash_result,
1111
};
1212
use self::graph::{MarkFrame, print_markframe_trace};
13-
pub use self::query::DepGraphQuery;
13+
pub use self::retained::RetainedDepGraph;
1414
pub use self::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
1515
pub use crate::dep_graph::debug::{DepNodeFilter, EdgeFilter};
1616
use crate::ty::print::with_reduced_queries;
@@ -21,7 +21,7 @@ pub(crate) mod dep_node;
2121
mod dep_node_key;
2222
mod edges;
2323
mod graph;
24-
mod query;
24+
mod retained;
2525
mod serialized;
2626

2727
/// 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)