2022-08-20 16:33:02 +00:00
|
|
|
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
|
2019-03-26 18:07:13 +00:00
|
|
|
use crate::interface::{Compiler, Result};
|
2023-08-07 06:23:01 +00:00
|
|
|
use crate::{passes, util};
|
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;
|
2023-05-07 23:52:19 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
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;
|
2023-03-14 11:51:00 +00:00
|
|
|
use rustc_data_structures::sync::{AppendOnlyIndexVec, Lrc, OnceCell, RwLock, WorkerLocal};
|
2023-08-07 06:23:01 +00:00
|
|
|
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
|
2023-02-15 17:19:38 +00:00
|
|
|
use rustc_hir::definitions::Definitions;
|
2018-12-08 19:30:23 +00:00
|
|
|
use rustc_incremental::DepGraphFuture;
|
2023-02-15 17:19:38 +00:00
|
|
|
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-16 14:07:42 +00:00
|
|
|
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
|
2023-08-07 06:23:01 +00:00
|
|
|
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
|
2023-02-15 17:19:38 +00:00
|
|
|
use rustc_session::cstore::Untracked;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_session::{output::find_crate_name, Session};
|
|
|
|
use rustc_span::symbol::sym;
|
2022-12-06 12:46:10 +00:00
|
|
|
use rustc_span::Symbol;
|
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-02-07 05:59:50 +00:00
|
|
|
gcx_cell: OnceCell<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-03-14 12:53:04 +00:00
|
|
|
pre_configure: Query<(ast::Crate, ast::AttrVec)>,
|
2022-12-06 12:46:10 +00:00
|
|
|
crate_name: Query<Symbol>,
|
2023-08-07 06:23:01 +00:00
|
|
|
crate_types: Query<Vec<CrateType>>,
|
|
|
|
stable_crate_id: Query<StableCrateId>,
|
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-02-07 05:59:50 +00:00
|
|
|
gcx_cell: 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()),
|
2019-11-24 14:59:22 +00:00
|
|
|
parse: Default::default(),
|
2023-03-14 12:53:04 +00:00
|
|
|
pre_configure: Default::default(),
|
2019-11-24 14:59:22 +00:00
|
|
|
crate_name: Default::default(),
|
2023-08-07 06:23:01 +00:00
|
|
|
crate_types: Default::default(),
|
|
|
|
stable_crate_id: Default::default(),
|
2023-02-07 05:59:50 +00:00
|
|
|
gcx: Default::default(),
|
2019-11-24 14:59:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn session(&self) -> &Lrc<Session> {
|
|
|
|
&self.compiler.sess
|
|
|
|
}
|
2023-06-21 23:18:09 +00:00
|
|
|
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
|
2021-09-30 17:38:50 +00:00
|
|
|
self.compiler.codegen_backend()
|
2019-11-24 14:59:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:02 +00:00
|
|
|
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
|
2022-12-07 09:24:00 +00:00
|
|
|
self.parse
|
|
|
|
.compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 12:53:04 +00:00
|
|
|
pub fn pre_configure(&self) -> Result<QueryResult<'_, (ast::Crate, ast::AttrVec)>> {
|
|
|
|
self.pre_configure.compute(|| {
|
|
|
|
let mut krate = self.parse()?.steal();
|
|
|
|
|
|
|
|
let sess = self.session();
|
|
|
|
rustc_builtin_macros::cmdline_attrs::inject(
|
|
|
|
&mut krate,
|
|
|
|
&sess.parse_sess,
|
|
|
|
&sess.opts.unstable_opts.crate_attr,
|
|
|
|
);
|
|
|
|
|
|
|
|
let pre_configured_attrs =
|
|
|
|
rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
|
|
|
|
Ok((krate, pre_configured_attrs))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
|
|
|
|
self.crate_name.compute(|| {
|
|
|
|
let pre_configure_result = self.pre_configure()?;
|
|
|
|
let (_, pre_configured_attrs) = &*pre_configure_result.borrow();
|
|
|
|
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
|
|
|
|
Ok(find_crate_name(self.session(), pre_configured_attrs))
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
fn crate_types(&self) -> Result<QueryResult<'_, Vec<CrateType>>> {
|
|
|
|
self.crate_types.compute(|| {
|
|
|
|
let pre_configure_result = self.pre_configure()?;
|
|
|
|
let (_, pre_configured_attrs) = &*pre_configure_result.borrow();
|
|
|
|
Ok(util::collect_crate_types(&self.session(), &pre_configured_attrs))
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
fn stable_crate_id(&self) -> Result<QueryResult<'_, StableCrateId>> {
|
|
|
|
self.stable_crate_id.compute(|| {
|
2021-05-24 17:24:58 +00:00
|
|
|
let sess = self.session();
|
2023-08-07 06:23:01 +00:00
|
|
|
Ok(StableCrateId::new(
|
|
|
|
*self.crate_name()?.borrow(),
|
|
|
|
self.crate_types()?.borrow().contains(&CrateType::Executable),
|
|
|
|
sess.opts.cg.metadata.clone(),
|
|
|
|
sess.cfg_version,
|
|
|
|
))
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
fn dep_graph_future(&self) -> Result<Option<DepGraphFuture>> {
|
|
|
|
let sess = self.session();
|
|
|
|
let crate_name = *self.crate_name()?.borrow();
|
|
|
|
let stable_crate_id = *self.stable_crate_id()?.borrow();
|
|
|
|
|
|
|
|
// `load_dep_graph` can only be called after `prepare_session_directory`.
|
|
|
|
rustc_incremental::prepare_session_directory(sess, crate_name, stable_crate_id)?;
|
|
|
|
let res = sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess));
|
|
|
|
|
|
|
|
if sess.opts.incremental.is_some() {
|
|
|
|
sess.time("incr_comp_garbage_collect_session_directories", || {
|
|
|
|
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
|
|
|
|
warn!(
|
|
|
|
"Error while trying to garbage collect incremental \
|
|
|
|
compilation cache directory: {}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dep_graph(&self, dep_graph_future: Option<DepGraphFuture>) -> DepGraph {
|
|
|
|
dep_graph_future
|
|
|
|
.and_then(|future| {
|
|
|
|
let sess = self.session();
|
|
|
|
let (prev_graph, mut prev_work_products) =
|
|
|
|
sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
|
|
|
|
// Convert from UnordMap to FxIndexMap by sorting
|
|
|
|
let prev_work_product_ids =
|
|
|
|
prev_work_products.items().map(|x| *x.0).into_sorted_stable_ord();
|
|
|
|
let prev_work_products = prev_work_product_ids
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| (x, prev_work_products.remove(&x).unwrap()))
|
|
|
|
.collect::<FxIndexMap<_, _>>();
|
|
|
|
rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(DepGraph::new_disabled)
|
|
|
|
}
|
|
|
|
|
2023-02-07 05:59:50 +00:00
|
|
|
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
|
|
|
|
self.gcx.compute(|| {
|
2023-08-07 06:23:01 +00:00
|
|
|
// 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.
|
|
|
|
let dep_graph_future = self.dep_graph_future()?;
|
2022-12-06 15:43:52 +00:00
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
let crate_name = self.crate_name()?.steal();
|
|
|
|
let crate_types = self.crate_types()?.steal();
|
|
|
|
let stable_crate_id = self.stable_crate_id()?.steal();
|
|
|
|
let (krate, pre_configured_attrs) = self.pre_configure()?.steal();
|
2022-12-08 10:53:20 +00:00
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
let sess = self.session();
|
|
|
|
let lint_store = Lrc::new(passes::create_lint_store(
|
|
|
|
sess,
|
|
|
|
&*self.codegen_backend().metadata_loader(),
|
|
|
|
self.compiler.register_lints.as_deref(),
|
|
|
|
&pre_configured_attrs,
|
|
|
|
));
|
|
|
|
let cstore = RwLock::new(Box::new(CStore::new(stable_crate_id)) as _);
|
|
|
|
let definitions = RwLock::new(Definitions::new(stable_crate_id));
|
2023-03-14 11:51:00 +00:00
|
|
|
let source_span = AppendOnlyIndexVec::new();
|
2023-02-15 17:19:38 +00:00
|
|
|
let _id = source_span.push(krate.spans.inner_span);
|
|
|
|
debug_assert_eq!(_id, CRATE_DEF_ID);
|
|
|
|
let untracked = Untracked { cstore, source_span, definitions };
|
|
|
|
|
2023-08-07 06:23:01 +00:00
|
|
|
// FIXME: Move these fields from session to tcx and make them immutable.
|
|
|
|
sess.init_crate_types(crate_types);
|
|
|
|
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
|
|
|
|
sess.init_features(rustc_expand::config::features(sess, &pre_configured_attrs));
|
|
|
|
|
2023-02-15 17:19:38 +00:00
|
|
|
let qcx = passes::create_global_ctxt(
|
2019-11-24 14:59:22 +00:00
|
|
|
self.compiler,
|
2023-02-16 11:36:44 +00:00
|
|
|
lint_store,
|
2023-08-07 06:23:01 +00:00
|
|
|
self.dep_graph(dep_graph_future),
|
2022-12-06 15:43:52 +00:00
|
|
|
untracked,
|
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,
|
2022-12-06 15:43:52 +00:00
|
|
|
);
|
|
|
|
|
2023-02-15 17:19:38 +00:00
|
|
|
qcx.enter(|tcx| {
|
|
|
|
let feed = tcx.feed_local_crate();
|
|
|
|
feed.crate_name(crate_name);
|
|
|
|
|
2022-12-06 15:43:52 +00:00
|
|
|
let feed = tcx.feed_unit_query();
|
2023-03-14 12:53:04 +00:00
|
|
|
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
|
2023-02-16 14:07:42 +00:00
|
|
|
feed.metadata_loader(
|
|
|
|
tcx.arena.alloc(Steal::new(self.codegen_backend().metadata_loader())),
|
2022-12-06 15:43:52 +00:00
|
|
|
);
|
|
|
|
feed.features_query(tcx.sess.features_untracked());
|
2023-02-16 14:03:31 +00:00
|
|
|
});
|
2023-02-15 17:19:38 +00:00
|
|
|
Ok(qcx)
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-21 01:26:49 +00:00
|
|
|
pub fn ongoing_codegen(&'tcx self) -> Result<Box<dyn Any>> {
|
|
|
|
self.global_ctxt()?.enter(|tcx| {
|
|
|
|
// Don't do code generation if there were any errors
|
|
|
|
self.session().compile_status()?;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-06-21 01:26:49 +00:00
|
|
|
// If we have any delayed bugs, for example because we created TyKind::Error earlier,
|
|
|
|
// it's likely that codegen will only cause more ICEs, obscuring the original problem
|
|
|
|
self.session().diagnostic().flush_delayed();
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2023-06-21 01:26:49 +00:00
|
|
|
// Hook for UI tests.
|
|
|
|
Self::check_for_rustc_errors_attr(tcx);
|
2022-09-27 18:56:05 +00:00
|
|
|
|
2023-06-21 23:18:09 +00:00
|
|
|
Ok(passes::start_codegen(&**self.codegen_backend(), tcx))
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:07:58 +00:00
|
|
|
/// 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
|
2020-03-12 23:07:58 +00:00
|
|
|
/// 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) {
|
2020-03-12 23:07:58 +00:00
|
|
|
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 => {
|
2022-08-20 16:33:02 +00:00
|
|
|
tcx.sess.emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
|
2020-03-12 23:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some other attribute.
|
|
|
|
Some(_) => {
|
2022-08-20 16:33:02 +00:00
|
|
|
tcx.sess.emit_warning(RustcErrorUnexpectedAnnotation {
|
|
|
|
span: tcx.def_span(def_id),
|
|
|
|
});
|
2020-03-12 23:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 01:26:49 +00:00
|
|
|
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
|
2019-11-24 15:32:57 +00:00
|
|
|
let sess = self.session().clone();
|
|
|
|
let codegen_backend = self.codegen_backend().clone();
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-12-12 10:48:02 +00:00
|
|
|
let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
|
2023-03-03 06:02:11 +00:00
|
|
|
(
|
|
|
|
if tcx.sess.needs_crate_hash() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { None },
|
|
|
|
tcx.output_filenames(()).clone(),
|
|
|
|
tcx.dep_graph.clone(),
|
|
|
|
)
|
2022-12-12 10:48:02 +00:00
|
|
|
});
|
2021-05-29 12:54:43 +00:00
|
|
|
|
2019-11-24 15:32:57 +00:00
|
|
|
Ok(Linker {
|
|
|
|
sess,
|
|
|
|
codegen_backend,
|
2021-05-29 12:54:43 +00:00
|
|
|
|
|
|
|
dep_graph,
|
|
|
|
prepare_outputs,
|
|
|
|
crate_hash,
|
|
|
|
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 {
|
2021-05-29 12:54:43 +00:00
|
|
|
// compilation inputs
|
2019-11-24 15:32:57 +00:00
|
|
|
sess: Lrc<Session>,
|
2023-06-21 23:18:09 +00:00
|
|
|
codegen_backend: Lrc<dyn CodegenBackend>,
|
2021-05-29 12:54:43 +00:00
|
|
|
|
|
|
|
// compilation outputs
|
2019-11-24 15:32:57 +00:00
|
|
|
dep_graph: DepGraph,
|
2022-12-03 12:28:01 +00:00
|
|
|
prepare_outputs: 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 {
|
|
|
|
pub fn link(self) -> Result<()> {
|
2021-12-13 00:00:00 +00:00
|
|
|
let (codegen_results, work_products) = self.codegen_backend.join_codegen(
|
|
|
|
self.ongoing_codegen,
|
|
|
|
&self.sess,
|
|
|
|
&self.prepare_outputs,
|
|
|
|
)?;
|
2020-10-10 13:14:58 +00:00
|
|
|
|
|
|
|
self.sess.compile_status()?;
|
|
|
|
|
|
|
|
let sess = &self.sess;
|
2020-01-09 02:48:00 +00:00
|
|
|
let dep_graph = self.dep_graph;
|
2020-10-10 13:14:58 +00:00
|
|
|
sess.time("serialize_work_products", || {
|
2021-09-30 17:38:50 +00:00
|
|
|
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
|
2020-10-10 13:14:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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));
|
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)
|
|
|
|
rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
|
|
|
|
|
2020-01-28 13:16:14 +00:00
|
|
|
if !self
|
|
|
|
.sess
|
|
|
|
.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 {
|
2020-10-10 14:18:36 +00:00
|
|
|
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
|
2023-05-08 09:12:38 +00:00
|
|
|
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
|
2022-08-22 13:58:26 +00:00
|
|
|
.map_err(|error| sess.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");
|
2020-01-28 13:16:14 +00:00
|
|
|
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
|
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
|
|
|
{
|
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 =
|
|
|
|
queries.session().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
|
|
|
}
|
|
|
|
|
2021-03-02 21:38:49 +00:00
|
|
|
self.session()
|
|
|
|
.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
|
2019-11-26 21:51:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:48:00 +00:00
|
|
|
_timer = Some(self.session().timer("free_global_ctxt"));
|
|
|
|
|
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
|
|
|
}
|