Skip to content

Commit e886137

Browse files
committed
Auto merge of rust-lang#117993 - nnethercote:streamline-Linker, r=bjorn3
Streamline `Linker` r? `@bjorn3`
2 parents 069a4af + 9582172 commit e886137

File tree

4 files changed

+42
-64
lines changed

4 files changed

+42
-64
lines changed

compiler/rustc_driver_impl/src/lib.rs

+5-6
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()?
484+
linker.link(sess, codegen_backend)?
486485
}
487486

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

compiler/rustc_interface/src/interface.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
3838
/// Can be used to run `rustc_interface` queries.
3939
/// Created by passing [`Config`] to [`run_compiler`].
4040
pub struct Compiler {
41-
pub(crate) sess: Lrc<Session>,
42-
codegen_backend: Lrc<dyn CodegenBackend>,
41+
sess: Session,
42+
codegen_backend: Box<dyn CodegenBackend>,
4343
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
4444
}
4545

4646
impl Compiler {
47-
pub fn session(&self) -> &Lrc<Session> {
47+
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: Lrc::new(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

+29-47
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;
@@ -101,10 +101,11 @@ impl<'tcx> Queries<'tcx> {
101101
}
102102
}
103103

104-
fn session(&self) -> &Lrc<Session> {
105-
&self.compiler.sess
104+
fn session(&self) -> &Session {
105+
&self.compiler.session()
106106
}
107-
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
107+
108+
fn codegen_backend(&self) -> &dyn CodegenBackend {
108109
self.compiler.codegen_backend()
109110
}
110111

@@ -197,7 +198,7 @@ impl<'tcx> Queries<'tcx> {
197198
// Hook for UI tests.
198199
Self::check_for_rustc_errors_attr(tcx);
199200

200-
Ok(passes::start_codegen(&**self.codegen_backend(), tcx))
201+
Ok(passes::start_codegen(self.codegen_backend(), tcx))
201202
})
202203
}
203204

@@ -236,67 +237,48 @@ impl<'tcx> Queries<'tcx> {
236237
}
237238

238239
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
239-
let sess = self.session().clone();
240-
let codegen_backend = self.codegen_backend().clone();
241-
242-
let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
243-
(
244-
if tcx.needs_crate_hash() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { None },
245-
tcx.output_filenames(()).clone(),
246-
tcx.dep_graph.clone(),
247-
)
248-
});
249-
250-
Ok(Linker {
251-
sess,
252-
codegen_backend,
253-
254-
dep_graph,
255-
prepare_outputs,
256-
crate_hash,
257-
ongoing_codegen,
240+
self.global_ctxt()?.enter(|tcx| {
241+
Ok(Linker {
242+
dep_graph: tcx.dep_graph.clone(),
243+
output_filenames: tcx.output_filenames(()).clone(),
244+
crate_hash: if tcx.needs_crate_hash() {
245+
Some(tcx.crate_hash(LOCAL_CRATE))
246+
} else {
247+
None
248+
},
249+
ongoing_codegen,
250+
})
258251
})
259252
}
260253
}
261254

262255
pub struct Linker {
263-
// compilation inputs
264-
sess: Lrc<Session>,
265-
codegen_backend: Lrc<dyn CodegenBackend>,
266-
267-
// compilation outputs
268256
dep_graph: DepGraph,
269-
prepare_outputs: Arc<OutputFilenames>,
257+
output_filenames: Arc<OutputFilenames>,
270258
// Only present when incr. comp. is enabled.
271259
crate_hash: Option<Svh>,
272260
ongoing_codegen: Box<dyn Any>,
273261
}
274262

275263
impl Linker {
276-
pub fn link(self) -> Result<()> {
277-
let (codegen_results, work_products) = self.codegen_backend.join_codegen(
278-
self.ongoing_codegen,
279-
&self.sess,
280-
&self.prepare_outputs,
281-
)?;
264+
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
265+
let (codegen_results, work_products) =
266+
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames)?;
282267

283-
self.sess.compile_status()?;
268+
sess.compile_status()?;
284269

285-
let sess = &self.sess;
286-
let dep_graph = self.dep_graph;
287270
sess.time("serialize_work_products", || {
288-
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
271+
rustc_incremental::save_work_product_index(sess, &self.dep_graph, work_products)
289272
});
290273

291-
let prof = self.sess.prof.clone();
292-
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
274+
let prof = sess.prof.clone();
275+
prof.generic_activity("drop_dep_graph").run(move || drop(self.dep_graph));
293276

294277
// Now that we won't touch anything in the incremental compilation directory
295278
// any more, we can finalize it (which involves renaming it)
296-
rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
279+
rustc_incremental::finalize_session_directory(sess, self.crate_hash);
297280

298-
if !self
299-
.sess
281+
if !sess
300282
.opts
301283
.output_types
302284
.keys()
@@ -306,14 +288,14 @@ impl Linker {
306288
}
307289

308290
if sess.opts.unstable_opts.no_link {
309-
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
291+
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
310292
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
311293
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
312294
return Ok(());
313295
}
314296

315297
let _timer = sess.prof.verbose_generic_activity("link_crate");
316-
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
298+
codegen_backend.link(sess, codegen_results, &self.output_filenames)
317299
}
318300
}
319301

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

+1-1
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().unwrap();
75+
linker.unwrap().link(compiler.session(), compiler.codegen_backend()).unwrap();
7676
});
7777
}

0 commit comments

Comments
 (0)