Skip to content

Commit 7265806

Browse files
committed
Create the previous dep graph index on a background thread
1 parent 36e2b8a commit 7265806

6 files changed

Lines changed: 188 additions & 54 deletions

File tree

compiler/rustc_data_structures/src/marker.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ macro_rules! impl_dyn_send {
7171
}
7272

7373
impl_dyn_send!(
74+
[std::thread::JoinHandle<T> where T]
7475
[std::sync::atomic::AtomicPtr<T> where T]
7576
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
7677
[std::sync::mpsc::Sender<T> where T: DynSend]
@@ -154,6 +155,7 @@ macro_rules! impl_dyn_sync {
154155
}
155156

156157
impl_dyn_sync!(
158+
[std::thread::JoinHandle<T> where T]
157159
[std::sync::atomic::AtomicPtr<T> where T]
158160
[std::sync::OnceLock<T> where T: DynSend + DynSync]
159161
[std::sync::Mutex<T> where T: ?Sized + DynSend]

compiler/rustc_incremental/src/persist/load.rs

Lines changed: 13 additions & 8 deletions
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
}
@@ -91,12 +91,16 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
9191
work_product::delete_workproduct_files(sess, &swp.work_product);
9292
}
9393

94-
fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
94+
fn load_dep_graph(
95+
sess: &Session,
96+
) -> LoadResult<(Arc<SerializedDepGraph<DepsType>>, WorkProductMap)> {
9597
let prof = sess.prof.clone();
9698

9799
if sess.opts.incremental.is_none() {
98100
// No incremental compilation.
99-
return LoadResult::Ok { data: Default::default() };
101+
return LoadResult::Ok {
102+
data: (Arc::new(SerializedDepGraph::empty(sess)), Default::default()),
103+
};
100104
}
101105

102106
let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
@@ -171,7 +175,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
171175
return LoadResult::DataOutOfDate;
172176
}
173177

174-
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder);
178+
let dep_graph = SerializedDepGraph::decode(&mut decoder, sess);
175179

176180
LoadResult::Ok { data: (dep_graph, prev_work_products) }
177181
}
@@ -228,7 +232,8 @@ pub fn setup_dep_graph(
228232
}
229233

230234
res.and_then(|result| {
231-
let (prev_graph, prev_work_products) = result.open(sess);
235+
let (prev_graph, prev_work_products) =
236+
result.open(sess, || (Arc::new(SerializedDepGraph::empty(sess)), Default::default()));
232237
build_dep_graph(sess, prev_graph, prev_work_products)
233238
})
234239
.unwrap_or_else(DepGraph::new_disabled)

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 2 additions & 2 deletions
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_query_system/src/dep_graph/graph.rs

Lines changed: 4 additions & 4 deletions
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> {
@@ -1192,7 +1192,7 @@ impl<D: Deps> CurrentDepGraph<D> {
11921192
session: &Session,
11931193
prev_graph_node_count: usize,
11941194
encoder: FileEncoder,
1195-
previous: Arc<SerializedDepGraph>,
1195+
previous: Arc<SerializedDepGraph<D>>,
11961196
) -> Self {
11971197
let mut stable_hasher = StableHasher::new();
11981198
previous.session_count().hash(&mut stable_hasher);
@@ -1281,7 +1281,7 @@ impl<D: Deps> CurrentDepGraph<D> {
12811281
#[inline]
12821282
fn debug_assert_not_in_new_nodes(
12831283
&self,
1284-
prev_graph: &SerializedDepGraph,
1284+
prev_graph: &SerializedDepGraph<D>,
12851285
prev_index: SerializedDepNodeIndex,
12861286
) {
12871287
if let Some(ref nodes_in_current_session) = self.nodes_in_current_session {

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 2 additions & 2 deletions
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;
@@ -92,7 +92,7 @@ pub trait DepContext: Copy {
9292
fn with_reduced_queries<T>(self, _: impl FnOnce() -> T) -> T;
9393
}
9494

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

0 commit comments

Comments
 (0)