Skip to content

Commit

Permalink
Remove queries from rustc_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jul 8, 2019
1 parent 59f099e commit 9b2133e
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 304 deletions.
129 changes: 61 additions & 68 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rustc::session::config::nightly_options;
use rustc::session::{early_error, early_warn};
use rustc::lint::Lint;
use rustc::lint;
use rustc::ty::TyCtxt;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::util::common::{ErrorReported, install_panic_hook, print_time_passes_entry};
use rustc::util::common::{set_time_depth, time};
Expand Down Expand Up @@ -106,11 +107,19 @@ pub trait Callbacks {
/// Called before creating the compiler instance
fn config(&mut self, _config: &mut interface::Config) {}
/// Called after parsing and returns true to continue execution
fn after_parsing(&mut self, _compiler: &interface::Compiler) -> bool {
fn after_parsing(
&mut self,
_compiler: &interface::Compiler,
_tcx: TyCtxt<'_>,
) -> bool {
true
}
/// Called after analysis and returns true to continue execution
fn after_analysis(&mut self, _compiler: &interface::Compiler) -> bool {
fn after_analysis(
&mut self,
_compiler: &interface::Compiler,
_tcx: TyCtxt<'_>,
) -> bool {
true
}
}
Expand Down Expand Up @@ -244,7 +253,8 @@ pub fn run_compiler(
let pretty_info = parse_pretty(&mut config.opts, &matches);

interface::run_compiler(config, |compiler| {
let sess = compiler.session();
let sess = compiler.session().clone();
let sess = &*sess;
let should_stop = RustcDefaultCalls::print_crate_info(
&**compiler.codegen_backend(),
sess,
Expand All @@ -262,9 +272,9 @@ pub fn run_compiler(
return sess.compile_status();
}

if let Some((ppm, opt_uii)) = pretty_info {
if ppm.needs_ast_map(&opt_uii) {
compiler.enter(|tcx| {
let link = compiler.enter(|compiler, tcx| {
if let Some((ppm, opt_uii)) = pretty_info {
if ppm.needs_ast_map(&opt_uii) {
let expansion_result = tcx.expand_macros(())?;
pretty::print_after_hir_lowering(
tcx,
Expand All @@ -274,11 +284,8 @@ pub fn run_compiler(
opt_uii.clone(),
compiler.output_file().as_ref().map(|p| &**p),
);
Ok(())
})?;
return sess.compile_status();
} else {
compiler.enter(|tcx| {
return sess.compile_status().map(|_| None);
} else {
let krate = tcx.parse(())?;
let krate = krate.borrow();
pretty::print_after_parsing(
Expand All @@ -288,57 +295,48 @@ pub fn run_compiler(
ppm,
compiler.output_file().as_ref().map(|p| &**p),
);
Ok(())
})?;
return sess.compile_status();
return sess.compile_status().map(|_| None);
}
}
}

compiler.enter(|tcx| tcx.parse(()))?;
tcx.parse(())?;

if !callbacks.after_parsing(compiler) {
return sess.compile_status();
}
if !callbacks.after_parsing(compiler, tcx) {
return sess.compile_status().map(|_| None);
}

if sess.opts.debugging_opts.parse_only ||
sess.opts.debugging_opts.show_span.is_some() ||
sess.opts.debugging_opts.ast_json_noexpand {
return sess.compile_status();
}
if sess.opts.debugging_opts.parse_only ||
sess.opts.debugging_opts.show_span.is_some() ||
sess.opts.debugging_opts.ast_json_noexpand {
return sess.compile_status().map(|_| None);
}

compiler.enter(|tcx| tcx.register_plugins(()))?;
tcx.register_plugins(())?;

// Lint plugins are registered; now we can process command line flags.
if sess.opts.describe_lints {
describe_lints(&sess, &sess.lint_store.borrow(), true);
return sess.compile_status();
}
// Lint plugins are registered; now we can process command line flags.
if sess.opts.describe_lints {
describe_lints(&sess, &sess.lint_store.borrow(), true);
return sess.compile_status().map(|_| None);
}

compiler.enter(|tcx| {
tcx.prepare_outputs(())?;
Ok(())
})?;

if sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& sess.opts.output_types.len() == 1
{
return sess.compile_status();
}
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& sess.opts.output_types.len() == 1
{
return sess.compile_status().map(|_| None);
}

compiler.enter(|tcx| {
tcx.lower_ast_to_hir(())?;
Ok(())
})?;

if sess.opts.debugging_opts.no_analysis ||
sess.opts.debugging_opts.ast_json {
return sess.compile_status();
}
if sess.opts.debugging_opts.no_analysis ||
sess.opts.debugging_opts.ast_json {
return sess.compile_status().map(|_| None);
}

if sess.opts.debugging_opts.save_analysis {
compiler.enter(|tcx| {
if sess.opts.debugging_opts.save_analysis {
let expansion_result = tcx.expand_macros(())?;
let result = tcx.analysis(LOCAL_CRATE);
tcx.analysis(LOCAL_CRATE).ok();
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();

time(sess, "save analysis", || {
Expand All @@ -351,41 +349,36 @@ pub fn run_compiler(
DumpHandler::new(compiler.output_dir().as_ref().map(|p| &**p), crate_name)
)
});

result
// AST will be dropped *after* the `after_analysis` callback
// (needed by the RLS)
})?;
} else {
compiler.enter(|tcx| {
} else {
// Drop AST after lowering HIR to free memory
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
});
}
}

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

if !callbacks.after_analysis(compiler) {
return sess.compile_status();
}
if !callbacks.after_analysis(compiler, tcx) {
return sess.compile_status().map(|_| None);
}

if sess.opts.debugging_opts.save_analysis {
compiler.enter(|tcx| {
// Drop AST after lowering HIR to free memory
if sess.opts.debugging_opts.save_analysis {
// Drop AST after running `after_analysis` callback to free memory
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
});
}
}

compiler.ongoing_codegen()?;
compiler.linker(tcx).map(|linker| Some(linker))
})?;

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

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

compiler.link()?;
// Run linker outside `enter` so GlobalCtxt is freed
if let Some(linker) = link {
linker.link()?;
}

if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
Expand Down
86 changes: 68 additions & 18 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use crate::queries::Queries;
use crate::util;
use crate::profile;
use crate::passes;
pub use crate::passes::BoxedResolver;

use rustc::lint;
use rustc::session::config::{self, Input, InputsAndOutputs};
use rustc::session::config::{self, Input, InputsAndOutputs, OutputType};
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::session::{DiagnosticOutput, Session};
use rustc::util::common::ErrorReported;
use rustc::ty::TyCtxt;
use rustc::ty::{self, TyCtxt};
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::OnDrop;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::{Lrc, OneThread};
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
use rustc_metadata::cstore::CStore;
use std::io::Write;
use std::path::PathBuf;
use std::result;
use std::sync::{Arc, Mutex};
use std::mem;
use syntax;
use syntax::source_map::{FileLoader, SourceMap};
use syntax_pos::edition;
Expand All @@ -31,7 +33,6 @@ pub struct Compiler {
codegen_backend: Arc<dyn CodegenBackend + Send + Sync>,
source_map: Lrc<SourceMap>,
pub(crate) io: InputsAndOutputs,
pub(crate) queries: Queries,
pub(crate) cstore: Lrc<CStore>,
pub(crate) crate_name: Option<String>,
}
Expand All @@ -58,11 +59,61 @@ impl Compiler {
pub fn output_file(&self) -> &Option<PathBuf> {
&self.io.output_file
}
pub fn enter<F, R>(&self, f: F) -> R
pub fn enter<F, R>(self, f: F) -> R
where
F: FnOnce(TyCtxt<'_>) -> R
F: FnOnce(&Compiler, TyCtxt<'_>) -> R
{
self.global_ctxt().unwrap().peek_mut().enter(f)
passes::enter_global_ctxt(&self, f)
}
pub fn linker(&self, tcx: TyCtxt<'_>) -> Result<Linker> {
tcx.ongoing_codegen(LOCAL_CRATE).map(|ongoing_codegen| {
Linker {
sess: self.sess.clone(),
ongoing_codegen,
codegen_backend: self.codegen_backend.clone(),
}
})
}
pub fn compile(self) -> Result<()> {
let link = self.enter(|compiler, tcx| {
tcx.prepare_outputs(())?;

if tcx.sess.opts.output_types.contains_key(&OutputType::DepInfo)
&& tcx.sess.opts.output_types.len() == 1
{
return Ok(None)
}

tcx.lower_ast_to_hir(())?;
// Drop AST after lowering HIR to free memory
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());

compiler.linker(tcx).map(|linker| Some(linker))
})?;

// Run linker outside `enter` so GlobalCtxt is freed
if let Some(linker) = link {
linker.link()
} else {
Ok(())
}
}
}

pub struct Linker {
sess: Lrc<Session>,
ongoing_codegen: Lrc<ty::OngoingCodegen>,
codegen_backend: Arc<dyn CodegenBackend + Send + Sync>,
}

impl Linker {
pub fn link(self) -> Result<()> {
self.codegen_backend.join_codegen_and_link(
OneThread::into_inner(self.ongoing_codegen.codegen_object.steal()),
&self.sess,
&self.ongoing_codegen.dep_graph,
&self.ongoing_codegen.outputs,
).map_err(|_| ErrorReported)
}
}

Expand Down Expand Up @@ -90,7 +141,7 @@ pub struct Config {

pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
where
F: FnOnce(&Compiler) -> R,
F: FnOnce(Compiler) -> R,
{
let (sess, codegen_backend, source_map) = util::create_session(
config.opts,
Expand All @@ -104,7 +155,7 @@ where
let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));

let compiler = Compiler {
sess,
sess: sess.clone(),
codegen_backend,
source_map,
cstore,
Expand All @@ -114,30 +165,29 @@ where
output_dir: config.output_dir,
output_file: config.output_file,
},
queries: Default::default(),
crate_name: config.crate_name,
};

let _sess_abort_error = OnDrop(|| {
compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
sess.diagnostic().print_error_count(&util::diagnostics_registry());
});

if compiler.sess.profile_queries() {
profile::begin(&compiler.sess);
if sess.profile_queries() {
profile::begin(&sess);
}

let r = f(&compiler);
let r = f(compiler);

if compiler.sess.profile_queries() {
profile::dump(&compiler.sess, "profile_queries".to_string())
if sess.profile_queries() {
profile::dump(&sess, "profile_queries".to_string())
}

r
}

pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
where
F: FnOnce(&Compiler) -> R + Send,
F: FnOnce(Compiler) -> R + Send,
R: Send,
{
let stderr = config.stderr.take();
Expand Down
1 change: 0 additions & 1 deletion src/librustc_interface/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extern crate libc;

pub mod interface;
mod passes;
mod queries;
pub mod util;
mod proc_macro_decls;
mod profile;
Expand Down
Loading

0 comments on commit 9b2133e

Please sign in to comment.