2024-06-21 20:16:42 +00:00
|
|
|
use crate::errors::FailedWritingFile;
|
2019-03-26 18:07:13 +00:00
|
|
|
use crate::interface::{Compiler, Result};
|
2024-06-21 20:10:26 +00:00
|
|
|
use crate::{errors, passes};
|
2019-03-26 18:07:13 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2022-04-02 15:26:39 +00:00
|
|
|
use rustc_codegen_ssa::CodegenResults;
|
2022-12-12 10:48:02 +00:00
|
|
|
use rustc_data_structures::steal::Steal;
|
2020-10-10 13:20:35 +00:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2024-06-21 20:10:26 +00:00
|
|
|
use rustc_data_structures::sync::{OnceLock, WorkerLocal};
|
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::arena::Arena;
|
|
|
|
use rustc_middle::dep_graph::DepGraph;
|
2023-02-16 14:07:42 +00:00
|
|
|
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
|
2023-10-28 01:26:43 +00:00
|
|
|
use rustc_serialize::opaque::FileEncodeResult;
|
2024-06-21 20:10:26 +00:00
|
|
|
use rustc_session::config::{self, OutputFilenames, OutputType};
|
2023-11-20 02:26:09 +00:00
|
|
|
use rustc_session::Session;
|
2018-12-08 19:30:23 +00:00
|
|
|
use std::any::Any;
|
2022-12-12 10:48:02 +00:00
|
|
|
use std::cell::{RefCell, RefMut};
|
2022-12-03 12:28:01 +00:00
|
|
|
use std::sync::Arc;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
/// Represent the result of a query.
|
2020-12-22 14:54:23 +00:00
|
|
|
///
|
2022-12-12 10:48:02 +00:00
|
|
|
/// This result can be stolen once with the [`steal`] method and generated with the [`compute`] method.
|
2020-12-22 14:54:23 +00:00
|
|
|
///
|
2022-12-12 10:48:02 +00:00
|
|
|
/// [`steal`]: Steal::steal
|
2020-12-22 14:54:23 +00:00
|
|
|
/// [`compute`]: Self::compute
|
2018-12-08 19:30:23 +00:00
|
|
|
pub struct Query<T> {
|
2022-12-12 10:48:02 +00:00
|
|
|
/// `None` means no value has been computed yet.
|
|
|
|
result: RefCell<Option<Result<Steal<T>>>>,
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Query<T> {
|
2022-12-12 10:48:02 +00:00
|
|
|
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)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
2022-12-12 10:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>>;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-12-12 10:48:02 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
2022-12-12 10:48:02 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-12-12 10:48:02 +00:00
|
|
|
impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
2022-12-12 10:48:02 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-02-07 05:59:50 +00:00
|
|
|
impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
|
2023-01-19 14:12:29 +00:00
|
|
|
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
|
2022-12-12 10:48:02 +00:00
|
|
|
(*self.0).get_mut().enter(f)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-08-31 23:14:33 +00:00
|
|
|
gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
|
2019-11-24 14:59:22 +00:00
|
|
|
|
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
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
parse: Query<ast::Crate>,
|
2023-02-07 05:59:50 +00:00
|
|
|
// This just points to what's in `gcx_cell`.
|
|
|
|
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:13:57 +00:00
|
|
|
impl<'tcx> Queries<'tcx> {
|
|
|
|
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
|
2019-11-24 14:59:22 +00:00
|
|
|
Queries {
|
|
|
|
compiler,
|
2023-08-31 23:14:33 +00:00
|
|
|
gcx_cell: OnceLock::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()),
|
2019-11-24 14:59:22 +00:00
|
|
|
parse: Default::default(),
|
2023-02-07 05:59:50 +00:00
|
|
|
gcx: Default::default(),
|
2019-11-24 14:59:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 01:26:43 +00:00
|
|
|
pub fn finish(&self) -> FileEncodeResult {
|
|
|
|
if let Some(gcx) = self.gcx_cell.get() { gcx.finish() } else { Ok(0) }
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:02 +00:00
|
|
|
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
|
2024-06-21 20:10:26 +00:00
|
|
|
self.parse.compute(|| passes::parse(&self.compiler.sess))
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:20:09 +00:00
|
|
|
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
|
|
|
|
self.gcx.compute(|| {
|
2024-06-21 20:10:26 +00:00
|
|
|
let krate = self.parse()?.steal();
|
|
|
|
|
|
|
|
passes::create_global_ctxt(
|
2019-11-24 14:59:22 +00:00
|
|
|
self.compiler,
|
2024-06-21 20:10:26 +00:00
|
|
|
krate,
|
2023-02-07 05:59:50 +00:00
|
|
|
&self.gcx_cell,
|
2019-11-27 12:24:19 +00:00
|
|
|
&self.arena,
|
2021-05-23 19:42:16 +00:00
|
|
|
&self.hir_arena,
|
2024-06-21 20:10:26 +00:00
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-04 17:35:28 +00:00
|
|
|
pub fn write_dep_info(&'tcx self) -> Result<()> {
|
|
|
|
self.global_ctxt()?.enter(|tcx| {
|
|
|
|
passes::write_dep_info(tcx);
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-20 04:09:05 +00:00
|
|
|
pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
|
2023-11-16 21:05:56 +00:00
|
|
|
self.global_ctxt()?.enter(|tcx| {
|
Overhaul the handling of errors at the top-level.
Currently `emit_stashed_diagnostic` is called from four(!) different
places: `print_error_count`, `DiagCtxtInner::drop`, `abort_if_errors`,
and `compile_status`.
And `flush_delayed` is called from two different places:
`DiagCtxtInner::drop` and `Queries`.
This is pretty gross! Each one should really be called from a single
place, but there's a bunch of entanglements. This commit cleans up this
mess.
Specifically, it:
- Removes all the existing calls to `emit_stashed_diagnostic`, and adds
a single new call in `finish_diagnostics`.
- Removes the early `flush_delayed` call in `codegen_and_build_linker`,
replacing it with a simple early return if delayed bugs are present.
- Changes `DiagCtxtInner::drop` and `DiagCtxtInner::flush_delayed` so
they both assert that the stashed diagnostics are empty (i.e.
processed beforehand).
- Changes `interface::run_compiler` so that any errors emitted during
`finish_diagnostics` (i.e. late-emitted stashed diagnostics) are
counted and cannot be overlooked. This requires adding
`ErrorGuaranteed` return values to several functions.
- Removes the `stashed_err_count` call in `analysis`. This is possible
now that we don't have to worry about calling `flush_delayed` early
from `codegen_and_build_linker` when stashed diagnostics are pending.
- Changes the `span_bug` case in `handle_tuple_field_pattern_match` to a
`delayed_span_bug`, because it now can be reached due to the removal
of the `stashed_err_count` call in `analysis`.
- Slightly changes the expected output of three tests. If no errors are
emitted but there are delayed bugs, the error count is no longer
printed. This is because delayed bugs are now always printed after the
error count is printed (or not printed, if the error count is zero).
There is a lot going on in this commit. It's hard to break into smaller
pieces because the existing code is very tangled. It took me a long time
and a lot of effort to understand how the different pieces interact, and
I think the new code is a lot simpler and easier to understand.
2024-02-18 23:00:19 +00:00
|
|
|
// Don't do code generation if there were any errors. Likewise if
|
|
|
|
// there were any delayed bugs, because codegen will likely cause
|
|
|
|
// more ICEs, obscuring the original problem.
|
|
|
|
if let Some(guar) = self.compiler.sess.dcx().has_errors_or_delayed_bugs() {
|
|
|
|
return Err(guar);
|
|
|
|
}
|
2023-11-20 04:09:05 +00:00
|
|
|
|
|
|
|
let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx);
|
|
|
|
|
2023-11-16 21:05:56 +00:00
|
|
|
Ok(Linker {
|
|
|
|
dep_graph: tcx.dep_graph.clone(),
|
2023-11-16 22:04:45 +00:00
|
|
|
output_filenames: tcx.output_filenames(()).clone(),
|
2023-11-16 21:05:56 +00:00
|
|
|
crate_hash: if tcx.needs_crate_hash() {
|
|
|
|
Some(tcx.crate_hash(LOCAL_CRATE))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
ongoing_codegen,
|
|
|
|
})
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
2019-11-24 14:59:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 15:32:57 +00:00
|
|
|
pub struct Linker {
|
|
|
|
dep_graph: DepGraph,
|
2023-11-16 22:04:45 +00:00
|
|
|
output_filenames: Arc<OutputFilenames>,
|
2023-03-03 06:02:11 +00:00
|
|
|
// Only present when incr. comp. is enabled.
|
|
|
|
crate_hash: Option<Svh>,
|
2019-11-24 15:32:57 +00:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Linker {
|
2023-11-16 21:34:55 +00:00
|
|
|
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
|
2023-11-16 21:15:36 +00:00
|
|
|
let (codegen_results, work_products) =
|
2024-02-16 23:51:35 +00:00
|
|
|
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames);
|
2020-10-10 13:14:58 +00:00
|
|
|
|
2024-02-18 23:13:51 +00:00
|
|
|
if let Some(guar) = sess.dcx().has_errors() {
|
|
|
|
return Err(guar);
|
|
|
|
}
|
2020-10-10 13:14:58 +00:00
|
|
|
|
|
|
|
sess.time("serialize_work_products", || {
|
2023-11-16 22:19:11 +00:00
|
|
|
rustc_incremental::save_work_product_index(sess, &self.dep_graph, work_products)
|
2020-10-10 13:14:58 +00:00
|
|
|
});
|
|
|
|
|
2023-11-16 21:15:36 +00:00
|
|
|
let prof = sess.prof.clone();
|
2023-11-16 22:19:11 +00:00
|
|
|
prof.generic_activity("drop_dep_graph").run(move || drop(self.dep_graph));
|
2020-01-28 13:16:14 +00:00
|
|
|
|
2020-10-10 13:20:35 +00:00
|
|
|
// Now that we won't touch anything in the incremental compilation directory
|
|
|
|
// any more, we can finalize it (which involves renaming it)
|
2023-11-16 21:15:36 +00:00
|
|
|
rustc_incremental::finalize_session_directory(sess, self.crate_hash);
|
2020-10-10 13:20:35 +00:00
|
|
|
|
2023-11-16 21:15:36 +00:00
|
|
|
if !sess
|
2020-01-28 13:16:14 +00:00
|
|
|
.opts
|
|
|
|
.output_types
|
|
|
|
.keys()
|
|
|
|
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
|
|
|
|
{
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-10-10 14:18:36 +00:00
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_opts.no_link {
|
2023-11-16 22:04:45 +00:00
|
|
|
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
|
2023-11-04 15:49:57 +00:00
|
|
|
CodegenResults::serialize_rlink(
|
|
|
|
sess,
|
|
|
|
&rlink_file,
|
|
|
|
&codegen_results,
|
|
|
|
&*self.output_filenames,
|
|
|
|
)
|
2023-12-18 11:21:37 +00:00
|
|
|
.map_err(|error| {
|
|
|
|
sess.dcx().emit_fatal(FailedWritingFile { path: &rlink_file, error })
|
|
|
|
})?;
|
2020-10-10 14:18:36 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:11:35 +00:00
|
|
|
let _timer = sess.prof.verbose_generic_activity("link_crate");
|
2023-11-16 22:04:45 +00:00
|
|
|
codegen_backend.link(sess, codegen_results, &self.output_filenames)
|
2019-11-24 15:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:59:22 +00:00
|
|
|
impl Compiler {
|
2019-11-25 17:36:18 +00:00
|
|
|
pub fn enter<F, T>(&self, f: F) -> T
|
2019-11-26 21:51:02 +00:00
|
|
|
where
|
|
|
|
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
|
2019-11-24 14:59:22 +00:00
|
|
|
{
|
2023-11-20 02:12:07 +00:00
|
|
|
// Must declare `_timer` first so that it is dropped after `queries`.
|
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);
|
2019-11-26 21:51:02 +00:00
|
|
|
let ret = f(&queries);
|
|
|
|
|
2021-01-22 18:46:52 +00:00
|
|
|
// NOTE: intentionally does not compute the global context if it hasn't been built yet,
|
|
|
|
// since that likely means there was a parse error.
|
2023-02-07 05:59:50 +00:00
|
|
|
if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
|
2022-12-12 10:48:02 +00:00
|
|
|
let gcx = gcx.get_mut();
|
2021-01-22 18:46:52 +00:00
|
|
|
// 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 =
|
2023-11-20 02:26:09 +00:00
|
|
|
queries.compiler.sess.prof.generic_activity("self_profile_alloc_query_strings");
|
2021-01-19 19:40:16 +00:00
|
|
|
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
|
2021-01-22 18:46:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 02:26:09 +00:00
|
|
|
self.sess.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
|
2024-03-09 08:29:16 +00:00
|
|
|
|
|
|
|
gcx.enter(rustc_query_impl::query_key_hash_verify_all);
|
2019-11-26 21:51:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 02:12:07 +00:00
|
|
|
// The timer's lifetime spans the dropping of `queries`, which contains
|
|
|
|
// the global context.
|
2023-11-20 02:26:09 +00:00
|
|
|
_timer = Some(self.sess.timer("free_global_ctxt"));
|
2023-10-28 01:26:43 +00:00
|
|
|
if let Err((path, error)) = queries.finish() {
|
2024-01-02 03:34:07 +00:00
|
|
|
self.sess.dcx().emit_fatal(errors::FailedWritingFile { path: &path, error });
|
2023-10-28 01:26:43 +00:00
|
|
|
}
|
2020-01-09 02:48:00 +00:00
|
|
|
|
2019-11-26 21:51:02 +00:00
|
|
|
ret
|
2019-11-24 14:59:22 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|