Skip to content

Commit 29c1a79

Browse files
committed
Remove queries from rustc_interface
1 parent c8158e7 commit 29c1a79

File tree

7 files changed

+171
-308
lines changed

7 files changed

+171
-308
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::{LocalCrate, LOCAL_CRATE};
4041
use rustc::util::common::{time, ErrorReported, install_panic_hook};
4142
use rustc_metadata::locator;
@@ -103,11 +104,19 @@ pub trait Callbacks {
103104
/// Called before creating the compiler instance
104105
fn config(&mut self, _config: &mut interface::Config) {}
105106
/// Called after parsing and returns true to continue execution
106-
fn after_parsing(&mut self, _compiler: &interface::Compiler) -> bool {
107+
fn after_parsing<'tcx>(
108+
&mut self,
109+
_compiler: &interface::Compiler,
110+
_tcx: TyCtxt<'_, 'tcx, 'tcx>,
111+
) -> bool {
107112
true
108113
}
109114
/// Called after analysis and returns true to continue execution
110-
fn after_analysis(&mut self, _compiler: &interface::Compiler) -> bool {
115+
fn after_analysis<'tcx>(
116+
&mut self,
117+
_compiler: &interface::Compiler,
118+
_tcx: TyCtxt<'_, 'tcx, 'tcx>,
119+
) -> bool {
111120
true
112121
}
113122
}
@@ -229,7 +238,8 @@ pub fn run_compiler(
229238
let pretty_info = parse_pretty(&mut config.opts, &matches);
230239

231240
interface::run_compiler(config, |compiler| {
232-
let sess = compiler.session();
241+
let sess = compiler.session().clone();
242+
let sess = &*sess;
233243
let should_stop = RustcDefaultCalls::print_crate_info(
234244
&**compiler.codegen_backend(),
235245
sess,
@@ -247,9 +257,9 @@ pub fn run_compiler(
247257
return sess.compile_status();
248258
}
249259

250-
if let Some((ppm, opt_uii)) = pretty_info {
251-
if ppm.needs_ast_map(&opt_uii) {
252-
compiler.enter(|tcx| {
260+
let link = compiler.enter(|compiler, tcx| {
261+
if let Some((ppm, opt_uii)) = pretty_info {
262+
if ppm.needs_ast_map(&opt_uii) {
253263
let expansion_result = tcx.expand_macros(LocalCrate)?;
254264
pretty::print_after_hir_lowering(
255265
tcx,
@@ -259,11 +269,8 @@ pub fn run_compiler(
259269
opt_uii.clone(),
260270
compiler.output_file().as_ref().map(|p| &**p),
261271
);
262-
Ok(())
263-
})?;
264-
return sess.compile_status();
265-
} else {
266-
compiler.enter(|tcx| {
272+
return sess.compile_status().map(|_| None);
273+
} else {
267274
let krate = tcx.parse(LocalCrate)?;
268275
let krate = krate.borrow();
269276
pretty::print_after_parsing(
@@ -273,57 +280,48 @@ pub fn run_compiler(
273280
ppm,
274281
compiler.output_file().as_ref().map(|p| &**p),
275282
);
276-
Ok(())
277-
})?;
278-
return sess.compile_status();
283+
return sess.compile_status().map(|_| None);
284+
}
279285
}
280-
}
281286

282-
compiler.enter(|tcx| tcx.parse(LocalCrate))?;
287+
tcx.parse(LocalCrate)?;
283288

284-
if !callbacks.after_parsing(compiler) {
285-
return sess.compile_status();
286-
}
289+
if !callbacks.after_parsing(compiler, tcx) {
290+
return sess.compile_status().map(|_| None);
291+
}
287292

288-
if sess.opts.debugging_opts.parse_only ||
289-
sess.opts.debugging_opts.show_span.is_some() ||
290-
sess.opts.debugging_opts.ast_json_noexpand {
291-
return sess.compile_status();
292-
}
293+
if sess.opts.debugging_opts.parse_only ||
294+
sess.opts.debugging_opts.show_span.is_some() ||
295+
sess.opts.debugging_opts.ast_json_noexpand {
296+
return sess.compile_status().map(|_| None);
297+
}
293298

294-
compiler.enter(|tcx| tcx.register_plugins(LocalCrate))?;
299+
tcx.register_plugins(LocalCrate)?;
295300

296-
// Lint plugins are registered; now we can process command line flags.
297-
if sess.opts.describe_lints {
298-
describe_lints(&sess, &sess.lint_store.borrow(), true);
299-
return sess.compile_status();
300-
}
301+
// Lint plugins are registered; now we can process command line flags.
302+
if sess.opts.describe_lints {
303+
describe_lints(&sess, &sess.lint_store.borrow(), true);
304+
return sess.compile_status().map(|_| None);
305+
}
301306

302-
compiler.enter(|tcx| {
303307
tcx.prepare_outputs(LocalCrate)?;
304-
Ok(())
305-
})?;
306308

307-
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
308-
&& sess.opts.output_types.len() == 1
309-
{
310-
return sess.compile_status();
311-
}
309+
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
310+
&& sess.opts.output_types.len() == 1
311+
{
312+
return sess.compile_status().map(|_| None);
313+
}
312314

313-
compiler.enter(|tcx| {
314315
tcx.lower_ast_to_hir(LocalCrate)?;
315-
Ok(())
316-
})?;
317316

318-
if sess.opts.debugging_opts.no_analysis ||
319-
sess.opts.debugging_opts.ast_json {
320-
return sess.compile_status();
321-
}
317+
if sess.opts.debugging_opts.no_analysis ||
318+
sess.opts.debugging_opts.ast_json {
319+
return sess.compile_status().map(|_| None);
320+
}
322321

323-
if sess.opts.debugging_opts.save_analysis {
324-
compiler.enter(|tcx| {
322+
if sess.opts.debugging_opts.save_analysis {
325323
let expansion_result = tcx.expand_macros(LocalCrate)?;
326-
let result = tcx.analysis(LOCAL_CRATE);
324+
tcx.analysis(LOCAL_CRATE).ok();
327325
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();
328326

329327
time(sess, "save analysis", || {
@@ -336,41 +334,36 @@ pub fn run_compiler(
336334
DumpHandler::new(compiler.output_dir().as_ref().map(|p| &**p), crate_name)
337335
)
338336
});
339-
340-
result
341337
// AST will be dropped *after* the `after_analysis` callback
342338
// (needed by the RLS)
343-
})?;
344-
} else {
345-
compiler.enter(|tcx| {
339+
} else {
346340
// Drop AST after lowering HIR to free memory
347341
mem::drop(tcx.expand_macros(LocalCrate).unwrap().ast_crate.steal());
348-
});
349-
}
342+
}
350343

351-
compiler.enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
344+
tcx.analysis(LOCAL_CRATE)?;
352345

353-
if !callbacks.after_analysis(compiler) {
354-
return sess.compile_status();
355-
}
346+
if !callbacks.after_analysis(compiler, tcx) {
347+
return sess.compile_status().map(|_| None);
348+
}
356349

357-
if sess.opts.debugging_opts.save_analysis {
358-
compiler.enter(|tcx| {
359-
// Drop AST after lowering HIR to free memory
350+
if sess.opts.debugging_opts.save_analysis {
351+
// Drop AST after running `after_analysis` callback to free memory
360352
mem::drop(tcx.expand_macros(LocalCrate).unwrap().ast_crate.steal());
361-
});
362-
}
353+
}
363354

364-
compiler.ongoing_codegen()?;
355+
compiler.linker(tcx).map(|linker| Some(linker))
356+
})?;
365357

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

369359
if sess.opts.debugging_opts.print_type_sizes {
370360
sess.code_stats.borrow().print_type_sizes();
371361
}
372362

373-
compiler.link()?;
363+
// Run linker outside `enter` so GlobalCtxt is freed
364+
if let Some(linker) = link {
365+
linker.link()?;
366+
}
374367

375368
if sess.opts.debugging_opts.perf_stats {
376369
sess.print_perf_stats();

src/librustc_interface/interface.rs

+71-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
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::session::config::{Input, OutputFilenames, };
9+
use rustc::hir::def_id::{LocalCrate, LOCAL_CRATE};
810
use rustc::session::{DiagnosticOutput, Session};
911
use rustc::util::common::ErrorReported;
10-
use rustc::ty::TyCtxt;
12+
use rustc::ty::{self, TyCtxt};
1113
use rustc_codegen_utils::codegen_backend::CodegenBackend;
1214
use rustc_data_structures::OnDrop;
13-
use rustc_data_structures::sync::Lrc;
15+
use rustc_data_structures::sync::{Lrc, OneThread};
1416
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1517
use rustc_metadata::cstore::CStore;
1618
use std::io::Write;
1719
use std::path::PathBuf;
1820
use std::result;
1921
use std::sync::{Arc, Mutex};
22+
use std::mem;
2023
use syntax;
2124
use syntax::source_map::{FileLoader, SourceMap};
2225

@@ -30,7 +33,6 @@ pub struct Compiler {
3033
codegen_backend: Arc<dyn CodegenBackend + Send + Sync>,
3134
source_map: Lrc<SourceMap>,
3235
pub(crate) io: InputsAndOutputs,
33-
pub(crate) queries: Queries,
3436
pub(crate) cstore: Lrc<CStore>,
3537
pub(crate) crate_name: Option<String>,
3638
}
@@ -57,11 +59,61 @@ impl Compiler {
5759
pub fn output_file(&self) -> &Option<PathBuf> {
5860
&self.io.output_file
5961
}
60-
pub fn enter<F, R>(&self, f: F) -> R
62+
pub fn enter<F, R>(self, f: F) -> R
6163
where
62-
F: for<'tcx> FnOnce(TyCtxt<'tcx, 'tcx, 'tcx>) -> R
64+
F: for<'tcx> FnOnce(&Compiler, TyCtxt<'tcx, 'tcx, 'tcx>) -> R
6365
{
64-
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(LocalCrate)?;
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(LocalCrate)?;
88+
// Drop AST after lowering HIR to free memory
89+
mem::drop(tcx.expand_macros(LocalCrate).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)
65117
}
66118
}
67119

@@ -89,7 +141,7 @@ pub struct Config {
89141

90142
pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
91143
where
92-
F: FnOnce(&Compiler) -> R,
144+
F: FnOnce(Compiler) -> R,
93145
{
94146
let (sess, codegen_backend, source_map) = util::create_session(
95147
config.opts,
@@ -103,7 +155,7 @@ where
103155
let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
104156

105157
let compiler = Compiler {
106-
sess,
158+
sess: sess.clone(),
107159
codegen_backend,
108160
source_map,
109161
cstore,
@@ -113,32 +165,31 @@ where
113165
output_dir: config.output_dir,
114166
output_file: config.output_file,
115167
},
116-
queries: Default::default(),
117168
crate_name: config.crate_name,
118169
};
119170

120-
let _sess_abort_error = OnDrop(|| compiler.sess.diagnostic().print_error_count());
171+
let _sess_abort_error = OnDrop(|| sess.diagnostic().print_error_count());
121172

122-
if compiler.sess.profile_queries() {
123-
profile::begin(&compiler.sess);
173+
if sess.profile_queries() {
174+
profile::begin(&sess);
124175
}
125176

126-
let r = f(&compiler);
177+
let r = f(compiler);
127178

128-
if compiler.sess.profile_queries() {
129-
profile::dump(&compiler.sess, "profile_queries".to_string())
179+
if sess.profile_queries() {
180+
profile::dump(&sess, "profile_queries".to_string())
130181
}
131182

132-
if compiler.sess.opts.debugging_opts.self_profile {
133-
compiler.sess.profiler(|p| p.dump_raw_events(&compiler.sess.opts));
183+
if sess.opts.debugging_opts.self_profile {
184+
sess.profiler(|p| p.dump_raw_events(&sess.opts));
134185
}
135186

136187
r
137188
}
138189

139190
pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
140191
where
141-
F: FnOnce(&Compiler) -> R + Send,
192+
F: FnOnce(Compiler) -> R + Send,
142193
R: Send,
143194
{
144195
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)