Skip to content

Commit 9ab2c07

Browse files
committed
Create the previous dep graph index on a background thread
1 parent 555e1d0 commit 9ab2c07

File tree

7 files changed

+194
-56
lines changed

7 files changed

+194
-56
lines changed

compiler/rustc_data_structures/src/marker.rs

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ macro_rules! impl_dyn_send {
7070
}
7171

7272
impl_dyn_send!(
73+
[std::thread::JoinHandle<T> where T]
7374
[std::sync::atomic::AtomicPtr<T> where T]
7475
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
7576
[std::sync::mpsc::Sender<T> where T: DynSend]
@@ -152,6 +153,7 @@ macro_rules! impl_dyn_sync {
152153
}
153154

154155
impl_dyn_sync!(
156+
[std::thread::JoinHandle<T> where T]
155157
[std::sync::atomic::AtomicPtr<T> where T]
156158
[std::sync::OnceLock<T> where T: DynSend + DynSync]
157159
[std::sync::Mutex<T> where T: ?Sized + DynSend]

compiler/rustc_incremental/src/persist/load.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ pub enum LoadResult<T> {
3535
LoadDepGraph(PathBuf, std::io::Error),
3636
}
3737

38-
impl<T: Default> LoadResult<T> {
38+
impl<T> LoadResult<T> {
3939
/// Accesses the data returned in [`LoadResult::Ok`].
40-
pub fn open(self, sess: &Session) -> T {
40+
pub fn open(self, sess: &Session, fallback: impl FnOnce() -> T) -> T {
4141
// Check for errors when using `-Zassert-incremental-state`
4242
match (sess.opts.assert_incr_state, &self) {
4343
(Some(IncrementalStateAssertion::NotLoaded), LoadResult::Ok { .. }) => {
@@ -55,14 +55,14 @@ impl<T: Default> LoadResult<T> {
5555
match self {
5656
LoadResult::LoadDepGraph(path, err) => {
5757
sess.dcx().emit_warn(errors::LoadDepGraph { path, err });
58-
Default::default()
58+
fallback()
5959
}
6060
LoadResult::DataOutOfDate => {
6161
if let Err(err) = delete_all_session_dir_contents(sess) {
6262
sess.dcx()
6363
.emit_err(errors::DeleteIncompatible { path: dep_graph_path(sess), err });
6464
}
65-
Default::default()
65+
fallback()
6666
}
6767
LoadResult::Ok { data } => data,
6868
}
@@ -93,13 +93,15 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
9393

9494
fn load_dep_graph(
9595
sess: &Session,
96-
deps: &DepsType,
97-
) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
96+
deps: &Arc<DepsType>,
97+
) -> LoadResult<(Arc<SerializedDepGraph<DepsType>>, WorkProductMap)> {
9898
let prof = sess.prof.clone();
9999

100100
if sess.opts.incremental.is_none() {
101101
// No incremental compilation.
102-
return LoadResult::Ok { data: Default::default() };
102+
return LoadResult::Ok {
103+
data: (Arc::new(SerializedDepGraph::empty(deps, sess)), Default::default()),
104+
};
103105
}
104106

105107
let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
@@ -174,7 +176,7 @@ fn load_dep_graph(
174176
return LoadResult::DataOutOfDate;
175177
}
176178

177-
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder, deps);
179+
let dep_graph = SerializedDepGraph::decode(&mut decoder, deps, sess);
178180

179181
LoadResult::Ok { data: (dep_graph, prev_work_products) }
180182
}
@@ -208,7 +210,7 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache> {
208210

209211
/// Setups the dependency graph by loading an existing graph from disk and set up streaming of a
210212
/// new graph to an incremental session directory.
211-
pub fn setup_dep_graph(sess: &Session, crate_name: Symbol, deps: &DepsType) -> DepGraph {
213+
pub fn setup_dep_graph(sess: &Session, crate_name: Symbol, deps: &Arc<DepsType>) -> DepGraph {
212214
// `load_dep_graph` can only be called after `prepare_session_directory`.
213215
prepare_session_directory(sess, crate_name);
214216

@@ -227,7 +229,8 @@ pub fn setup_dep_graph(sess: &Session, crate_name: Symbol, deps: &DepsType) -> D
227229
}
228230

229231
res.and_then(|result| {
230-
let (prev_graph, prev_work_products) = result.open(sess);
232+
let (prev_graph, prev_work_products) = result
233+
.open(sess, || (Arc::new(SerializedDepGraph::empty(deps, sess)), Default::default()));
231234
build_dep_graph(sess, prev_graph, prev_work_products)
232235
})
233236
.unwrap_or_else(DepGraph::new_disabled)

compiler/rustc_incremental/src/persist/save.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44
use rustc_data_structures::fx::FxIndexMap;
55
use rustc_data_structures::sync::join;
66
use rustc_middle::dep_graph::{
7-
DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
7+
DepGraph, DepsType, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
88
};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_serialize::Encodable as RustcEncodable;
@@ -148,7 +148,7 @@ fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult
148148
/// and moves it to the permanent dep-graph path
149149
pub(crate) fn build_dep_graph(
150150
sess: &Session,
151-
prev_graph: Arc<SerializedDepGraph>,
151+
prev_graph: Arc<SerializedDepGraph<DepsType>>,
152152
prev_work_products: WorkProductMap,
153153
) -> Option<DepGraph> {
154154
if sess.opts.incremental.is_none() {

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
803803

804804
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
805805

806-
let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
806+
let dep_type = Arc::new(DepsType { dep_names: rustc_query_impl::dep_kind_names() });
807807
let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
808808

809809
let cstore =

compiler/rustc_query_system/src/dep_graph/graph.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) struct DepGraphData<D: Deps> {
9191

9292
/// The dep-graph from the previous compilation session. It contains all
9393
/// nodes and edges as well as all fingerprints of nodes that have them.
94-
previous: Arc<SerializedDepGraph>,
94+
previous: Arc<SerializedDepGraph<D>>,
9595

9696
colors: DepNodeColorMap,
9797

@@ -121,7 +121,7 @@ where
121121
impl<D: Deps> DepGraph<D> {
122122
pub fn new(
123123
session: &Session,
124-
prev_graph: Arc<SerializedDepGraph>,
124+
prev_graph: Arc<SerializedDepGraph<D>>,
125125
prev_work_products: WorkProductMap,
126126
encoder: FileEncoder,
127127
record_graph: bool,
@@ -1190,7 +1190,7 @@ impl<D: Deps> CurrentDepGraph<D> {
11901190
encoder: FileEncoder,
11911191
record_graph: bool,
11921192
record_stats: bool,
1193-
previous: Arc<SerializedDepGraph>,
1193+
previous: Arc<SerializedDepGraph<D>>,
11941194
) -> Self {
11951195
let mut stable_hasher = StableHasher::new();
11961196
previous.session_count().hash(&mut stable_hasher);
@@ -1286,7 +1286,7 @@ impl<D: Deps> CurrentDepGraph<D> {
12861286
#[inline]
12871287
fn debug_assert_not_in_new_nodes(
12881288
&self,
1289-
prev_graph: &SerializedDepGraph,
1289+
prev_graph: &SerializedDepGraph<D>,
12901290
prev_index: SerializedDepNodeIndex,
12911291
) {
12921292
if let Some(ref nodes_in_current_session) = self.nodes_in_current_session {

compiler/rustc_query_system/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub trait DepContext: Copy {
8989
}
9090
}
9191

92-
pub trait Deps {
92+
pub trait Deps: Send + Sync + 'static {
9393
/// Execute the operation with provided dependencies.
9494
fn with_deps<OP, R>(deps: TaskDepsRef<'_>, op: OP) -> R
9595
where

0 commit comments

Comments
 (0)