Skip to content

Commit

Permalink
Run early lint checks in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jan 16, 2020
1 parent 3e2090c commit a6ca4ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
8 changes: 7 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ pub fn run_compiler(
})?;
None
} else {
// Drop AST after creating GlobalCtxt to free memory
// Drop a reference to the AST
let prof = sess.prof.clone();
let ast = queries.expansion()?.take().0;
Some(Future::spawn(move || {
Expand All @@ -400,6 +400,11 @@ pub fn run_compiler(
}))
};

queries.global_ctxt()?;

// Drop a reference to the AST by waiting on the lint future.
queries.lower_to_hir()?.take().1.join();

queries.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;

// Ensure the AST is dropped by this point.
Expand All @@ -410,6 +415,7 @@ pub fn run_compiler(
}

if sess.opts.debugging_opts.save_analysis {
// Drop AST to free memory
mem::drop(queries.expansion()?.take());
}

Expand Down
36 changes: 20 additions & 16 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,16 @@ fn configure_and_expand_inner<'a>(
}

pub fn lower_to_hir<'res, 'tcx>(
sess: &'tcx Session,
lint_store: &LintStore,
sess: Lrc<Session>,
lint_store: Lrc<LintStore>,
resolver: &'res mut Resolver<'_>,
dep_graph: &'res DepGraph,
krate: &'res ast::Crate,
krate: Lrc<ast::Crate>,
arena: &'tcx Arena<'tcx>,
) -> Result<map::Forest<'tcx>> {
) -> Result<(map::Forest<'tcx>, Future<'static, ()>)> {
// Lower AST to HIR.
let hir_crate = rustc_ast_lowering::lower_crate(
sess,
&sess,
&dep_graph,
&krate,
resolver,
Expand All @@ -458,23 +458,27 @@ pub fn lower_to_hir<'res, 'tcx>(

let hir_forest = map::Forest::new(hir_crate, &dep_graph);

sess.time("early_lint_checks", || {
rustc_lint::check_ast_crate(
sess,
lint_store,
&krate,
false,
Some(std::mem::take(resolver.lint_buffer())),
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
)
});
let lint_buffer = std::mem::take(resolver.lint_buffer());

// Discard hygiene data, which isn't required after lowering to HIR.
if !sess.opts.debugging_opts.keep_hygiene_data {
rustc_span::hygiene::clear_syntax_context_map();
}

Ok(hir_forest)
let lints = Future::spawn(move || {
sess.time("early_lint_checks", || {
rustc_lint::check_ast_crate(
&sess,
&lint_store,
&krate,
false,
Some(lint_buffer),
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
)
})
});

Ok((hir_forest, lints))
}

// Returns all the paths that correspond to generated files.
Expand Down
32 changes: 20 additions & 12 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ pub struct Queries<'tcx> {
Steal<(ast::Crate, Lrc<LintStore>)>,
Steal<Future<'static, Option<DepGraphFuture>>>,
)>,
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
expansion: Query<(Lrc<ast::Crate>, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
dep_graph: Query<DepGraph>,
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>,
lower_to_hir: Query<(&'tcx map::Forest<'tcx>, Future<'static, ()>, Steal<ResolverOutputs>)>,
prepare_outputs: Query<OutputFilenames>,
global_ctxt: Query<QueryContext<'tcx>>,
ongoing_codegen: Query<Box<dyn Any>>,
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'tcx> Queries<'tcx> {

pub fn expansion(
&self,
) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
) -> Result<&Query<(Lrc<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()?.peek().0.steal();
Expand All @@ -176,7 +176,7 @@ impl<'tcx> Queries<'tcx> {
&crate_name,
)
.map(|(krate, resolver)| {
(krate, Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
(Lrc::new(krate), Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
})
})
}
Expand Down Expand Up @@ -204,25 +204,26 @@ impl<'tcx> Queries<'tcx> {

pub fn lower_to_hir(
&'tcx self,
) -> Result<&Query<(&'tcx map::Forest<'tcx>, Steal<ResolverOutputs>)>> {
) -> Result<&Query<(&'tcx map::Forest<'tcx>, Future<'static, ()>, Steal<ResolverOutputs>)>>
{
self.lower_to_hir.compute(|| {
let expansion_result = self.expansion()?;
let peeked = expansion_result.peek();
let krate = &peeked.0;
let krate = peeked.0.clone();
let resolver = peeked.1.steal();
let lint_store = &peeked.2;
let hir = resolver.borrow_mut().access(|resolver| {
let lint_store = peeked.2.clone();
let (hir, lints) = resolver.borrow_mut().access(|resolver| {
passes::lower_to_hir(
self.session(),
self.session().clone(),
lint_store,
resolver,
&*self.dep_graph()?.peek(),
&krate,
krate,
&self.arena,
)
})?;
let hir = self.arena.alloc(hir);
Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
Ok((hir, lints, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
})
}

Expand All @@ -248,7 +249,7 @@ impl<'tcx> Queries<'tcx> {
let outputs = self.prepare_outputs()?.peek().clone();
let lint_store = self.expansion()?.peek().2.clone();
let hir = self.lower_to_hir()?.peek();
let (ref hir_forest, ref resolver_outputs) = &*hir;
let (ref hir_forest, _, ref resolver_outputs) = &*hir;
let _timer = self.session().timer("create_global_ctxt");
Ok(passes::create_global_ctxt(
self.compiler,
Expand Down Expand Up @@ -338,6 +339,12 @@ impl Compiler {
});
});

// Join the early lint check future if has started, but haven't been stolen yet.
let _join_lint_future = OnDrop(|| {
let result = queries.lower_to_hir.result.borrow_mut().take();
result.map(|result| result.map(|result| result.1.join()));
});

let ret = f(&queries);

if self.session().opts.debugging_opts.query_stats {
Expand Down Expand Up @@ -369,6 +376,7 @@ impl Compiler {
queries.global_ctxt()?;

// Drop AST after creating GlobalCtxt to free memory.
queries.lower_to_hir()?.take().1.join();
mem::drop(queries.expansion()?.take());

queries.ongoing_codegen()?;
Expand Down

0 comments on commit a6ca4ff

Please sign in to comment.