Skip to content

Commit 203e1ce

Browse files
committed
Introduce a OnDiskCache trait.
1 parent 268ab6d commit 203e1ce

File tree

7 files changed

+77
-76
lines changed

7 files changed

+77
-76
lines changed

compiler/rustc_incremental/src/persist/save.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,5 @@ fn encode_work_product_index(
235235
}
236236

237237
fn encode_query_cache(tcx: TyCtxt<'_>, encoder: &mut Encoder) {
238-
tcx.sess.time("incr_comp_serialize_result_cache", || {
239-
tcx.serialize_query_result_cache(encoder).unwrap();
240-
})
238+
tcx.sess.time("incr_comp_serialize_result_cache", || tcx.serialize_query_result_cache(encoder))
241239
}

compiler/rustc_middle/src/dep_graph/mod.rs

+7-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::ich::StableHashingContext;
22
use crate::ty::query::try_load_from_on_disk_cache;
3+
use crate::ty::query::OnDiskCache;
34
use crate::ty::{self, TyCtxt};
45
use rustc_data_structures::profiling::SelfProfilerRef;
56
use rustc_data_structures::sync::Lock;
6-
use rustc_data_structures::thin_vec::ThinVec;
7-
use rustc_errors::Diagnostic;
87
use rustc_hir::def_id::{DefPathHash, LocalDefId};
98
use rustc_session::Session;
109

@@ -90,6 +89,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
9089

9190
impl<'tcx> DepContext for TyCtxt<'tcx> {
9291
type DepKind = DepKind;
92+
type OnDiskCache = OnDiskCache<'tcx>;
9393
type StableHashingContext = StableHashingContext<'tcx>;
9494

9595
fn register_reused_dep_path_hash(&self, hash: DefPathHash) {
@@ -102,6 +102,11 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
102102
TyCtxt::create_stable_hashing_context(*self)
103103
}
104104

105+
#[inline(always)]
106+
fn on_disk_cache(&self) -> Option<&OnDiskCache<'tcx>> {
107+
self.queries.on_disk_cache.as_ref()
108+
}
109+
105110
#[inline(always)]
106111
fn profiler(&self) -> &SelfProfilerRef {
107112
&self.prof
@@ -163,30 +168,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
163168
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
164169
try_load_from_on_disk_cache(*self, dep_node)
165170
}
166-
167-
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
168-
self.queries
169-
.on_disk_cache
170-
.as_ref()
171-
.map(|c| c.load_diagnostics(*self, prev_dep_node_index))
172-
.unwrap_or_default()
173-
}
174-
175-
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
176-
if let Some(c) = self.queries.on_disk_cache.as_ref() {
177-
c.store_diagnostics(dep_node_index, diagnostics)
178-
}
179-
}
180-
181-
fn store_diagnostics_for_anon_node(
182-
&self,
183-
dep_node_index: DepNodeIndex,
184-
diagnostics: ThinVec<Diagnostic>,
185-
) {
186-
if let Some(c) = self.queries.on_disk_cache.as_ref() {
187-
c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
188-
}
189-
}
190171
}
191172

192173
fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {

compiler/rustc_middle/src/ty/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_hir::{
4747
};
4848
use rustc_index::vec::{Idx, IndexVec};
4949
use rustc_macros::HashStable;
50+
use rustc_query_system::dep_graph::OnDiskCache as _;
5051
use rustc_serialize::opaque;
5152
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
5253
use rustc_session::lint::{Level, Lint};
@@ -1336,8 +1337,8 @@ impl<'tcx> TyCtxt<'tcx> {
13361337
}
13371338
}
13381339

1339-
pub fn serialize_query_result_cache(self, encoder: &mut opaque::Encoder) -> Result<(), !> {
1340-
self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(Ok(()))
1340+
pub fn serialize_query_result_cache(self, encoder: &mut opaque::Encoder) {
1341+
self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(())
13411342
}
13421343

13431344
/// If `true`, we should use the MIR-based borrowck, but also

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,12 @@ impl<'sess> OnDiskCache<'sess> {
239239
def_path_hash_to_def_id_cache: Default::default(),
240240
}
241241
}
242+
}
242243

243-
crate fn serialize<'tcx>(
244-
&self,
245-
tcx: TyCtxt<'tcx>,
246-
encoder: &mut opaque::Encoder,
247-
) -> Result<(), !> {
244+
impl<'tcx> rustc_query_system::dep_graph::OnDiskCache<TyCtxt<'tcx>> for OnDiskCache<'tcx> {
245+
fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut opaque::Encoder) {
248246
// Serializing the `DepGraph` should not modify it.
249-
tcx.dep_graph.with_ignore(|| {
247+
let ret: Result<(), !> = tcx.dep_graph.with_ignore(|| {
250248
// Allocate `SourceFileIndex`es.
251249
let (file_to_file_index, file_index_to_stable_id) = {
252250
let files = tcx.sess.source_map().files();
@@ -418,11 +416,12 @@ impl<'sess> OnDiskCache<'sess> {
418416
cnums.dedup();
419417
cnums
420418
}
421-
})
419+
});
420+
ret.unwrap()
422421
}
423422

424423
/// Loads a diagnostic emitted during the previous compilation session.
425-
crate fn load_diagnostics(
424+
fn load_diagnostics(
426425
&self,
427426
tcx: TyCtxt<'_>,
428427
dep_node_index: SerializedDepNodeIndex,
@@ -438,16 +437,32 @@ impl<'sess> OnDiskCache<'sess> {
438437
/// the next compilation session.
439438
#[inline(never)]
440439
#[cold]
441-
crate fn store_diagnostics(
440+
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
441+
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
442+
let prev = current_diagnostics.insert(dep_node_index, diagnostics.into());
443+
debug_assert!(prev.is_none());
444+
}
445+
446+
/// Stores a diagnostic emitted during computation of an anonymous query.
447+
/// Since many anonymous queries can share the same `DepNode`, we aggregate
448+
/// them -- as opposed to regular queries where we assume that there is a
449+
/// 1:1 relationship between query-key and `DepNode`.
450+
#[inline(never)]
451+
#[cold]
452+
fn store_diagnostics_for_anon_node(
442453
&self,
443454
dep_node_index: DepNodeIndex,
444455
diagnostics: ThinVec<Diagnostic>,
445456
) {
446457
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
447-
let prev = current_diagnostics.insert(dep_node_index, diagnostics.into());
448-
debug_assert!(prev.is_none());
458+
459+
let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new());
460+
461+
x.extend(Into::<Vec<_>>::into(diagnostics));
449462
}
463+
}
450464

465+
impl<'sess> OnDiskCache<'sess> {
451466
fn get_raw_def_id(&self, hash: &DefPathHash) -> Option<RawDefId> {
452467
self.foreign_def_path_hashes.get(hash).copied()
453468
}
@@ -500,24 +515,6 @@ impl<'sess> OnDiskCache<'sess> {
500515
self.load_indexed(tcx, dep_node_index, &self.query_result_index, "query result")
501516
}
502517

503-
/// Stores a diagnostic emitted during computation of an anonymous query.
504-
/// Since many anonymous queries can share the same `DepNode`, we aggregate
505-
/// them -- as opposed to regular queries where we assume that there is a
506-
/// 1:1 relationship between query-key and `DepNode`.
507-
#[inline(never)]
508-
#[cold]
509-
crate fn store_diagnostics_for_anon_node(
510-
&self,
511-
dep_node_index: DepNodeIndex,
512-
diagnostics: ThinVec<Diagnostic>,
513-
) {
514-
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
515-
516-
let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new());
517-
518-
x.extend(Into::<Vec<_>>::into(diagnostics));
519-
}
520-
521518
fn load_indexed<'tcx, T>(
522519
&self,
523520
tcx: TyCtxt<'tcx>,

compiler/rustc_query_system/src/dep_graph/graph.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::debug::EdgeFilter;
2121
use super::prev::PreviousDepGraph;
2222
use super::query::DepGraphQuery;
2323
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
24-
use super::{DepContext, DepKind, DepNode, WorkProductId};
24+
use super::{DepContext, DepKind, DepNode, OnDiskCache, WorkProductId};
2525

2626
#[derive(Clone)]
2727
pub struct DepGraph<K: DepKind> {
@@ -703,7 +703,10 @@ impl<K: DepKind> DepGraph<K> {
703703

704704
// FIXME: Store the fact that a node has diagnostics in a bit in the dep graph somewhere
705705
// Maybe store a list on disk and encode this fact in the DepNodeState
706-
let diagnostics = tcx.load_diagnostics(prev_dep_node_index);
706+
let diagnostics = tcx
707+
.on_disk_cache()
708+
.map(|c| c.load_diagnostics(tcx, prev_dep_node_index))
709+
.unwrap_or(Vec::new());
707710

708711
#[cfg(not(parallel_compiler))]
709712
debug_assert!(
@@ -751,7 +754,9 @@ impl<K: DepKind> DepGraph<K> {
751754
mem::drop(emitting);
752755

753756
// Promote the previous diagnostics to the current session.
754-
tcx.store_diagnostics(dep_node_index, diagnostics.clone().into());
757+
if let Some(c) = tcx.on_disk_cache() {
758+
c.store_diagnostics(dep_node_index, diagnostics.clone().into());
759+
}
755760

756761
let handle = tcx.sess().diagnostic();
757762

compiler/rustc_query_system/src/dep_graph/mod.rs

+25-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
1515
use rustc_data_structures::sync::Lock;
1616
use rustc_data_structures::thin_vec::ThinVec;
1717
use rustc_errors::Diagnostic;
18+
use rustc_serialize::opaque;
1819
use rustc_session::Session;
1920
use rustc_span::def_id::DefPathHash;
2021

@@ -23,6 +24,7 @@ use std::hash::Hash;
2324

2425
pub trait DepContext: Copy {
2526
type DepKind: self::DepKind;
27+
type OnDiskCache: self::OnDiskCache<Self>;
2628
type StableHashingContext;
2729

2830
/// Create a hashing context for hashing new results.
@@ -36,24 +38,37 @@ pub trait DepContext: Copy {
3638
/// Load data from the on-disk cache.
3739
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>);
3840

39-
/// Load diagnostics associated to the node in the previous session.
40-
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic>;
41+
/// Access the on_disk_cache.
42+
fn on_disk_cache(&self) -> Option<&Self::OnDiskCache>;
4143

42-
/// Register diagnostics for the given node, for use in next session.
44+
/// Access the profiler.
45+
fn profiler(&self) -> &SelfProfilerRef;
46+
47+
/// Access the compiler session.
48+
fn sess(&self) -> &Session;
49+
}
50+
51+
pub trait OnDiskCache<CTX> {
52+
fn serialize(&self, tcx: CTX, encoder: &mut opaque::Encoder);
53+
54+
/// Loads a diagnostic emitted during the previous compilation session.
55+
fn load_diagnostics(&self, tcx: CTX, dep_node_index: SerializedDepNodeIndex)
56+
-> Vec<Diagnostic>;
57+
58+
/// Stores a diagnostic emitted during the current compilation session.
59+
/// Anything stored like this will be available via `load_diagnostics` in
60+
/// the next compilation session.
4361
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>);
4462

45-
/// Register diagnostics for the given node, for use in next session.
63+
/// Stores a diagnostic emitted during computation of an anonymous query.
64+
/// Since many anonymous queries can share the same `DepNode`, we aggregate
65+
/// them -- as opposed to regular queries where we assume that there is a
66+
/// 1:1 relationship between query-key and `DepNode`.
4667
fn store_diagnostics_for_anon_node(
4768
&self,
4869
dep_node_index: DepNodeIndex,
4970
diagnostics: ThinVec<Diagnostic>,
5071
);
51-
52-
/// Access the profiler.
53-
fn profiler(&self) -> &SelfProfilerRef;
54-
55-
/// Access the compiler session.
56-
fn sess(&self) -> &Session;
5772
}
5873

5974
/// Describe the different families of dependency nodes.

compiler/rustc_query_system/src/query/plumbing.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5-
use crate::dep_graph::{DepKind, DepNode};
5+
use crate::dep_graph::{DepKind, DepNode, OnDiskCache};
66
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
77
use crate::query::caches::QueryCache;
88
use crate::query::config::{QueryDescription, QueryVtable, QueryVtableExt};
@@ -440,7 +440,9 @@ where
440440
tcx.dep_graph().read_index(dep_node_index);
441441

442442
if unlikely!(!diagnostics.is_empty()) {
443-
tcx.store_diagnostics_for_anon_node(dep_node_index, diagnostics);
443+
if let Some(c) = tcx.on_disk_cache() {
444+
c.store_diagnostics_for_anon_node(dep_node_index, diagnostics);
445+
}
444446
}
445447

446448
return job.complete(result, dep_node_index);
@@ -613,7 +615,9 @@ where
613615
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
614616

615617
if unlikely!(!diagnostics.is_empty()) && dep_node.kind != DepKind::NULL {
616-
tcx.store_diagnostics(dep_node_index, diagnostics);
618+
if let Some(c) = tcx.on_disk_cache() {
619+
c.store_diagnostics(dep_node_index, diagnostics);
620+
}
617621
}
618622

619623
let result = job.complete(result, dep_node_index);

0 commit comments

Comments
 (0)