Skip to content
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

Move metadata generation before analysis #64112

Closed
wants to merge 6 commits into from
Closed
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
6 changes: 4 additions & 2 deletions src/librustc/middle/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ pub enum Linkage {
Dynamic,
}

pub fn calculate(tcx: TyCtxt<'_>) {
pub fn calculate(tcx: TyCtxt<'_>, abort_if_errors: bool) {
let sess = &tcx.sess;
let fmts = sess.crate_types.borrow().iter().map(|&ty| {
let linkage = calculate_type(tcx, ty);
verify_ok(tcx, &linkage);
(ty, linkage)
}).collect::<FxHashMap<_, _>>();
sess.abort_if_errors();
if abort_if_errors {
sess.abort_if_errors();
}
sess.dependency_formats.set(fmts);
}

Expand Down
14 changes: 8 additions & 6 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ pub fn run_compiler(
return sess.compile_status();
}

// We want metadata generation to be as early as possible, i.e. before
// the analysis phases, to maximize pipelining opportunities. But wait
// until after the analysis phases to abort if there are errors.
compiler.do_metadata(/* abort_if_errors */ false)?;

if sess.opts.debugging_opts.save_analysis {
let expanded_crate = &compiler.expansion()?.peek().0;
let crate_name = compiler.crate_name()?.peek().clone();
Expand Down Expand Up @@ -375,6 +380,8 @@ pub fn run_compiler(

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

sess.abort_if_errors();

if callbacks.after_analysis(compiler) == Compilation::Stop {
return sess.compile_status();
}
Expand All @@ -383,17 +390,12 @@ pub fn run_compiler(
mem::drop(compiler.expansion()?.take());
}

compiler.ongoing_codegen()?;

// Drop GlobalCtxt after starting codegen to free memory
mem::drop(compiler.global_ctxt()?.take());
compiler.codegen_and_link()?;

if sess.opts.debugging_opts.print_type_sizes {
sess.code_stats.borrow().print_type_sizes();
}

compiler.link()?;

if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
}
Expand Down
29 changes: 17 additions & 12 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ pub struct PluginInfo {
}

pub fn register_plugins<'a>(
compiler: &Compiler,
sess: &'a Session,
cstore: &'a CStore,
mut krate: ast::Crate,
Expand Down Expand Up @@ -261,9 +260,6 @@ pub fn register_plugins<'a>(
});
}

// If necessary, compute the dependency graph (in the background).
compiler.dep_graph_future().ok();

time(sess, "recursion limit", || {
middle::recursion_limit::update_limits(sess, &krate);
});
Expand Down Expand Up @@ -1081,27 +1077,36 @@ fn encode_and_write_metadata(
(metadata, need_metadata_module)
}

/// Do metadata generation.
pub fn do_metadata<'tcx>(
tcx: TyCtxt<'tcx>,
outputs: &OutputFilenames,
abort_if_errors: bool,
) -> (middle::cstore::EncodedMetadata, bool) {
time(tcx.sess, "resolving dependency formats", || {
middle::dependency_format::calculate(tcx, abort_if_errors)
});

time(tcx.sess, "metadata encoding and writing", || {
encode_and_write_metadata(tcx, outputs)
})
}

/// Runs the codegen backend, after which the AST and analysis can
/// be discarded.
pub fn start_codegen<'tcx>(
codegen_backend: &dyn CodegenBackend,
tcx: TyCtxt<'tcx>,
rx: mpsc::Receiver<Box<dyn Any + Send>>,
metadata: middle::cstore::EncodedMetadata,
need_metadata_module: bool,
outputs: &OutputFilenames,
) -> Box<dyn Any> {
if log_enabled!(::log::Level::Info) {
println!("Pre-codegen");
tcx.print_debug_stats();
}

time(tcx.sess, "resolving dependency formats", || {
middle::dependency_format::calculate(tcx)
});

let (metadata, need_metadata_module) = time(tcx.sess, "metadata encoding and writing", || {
encode_and_write_metadata(tcx, outputs)
});

tcx.sess.profiler(|p| p.start_activity("codegen crate"));
let codegen = time(tcx.sess, "codegen", move || {
codegen_backend.codegen_crate(tcx, metadata, need_metadata_module, rx)
Expand Down
107 changes: 52 additions & 55 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use crate::interface::{Compiler, Result};
use crate::passes::{self, BoxedResolver, ExpansionResult, BoxedGlobalCtxt, PluginInfo};

use rustc_incremental::DepGraphFuture;
use rustc::session::config::{OutputFilenames, OutputType};
use rustc::session::config::OutputFilenames;
use rustc::util::common::{time, ErrorReported};
use rustc::hir;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::middle::cstore;
use rustc::ty::steal::Steal;
use rustc::dep_graph::DepGraph;
use std::any::Any;
use std::cell::{Ref, RefMut, RefCell};
use std::mem;
use std::rc::Rc;
use std::sync::mpsc;
use std::any::Any;
use std::mem;
use syntax::{self, ast};

/// Represent the result of a query.
Expand Down Expand Up @@ -83,8 +84,8 @@ pub(crate) struct Queries {
codegen_channel: Query<(Steal<mpsc::Sender<Box<dyn Any + Send>>>,
Steal<mpsc::Receiver<Box<dyn Any + Send>>>)>,
global_ctxt: Query<BoxedGlobalCtxt>,
ongoing_codegen: Query<Box<dyn Any>>,
link: Query<()>,
do_metadata: Query<(cstore::EncodedMetadata, bool)>,
codegen_and_link: Query<()>,
}

impl Compiler {
Expand Down Expand Up @@ -114,29 +115,38 @@ impl Compiler {
let crate_name = self.crate_name()?.peek().clone();
let krate = self.parse()?.take();

passes::register_plugins(
self,
let result = passes::register_plugins(
self.session(),
self.cstore(),
krate,
&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
})
}

pub fn crate_name(&self) -> Result<&Query<String>> {
self.queries.crate_name.compute(|| {
let parse_result = self.parse()?;
let krate = parse_result.peek();
let result = match self.crate_name {
Ok(match self.crate_name {
Some(ref crate_name) => crate_name.clone(),
None => rustc_codegen_utils::link::find_crate_name(
Some(self.session()),
&krate.attrs,
&self.input
),
};
Ok(result)
None => {
let parse_result = self.parse()?;
let krate = parse_result.peek();
rustc_codegen_utils::link::find_crate_name(
Some(self.session()),
&krate.attrs,
&self.input
)
}
})
})
}

Expand Down Expand Up @@ -194,7 +204,6 @@ impl Compiler {

pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
self.queries.prepare_outputs.compute(|| {
self.lower_to_hir()?;
let krate = self.expansion()?;
let krate = krate.peek();
let crate_name = self.crate_name()?;
Expand Down Expand Up @@ -230,62 +239,50 @@ impl Compiler {
})
}

pub fn ongoing_codegen(&self) -> Result<&Query<Box<dyn Any>>> {
self.queries.ongoing_codegen.compute(|| {
pub fn do_metadata(&self, abort_if_errors: bool)
-> Result<&Query<(cstore::EncodedMetadata, bool)>> {
self.queries.do_metadata.compute(|| {
let outputs = self.prepare_outputs()?;
Ok(self.global_ctxt()?.peek_mut().enter(|tcx| {
passes::do_metadata(tcx, &*outputs.peek(), abort_if_errors)
}))
})
}

pub fn codegen_and_link(&self) -> Result<&Query<()>> {
self.queries.codegen_and_link.compute(|| {
let rx = self.codegen_channel()?.peek().1.steal();
let outputs = self.prepare_outputs()?;
self.global_ctxt()?.peek_mut().enter(|tcx| {
let (metadata, need_metadata_module) =
self.do_metadata(/* abort_if_errors */ true)?.take();

let ongoing_codegen = self.global_ctxt()?.peek_mut().enter(|tcx| {
tcx.analysis(LOCAL_CRATE).ok();

// Don't do code generation if there were any errors
// Don't do code generation if there were any errors.
self.session().compile_status()?;

Ok(passes::start_codegen(
&***self.codegen_backend(),
tcx,
rx,
metadata,
need_metadata_module,
&*outputs.peek()
))
})
})
}

pub fn link(&self) -> Result<&Query<()>> {
self.queries.link.compute(|| {
let sess = self.session();
})?;

let ongoing_codegen = self.ongoing_codegen()?.take();
// Drop GlobalCtxt after starting codegen to free memory.
mem::drop(self.global_ctxt()?.take());

self.codegen_backend().join_codegen_and_link(
ongoing_codegen,
sess,
self.session(),
&*self.dep_graph()?.peek(),
&*self.prepare_outputs()?.peek(),
&*outputs.peek(),
).map_err(|_| ErrorReported)?;

Ok(())
})
}

pub fn compile(&self) -> Result<()> {
self.prepare_outputs()?;

if self.session().opts.output_types.contains_key(&OutputType::DepInfo)
&& self.session().opts.output_types.len() == 1
{
return Ok(())
}

self.global_ctxt()?;

// Drop AST after creating GlobalCtxt to free memory
mem::drop(self.expansion()?.take());

self.ongoing_codegen()?;

// Drop GlobalCtxt after starting codegen to free memory
mem::drop(self.global_ctxt()?.take());

self.link().map(|_| ())
}
}
2 changes: 1 addition & 1 deletion src/test/run-make-fulldeps/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
};

interface::run_compiler(config, |compiler| {
compiler.compile().ok();
compiler.codegen_and_link().ok();
});
}

This file was deleted.

This file was deleted.

35 changes: 0 additions & 35 deletions src/test/ui/associated-types/associated-types-issue-20346.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/ui/associated-types/associated-types-issue-20346.stderr

This file was deleted.

Loading