Skip to content

Commit 0a36b4d

Browse files
committed
Do incremental setup in the background
1 parent 64f2bbd commit 0a36b4d

File tree

4 files changed

+66
-44
lines changed

4 files changed

+66
-44
lines changed

src/librustc/ty/steal.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ impl<T> Steal<T> {
4141
let value = value_ref.take();
4242
value.expect("attempt to read from stolen value")
4343
}
44+
45+
pub fn into_inner(self) -> Option<T> {
46+
self.value.into_inner()
47+
}
4448
}

src/librustc_driver/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,13 @@ pub fn run_compiler(
335335
}
336336

337337
{
338-
let (_, lint_store) = &*queries.register_plugins()?.peek();
338+
let peek = queries.register_plugins()?.peek();
339+
let peek = peek.0.borrow();
340+
let lint_store = &peek.1;
339341

340342
// Lint plugins are registered; now we can process command line flags.
341343
if sess.opts.describe_lints {
342-
describe_lints(&sess, &lint_store, true);
344+
describe_lints(&sess, lint_store, true);
343345
return early_exit();
344346
}
345347
}

src/librustc_interface/passes.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ use rustc_builtin_macros;
2121
use rustc_codegen_ssa::back::link::emit_metadata;
2222
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2323
use rustc_codegen_utils::link::filename_for_metadata;
24+
use rustc_data_structures::sync::future::Future;
2425
use rustc_data_structures::sync::{join, par_for_each, Lrc, Once, WorkerLocal};
2526
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
2627
use rustc_errors::PResult;
2728
use rustc_expand::base::ExtCtxt;
2829
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2930
use rustc_incremental;
31+
use rustc_incremental::DepGraphFuture;
3032
use rustc_lint::LintStore;
3133
use rustc_mir as mir;
3234
use rustc_mir_build as mir_build;
@@ -150,12 +152,12 @@ impl BoxedResolver {
150152
}
151153

152154
pub fn register_plugins<'a>(
153-
sess: &'a Session,
155+
sess: &'a Lrc<Session>,
154156
metadata_loader: &'a dyn MetadataLoader,
155157
register_lints: impl Fn(&Session, &mut LintStore),
156158
mut krate: ast::Crate,
157-
crate_name: &str,
158-
) -> Result<(ast::Crate, Lrc<LintStore>)> {
159+
crate_name: String,
160+
) -> (ast::Crate, Lrc<LintStore>, Future<'static, Option<DepGraphFuture>>) {
159161
krate = sess.time("attributes_injection", || {
160162
rustc_builtin_macros::cmdline_attrs::inject(
161163
krate,
@@ -178,19 +180,31 @@ pub fn register_plugins<'a>(
178180

179181
let disambiguator = util::compute_crate_disambiguator(sess);
180182
sess.crate_disambiguator.set(disambiguator);
181-
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator);
182183

183-
if sess.opts.incremental.is_some() {
184-
sess.time("incr_comp_garbage_collect_session_directories", || {
185-
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
186-
warn!(
187-
"Error while trying to garbage collect incremental \
184+
let sess_ = sess.clone();
185+
let dep_graph_future = Future::spawn(move || {
186+
let sess = &*sess_;
187+
rustc_incremental::prepare_session_directory(sess, &crate_name, disambiguator);
188+
189+
if sess.opts.incremental.is_some() {
190+
sess.time("incr_comp_garbage_collect_session_directories", || {
191+
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
192+
warn!(
193+
"Error while trying to garbage collect incremental \
188194
compilation cache directory: {}",
189-
e
190-
);
191-
}
192-
});
193-
}
195+
e
196+
);
197+
}
198+
});
199+
}
200+
201+
// Compute the dependency graph (in the background). We want to do
202+
// this as early as possible, to give the DepGraph maximum time to
203+
// load before dep_graph() is called, but it also can't happen
204+
// until after rustc_incremental::prepare_session_directory() is
205+
// called, which happens within passes::register_plugins().
206+
sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess))
207+
});
194208

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

214-
Ok((krate, Lrc::new(lint_store)))
228+
(krate, Lrc::new(lint_store), dep_graph_future)
215229
}
216230

217231
fn configure_and_expand_inner<'a>(

src/librustc_interface/queries.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use rustc::ty::steal::Steal;
1010
use rustc::ty::{AllArenas, GlobalCtxt, ResolverOutputs};
1111
use rustc::util::common::ErrorReported;
1212
use rustc_codegen_utils::codegen_backend::CodegenBackend;
13+
use rustc_data_structures::sync::future::Future;
1314
use rustc_data_structures::sync::{Lrc, Once, WorkerLocal};
15+
use rustc_data_structures::OnDrop;
1416
use rustc_hir::def_id::LOCAL_CRATE;
1517
use rustc_incremental::DepGraphFuture;
1618
use rustc_lint::LintStore;
@@ -69,10 +71,12 @@ pub struct Queries<'tcx> {
6971
all_arenas: AllArenas,
7072
arena: WorkerLocal<Arena<'tcx>>,
7173

72-
dep_graph_future: Query<Option<DepGraphFuture>>,
7374
parse: Query<ast::Crate>,
7475
crate_name: Query<String>,
75-
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
76+
register_plugins: Query<(
77+
Steal<(ast::Crate, Lrc<LintStore>)>,
78+
Steal<Future<'static, Option<DepGraphFuture>>>,
79+
)>,
7680
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
7781
dep_graph: Query<DepGraph>,
7882
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>,
@@ -88,7 +92,6 @@ impl<'tcx> Queries<'tcx> {
8892
gcx: Once::new(),
8993
all_arenas: AllArenas::new(),
9094
arena: WorkerLocal::new(|_| Arena::default()),
91-
dep_graph_future: Default::default(),
9295
parse: Default::default(),
9396
crate_name: Default::default(),
9497
register_plugins: Default::default(),
@@ -108,16 +111,6 @@ impl<'tcx> Queries<'tcx> {
108111
&self.compiler.codegen_backend()
109112
}
110113

111-
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
112-
self.dep_graph_future.compute(|| {
113-
Ok(self
114-
.session()
115-
.opts
116-
.build_dep_graph()
117-
.then(|| rustc_incremental::load_dep_graph(self.session())))
118-
})
119-
}
120-
121114
pub fn parse(&self) -> Result<&Query<ast::Crate>> {
122115
self.parse.compute(|| {
123116
passes::parse(self.session(), &self.compiler.input).map_err(|mut parse_error| {
@@ -127,28 +120,27 @@ impl<'tcx> Queries<'tcx> {
127120
})
128121
}
129122

130-
pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> {
123+
pub fn register_plugins(
124+
&self,
125+
) -> Result<
126+
&Query<(
127+
Steal<(ast::Crate, Lrc<LintStore>)>,
128+
Steal<Future<'static, Option<DepGraphFuture>>>,
129+
)>,
130+
> {
131131
self.register_plugins.compute(|| {
132132
let crate_name = self.crate_name()?.peek().clone();
133133
let krate = self.parse()?.take();
134134

135135
let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
136-
let result = passes::register_plugins(
136+
let (krate, lint_store, future) = passes::register_plugins(
137137
self.session(),
138138
&*self.codegen_backend().metadata_loader(),
139139
self.compiler.register_lints.as_ref().map(|p| &**p).unwrap_or_else(|| empty),
140140
krate,
141-
&crate_name,
141+
crate_name,
142142
);
143-
144-
// Compute the dependency graph (in the background). We want to do
145-
// this as early as possible, to give the DepGraph maximum time to
146-
// load before dep_graph() is called, but it also can't happen
147-
// until after rustc_incremental::prepare_session_directory() is
148-
// called, which happens within passes::register_plugins().
149-
self.dep_graph_future().ok();
150-
151-
result
143+
Ok((Steal::new((krate, lint_store)), Steal::new(future)))
152144
})
153145
}
154146

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

192184
pub fn dep_graph(&self) -> Result<&Query<DepGraph>> {
193185
self.dep_graph.compute(|| {
194-
Ok(match self.dep_graph_future()?.take() {
186+
let future = self.register_plugins()?.peek().1.steal().join();
187+
Ok(match future {
195188
None => DepGraph::new_disabled(),
196189
Some(future) => {
197190
let (prev_graph, prev_work_products) =
@@ -336,6 +329,15 @@ impl Compiler {
336329
{
337330
let mut _timer = None;
338331
let queries = Queries::new(&self);
332+
333+
// Join the dep graph future if has started, but haven't been stolen yet.
334+
let _join_dep_graph_future = OnDrop(|| {
335+
let result = queries.register_plugins.result.borrow_mut().take();
336+
result.map(|result| {
337+
result.map(|result| result.1.into_inner().map(|future| future.join()))
338+
});
339+
});
340+
339341
let ret = f(&queries);
340342

341343
if self.session().opts.debugging_opts.query_stats {

0 commit comments

Comments
 (0)