Get rid of of the global_ctxt query

This commit is contained in:
bjorn3 2024-10-31 14:43:23 +00:00
parent ed14192604
commit 6ece803632
6 changed files with 48 additions and 70 deletions

View file

@ -8,7 +8,7 @@
// tidy-alphabetical-end
mod callbacks;
mod errors;
pub mod errors;
pub mod interface;
pub mod passes;
mod proc_macro_decls;
@ -17,7 +17,7 @@ pub mod util;
pub use callbacks::setup_callbacks;
pub use interface::{Config, run_compiler};
pub use passes::DEFAULT_QUERY_PROVIDERS;
pub use passes::{DEFAULT_QUERY_PROVIDERS, create_and_enter_global_ctxt};
pub use queries::{Linker, Queries};
#[cfg(test)]

View file

@ -41,7 +41,7 @@ use tracing::{info, instrument};
use crate::interface::Compiler;
use crate::{errors, proc_macro_decls, util};
pub(crate) fn parse<'a>(sess: &'a Session) -> ast::Crate {
pub fn parse<'a>(sess: &'a Session) -> ast::Crate {
let krate = sess
.time("parse_crate", || {
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
@ -709,7 +709,22 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
*providers
});
pub(crate) fn create_global_ctxt<'tcx>(
pub fn create_and_enter_global_ctxt<'tcx, T>(
compiler: &'tcx Compiler,
krate: rustc_ast::Crate,
f: impl for<'a> FnOnce(TyCtxt<'a>) -> T,
) -> T {
let gcx_cell = OnceLock::new();
let arena = WorkerLocal::new(|_| Arena::default());
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
let gcx = create_global_ctxt(compiler, krate, &gcx_cell, &arena, &hir_arena);
let ret = gcx.enter(f);
gcx.finish();
ret
}
fn create_global_ctxt<'tcx>(
compiler: &'tcx Compiler,
mut krate: rustc_ast::Crate,
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,

View file

@ -7,9 +7,7 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{OnceLock, WorkerLocal};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
use rustc_session::Session;
@ -65,51 +63,18 @@ impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
pub struct Queries<'tcx> {
compiler: &'tcx Compiler,
gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
arena: WorkerLocal<Arena<'tcx>>,
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
parse: Query<ast::Crate>,
// This just points to what's in `gcx_cell`.
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
}
impl<'tcx> Queries<'tcx> {
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
Queries {
compiler,
gcx_cell: OnceLock::new(),
arena: WorkerLocal::new(|_| Arena::default()),
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
parse: Query { result: RefCell::new(None) },
gcx: Query { result: RefCell::new(None) },
}
}
pub fn finish(&'tcx self) {
if let Some(gcx) = self.gcx_cell.get() {
gcx.finish();
}
Queries { compiler, parse: Query { result: RefCell::new(None) } }
}
pub fn parse(&self) -> QueryResult<'_, ast::Crate> {
self.parse.compute(|| passes::parse(&self.compiler.sess))
}
pub fn global_ctxt(&'tcx self) -> QueryResult<'tcx, &'tcx GlobalCtxt<'tcx>> {
self.gcx.compute(|| {
let krate = self.parse().steal();
passes::create_global_ctxt(
self.compiler,
krate,
&self.gcx_cell,
&self.arena,
&self.hir_arena,
)
})
}
}
pub struct Linker {
@ -192,16 +157,7 @@ impl Compiler {
where
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
{
// Must declare `_timer` first so that it is dropped after `queries`.
let _timer;
let queries = Queries::new(self);
let ret = f(&queries);
// The timer's lifetime spans the dropping of `queries`, which contains
// the global context.
_timer = self.sess.timer("free_global_ctxt");
queries.finish();
ret
f(&queries)
}
}