Move CodegenBackend out of Linker.

It can easily be passed in. And that removes the single clone of
`Compiler::codegen_backend`, which means it no longer needs to be `Lrc`.
This commit is contained in:
Nicholas Nethercote 2023-11-17 08:34:55 +11:00
parent de91b6d249
commit aed8e1f3b6
4 changed files with 17 additions and 26 deletions

View File

@ -361,7 +361,7 @@ fn run_compiler(
} }
let should_stop = print_crate_info( let should_stop = print_crate_info(
&handler, &handler,
&**compiler.codegen_backend(), compiler.codegen_backend(),
compiler.session(), compiler.session(),
false, false,
); );
@ -385,12 +385,11 @@ fn run_compiler(
interface::run_compiler(config, |compiler| { interface::run_compiler(config, |compiler| {
let sess = compiler.session(); let sess = compiler.session();
let codegen_backend = compiler.codegen_backend();
let handler = EarlyErrorHandler::new(sess.opts.error_format); let handler = EarlyErrorHandler::new(sess.opts.error_format);
let should_stop = print_crate_info(&handler, &**compiler.codegen_backend(), sess, true) let should_stop = print_crate_info(&handler, codegen_backend, sess, true)
.and_then(|| { .and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader()))
list_metadata(&handler, sess, &*compiler.codegen_backend().metadata_loader())
})
.and_then(|| try_process_rlink(sess, compiler)); .and_then(|| try_process_rlink(sess, compiler));
if should_stop == Compilation::Stop { if should_stop == Compilation::Stop {
@ -482,7 +481,7 @@ fn run_compiler(
if let Some(linker) = linker { if let Some(linker) = linker {
let _timer = sess.timer("link"); let _timer = sess.timer("link");
linker.link(sess)? linker.link(sess, codegen_backend)?
} }
if sess.opts.unstable_opts.print_fuel.is_some() { if sess.opts.unstable_opts.print_fuel.is_some() {

View File

@ -39,7 +39,7 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
/// Created by passing [`Config`] to [`run_compiler`]. /// Created by passing [`Config`] to [`run_compiler`].
pub struct Compiler { pub struct Compiler {
pub(crate) sess: Session, pub(crate) sess: Session,
codegen_backend: Lrc<dyn CodegenBackend>, codegen_backend: Box<dyn CodegenBackend>,
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>, pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
} }
@ -47,8 +47,8 @@ impl Compiler {
pub fn session(&self) -> &Session { pub fn session(&self) -> &Session {
&self.sess &self.sess
} }
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> { pub fn codegen_backend(&self) -> &dyn CodegenBackend {
&self.codegen_backend &*self.codegen_backend
} }
pub fn build_output_filenames( pub fn build_output_filenames(
&self, &self,
@ -491,11 +491,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
} }
sess.lint_store = Some(Lrc::new(lint_store)); sess.lint_store = Some(Lrc::new(lint_store));
let compiler = Compiler { let compiler =
sess, Compiler { sess, codegen_backend, override_queries: config.override_queries };
codegen_backend: Lrc::from(codegen_backend),
override_queries: config.override_queries,
};
rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || { rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
let r = { let r = {

View File

@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::CodegenResults; use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, Lrc, OnceLock, WorkerLocal}; use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::Definitions; use rustc_hir::definitions::Definitions;
use rustc_incremental::setup_dep_graph; use rustc_incremental::setup_dep_graph;
@ -105,7 +105,7 @@ impl<'tcx> Queries<'tcx> {
&self.compiler.sess &self.compiler.sess
} }
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> { fn codegen_backend(&self) -> &dyn CodegenBackend {
self.compiler.codegen_backend() self.compiler.codegen_backend()
} }
@ -198,7 +198,7 @@ impl<'tcx> Queries<'tcx> {
// Hook for UI tests. // Hook for UI tests.
Self::check_for_rustc_errors_attr(tcx); Self::check_for_rustc_errors_attr(tcx);
Ok(passes::start_codegen(&**self.codegen_backend(), tcx)) Ok(passes::start_codegen(self.codegen_backend(), tcx))
}) })
} }
@ -239,7 +239,6 @@ impl<'tcx> Queries<'tcx> {
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> { pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
self.global_ctxt()?.enter(|tcx| { self.global_ctxt()?.enter(|tcx| {
Ok(Linker { Ok(Linker {
codegen_backend: self.codegen_backend().clone(),
dep_graph: tcx.dep_graph.clone(), dep_graph: tcx.dep_graph.clone(),
prepare_outputs: tcx.output_filenames(()).clone(), prepare_outputs: tcx.output_filenames(()).clone(),
crate_hash: if tcx.needs_crate_hash() { crate_hash: if tcx.needs_crate_hash() {
@ -254,10 +253,6 @@ impl<'tcx> Queries<'tcx> {
} }
pub struct Linker { pub struct Linker {
// compilation inputs
codegen_backend: Lrc<dyn CodegenBackend>,
// compilation outputs
dep_graph: DepGraph, dep_graph: DepGraph,
prepare_outputs: Arc<OutputFilenames>, prepare_outputs: Arc<OutputFilenames>,
// Only present when incr. comp. is enabled. // Only present when incr. comp. is enabled.
@ -266,9 +261,9 @@ pub struct Linker {
} }
impl Linker { impl Linker {
pub fn link(self, sess: &Session) -> Result<()> { pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
let (codegen_results, work_products) = let (codegen_results, work_products) =
self.codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?; codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
sess.compile_status()?; sess.compile_status()?;
@ -301,7 +296,7 @@ impl Linker {
} }
let _timer = sess.prof.verbose_generic_activity("link_crate"); let _timer = sess.prof.verbose_generic_activity("link_crate");
self.codegen_backend.link(sess, codegen_results, &self.prepare_outputs) codegen_backend.link(sess, codegen_results, &self.prepare_outputs)
} }
} }

View File

@ -72,6 +72,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
let ongoing_codegen = queries.ongoing_codegen()?; let ongoing_codegen = queries.ongoing_codegen()?;
queries.linker(ongoing_codegen) queries.linker(ongoing_codegen)
}); });
linker.unwrap().link(compiler.session()).unwrap(); linker.unwrap().link(compiler.session(), compiler.codegen_backend()).unwrap();
}); });
} }