Skip to content

Commit 9b2133e

Browse files
committed
Remove queries from rustc_interface
1 parent 59f099e commit 9b2133e

File tree

7 files changed

+167
-304
lines changed

7 files changed

+167
-304
lines changed

src/librustc_driver/lib.rs

+61-68
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc::session::config::nightly_options;
3636
use rustc::session::{early_error, early_warn};
3737
use rustc::lint::Lint;
3838
use rustc::lint;
39+
use rustc::ty::TyCtxt;
3940
use rustc::hir::def_id::LOCAL_CRATE;
4041
use rustc::util::common::{ErrorReported, install_panic_hook, print_time_passes_entry};
4142
use rustc::util::common::{set_time_depth, time};
@@ -106,11 +107,19 @@ pub trait Callbacks {
106107
/// Called before creating the compiler instance
107108
fn config(&mut self, _config: &mut interface::Config) {}
108109
/// Called after parsing and returns true to continue execution
109-
fn after_parsing(&mut self, _compiler: &interface::Compiler) -> bool {
110+
fn after_parsing(
111+
&mut self,
112+
_compiler: &interface::Compiler,
113+
_tcx: TyCtxt<'_>,
114+
) -> bool {
110115
true
111116
}
112117
/// Called after analysis and returns true to continue execution
113-
fn after_analysis(&mut self, _compiler: &interface::Compiler) -> bool {
118+
fn after_analysis(
119+
&mut self,
120+
_compiler: &interface::Compiler,
121+
_tcx: TyCtxt<'_>,
122+
) -> bool {
114123
true
115124
}
116125
}
@@ -244,7 +253,8 @@ pub fn run_compiler(
244253
let pretty_info = parse_pretty(&mut config.opts, &matches);
245254

246255
interface::run_compiler(config, |compiler| {
247-
let sess = compiler.session();
256+
let sess = compiler.session().clone();
257+
let sess = &*sess;
248258
let should_stop = RustcDefaultCalls::print_crate_info(
249259
&**compiler.codegen_backend(),
250260
sess,
@@ -262,9 +272,9 @@ pub fn run_compiler(
262272
return sess.compile_status();
263273
}
264274

265-
if let Some((ppm, opt_uii)) = pretty_info {
266-
if ppm.needs_ast_map(&opt_uii) {
267-
compiler.enter(|tcx| {
275+
let link = compiler.enter(|compiler, tcx| {
276+
if let Some((ppm, opt_uii)) = pretty_info {
277+
if ppm.needs_ast_map(&opt_uii) {
268278
let expansion_result = tcx.expand_macros(())?;
269279
pretty::print_after_hir_lowering(
270280
tcx,
@@ -274,11 +284,8 @@ pub fn run_compiler(
274284
opt_uii.clone(),
275285
compiler.output_file().as_ref().map(|p| &**p),
276286
);
277-
Ok(())
278-
})?;
279-
return sess.compile_status();
280-
} else {
281-
compiler.enter(|tcx| {
287+
return sess.compile_status().map(|_| None);
288+
} else {
282289
let krate = tcx.parse(())?;
283290
let krate = krate.borrow();
284291
pretty::print_after_parsing(
@@ -288,57 +295,48 @@ pub fn run_compiler(
288295
ppm,
289296
compiler.output_file().as_ref().map(|p| &**p),
290297
);
291-
Ok(())
292-
})?;
293-
return sess.compile_status();
298+
return sess.compile_status().map(|_| None);
299+
}
294300
}
295-
}
296301

297-
compiler.enter(|tcx| tcx.parse(()))?;
302+
tcx.parse(())?;
298303

299-
if !callbacks.after_parsing(compiler) {
300-
return sess.compile_status();
301-
}
304+
if !callbacks.after_parsing(compiler, tcx) {
305+
return sess.compile_status().map(|_| None);
306+
}
302307

303-
if sess.opts.debugging_opts.parse_only ||
304-
sess.opts.debugging_opts.show_span.is_some() ||
305-
sess.opts.debugging_opts.ast_json_noexpand {
306-
return sess.compile_status();
307-
}
308+
if sess.opts.debugging_opts.parse_only ||
309+
sess.opts.debugging_opts.show_span.is_some() ||
310+
sess.opts.debugging_opts.ast_json_noexpand {
311+
return sess.compile_status().map(|_| None);
312+
}
308313

309-
compiler.enter(|tcx| tcx.register_plugins(()))?;
314+
tcx.register_plugins(())?;
310315

311-
// Lint plugins are registered; now we can process command line flags.
312-
if sess.opts.describe_lints {
313-
describe_lints(&sess, &sess.lint_store.borrow(), true);
314-
return sess.compile_status();
315-
}
316+
// Lint plugins are registered; now we can process command line flags.
317+
if sess.opts.describe_lints {
318+
describe_lints(&sess, &sess.lint_store.borrow(), true);
319+
return sess.compile_status().map(|_| None);
320+
}
316321

317-
compiler.enter(|tcx| {
318322
tcx.prepare_outputs(())?;
319-
Ok(())
320-
})?;
321323

322-
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
323-
&& sess.opts.output_types.len() == 1
324-
{
325-
return sess.compile_status();
326-
}
324+
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
325+
&& sess.opts.output_types.len() == 1
326+
{
327+
return sess.compile_status().map(|_| None);
328+
}
327329

328-
compiler.enter(|tcx| {
329330
tcx.lower_ast_to_hir(())?;
330-
Ok(())
331-
})?;
332331

333-
if sess.opts.debugging_opts.no_analysis ||
334-
sess.opts.debugging_opts.ast_json {
335-
return sess.compile_status();
336-
}
332+
if sess.opts.debugging_opts.no_analysis ||
333+
sess.opts.debugging_opts.ast_json {
334+
return sess.compile_status().map(|_| None);
335+
}
337336

338-
if sess.opts.debugging_opts.save_analysis {
339-
compiler.enter(|tcx| {
337+
if sess.opts.debugging_opts.save_analysis {
340338
let expansion_result = tcx.expand_macros(())?;
341-
let result = tcx.analysis(LOCAL_CRATE);
339+
tcx.analysis(LOCAL_CRATE).ok();
342340
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();
343341

344342
time(sess, "save analysis", || {
@@ -351,41 +349,36 @@ pub fn run_compiler(
351349
DumpHandler::new(compiler.output_dir().as_ref().map(|p| &**p), crate_name)
352350
)
353351
});
354-
355-
result
356352
// AST will be dropped *after* the `after_analysis` callback
357353
// (needed by the RLS)
358-
})?;
359-
} else {
360-
compiler.enter(|tcx| {
354+
} else {
361355
// Drop AST after lowering HIR to free memory
362356
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
363-
});
364-
}
357+
}
365358

366-
compiler.enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
359+
tcx.analysis(LOCAL_CRATE)?;
367360

368-
if !callbacks.after_analysis(compiler) {
369-
return sess.compile_status();
370-
}
361+
if !callbacks.after_analysis(compiler, tcx) {
362+
return sess.compile_status().map(|_| None);
363+
}
371364

372-
if sess.opts.debugging_opts.save_analysis {
373-
compiler.enter(|tcx| {
374-
// Drop AST after lowering HIR to free memory
365+
if sess.opts.debugging_opts.save_analysis {
366+
// Drop AST after running `after_analysis` callback to free memory
375367
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
376-
});
377-
}
368+
}
378369

379-
compiler.ongoing_codegen()?;
370+
compiler.linker(tcx).map(|linker| Some(linker))
371+
})?;
380372

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

384374
if sess.opts.debugging_opts.print_type_sizes {
385375
sess.code_stats.borrow().print_type_sizes();
386376
}
387377

388-
compiler.link()?;
378+
// Run linker outside `enter` so GlobalCtxt is freed
379+
if let Some(linker) = link {
380+
linker.link()?;
381+
}
389382

390383
if sess.opts.debugging_opts.perf_stats {
391384
sess.print_perf_stats();

src/librustc_interface/interface.rs

+68-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
use crate::queries::Queries;
21
use crate::util;
32
use crate::profile;
3+
use crate::passes;
44
pub use crate::passes::BoxedResolver;
55

66
use rustc::lint;
7-
use rustc::session::config::{self, Input, InputsAndOutputs};
7+
use rustc::session::config::{self, Input, InputsAndOutputs, OutputType};
8+
use rustc::hir::def_id::LOCAL_CRATE;
89
use rustc::session::{DiagnosticOutput, Session};
910
use rustc::util::common::ErrorReported;
10-
use rustc::ty::TyCtxt;
11+
use rustc::ty::{self, TyCtxt};
1112
use rustc_codegen_utils::codegen_backend::CodegenBackend;
1213
use rustc_data_structures::OnDrop;
13-
use rustc_data_structures::sync::Lrc;
14+
use rustc_data_structures::sync::{Lrc, OneThread};
1415
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1516
use rustc_metadata::cstore::CStore;
1617
use std::io::Write;
1718
use std::path::PathBuf;
1819
use std::result;
1920
use std::sync::{Arc, Mutex};
21+
use std::mem;
2022
use syntax;
2123
use syntax::source_map::{FileLoader, SourceMap};
2224
use syntax_pos::edition;
@@ -31,7 +33,6 @@ pub struct Compiler {
3133
codegen_backend: Arc<dyn CodegenBackend + Send + Sync>,
3234
source_map: Lrc<SourceMap>,
3335
pub(crate) io: InputsAndOutputs,
34-
pub(crate) queries: Queries,
3536
pub(crate) cstore: Lrc<CStore>,
3637
pub(crate) crate_name: Option<String>,
3738
}
@@ -58,11 +59,61 @@ impl Compiler {
5859
pub fn output_file(&self) -> &Option<PathBuf> {
5960
&self.io.output_file
6061
}
61-
pub fn enter<F, R>(&self, f: F) -> R
62+
pub fn enter<F, R>(self, f: F) -> R
6263
where
63-
F: FnOnce(TyCtxt<'_>) -> R
64+
F: FnOnce(&Compiler, TyCtxt<'_>) -> R
6465
{
65-
self.global_ctxt().unwrap().peek_mut().enter(f)
66+
passes::enter_global_ctxt(&self, f)
67+
}
68+
pub fn linker(&self, tcx: TyCtxt<'_>) -> Result<Linker> {
69+
tcx.ongoing_codegen(LOCAL_CRATE).map(|ongoing_codegen| {
70+
Linker {
71+
sess: self.sess.clone(),
72+
ongoing_codegen,
73+
codegen_backend: self.codegen_backend.clone(),
74+
}
75+
})
76+
}
77+
pub fn compile(self) -> Result<()> {
78+
let link = self.enter(|compiler, tcx| {
79+
tcx.prepare_outputs(())?;
80+
81+
if tcx.sess.opts.output_types.contains_key(&OutputType::DepInfo)
82+
&& tcx.sess.opts.output_types.len() == 1
83+
{
84+
return Ok(None)
85+
}
86+
87+
tcx.lower_ast_to_hir(())?;
88+
// Drop AST after lowering HIR to free memory
89+
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
90+
91+
compiler.linker(tcx).map(|linker| Some(linker))
92+
})?;
93+
94+
// Run linker outside `enter` so GlobalCtxt is freed
95+
if let Some(linker) = link {
96+
linker.link()
97+
} else {
98+
Ok(())
99+
}
100+
}
101+
}
102+
103+
pub struct Linker {
104+
sess: Lrc<Session>,
105+
ongoing_codegen: Lrc<ty::OngoingCodegen>,
106+
codegen_backend: Arc<dyn CodegenBackend + Send + Sync>,
107+
}
108+
109+
impl Linker {
110+
pub fn link(self) -> Result<()> {
111+
self.codegen_backend.join_codegen_and_link(
112+
OneThread::into_inner(self.ongoing_codegen.codegen_object.steal()),
113+
&self.sess,
114+
&self.ongoing_codegen.dep_graph,
115+
&self.ongoing_codegen.outputs,
116+
).map_err(|_| ErrorReported)
66117
}
67118
}
68119

@@ -90,7 +141,7 @@ pub struct Config {
90141

91142
pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
92143
where
93-
F: FnOnce(&Compiler) -> R,
144+
F: FnOnce(Compiler) -> R,
94145
{
95146
let (sess, codegen_backend, source_map) = util::create_session(
96147
config.opts,
@@ -104,7 +155,7 @@ where
104155
let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
105156

106157
let compiler = Compiler {
107-
sess,
158+
sess: sess.clone(),
108159
codegen_backend,
109160
source_map,
110161
cstore,
@@ -114,30 +165,29 @@ where
114165
output_dir: config.output_dir,
115166
output_file: config.output_file,
116167
},
117-
queries: Default::default(),
118168
crate_name: config.crate_name,
119169
};
120170

121171
let _sess_abort_error = OnDrop(|| {
122-
compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
172+
sess.diagnostic().print_error_count(&util::diagnostics_registry());
123173
});
124174

125-
if compiler.sess.profile_queries() {
126-
profile::begin(&compiler.sess);
175+
if sess.profile_queries() {
176+
profile::begin(&sess);
127177
}
128178

129-
let r = f(&compiler);
179+
let r = f(compiler);
130180

131-
if compiler.sess.profile_queries() {
132-
profile::dump(&compiler.sess, "profile_queries".to_string())
181+
if sess.profile_queries() {
182+
profile::dump(&sess, "profile_queries".to_string())
133183
}
134184

135185
r
136186
}
137187

138188
pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
139189
where
140-
F: FnOnce(&Compiler) -> R + Send,
190+
F: FnOnce(Compiler) -> R + Send,
141191
R: Send,
142192
{
143193
let stderr = config.stderr.take();

src/librustc_interface/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern crate libc;
1818

1919
pub mod interface;
2020
mod passes;
21-
mod queries;
2221
pub mod util;
2322
mod proc_macro_decls;
2423
mod profile;

0 commit comments

Comments
 (0)