Auto merge of #134302 - bjorn3:remove_driver_queries, r=oli-obk,jieyouxu
Remove queries from the driver interface All uses of driver queries in the public api of rustc_driver have been removed in https://github.com/rust-lang/rust/pull/134130 already. This removes driver queries from rustc_interface and does a couple of cleanups around TyCtxt construction and entering enabled by this removal. Finishes the removal of driver queries started with https://github.com/rust-lang/rust/pull/126834.
This commit is contained in:
commit
978c659b72
9 changed files with 200 additions and 326 deletions
|
@ -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,8 +17,8 @@ pub mod util;
|
|||
|
||||
pub use callbacks::setup_callbacks;
|
||||
pub use interface::{Config, run_compiler};
|
||||
pub use passes::DEFAULT_QUERY_PROVIDERS;
|
||||
pub use queries::{Linker, Queries};
|
||||
pub use passes::{DEFAULT_QUERY_PROVIDERS, create_and_enter_global_ctxt, parse};
|
||||
pub use queries::Linker;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
@ -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 {
|
||||
|
@ -711,13 +711,11 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
|
|||
*providers
|
||||
});
|
||||
|
||||
pub(crate) fn create_global_ctxt<'tcx>(
|
||||
compiler: &'tcx Compiler,
|
||||
pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
|
||||
compiler: &Compiler,
|
||||
mut krate: rustc_ast::Crate,
|
||||
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
|
||||
arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
||||
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
|
||||
) -> &'tcx GlobalCtxt<'tcx> {
|
||||
f: F,
|
||||
) -> T {
|
||||
let sess = &compiler.sess;
|
||||
|
||||
rustc_builtin_macros::cmdline_attrs::inject(
|
||||
|
@ -765,44 +763,64 @@ pub(crate) fn create_global_ctxt<'tcx>(
|
|||
|
||||
let incremental = dep_graph.is_fully_enabled();
|
||||
|
||||
sess.time("setup_global_ctxt", || {
|
||||
let qcx = gcx_cell.get_or_init(move || {
|
||||
TyCtxt::create_global_ctxt(
|
||||
sess,
|
||||
crate_types,
|
||||
stable_crate_id,
|
||||
arena,
|
||||
hir_arena,
|
||||
untracked,
|
||||
dep_graph,
|
||||
rustc_query_impl::query_callbacks(arena),
|
||||
rustc_query_impl::query_system(
|
||||
providers.queries,
|
||||
providers.extern_queries,
|
||||
query_result_on_disk_cache,
|
||||
incremental,
|
||||
),
|
||||
providers.hooks,
|
||||
compiler.current_gcx.clone(),
|
||||
)
|
||||
});
|
||||
let gcx_cell = OnceLock::new();
|
||||
let arena = WorkerLocal::new(|_| Arena::default());
|
||||
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
|
||||
|
||||
qcx.enter(|tcx| {
|
||||
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
|
||||
assert_eq!(feed.key(), LOCAL_CRATE);
|
||||
feed.crate_name(crate_name);
|
||||
// This closure is necessary to force rustc to perform the correct lifetime
|
||||
// subtyping for GlobalCtxt::enter to be allowed.
|
||||
let inner: Box<
|
||||
dyn for<'tcx> FnOnce(
|
||||
&'tcx Compiler,
|
||||
&'tcx OnceLock<GlobalCtxt<'tcx>>,
|
||||
&'tcx WorkerLocal<Arena<'tcx>>,
|
||||
&'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
|
||||
F,
|
||||
) -> T,
|
||||
> = Box::new(move |compiler, gcx_cell, arena, hir_arena, f| {
|
||||
let sess = &compiler.sess;
|
||||
|
||||
let feed = tcx.feed_unit_query();
|
||||
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
|
||||
sess,
|
||||
&pre_configured_attrs,
|
||||
crate_name,
|
||||
)));
|
||||
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
|
||||
feed.output_filenames(Arc::new(outputs));
|
||||
});
|
||||
qcx
|
||||
})
|
||||
TyCtxt::create_global_ctxt(
|
||||
gcx_cell,
|
||||
sess,
|
||||
crate_types,
|
||||
stable_crate_id,
|
||||
arena,
|
||||
hir_arena,
|
||||
untracked,
|
||||
dep_graph,
|
||||
rustc_query_impl::query_callbacks(arena),
|
||||
rustc_query_impl::query_system(
|
||||
providers.queries,
|
||||
providers.extern_queries,
|
||||
query_result_on_disk_cache,
|
||||
incremental,
|
||||
),
|
||||
providers.hooks,
|
||||
compiler.current_gcx.clone(),
|
||||
|tcx| {
|
||||
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
|
||||
assert_eq!(feed.key(), LOCAL_CRATE);
|
||||
feed.crate_name(crate_name);
|
||||
|
||||
let feed = tcx.feed_unit_query();
|
||||
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
|
||||
sess,
|
||||
&pre_configured_attrs,
|
||||
crate_name,
|
||||
)));
|
||||
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
|
||||
feed.output_filenames(Arc::new(outputs));
|
||||
|
||||
let res = f(tcx);
|
||||
// FIXME maybe run finish even when a fatal error occured? or at least tcx.alloc_self_profile_query_strings()?
|
||||
tcx.finish();
|
||||
res
|
||||
},
|
||||
)
|
||||
});
|
||||
|
||||
inner(compiler, &gcx_cell, &arena, &hir_arena, f)
|
||||
}
|
||||
|
||||
/// Runs all analyses that we guarantee to run, even if errors were reported in earlier analyses.
|
||||
|
|
|
@ -1,117 +1,18 @@
|
|||
use std::any::Any;
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::sync::Arc;
|
||||
|
||||
use rustc_ast as ast;
|
||||
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_middle::ty::TyCtxt;
|
||||
use rustc_session::Session;
|
||||
use rustc_session::config::{self, OutputFilenames, OutputType};
|
||||
|
||||
use crate::errors::FailedWritingFile;
|
||||
use crate::interface::Compiler;
|
||||
use crate::passes;
|
||||
|
||||
/// Represent the result of a query.
|
||||
///
|
||||
/// This result can be stolen once with the [`steal`] method and generated with the [`compute`] method.
|
||||
///
|
||||
/// [`steal`]: Steal::steal
|
||||
/// [`compute`]: Self::compute
|
||||
pub struct Query<T> {
|
||||
/// `None` means no value has been computed yet.
|
||||
result: RefCell<Option<Steal<T>>>,
|
||||
}
|
||||
|
||||
impl<T> Query<T> {
|
||||
fn compute<F: FnOnce() -> T>(&self, f: F) -> QueryResult<'_, T> {
|
||||
QueryResult(RefMut::map(
|
||||
self.result.borrow_mut(),
|
||||
|r: &mut Option<Steal<T>>| -> &mut Steal<T> {
|
||||
r.get_or_insert_with(|| Steal::new(f()))
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct QueryResult<'a, T>(RefMut<'a, Steal<T>>);
|
||||
|
||||
impl<'a, T> std::ops::Deref for QueryResult<'a, T> {
|
||||
type Target = RefMut<'a, Steal<T>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
|
||||
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
|
||||
(*self.0).borrow().enter(f)
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
dep_graph: DepGraph,
|
||||
output_filenames: Arc<OutputFilenames>,
|
||||
|
@ -186,22 +87,3 @@ impl Linker {
|
|||
codegen_backend.link(sess, codegen_results, &self.output_filenames)
|
||||
}
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
pub fn enter<F, T>(&self, f: F) -> T
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue