rust/compiler/rustc_interface/src/queries.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

416 lines
15 KiB
Rust
Raw Normal View History

use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
2019-03-26 18:07:13 +00:00
use crate::interface::{Compiler, Result};
2022-12-13 09:46:37 +00:00
use crate::passes;
2019-03-26 18:07:13 +00:00
2020-04-27 17:56:11 +00:00
use rustc_ast as ast;
use rustc_codegen_ssa::traits::CodegenBackend;
2022-04-02 15:26:39 +00:00
use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{Lrc, OnceCell, RwLock, WorkerLocal};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_incremental::DepGraphFuture;
use rustc_index::vec::IndexVec;
use rustc_lint::LintStore;
use rustc_metadata::creader::CStore;
2020-03-29 15:19:48 +00:00
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
2023-02-14 09:38:49 +00:00
use rustc_middle::ty::{self, GlobalCtxt, TyCtxt};
use rustc_query_impl::Queries as TcxQueries;
2022-12-13 09:46:37 +00:00
use rustc_resolve::Resolver;
use rustc_session::config::{self, OutputFilenames, OutputType};
use rustc_session::cstore::Untracked;
use rustc_session::{output::find_crate_name, Session};
use rustc_span::symbol::sym;
use rustc_span::Symbol;
use std::any::Any;
use std::cell::{RefCell, RefMut};
use std::sync::Arc;
/// 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<Result<Steal<T>>>>,
}
impl<T> Query<T> {
fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<QueryResult<'_, T>> {
RefMut::filter_map(
self.result.borrow_mut(),
|r: &mut Option<Result<Steal<T>>>| -> Option<&mut Steal<T>> {
r.get_or_insert_with(|| f().map(Steal::new)).as_mut().ok()
},
)
.map_err(|r| *r.as_ref().unwrap().as_ref().map(|_| ()).unwrap_err())
.map(QueryResult)
}
}
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).get_mut().enter(f)
}
}
impl<T> Default for Query<T> {
fn default() -> Self {
Query { result: RefCell::new(None) }
}
}
2019-11-27 12:13:57 +00:00
pub struct Queries<'tcx> {
compiler: &'tcx Compiler,
gcx_cell: OnceCell<GlobalCtxt<'tcx>>,
queries: OnceCell<TcxQueries<'tcx>>,
2019-11-27 12:24:19 +00:00
arena: WorkerLocal<Arena<'tcx>>,
2021-07-13 16:45:20 +00:00
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
2019-11-26 22:16:48 +00:00
dep_graph_future: Query<Option<DepGraphFuture>>,
parse: Query<ast::Crate>,
crate_name: Query<Symbol>,
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
dep_graph: Query<DepGraph>,
// This just points to what's in `gcx_cell`.
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
ongoing_codegen: Query<Box<dyn Any>>,
}
2019-11-27 12:13:57 +00:00
impl<'tcx> Queries<'tcx> {
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
Queries {
compiler,
gcx_cell: OnceCell::new(),
2020-10-11 08:34:13 +00:00
queries: OnceCell::new(),
2019-11-27 12:24:19 +00:00
arena: WorkerLocal::new(|_| Arena::default()),
2021-07-13 16:45:20 +00:00
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
dep_graph_future: Default::default(),
parse: Default::default(),
crate_name: Default::default(),
register_plugins: Default::default(),
dep_graph: Default::default(),
gcx: Default::default(),
ongoing_codegen: Default::default(),
}
}
fn session(&self) -> &Lrc<Session> {
&self.compiler.sess
}
fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
2021-09-30 17:38:50 +00:00
self.compiler.codegen_backend()
}
fn dep_graph_future(&self) -> Result<QueryResult<'_, Option<DepGraphFuture>>> {
self.dep_graph_future.compute(|| {
2021-05-24 17:24:58 +00:00
let sess = self.session();
Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess)))
})
}
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
self.parse
.compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
}
pub fn register_plugins(&self) -> Result<QueryResult<'_, (ast::Crate, Lrc<LintStore>)>> {
self.register_plugins.compute(|| {
let crate_name = *self.crate_name()?.borrow();
let krate = self.parse()?.steal();
let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
let (krate, lint_store) = passes::register_plugins(
self.session(),
&*self.codegen_backend().metadata_loader(),
self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
krate,
crate_name,
)?;
// Compute the dependency graph (in the background). We want to do
// this as early as possible, to give the DepGraph maximum time to
// load before dep_graph() is called, but it also can't happen
// until after rustc_incremental::prepare_session_directory() is
// called, which happens within passes::register_plugins().
self.dep_graph_future().ok();
Ok((krate, Lrc::new(lint_store)))
})
}
fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
self.crate_name.compute(|| {
Ok({
let parse_result = self.parse()?;
let krate = parse_result.borrow();
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
find_crate_name(self.session(), &krate.attrs)
})
})
}
fn dep_graph(&self) -> Result<QueryResult<'_, DepGraph>> {
self.dep_graph.compute(|| {
2021-05-24 17:24:58 +00:00
let sess = self.session();
let future_opt = self.dep_graph_future()?.steal();
2021-05-24 17:24:58 +00:00
let dep_graph = future_opt
.and_then(|future| {
let (prev_graph, prev_work_products) =
2021-05-24 17:24:58 +00:00
sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
})
.unwrap_or_else(DepGraph::new_disabled);
Ok(dep_graph)
})
}
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
self.gcx.compute(|| {
let crate_name = *self.crate_name()?.borrow();
2023-02-14 09:50:13 +00:00
let (krate, lint_store) = self.register_plugins()?.steal();
let sess = self.session();
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
let mut source_span = IndexVec::default();
let _id = source_span.push(krate.spans.inner_span);
debug_assert_eq!(_id, CRATE_DEF_ID);
let source_span = RwLock::new(source_span);
let untracked = Untracked { cstore, source_span, definitions };
let qcx = passes::create_global_ctxt(
self.compiler,
lint_store,
2022-12-06 15:07:00 +00:00
self.dep_graph()?.steal(),
untracked,
2020-10-11 08:34:13 +00:00
&self.queries,
&self.gcx_cell,
2019-11-27 12:24:19 +00:00
&self.arena,
2021-05-23 19:42:16 +00:00
&self.hir_arena,
);
qcx.enter(|tcx| {
let feed = tcx.feed_local_crate();
feed.crate_name(crate_name);
let (krate, resolver_outputs) = {
let _timer = sess.timer("configure_and_expand");
let arenas = Resolver::arenas();
let mut resolver = Resolver::new(
2022-12-09 22:53:31 +00:00
tcx,
&krate,
crate_name,
self.codegen_backend().metadata_loader(),
&arenas,
);
let krate = passes::configure_and_expand(tcx, krate, &mut resolver);
// Make sure we don't mutate the cstore from here on.
tcx.untracked().cstore.leak();
(Lrc::new(krate), resolver.into_outputs())
};
let ty::ResolverOutputs {
global_ctxt: untracked_resolutions,
ast_lowering: untracked_resolver_for_lowering,
} = resolver_outputs;
let feed = tcx.feed_unit_query();
feed.resolver_for_lowering(
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
);
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
feed.features_query(tcx.sess.features_untracked());
});
Ok(qcx)
})
}
pub fn ongoing_codegen(&'tcx self) -> Result<QueryResult<'_, Box<dyn Any>>> {
self.ongoing_codegen.compute(|| {
self.global_ctxt()?.enter(|tcx| {
2021-05-11 12:50:54 +00:00
tcx.analysis(()).ok();
// Don't do code generation if there were any errors
self.session().compile_status()?;
// If we have any delayed bugs, for example because we created TyKind::Error earlier,
2022-09-30 19:02:53 +00:00
// it's likely that codegen will only cause more ICEs, obscuring the original problem
self.session().diagnostic().flush_delayed();
2020-12-28 17:15:16 +00:00
// Hook for UI tests.
Self::check_for_rustc_errors_attr(tcx);
Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
})
})
}
/// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
2020-12-28 17:15:16 +00:00
/// to write UI tests that actually test that compilation succeeds without reporting
/// an error.
fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
2022-02-18 23:48:49 +00:00
let Some((def_id, _)) = tcx.entry_fn(()) else { return };
2022-05-02 07:31:56 +00:00
for attr in tcx.get_attrs(def_id, sym::rustc_error) {
match attr.meta_item_list() {
// Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
Some(list)
if list.iter().any(|list_item| {
matches!(
list_item.ident().map(|i| i.name),
Some(sym::delay_span_bug_from_inside_query)
)
}) =>
{
tcx.ensure().trigger_delay_span_bug(def_id);
}
// Bare `#[rustc_error]`.
None => {
tcx.sess.emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
}
// Some other attribute.
Some(_) => {
tcx.sess.emit_warning(RustcErrorUnexpectedAnnotation {
span: tcx.def_span(def_id),
});
}
}
}
}
2019-11-27 12:13:57 +00:00
pub fn linker(&'tcx self) -> Result<Linker> {
let sess = self.session().clone();
let codegen_backend = self.codegen_backend().clone();
let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
(tcx.crate_hash(LOCAL_CRATE), tcx.output_filenames(()).clone(), tcx.dep_graph.clone())
});
let ongoing_codegen = self.ongoing_codegen()?.steal();
2021-05-29 12:54:43 +00:00
Ok(Linker {
sess,
codegen_backend,
2021-05-29 12:54:43 +00:00
dep_graph,
prepare_outputs,
crate_hash,
ongoing_codegen,
})
}
}
pub struct Linker {
2021-05-29 12:54:43 +00:00
// compilation inputs
sess: Lrc<Session>,
2021-05-29 12:54:43 +00:00
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
// compilation outputs
dep_graph: DepGraph,
prepare_outputs: Arc<OutputFilenames>,
crate_hash: Svh,
ongoing_codegen: Box<dyn Any>,
}
impl Linker {
pub fn link(self) -> Result<()> {
let (codegen_results, work_products) = self.codegen_backend.join_codegen(
self.ongoing_codegen,
&self.sess,
&self.prepare_outputs,
)?;
self.sess.compile_status()?;
let sess = &self.sess;
2020-01-09 02:48:00 +00:00
let dep_graph = self.dep_graph;
sess.time("serialize_work_products", || {
2021-09-30 17:38:50 +00:00
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
});
let prof = self.sess.prof.clone();
2020-01-09 02:48:00 +00:00
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
if !self
.sess
.opts
.output_types
.keys()
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
{
return Ok(());
}
if sess.opts.unstable_opts.no_link {
let encoded = CodegenResults::serialize_rlink(&codegen_results);
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
2022-08-22 13:58:26 +00:00
std::fs::write(&rlink_file, encoded)
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
return Ok(());
}
let _timer = sess.prof.verbose_generic_activity("link_crate");
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
}
}
impl Compiler {
2019-11-25 17:36:18 +00:00
pub fn enter<F, T>(&self, f: F) -> T
where
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
{
2020-01-09 02:48:00 +00:00
let mut _timer = None;
2021-09-30 17:38:50 +00:00
let queries = Queries::new(self);
let ret = f(&queries);
// NOTE: intentionally does not compute the global context if it hasn't been built yet,
// since that likely means there was a parse error.
if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
let gcx = gcx.get_mut();
// 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.
{
let _prof_timer =
queries.session().prof.generic_activity("self_profile_alloc_query_strings");
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
}
2021-03-02 21:38:49 +00:00
self.session()
.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
}
2020-01-09 02:48:00 +00:00
_timer = Some(self.session().timer("free_global_ctxt"));
ret
}
}