Skip to content

Create the previous dep graph index on a background thread #116375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_data_structures/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ macro_rules! impl_dyn_send {
}

impl_dyn_send!(
[std::thread::JoinHandle<T> where T]
[std::sync::atomic::AtomicPtr<T> where T]
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
[std::sync::mpsc::Sender<T> where T: DynSend]
Expand Down Expand Up @@ -152,6 +153,7 @@ macro_rules! impl_dyn_sync {
}

impl_dyn_sync!(
[std::thread::JoinHandle<T> where T]
[std::sync::atomic::AtomicPtr<T> where T]
[std::sync::OnceLock<T> where T: DynSend + DynSync]
[std::sync::Mutex<T> where T: ?Sized + DynSend]
Expand Down
23 changes: 13 additions & 10 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub enum LoadResult<T> {
LoadDepGraph(PathBuf, std::io::Error),
}

impl<T: Default> LoadResult<T> {
impl<T> LoadResult<T> {
/// Accesses the data returned in [`LoadResult::Ok`].
pub fn open(self, sess: &Session) -> T {
pub fn open(self, sess: &Session, fallback: impl FnOnce() -> T) -> T {
// Check for errors when using `-Zassert-incremental-state`
match (sess.opts.assert_incr_state, &self) {
(Some(IncrementalStateAssertion::NotLoaded), LoadResult::Ok { .. }) => {
Expand All @@ -55,14 +55,14 @@ impl<T: Default> LoadResult<T> {
match self {
LoadResult::LoadDepGraph(path, err) => {
sess.dcx().emit_warn(errors::LoadDepGraph { path, err });
Default::default()
fallback()
}
LoadResult::DataOutOfDate => {
if let Err(err) = delete_all_session_dir_contents(sess) {
sess.dcx()
.emit_err(errors::DeleteIncompatible { path: dep_graph_path(sess), err });
}
Default::default()
fallback()
}
LoadResult::Ok { data } => data,
}
Expand Down Expand Up @@ -93,13 +93,15 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {

fn load_dep_graph(
sess: &Session,
deps: &DepsType,
) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
deps: &Arc<DepsType>,
) -> LoadResult<(Arc<SerializedDepGraph<DepsType>>, WorkProductMap)> {
let prof = sess.prof.clone();

if sess.opts.incremental.is_none() {
// No incremental compilation.
return LoadResult::Ok { data: Default::default() };
return LoadResult::Ok {
data: (Arc::new(SerializedDepGraph::empty(deps, sess)), Default::default()),
};
}

let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
Expand Down Expand Up @@ -174,7 +176,7 @@ fn load_dep_graph(
return LoadResult::DataOutOfDate;
}

let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder, deps);
let dep_graph = SerializedDepGraph::decode(&mut decoder, deps, sess);

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

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

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

res.and_then(|result| {
let (prev_graph, prev_work_products) = result.open(sess);
let (prev_graph, prev_work_products) = result
.open(sess, || (Arc::new(SerializedDepGraph::empty(deps, sess)), Default::default()));
build_dep_graph(sess, prev_graph, prev_work_products)
})
.unwrap_or_else(DepGraph::new_disabled)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::join;
use rustc_middle::dep_graph::{
DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
DepGraph, DepsType, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
};
use rustc_middle::ty::TyCtxt;
use rustc_serialize::Encodable as RustcEncodable;
Expand Down Expand Up @@ -144,7 +144,7 @@ fn encode_query_cache(tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult
/// and moves it to the permanent dep-graph path
pub(crate) fn build_dep_graph(
sess: &Session,
prev_graph: Arc<SerializedDepGraph>,
prev_graph: Arc<SerializedDepGraph<DepsType>>,
prev_work_products: WorkProductMap,
) -> Option<DepGraph> {
if sess.opts.incremental.is_none() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(

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

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

let cstore =
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub(crate) struct DepGraphData<D: Deps> {

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

colors: DepNodeColorMap,

Expand Down Expand Up @@ -121,7 +121,7 @@ where
impl<D: Deps> DepGraph<D> {
pub fn new(
session: &Session,
prev_graph: Arc<SerializedDepGraph>,
prev_graph: Arc<SerializedDepGraph<D>>,
prev_work_products: WorkProductMap,
encoder: FileEncoder,
) -> DepGraph<D> {
Expand Down Expand Up @@ -1171,7 +1171,7 @@ impl<D: Deps> CurrentDepGraph<D> {
session: &Session,
prev_graph_node_count: usize,
encoder: FileEncoder,
previous: Arc<SerializedDepGraph>,
previous: Arc<SerializedDepGraph<D>>,
) -> Self {
let mut stable_hasher = StableHasher::new();
previous.session_count().hash(&mut stable_hasher);
Expand Down Expand Up @@ -1260,7 +1260,7 @@ impl<D: Deps> CurrentDepGraph<D> {
#[inline]
fn debug_assert_not_in_new_nodes(
&self,
prev_graph: &SerializedDepGraph,
prev_graph: &SerializedDepGraph<D>,
prev_index: SerializedDepNodeIndex,
) {
if let Some(ref nodes_in_current_session) = self.nodes_in_current_session {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_query_system/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) use graph::DepGraphData;
pub use graph::{DepGraph, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result};
pub use query::DepGraphQuery;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::DynSync;
use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_session::Session;
pub use serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use tracing::instrument;
Expand Down Expand Up @@ -90,7 +90,7 @@ pub trait DepContext: Copy {
}
}

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