1
Fork 0

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:
bors 2024-12-17 04:57:40 +00:00
commit 978c659b72
9 changed files with 200 additions and 326 deletions

View file

@ -45,7 +45,7 @@ use rustc_errors::registry::Registry;
use rustc_errors::{ColorConfig, DiagCtxt, ErrCode, FatalError, PResult, markdown};
use rustc_feature::find_gated_cfg;
use rustc_interface::util::{self, get_codegen_backend};
use rustc_interface::{Linker, interface, passes};
use rustc_interface::{Linker, create_and_enter_global_ctxt, interface, passes};
use rustc_lint::unerased_lint_store;
use rustc_metadata::creader::MetadataLoader;
use rustc_metadata::locator;
@ -387,37 +387,26 @@ fn run_compiler(
return early_exit();
}
let linker = compiler.enter(|queries| {
let early_exit = || {
sess.dcx().abort_if_errors();
None
};
// Parse the crate root source code (doesn't parse submodules yet)
// Everything else is parsed during macro expansion.
queries.parse();
let krate = passes::parse(sess);
// If pretty printing is requested: Figure out the representation, print it and exit
if let Some(pp_mode) = sess.opts.pretty {
if pp_mode.needs_ast_map() {
queries.global_ctxt().enter(|tcx| {
create_and_enter_global_ctxt(compiler, krate, |tcx| {
tcx.ensure().early_lint_checks(());
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
passes::write_dep_info(tcx);
});
} else {
let krate = queries.parse();
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing {
krate: &*krate.borrow(),
});
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing { krate: &krate });
}
trace!("finished pretty-printing");
return early_exit();
}
if callbacks.after_crate_root_parsing(compiler, &*queries.parse().borrow())
== Compilation::Stop
{
if callbacks.after_crate_root_parsing(compiler, &krate) == Compilation::Stop {
return early_exit();
}
@ -425,7 +414,12 @@ fn run_compiler(
return early_exit();
}
queries.global_ctxt().enter(|tcx| {
let linker = create_and_enter_global_ctxt(compiler, krate, |tcx| {
let early_exit = || {
sess.dcx().abort_if_errors();
None
};
// Make sure name resolution and macro expansion is run.
let _ = tcx.resolver_for_lowering();
@ -456,7 +450,6 @@ fn run_compiler(
}
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
})
});
// Linking is done outside the `compiler.enter()` so that the

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,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;

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 {
@ -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,9 +763,25 @@ 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 || {
let gcx_cell = OnceLock::new();
let arena = WorkerLocal::new(|_| Arena::default());
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
// 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;
TyCtxt::create_global_ctxt(
gcx_cell,
sess,
crate_types,
stable_crate_id,
@ -784,10 +798,7 @@ pub(crate) fn create_global_ctxt<'tcx>(
),
providers.hooks,
compiler.current_gcx.clone(),
)
});
qcx.enter(|tcx| {
|tcx| {
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
assert_eq!(feed.key(), LOCAL_CRATE);
feed.crate_name(crate_name);
@ -800,9 +811,16 @@ pub(crate) fn create_global_ctxt<'tcx>(
)));
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
},
)
});
qcx
})
inner(compiler, &gcx_cell, &arena, &hir_arena, f)
}
/// Runs all analyses that we guarantee to run, even if errors were reported in earlier analyses.

View file

@ -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
}
}

View file

@ -10,6 +10,7 @@ use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::{Bound, Deref};
use std::sync::OnceLock;
use std::{fmt, iter, mem};
use rustc_abi::{ExternAbi, FieldIdx, Layout, LayoutData, TargetDataLayout, VariantIdx};
@ -1346,46 +1347,6 @@ pub struct GlobalCtxt<'tcx> {
/// Stores memory for globals (statics/consts).
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
current_gcx: CurrentGcx,
}
impl<'tcx> GlobalCtxt<'tcx> {
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
/// `f`.
pub fn enter<F, R>(&'tcx self, f: F) -> R
where
F: FnOnce(TyCtxt<'tcx>) -> R,
{
let icx = tls::ImplicitCtxt::new(self);
// Reset `current_gcx` to `None` when we exit.
let _on_drop = defer(move || {
*self.current_gcx.value.write() = None;
});
// Set this `GlobalCtxt` as the current one.
{
let mut guard = self.current_gcx.value.write();
assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
*guard = Some(self as *const _ as *const ());
}
tls::enter_context(&icx, || f(icx.tcx))
}
pub fn finish(&'tcx self) {
// We assume that no queries are run past here. If there are new queries
// after this point, they'll show up as "<unknown>" in self-profiling data.
self.enter(|tcx| tcx.alloc_self_profile_query_strings());
self.enter(|tcx| tcx.save_dep_graph());
self.enter(|tcx| tcx.query_key_hash_verify_all());
if let Err((path, error)) = self.dep_graph.finish_encoding() {
self.sess.dcx().emit_fatal(crate::error::FailedWritingFile { path: &path, error });
}
}
}
/// This is used to get a reference to a `GlobalCtxt` if one is available.
@ -1529,7 +1490,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// By only providing the `TyCtxt` inside of the closure we enforce that the type
/// context and any interned value (types, args, etc.) can only be used while `ty::tls`
/// has a valid reference to the context, to allow formatting values that need it.
pub fn create_global_ctxt(
pub fn create_global_ctxt<T>(
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
s: &'tcx Session,
crate_types: Vec<CrateType>,
stable_crate_id: StableCrateId,
@ -1541,7 +1503,8 @@ impl<'tcx> TyCtxt<'tcx> {
query_system: QuerySystem<'tcx>,
hooks: crate::hooks::Providers,
current_gcx: CurrentGcx,
) -> GlobalCtxt<'tcx> {
f: impl FnOnce(TyCtxt<'tcx>) -> T,
) -> T {
let data_layout = s.target.parse_data_layout().unwrap_or_else(|err| {
s.dcx().emit_fatal(err);
});
@ -1550,7 +1513,7 @@ impl<'tcx> TyCtxt<'tcx> {
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types, s, &untracked);
GlobalCtxt {
let gcx = gcx_cell.get_or_init(|| GlobalCtxt {
sess: s,
crate_types,
stable_crate_id,
@ -1574,8 +1537,23 @@ impl<'tcx> TyCtxt<'tcx> {
canonical_param_env_cache: Default::default(),
data_layout,
alloc_map: Lock::new(interpret::AllocMap::new()),
current_gcx,
});
let icx = tls::ImplicitCtxt::new(&gcx);
// Reset `current_gcx` to `None` when we exit.
let _on_drop = defer(|| {
*current_gcx.value.write() = None;
});
// Set this `GlobalCtxt` as the current one.
{
let mut guard = current_gcx.value.write();
assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
*guard = Some(&gcx as *const _ as *const ());
}
tls::enter_context(&icx, || f(icx.tcx))
}
/// Obtain all lang items of this crate and all dependencies (recursively)
@ -2119,6 +2097,19 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
self.hir().expect_opaque_ty(def_id).origin
}
pub fn finish(self) {
// We assume that no queries are run past here. If there are new queries
// after this point, they'll show up as "<unknown>" in self-profiling data.
self.alloc_self_profile_query_strings();
self.save_dep_graph();
self.query_key_hash_verify_all();
if let Err((path, error)) = self.dep_graph.finish_encoding() {
self.sess.dcx().emit_fatal(crate::error::FailedWritingFile { path: &path, error });
}
}
}
macro_rules! nop_lift {

View file

@ -318,7 +318,7 @@ macro_rules! run_driver {
($args:expr, $callback:expr $(, $with_tcx:ident)?) => {{
use rustc_driver::{Callbacks, Compilation, RunCompiler};
use rustc_middle::ty::TyCtxt;
use rustc_interface::{interface, Queries};
use rustc_interface::interface;
use stable_mir::CompilerError;
use std::ops::ControlFlow;

View file

@ -174,8 +174,9 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
compiling_test_count,
..
} = interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
let collector = queries.global_ctxt().enter(|tcx| {
let krate = rustc_interface::passes::parse(&compiler.sess);
let collector = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
@ -195,7 +196,6 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
compiler.sess.dcx().abort_if_errors();
collector
})
});
run_tests(opts, &rustdoc_options, &unused_extern_reports, standalone_tests, mergeable_tests);

View file

@ -856,27 +856,19 @@ fn main_args(
return;
}
compiler.enter(|queries| {
let mut gcx = queries.global_ctxt();
let krate = rustc_interface::passes::parse(sess);
if sess.dcx().has_errors().is_some() {
sess.dcx().fatal("Compilation failed, aborting rustdoc");
}
gcx.enter(|tcx| {
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
});
info!("finished with rustc");
if let Some(options) = scrape_examples_options {
return scrape_examples::run(
krate,
render_opts,
cache,
tcx,
options,
bin_crate,
);
return scrape_examples::run(krate, render_opts, cache, tcx, options, bin_crate);
}
cache.crate_version = crate_version;
@ -901,5 +893,4 @@ fn main_args(
}
})
})
})
}

View file

@ -77,11 +77,10 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf, linker: Option<&Path
};
interface::run_compiler(config, |compiler| {
let linker = compiler.enter(|queries| {
queries.global_ctxt().enter(|tcx| {
let krate = rustc_interface::passes::parse(&compiler.sess);
let linker = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
let _ = tcx.analysis(());
Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)
})
});
linker.link(&compiler.sess, &*compiler.codegen_backend);
});