Skip to content

Commit 980e359

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 4180d10 commit 980e359

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ fn run_compiler(
370370
}
371371
let should_stop = print_crate_info(
372372
&handler,
373-
&**compiler.codegen_backend(),
373+
compiler.codegen_backend(),
374374
compiler.session(),
375375
false,
376376
);
@@ -394,12 +394,11 @@ fn run_compiler(
394394

395395
interface::run_compiler(config, |compiler| {
396396
let sess = compiler.session();
397+
let codegen_backend = compiler.codegen_backend();
397398
let handler = EarlyErrorHandler::new(sess.opts.error_format);
398399

399-
let should_stop = print_crate_info(&handler, &**compiler.codegen_backend(), sess, true)
400-
.and_then(|| {
401-
list_metadata(&handler, sess, &*compiler.codegen_backend().metadata_loader())
402-
})
400+
let should_stop = print_crate_info(&handler, codegen_backend, sess, true)
401+
.and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader()))
403402
.and_then(|| try_process_rlink(sess, compiler));
404403

405404
if should_stop == Compilation::Stop {
@@ -493,7 +492,7 @@ fn run_compiler(
493492

494493
if let Some(linker) = linker {
495494
let _timer = sess.timer("link");
496-
linker.link(sess)?
495+
linker.link(sess, codegen_backend)?
497496
}
498497

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

compiler/rustc_interface/src/interface.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::defer;
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
88
use rustc_data_structures::stable_hasher::StableHasher;
9-
use rustc_data_structures::sync::Lrc;
109
use rustc_errors::registry::Registry;
1110
use rustc_errors::{ErrorGuaranteed, Handler};
1211
use rustc_lint::LintStore;
@@ -39,7 +38,7 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
3938
/// Created by passing [`Config`] to [`run_compiler`].
4039
pub struct Compiler {
4140
pub(crate) sess: Session,
42-
codegen_backend: Lrc<dyn CodegenBackend>,
41+
codegen_backend: Box<dyn CodegenBackend>,
4342
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
4443
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
4544
}
@@ -48,8 +47,8 @@ impl Compiler {
4847
pub fn session(&self) -> &Session {
4948
&self.sess
5049
}
51-
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
52-
&self.codegen_backend
50+
pub fn codegen_backend(&self) -> &dyn CodegenBackend {
51+
&*self.codegen_backend
5352
}
5453
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
5554
&self.register_lints
@@ -475,7 +474,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
475474

476475
let compiler = Compiler {
477476
sess,
478-
codegen_backend: Lrc::from(codegen_backend),
477+
codegen_backend,
479478
register_lints: config.register_lints,
480479
override_queries: config.override_queries,
481480
};

compiler/rustc_interface/src/queries.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -201,7 +201,7 @@ impl<'tcx> Queries<'tcx> {
201201
// Hook for UI tests.
202202
Self::check_for_rustc_errors_attr(tcx);
203203

204-
Ok(passes::start_codegen(&**self.codegen_backend(), tcx))
204+
Ok(passes::start_codegen(self.codegen_backend(), tcx))
205205
})
206206
}
207207

@@ -242,7 +242,6 @@ impl<'tcx> Queries<'tcx> {
242242
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
243243
self.global_ctxt()?.enter(|tcx| {
244244
Ok(Linker {
245-
codegen_backend: self.codegen_backend().clone(),
246245
dep_graph: tcx.dep_graph.clone(),
247246
prepare_outputs: tcx.output_filenames(()).clone(),
248247
crate_hash: if tcx.needs_crate_hash() {
@@ -257,10 +256,6 @@ impl<'tcx> Queries<'tcx> {
257256
}
258257

259258
pub struct Linker {
260-
// compilation inputs
261-
codegen_backend: Lrc<dyn CodegenBackend>,
262-
263-
// compilation outputs
264259
dep_graph: DepGraph,
265260
prepare_outputs: Arc<OutputFilenames>,
266261
// Only present when incr. comp. is enabled.
@@ -269,9 +264,9 @@ pub struct Linker {
269264
}
270265

271266
impl Linker {
272-
pub fn link(self, sess: &Session) -> Result<()> {
267+
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
273268
let (codegen_results, work_products) =
274-
self.codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
269+
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
275270

276271
sess.compile_status()?;
277272

@@ -304,7 +299,7 @@ impl Linker {
304299
}
305300

306301
let _timer = sess.prof.verbose_generic_activity("link_crate");
307-
self.codegen_backend.link(sess, codegen_results, &self.prepare_outputs)
302+
codegen_backend.link(sess, codegen_results, &self.prepare_outputs)
308303
}
309304
}
310305

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)