Skip to content

Commit 9a32226

Browse files
authored
Rollup merge of rust-lang#66791 - cjgillot:arena, r=Mark-Simulacrum
Handle GlobalCtxt directly from librustc_interface query system This PR constructs the `GlobalCtxt` as a member of the `Queries` in librustc_interface. This simplifies the code to construct it, at the expense of added complexity in the query control flow. This allows to handle the arenas directly from librustc_interface. Based on rust-lang#66707 r? @Zoxc
2 parents b3d835f + 1e12f39 commit 9a32226

File tree

10 files changed

+328
-256
lines changed

10 files changed

+328
-256
lines changed

src/librustc/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ macro_rules! arena_types {
9393
rustc::hir::def_id::CrateNum
9494
>
9595
>,
96+
[few] hir_forest: rustc::hir::map::Forest,
9697
[few] diagnostic_items: rustc_data_structures::fx::FxHashMap<
9798
syntax::symbol::Symbol,
9899
rustc::hir::def_id::DefId,

src/librustc/hir/map/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub struct Map<'hir> {
200200

201201
map: HirEntryMap<'hir>,
202202

203-
definitions: &'hir Definitions,
203+
definitions: Definitions,
204204

205205
/// The reverse mapping of `node_to_hir_id`.
206206
hir_to_node_id: FxHashMap<HirId, NodeId>,
@@ -267,8 +267,8 @@ impl<'hir> Map<'hir> {
267267
}
268268

269269
#[inline]
270-
pub fn definitions(&self) -> &'hir Definitions {
271-
self.definitions
270+
pub fn definitions(&self) -> &Definitions {
271+
&self.definitions
272272
}
273273

274274
pub fn def_key(&self, def_id: DefId) -> DefKey {
@@ -1251,7 +1251,7 @@ impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
12511251
pub fn map_crate<'hir>(sess: &crate::session::Session,
12521252
cstore: &CrateStoreDyn,
12531253
forest: &'hir Forest,
1254-
definitions: &'hir Definitions)
1254+
definitions: Definitions)
12551255
-> Map<'hir> {
12561256
let _prof_timer = sess.prof.generic_activity("build_hir_map");
12571257

@@ -1260,7 +1260,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
12601260
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
12611261

12621262
let (map, crate_hash) = {
1263-
let hcx = crate::ich::StableHashingContext::new(sess, &forest.krate, definitions, cstore);
1263+
let hcx = crate::ich::StableHashingContext::new(sess, &forest.krate, &definitions, cstore);
12641264

12651265
let mut collector = NodeCollector::root(sess,
12661266
&forest.krate,

src/librustc/ty/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ impl<'tcx> Deref for TyCtxt<'tcx> {
995995
}
996996

997997
pub struct GlobalCtxt<'tcx> {
998-
pub arena: WorkerLocal<Arena<'tcx>>,
998+
pub arena: &'tcx WorkerLocal<Arena<'tcx>>,
999999

10001000
interners: CtxtInterners<'tcx>,
10011001

@@ -1170,6 +1170,7 @@ impl<'tcx> TyCtxt<'tcx> {
11701170
local_providers: ty::query::Providers<'tcx>,
11711171
extern_providers: ty::query::Providers<'tcx>,
11721172
arenas: &'tcx AllArenas,
1173+
arena: &'tcx WorkerLocal<Arena<'tcx>>,
11731174
resolutions: ty::ResolverOutputs,
11741175
hir: hir_map::Map<'tcx>,
11751176
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
@@ -1225,7 +1226,7 @@ impl<'tcx> TyCtxt<'tcx> {
12251226
sess: s,
12261227
lint_store,
12271228
cstore,
1228-
arena: WorkerLocal::new(|_| Arena::default()),
1229+
arena,
12291230
interners,
12301231
dep_graph,
12311232
prof: s.prof.clone(),

src/librustc_driver/lib.rs

+99-92
Original file line numberDiff line numberDiff line change
@@ -283,120 +283,127 @@ pub fn run_compiler(
283283
return sess.compile_status();
284284
}
285285

286-
compiler.parse()?;
287-
288-
if let Some(ppm) = &sess.opts.pretty {
289-
if ppm.needs_ast_map() {
290-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
291-
let expanded_crate = compiler.expansion()?.take().0;
292-
pretty::print_after_hir_lowering(
293-
tcx,
294-
compiler.input(),
295-
&expanded_crate,
286+
let linker = compiler.enter(|queries| {
287+
let early_exit = || sess.compile_status().map(|_| None);
288+
queries.parse()?;
289+
290+
if let Some(ppm) = &sess.opts.pretty {
291+
if ppm.needs_ast_map() {
292+
queries.global_ctxt()?.peek_mut().enter(|tcx| {
293+
let expanded_crate = queries.expansion()?.take().0;
294+
pretty::print_after_hir_lowering(
295+
tcx,
296+
compiler.input(),
297+
&expanded_crate,
298+
*ppm,
299+
compiler.output_file().as_ref().map(|p| &**p),
300+
);
301+
Ok(())
302+
})?;
303+
} else {
304+
let krate = queries.parse()?.take();
305+
pretty::print_after_parsing(
306+
sess,
307+
&compiler.input(),
308+
&krate,
296309
*ppm,
297310
compiler.output_file().as_ref().map(|p| &**p),
298311
);
299-
Ok(())
300-
})?;
301-
} else {
302-
let krate = compiler.parse()?.take();
303-
pretty::print_after_parsing(
304-
sess,
305-
&compiler.input(),
306-
&krate,
307-
*ppm,
308-
compiler.output_file().as_ref().map(|p| &**p),
309-
);
312+
}
313+
return early_exit();
310314
}
311-
return sess.compile_status();
312-
}
313315

314-
if callbacks.after_parsing(compiler) == Compilation::Stop {
315-
return sess.compile_status();
316-
}
316+
if callbacks.after_parsing(compiler) == Compilation::Stop {
317+
return early_exit();
318+
}
317319

318-
if sess.opts.debugging_opts.parse_only ||
319-
sess.opts.debugging_opts.show_span.is_some() ||
320-
sess.opts.debugging_opts.ast_json_noexpand {
321-
return sess.compile_status();
322-
}
320+
if sess.opts.debugging_opts.parse_only ||
321+
sess.opts.debugging_opts.show_span.is_some() ||
322+
sess.opts.debugging_opts.ast_json_noexpand {
323+
return early_exit();
324+
}
323325

324-
{
325-
let (_, lint_store) = &*compiler.register_plugins()?.peek();
326+
{
327+
let (_, lint_store) = &*queries.register_plugins()?.peek();
326328

327-
// Lint plugins are registered; now we can process command line flags.
328-
if sess.opts.describe_lints {
329-
describe_lints(&sess, &lint_store, true);
330-
return sess.compile_status();
329+
// Lint plugins are registered; now we can process command line flags.
330+
if sess.opts.describe_lints {
331+
describe_lints(&sess, &lint_store, true);
332+
return early_exit();
333+
}
331334
}
332-
}
333335

334-
compiler.expansion()?;
335-
if callbacks.after_expansion(compiler) == Compilation::Stop {
336-
return sess.compile_status();
337-
}
336+
queries.expansion()?;
337+
if callbacks.after_expansion(compiler) == Compilation::Stop {
338+
return early_exit();
339+
}
338340

339-
compiler.prepare_outputs()?;
341+
queries.prepare_outputs()?;
340342

341-
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
342-
&& sess.opts.output_types.len() == 1
343-
{
344-
return sess.compile_status();
345-
}
343+
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
344+
&& sess.opts.output_types.len() == 1
345+
{
346+
return early_exit();
347+
}
346348

347-
compiler.global_ctxt()?;
349+
queries.global_ctxt()?;
348350

349-
if sess.opts.debugging_opts.no_analysis ||
350-
sess.opts.debugging_opts.ast_json {
351-
return sess.compile_status();
352-
}
351+
if sess.opts.debugging_opts.no_analysis ||
352+
sess.opts.debugging_opts.ast_json {
353+
return early_exit();
354+
}
353355

354-
if sess.opts.debugging_opts.save_analysis {
355-
let expanded_crate = &compiler.expansion()?.peek().0;
356-
let crate_name = compiler.crate_name()?.peek().clone();
357-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
358-
let result = tcx.analysis(LOCAL_CRATE);
359-
360-
time(sess, "save analysis", || {
361-
save::process_crate(
362-
tcx,
363-
&expanded_crate,
364-
&crate_name,
365-
&compiler.input(),
366-
None,
367-
DumpHandler::new(compiler.output_dir().as_ref().map(|p| &**p), &crate_name)
368-
)
369-
});
370-
371-
result
372-
// AST will be dropped *after* the `after_analysis` callback
373-
// (needed by the RLS)
374-
})?;
375-
} else {
376-
// Drop AST after creating GlobalCtxt to free memory
377-
mem::drop(compiler.expansion()?.take());
378-
}
356+
if sess.opts.debugging_opts.save_analysis {
357+
let expanded_crate = &queries.expansion()?.peek().0;
358+
let crate_name = queries.crate_name()?.peek().clone();
359+
queries.global_ctxt()?.peek_mut().enter(|tcx| {
360+
let result = tcx.analysis(LOCAL_CRATE);
361+
362+
time(sess, "save analysis", || {
363+
save::process_crate(
364+
tcx,
365+
&expanded_crate,
366+
&crate_name,
367+
&compiler.input(),
368+
None,
369+
DumpHandler::new(
370+
compiler.output_dir().as_ref().map(|p| &**p), &crate_name
371+
)
372+
)
373+
});
379374

380-
compiler.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
375+
result
376+
// AST will be dropped *after* the `after_analysis` callback
377+
// (needed by the RLS)
378+
})?;
379+
} else {
380+
// Drop AST after creating GlobalCtxt to free memory
381+
mem::drop(queries.expansion()?.take());
382+
}
381383

382-
if callbacks.after_analysis(compiler) == Compilation::Stop {
383-
return sess.compile_status();
384-
}
384+
queries.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
385385

386-
if sess.opts.debugging_opts.save_analysis {
387-
mem::drop(compiler.expansion()?.take());
388-
}
386+
if callbacks.after_analysis(compiler) == Compilation::Stop {
387+
return early_exit();
388+
}
389389

390-
compiler.ongoing_codegen()?;
390+
if sess.opts.debugging_opts.save_analysis {
391+
mem::drop(queries.expansion()?.take());
392+
}
391393

392-
// Drop GlobalCtxt after starting codegen to free memory
393-
mem::drop(compiler.global_ctxt()?.take());
394+
queries.ongoing_codegen()?;
394395

395-
if sess.opts.debugging_opts.print_type_sizes {
396-
sess.code_stats.print_type_sizes();
397-
}
396+
if sess.opts.debugging_opts.print_type_sizes {
397+
sess.code_stats.print_type_sizes();
398+
}
398399

399-
compiler.link()?;
400+
let linker = queries.linker()?;
401+
Ok(Some(linker))
402+
})?;
403+
404+
if let Some(linker) = linker {
405+
linker.link()?
406+
}
400407

401408
if sess.opts.debugging_opts.perf_stats {
402409
sess.print_perf_stats();

src/librustc_interface/interface.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::queries::Queries;
21
use crate::util;
32
pub use crate::passes::BoxedResolver;
43

@@ -36,7 +35,6 @@ pub struct Compiler {
3635
pub(crate) input_path: Option<PathBuf>,
3736
pub(crate) output_dir: Option<PathBuf>,
3837
pub(crate) output_file: Option<PathBuf>,
39-
pub(crate) queries: Queries,
4038
pub(crate) crate_name: Option<String>,
4139
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
4240
pub(crate) override_queries:
@@ -169,7 +167,6 @@ pub fn run_compiler_in_existing_thread_pool<R>(
169167
input_path: config.input_path,
170168
output_dir: config.output_dir,
171169
output_file: config.output_file,
172-
queries: Default::default(),
173170
crate_name: config.crate_name,
174171
register_lints: config.register_lints,
175172
override_queries: config.override_queries,

0 commit comments

Comments
 (0)