Skip to content

Commit aed8e1f

Browse files
committed
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`.
1 parent de91b6d commit aed8e1f

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ fn run_compiler(
361361
}
362362
let should_stop = print_crate_info(
363363
&handler,
364-
&**compiler.codegen_backend(),
364+
compiler.codegen_backend(),
365365
compiler.session(),
366366
false,
367367
);
@@ -385,12 +385,11 @@ fn run_compiler(
385385

386386
interface::run_compiler(config, |compiler| {
387387
let sess = compiler.session();
388+
let codegen_backend = compiler.codegen_backend();
388389
let handler = EarlyErrorHandler::new(sess.opts.error_format);
389390

390-
let should_stop = print_crate_info(&handler, &**compiler.codegen_backend(), sess, true)
391-
.and_then(|| {
392-
list_metadata(&handler, sess, &*compiler.codegen_backend().metadata_loader())
393-
})
391+
let should_stop = print_crate_info(&handler, codegen_backend, sess, true)
392+
.and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader()))
394393
.and_then(|| try_process_rlink(sess, compiler));
395394

396395
if should_stop == Compilation::Stop {
@@ -482,7 +481,7 @@ fn run_compiler(
482481

483482
if let Some(linker) = linker {
484483
let _timer = sess.timer("link");
485-
linker.link(sess)?
484+
linker.link(sess, codegen_backend)?
486485
}
487486

488487
if sess.opts.unstable_opts.print_fuel.is_some() {

compiler/rustc_interface/src/interface.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
3939
/// Created by passing [`Config`] to [`run_compiler`].
4040
pub struct Compiler {
4141
pub(crate) sess: Session,
42-
codegen_backend: Lrc<dyn CodegenBackend>,
42+
codegen_backend: Box<dyn CodegenBackend>,
4343
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
4444
}
4545

4646
impl Compiler {
4747
pub fn session(&self) -> &Session {
4848
&self.sess
4949
}
50-
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
51-
&self.codegen_backend
50+
pub fn codegen_backend(&self) -> &dyn CodegenBackend {
51+
&*self.codegen_backend
5252
}
5353
pub fn build_output_filenames(
5454
&self,
@@ -491,11 +491,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
491491
}
492492
sess.lint_store = Some(Lrc::new(lint_store));
493493

494-
let compiler = Compiler {
495-
sess,
496-
codegen_backend: Lrc::from(codegen_backend),
497-
override_queries: config.override_queries,
498-
};
494+
let compiler =
495+
Compiler { sess, codegen_backend, override_queries: config.override_queries };
499496

500497
rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
501498
let r = {

compiler/rustc_interface/src/queries.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
77
use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
10-
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, Lrc, OnceLock, WorkerLocal};
10+
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
1111
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
1212
use rustc_hir::definitions::Definitions;
1313
use rustc_incremental::setup_dep_graph;
@@ -105,7 +105,7 @@ impl<'tcx> Queries<'tcx> {
105105
&self.compiler.sess
106106
}
107107

108-
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
108+
fn codegen_backend(&self) -> &dyn CodegenBackend {
109109
self.compiler.codegen_backend()
110110
}
111111

@@ -198,7 +198,7 @@ impl<'tcx> Queries<'tcx> {
198198
// Hook for UI tests.
199199
Self::check_for_rustc_errors_attr(tcx);
200200

201-
Ok(passes::start_codegen(&**self.codegen_backend(), tcx))
201+
Ok(passes::start_codegen(self.codegen_backend(), tcx))
202202
})
203203
}
204204

@@ -239,7 +239,6 @@ impl<'tcx> Queries<'tcx> {
239239
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
240240
self.global_ctxt()?.enter(|tcx| {
241241
Ok(Linker {
242-
codegen_backend: self.codegen_backend().clone(),
243242
dep_graph: tcx.dep_graph.clone(),
244243
prepare_outputs: tcx.output_filenames(()).clone(),
245244
crate_hash: if tcx.needs_crate_hash() {
@@ -254,10 +253,6 @@ impl<'tcx> Queries<'tcx> {
254253
}
255254

256255
pub struct Linker {
257-
// compilation inputs
258-
codegen_backend: Lrc<dyn CodegenBackend>,
259-
260-
// compilation outputs
261256
dep_graph: DepGraph,
262257
prepare_outputs: Arc<OutputFilenames>,
263258
// Only present when incr. comp. is enabled.
@@ -266,9 +261,9 @@ pub struct Linker {
266261
}
267262

268263
impl Linker {
269-
pub fn link(self, sess: &Session) -> Result<()> {
264+
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
270265
let (codegen_results, work_products) =
271-
self.codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
266+
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
272267

273268
sess.compile_status()?;
274269

@@ -301,7 +296,7 @@ impl Linker {
301296
}
302297

303298
let _timer = sess.prof.verbose_generic_activity("link_crate");
304-
self.codegen_backend.link(sess, codegen_results, &self.prepare_outputs)
299+
codegen_backend.link(sess, codegen_results, &self.prepare_outputs)
305300
}
306301
}
307302

tests/run-make-fulldeps/issue-19371/foo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
7272
let ongoing_codegen = queries.ongoing_codegen()?;
7373
queries.linker(ongoing_codegen)
7474
});
75-
linker.unwrap().link(compiler.session()).unwrap();
75+
linker.unwrap().link(compiler.session(), compiler.codegen_backend()).unwrap();
7676
});
7777
}

0 commit comments

Comments
 (0)