Skip to content

Commit 9a80997

Browse files
committed
Introduce non-incremental queries and load the dep graph with a query
1 parent 391fc05 commit 9a80997

File tree

33 files changed

+358
-259
lines changed

33 files changed

+358
-259
lines changed

src/librustc/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ macro_rules! arena_types {
3131
rustc::hir::def_id::DefId,
3232
rustc::ty::subst::SubstsRef<$tcx>
3333
)>,
34+
[few] dep_graph: rustc::dep_graph::DepGraph,
3435
[few] lowered_hir: rustc::hir::LoweredHir,
3536
[few] hir_map: rustc::hir::map::Map<$tcx>,
3637
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,

src/librustc/dep_graph/dep_node.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ macro_rules! define_dep_nodes {
224224
(tcx.sess.opts.debugging_opts.incremental_info ||
225225
tcx.sess.opts.debugging_opts.query_dep_graph)
226226
{
227-
tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
227+
tcx.dep_graph().register_dep_node_debug_str(dep_node, || {
228228
arg.to_debug_str(tcx)
229229
});
230230
}
@@ -247,7 +247,7 @@ macro_rules! define_dep_nodes {
247247
(tcx.sess.opts.debugging_opts.incremental_info ||
248248
tcx.sess.opts.debugging_opts.query_dep_graph)
249249
{
250-
tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
250+
tcx.dep_graph().register_dep_node_debug_str(dep_node, || {
251251
tupled_args.to_debug_str(tcx)
252252
});
253253
}
@@ -368,7 +368,7 @@ impl fmt::Debug for DepNode {
368368
if let Some(tcx) = opt_tcx {
369369
if let Some(def_id) = self.extract_def_id(tcx) {
370370
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
371-
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*self) {
371+
} else if let Some(ref s) = tcx.dep_graph().dep_node_debug_str(*self) {
372372
write!(f, "{}", s)?;
373373
} else {
374374
write!(f, "{}", self.hash)?;
@@ -402,6 +402,10 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
402402
// We use this for most things when incr. comp. is turned off.
403403
[] Null,
404404

405+
// Represents all queries which are not incremental.
406+
// This is always treated as a red dep node.
407+
[] NonIncremental,
408+
405409
// Represents the `Krate` as a whole (the `hir::Krate` value) (as
406410
// distinct from the krate module). This is basically a hash of
407411
// the entire krate, so if you read from `Krate` (e.g., by calling
@@ -430,8 +434,6 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
430434
[anon] TraitSelect,
431435

432436
[] CompileCodegenUnit(InternedString),
433-
434-
[eval_always] Analysis(CrateNum),
435437
]);
436438

437439
pub trait RecoverKey<'tcx>: Sized {

src/librustc/dep_graph/graph.rs

+70-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ use super::safe::DepGraphSafe;
2020
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
2121
use super::prev::PreviousDepGraph;
2222

23+
pub type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;
24+
25+
pub enum LoadResult<T> {
26+
Ok { data: T },
27+
DataOutOfDate,
28+
Error { message: String },
29+
}
30+
31+
/// Either a result that has already be computed or a
32+
/// handle that will let us wait until it is computed
33+
/// by a background thread.
34+
pub enum MaybeAsync<T> {
35+
Sync(T),
36+
Async(std::thread::JoinHandle<T>)
37+
}
38+
impl<T> MaybeAsync<T> {
39+
pub fn open(self) -> std::thread::Result<T> {
40+
match self {
41+
MaybeAsync::Sync(result) => Ok(result),
42+
MaybeAsync::Async(handle) => handle.join()
43+
}
44+
}
45+
}
46+
47+
pub type DepGraphFuture = MaybeAsync<LoadResult<(PreviousDepGraph, WorkProductMap)>>;
48+
2349
#[derive(Clone)]
2450
pub struct DepGraph {
2551
data: Option<Lrc<DepGraphData>>,
@@ -30,7 +56,7 @@ newtype_index! {
3056
}
3157

3258
impl DepNodeIndex {
33-
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
59+
pub(crate) const INVALID: DepNodeIndex = DepNodeIndex::MAX;
3460
}
3561

3662
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -94,17 +120,29 @@ impl DepGraph {
94120
prev_work_products: FxHashMap<WorkProductId, WorkProduct>) -> DepGraph {
95121
let prev_graph_node_count = prev_graph.node_count();
96122

123+
let mut data = DepGraphData {
124+
previous_work_products: prev_work_products,
125+
dep_node_debug: Default::default(),
126+
current: Lock::new(CurrentDepGraph::new(prev_graph_node_count)),
127+
emitted_diagnostics: Default::default(),
128+
emitted_diagnostics_cond_var: Condvar::new(),
129+
previous: prev_graph,
130+
colors: DepNodeColorMap::new(prev_graph_node_count),
131+
loaded_from_cache: Default::default(),
132+
};
133+
134+
let non_incr_dep_node = DepNode::new_no_params(DepKind::NonIncremental);
135+
136+
// Allocate the NonIncremental node
137+
data.current.get_mut().alloc_node(non_incr_dep_node, smallvec![], Fingerprint::ZERO);
138+
139+
data.previous.node_to_index_opt(&non_incr_dep_node).map(|prev_index| {
140+
// Color previous NonIncremental node as red
141+
data.colors.insert(prev_index, DepNodeColor::Red);
142+
});
143+
97144
DepGraph {
98-
data: Some(Lrc::new(DepGraphData {
99-
previous_work_products: prev_work_products,
100-
dep_node_debug: Default::default(),
101-
current: Lock::new(CurrentDepGraph::new(prev_graph_node_count)),
102-
emitted_diagnostics: Default::default(),
103-
emitted_diagnostics_cond_var: Condvar::new(),
104-
previous: prev_graph,
105-
colors: DepNodeColorMap::new(prev_graph_node_count),
106-
loaded_from_cache: Default::default(),
107-
})),
145+
data: Some(Lrc::new(data)),
108146
}
109147
}
110148

@@ -135,18 +173,21 @@ impl DepGraph {
135173
DepGraphQuery::new(&nodes[..], &edges[..])
136174
}
137175

138-
pub fn assert_ignored(&self)
139-
{
140-
if let Some(..) = self.data {
141-
ty::tls::with_context_opt(|icx| {
142-
let icx = if let Some(icx) = icx { icx } else { return };
143-
assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
144-
})
145-
}
176+
pub fn assert_ignored() {
177+
ty::tls::with_context_opt(|icx| {
178+
let icx = if let Some(icx) = icx { icx } else { return };
179+
assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
180+
})
146181
}
147182

148183
pub fn with_ignore<OP,R>(&self, op: OP) -> R
149184
where OP: FnOnce() -> R
185+
{
186+
Self::ignore_deps(op)
187+
}
188+
189+
pub fn ignore_deps<OP,R>(op: OP) -> R
190+
where OP: FnOnce() -> R
150191
{
151192
ty::tls::with_context(|icx| {
152193
let icx = ty::tls::ImplicitCtxt {
@@ -394,6 +435,16 @@ impl DepGraph {
394435
hash_result)
395436
}
396437

438+
#[inline]
439+
pub fn read_non_incr(tcx: TyCtxt<'_>) {
440+
// Avoid loading the `dep_graph` here if we don't need to track dependencies.
441+
// We want to load the `dep_graph` in the background.
442+
if ty::tls::with_context(|icx| icx.task_deps.is_none()) {
443+
return;
444+
}
445+
tcx.dep_graph().read(DepNode::new_no_params(DepKind::NonIncremental));
446+
}
447+
397448
#[inline]
398449
pub fn read(&self, v: DepNode) {
399450
if let Some(ref data) = self.data {

src/librustc/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod cgu_reuse_tracker;
1111
pub use self::dep_tracking_map::{DepTrackingMap, DepTrackingMapConfig};
1212
pub use self::dep_node::{DepNode, DepKind, DepConstructor, WorkProductId, RecoverKey, label_strs};
1313
pub use self::graph::{DepGraph, WorkProduct, DepNodeIndex, DepNodeColor, TaskDeps, hash_result};
14-
pub use self::graph::WorkProductFileKind;
14+
pub use self::graph::{WorkProductFileKind, DepGraphFuture, LoadResult, WorkProductMap, MaybeAsync};
1515
pub use self::prev::PreviousDepGraph;
1616
pub use self::query::DepGraphQuery;
1717
pub use self::safe::AssertDepGraphSafe;

src/librustc/hir/lowering.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,13 @@ impl<'a> ImplTraitContext<'a> {
232232
pub fn lower_crate(
233233
sess: &Session,
234234
cstore: &dyn CrateStore,
235-
dep_graph: &DepGraph,
236235
krate: &Crate,
237236
resolver: &mut dyn Resolver,
238237
) -> hir::Crate {
239238
// We're constructing the HIR here; we don't care what we will
240239
// read, since we haven't even constructed the *input* to
241240
// incr. comp. yet.
242-
dep_graph.assert_ignored();
241+
DepGraph::assert_ignored();
243242

244243
LoweringContext {
245244
crate_root: std_inject::injected_crate_name().map(Symbol::intern),

src/librustc/hir/map/hir_id_validator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
22
use crate::hir::{self, intravisit, HirId, ItemLocalId};
3+
use crate::dep_graph::DepGraph;
34
use crate::hir::itemlikevisit::ItemLikeVisitor;
45
use rustc_data_structures::fx::FxHashSet;
56
use rustc_data_structures::sync::{Lock, ParallelIterator, par_iter};
67

78
pub fn check_crate(hir_map: &hir::map::Map<'_>) {
8-
hir_map.dep_graph.assert_ignored();
9+
DepGraph::assert_ignored();
910

1011
let errors = Lock::new(Vec::new());
1112

src/librustc/hir/map/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
11491149
let mut collector = NodeCollector::root(
11501150
tcx.sess,
11511151
krate,
1152-
&tcx.dep_graph,
1152+
&tcx.dep_graph(),
11531153
&hir.defs,
11541154
&hir_to_node_id,
11551155
hcx
@@ -1167,7 +1167,7 @@ pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
11671167

11681168
let map = Map {
11691169
forest: &hir.forest,
1170-
dep_graph: tcx.dep_graph.clone(),
1170+
dep_graph: tcx.dep_graph().clone(),
11711171
crate_hash,
11721172
map,
11731173
hir_to_node_id,

src/librustc/mir/mono.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl<'tcx> CodegenUnit<'tcx> {
361361

362362
pub fn work_product(&self, tcx: TyCtxt<'_>) -> WorkProduct {
363363
let work_product_id = self.work_product_id();
364-
tcx.dep_graph
364+
tcx.dep_graph()
365365
.previous_work_product(&work_product_id)
366366
.unwrap_or_else(|| {
367367
panic!("Could not find work-product for CGU `{}`", self.name())

src/librustc/query/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ use syntax_pos::symbol::InternedString;
3131
// as they will raise an fatal error on query cycles instead.
3232
rustc_queries! {
3333
Other {
34+
query dep_graph_future(_: ()) -> Lrc<Steal<Option<DepGraphFuture>>> {
35+
no_hash
36+
eval_always
37+
desc { "loading the dependency graph in the background" }
38+
}
39+
40+
query load_dep_graph(_: ()) -> &'tcx DepGraph {
41+
no_hash
42+
eval_always
43+
desc { "loading the dependency graph" }
44+
}
45+
3446
query parse(_: ()) -> Result<Lrc<Steal<ast::Crate>>, ErrorReported> {
3547
no_hash
3648
eval_always
@@ -77,6 +89,13 @@ rustc_queries! {
7789
desc { "indexing HIR" }
7890
}
7991

92+
/// Run analysis passes on the crate
93+
query analysis(_: CrateNum) -> Result<(), ErrorReported> {
94+
no_hash
95+
eval_always
96+
desc { "running analysis passes on this crate" }
97+
}
98+
8099
/// Records the type of every item.
81100
query type_of(key: DefId) -> Ty<'tcx> {
82101
cache_on_disk_if { key.is_local() }

src/librustc/traits/select.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1318,10 +1318,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13181318
where
13191319
OP: FnOnce(&mut Self) -> R,
13201320
{
1321-
let (result, dep_node) = self.tcx()
1322-
.dep_graph
1323-
.with_anon_task(DepKind::TraitSelect, || op(self));
1324-
self.tcx().dep_graph.read_index(dep_node);
1321+
let dep_graph = self.tcx().dep_graph();
1322+
let (result, dep_node) = dep_graph.with_anon_task(DepKind::TraitSelect, || op(self));
1323+
dep_graph.read_index(dep_node);
13251324
(result, dep_node)
13261325
}
13271326

@@ -4328,7 +4327,7 @@ impl<T: Clone> WithDepNode<T> {
43284327
}
43294328

43304329
pub fn get(&self, tcx: TyCtxt<'_>) -> T {
4331-
tcx.dep_graph.read_index(self.dep_node);
4330+
tcx.dep_graph().read_index(self.dep_node);
43324331
self.cached_value.clone()
43334332
}
43344333
}

0 commit comments

Comments
 (0)