Skip to content

Commit bde2a86

Browse files
committed
Create the previous dep graph index on a background thread
1 parent 3ef8e64 commit bde2a86

File tree

7 files changed

+194
-57
lines changed

7 files changed

+194
-57
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;
@@ -144,7 +144,7 @@ fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult
144144
/// and moves it to the permanent dep-graph path
145145
pub(crate) fn build_dep_graph(
146146
sess: &Session,
147-
prev_graph: Arc<SerializedDepGraph>,
147+
prev_graph: Arc<SerializedDepGraph<DepsType>>,
148148
prev_work_products: WorkProductMap,
149149
) -> Option<DepGraph> {
150150
if sess.opts.incremental.is_none() {

compiler/rustc_interface/src/passes.rs

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

829829
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
830830

831-
let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
831+
let dep_type = Arc::new(DepsType { dep_names: rustc_query_impl::dep_kind_names() });
832832
let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
833833

834834
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
) -> DepGraph<D> {
@@ -1171,7 +1171,7 @@ impl<D: Deps> CurrentDepGraph<D> {
11711171
session: &Session,
11721172
prev_graph_node_count: usize,
11731173
encoder: FileEncoder,
1174-
previous: Arc<SerializedDepGraph>,
1174+
previous: Arc<SerializedDepGraph<D>>,
11751175
) -> Self {
11761176
let mut stable_hasher = StableHasher::new();
11771177
previous.session_count().hash(&mut stable_hasher);
@@ -1260,7 +1260,7 @@ impl<D: Deps> CurrentDepGraph<D> {
12601260
#[inline]
12611261
fn debug_assert_not_in_new_nodes(
12621262
&self,
1263-
prev_graph: &SerializedDepGraph,
1263+
prev_graph: &SerializedDepGraph<D>,
12641264
prev_index: SerializedDepNodeIndex,
12651265
) {
12661266
if let Some(ref nodes_in_current_session) = self.nodes_in_current_session {

compiler/rustc_query_system/src/dep_graph/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) use graph::DepGraphData;
1212
pub use graph::{DepGraph, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result};
1313
pub use query::DepGraphQuery;
1414
use rustc_data_structures::profiling::SelfProfilerRef;
15-
use rustc_data_structures::sync::DynSync;
15+
use rustc_data_structures::sync::{DynSend, DynSync};
1616
use rustc_session::Session;
1717
pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
1818
use tracing::instrument;
@@ -90,7 +90,7 @@ pub trait DepContext: Copy {
9090
}
9191
}
9292

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

0 commit comments

Comments
 (0)