Skip to content

Commit

Permalink
Do incremental setup in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jan 16, 2020
1 parent 64f2bbd commit 0a36b4d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 44 deletions.
4 changes: 4 additions & 0 deletions src/librustc/ty/steal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ impl<T> Steal<T> {
let value = value_ref.take();
value.expect("attempt to read from stolen value")
}

pub fn into_inner(self) -> Option<T> {
self.value.into_inner()
}
}
6 changes: 4 additions & 2 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ pub fn run_compiler(
}

{
let (_, lint_store) = &*queries.register_plugins()?.peek();
let peek = queries.register_plugins()?.peek();
let peek = peek.0.borrow();
let lint_store = &peek.1;

// Lint plugins are registered; now we can process command line flags.
if sess.opts.describe_lints {
describe_lints(&sess, &lint_store, true);
describe_lints(&sess, lint_store, true);
return early_exit();
}
}
Expand Down
44 changes: 29 additions & 15 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use rustc_builtin_macros;
use rustc_codegen_ssa::back::link::emit_metadata;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_codegen_utils::link::filename_for_metadata;
use rustc_data_structures::sync::future::Future;
use rustc_data_structures::sync::{join, par_for_each, Lrc, Once, WorkerLocal};
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
use rustc_errors::PResult;
use rustc_expand::base::ExtCtxt;
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_incremental;
use rustc_incremental::DepGraphFuture;
use rustc_lint::LintStore;
use rustc_mir as mir;
use rustc_mir_build as mir_build;
Expand Down Expand Up @@ -150,12 +152,12 @@ impl BoxedResolver {
}

pub fn register_plugins<'a>(
sess: &'a Session,
sess: &'a Lrc<Session>,
metadata_loader: &'a dyn MetadataLoader,
register_lints: impl Fn(&Session, &mut LintStore),
mut krate: ast::Crate,
crate_name: &str,
) -> Result<(ast::Crate, Lrc<LintStore>)> {
crate_name: String,
) -> (ast::Crate, Lrc<LintStore>, Future<'static, Option<DepGraphFuture>>) {
krate = sess.time("attributes_injection", || {
rustc_builtin_macros::cmdline_attrs::inject(
krate,
Expand All @@ -178,19 +180,31 @@ pub fn register_plugins<'a>(

let disambiguator = util::compute_crate_disambiguator(sess);
sess.crate_disambiguator.set(disambiguator);
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator);

if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
warn!(
"Error while trying to garbage collect incremental \
let sess_ = sess.clone();
let dep_graph_future = Future::spawn(move || {
let sess = &*sess_;
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator);

if sess.opts.incremental.is_some() {
sess.time("incr_comp_garbage_collect_session_directories", || {
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
warn!(
"Error while trying to garbage collect incremental \
compilation cache directory: {}",
e
);
}
});
}
e
);
}
});
}

// Compute the dependency graph (in the background). We want to do
// this as early as possible, to give the DepGraph maximum time to
// load before dep_graph() is called, but it also can't happen
// until after rustc_incremental::prepare_session_directory() is
// called, which happens within passes::register_plugins().
sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess))
});

sess.time("recursion_limit", || {
middle::recursion_limit::update_limits(sess, &krate);
Expand All @@ -211,7 +225,7 @@ pub fn register_plugins<'a>(
}
});

Ok((krate, Lrc::new(lint_store)))
(krate, Lrc::new(lint_store), dep_graph_future)
}

fn configure_and_expand_inner<'a>(
Expand Down
56 changes: 29 additions & 27 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use rustc::ty::steal::Steal;
use rustc::ty::{AllArenas, GlobalCtxt, ResolverOutputs};
use rustc::util::common::ErrorReported;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::sync::future::Future;
use rustc_data_structures::sync::{Lrc, Once, WorkerLocal};
use rustc_data_structures::OnDrop;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_incremental::DepGraphFuture;
use rustc_lint::LintStore;
Expand Down Expand Up @@ -69,10 +71,12 @@ pub struct Queries<'tcx> {
all_arenas: AllArenas,
arena: WorkerLocal<Arena<'tcx>>,

dep_graph_future: Query<Option<DepGraphFuture>>,
parse: Query<ast::Crate>,
crate_name: Query<String>,
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
register_plugins: Query<(
Steal<(ast::Crate, Lrc<LintStore>)>,
Steal<Future<'static, Option<DepGraphFuture>>>,
)>,
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
dep_graph: Query<DepGraph>,
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>,
Expand All @@ -88,7 +92,6 @@ impl<'tcx> Queries<'tcx> {
gcx: Once::new(),
all_arenas: AllArenas::new(),
arena: WorkerLocal::new(|_| Arena::default()),
dep_graph_future: Default::default(),
parse: Default::default(),
crate_name: Default::default(),
register_plugins: Default::default(),
Expand All @@ -108,16 +111,6 @@ impl<'tcx> Queries<'tcx> {
&self.compiler.codegen_backend()
}

pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
self.dep_graph_future.compute(|| {
Ok(self
.session()
.opts
.build_dep_graph()
.then(|| rustc_incremental::load_dep_graph(self.session())))
})
}

pub fn parse(&self) -> Result<&Query<ast::Crate>> {
self.parse.compute(|| {
passes::parse(self.session(), &self.compiler.input).map_err(|mut parse_error| {
Expand All @@ -127,28 +120,27 @@ impl<'tcx> Queries<'tcx> {
})
}

pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> {
pub fn register_plugins(
&self,
) -> Result<
&Query<(
Steal<(ast::Crate, Lrc<LintStore>)>,
Steal<Future<'static, Option<DepGraphFuture>>>,
)>,
> {
self.register_plugins.compute(|| {
let crate_name = self.crate_name()?.peek().clone();
let krate = self.parse()?.take();

let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
let result = passes::register_plugins(
let (krate, lint_store, future) = passes::register_plugins(
self.session(),
&*self.codegen_backend().metadata_loader(),
self.compiler.register_lints.as_ref().map(|p| &**p).unwrap_or_else(|| empty),
krate,
&crate_name,
crate_name,
);

// Compute the dependency graph (in the background). We want to do
// this as early as possible, to give the DepGraph maximum time to
// load before dep_graph() is called, but it also can't happen
// until after rustc_incremental::prepare_session_directory() is
// called, which happens within passes::register_plugins().
self.dep_graph_future().ok();

result
Ok((Steal::new((krate, lint_store)), Steal::new(future)))
})
}

Expand All @@ -174,7 +166,7 @@ impl<'tcx> Queries<'tcx> {
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
self.expansion.compute(|| {
let crate_name = self.crate_name()?.peek().clone();
let (krate, lint_store) = self.register_plugins()?.take();
let (krate, lint_store) = self.register_plugins()?.peek().0.steal();
let _timer = self.session().timer("configure_and_expand");
passes::configure_and_expand(
self.session().clone(),
Expand All @@ -191,7 +183,8 @@ impl<'tcx> Queries<'tcx> {

pub fn dep_graph(&self) -> Result<&Query<DepGraph>> {
self.dep_graph.compute(|| {
Ok(match self.dep_graph_future()?.take() {
let future = self.register_plugins()?.peek().1.steal().join();
Ok(match future {
None => DepGraph::new_disabled(),
Some(future) => {
let (prev_graph, prev_work_products) =
Expand Down Expand Up @@ -336,6 +329,15 @@ impl Compiler {
{
let mut _timer = None;
let queries = Queries::new(&self);

// Join the dep graph future if has started, but haven't been stolen yet.
let _join_dep_graph_future = OnDrop(|| {
let result = queries.register_plugins.result.borrow_mut().take();
result.map(|result| {
result.map(|result| result.1.into_inner().map(|future| future.join()))
});
});

let ret = f(&queries);

if self.session().opts.debugging_opts.query_stats {
Expand Down

0 comments on commit 0a36b4d

Please sign in to comment.