1
Fork 0

Move Session out of Linker.

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

View file

@ -482,7 +482,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()? linker.link(sess)?
} }
if sess.opts.unstable_opts.print_fuel.is_some() { if sess.opts.unstable_opts.print_fuel.is_some() {

View file

@ -38,13 +38,13 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
/// Can be used to run `rustc_interface` queries. /// Can be used to run `rustc_interface` queries.
/// Created by passing [`Config`] to [`run_compiler`]. /// Created by passing [`Config`] to [`run_compiler`].
pub struct Compiler { pub struct Compiler {
pub(crate) sess: Lrc<Session>, pub(crate) sess: Session,
codegen_backend: Lrc<dyn CodegenBackend>, codegen_backend: Lrc<dyn CodegenBackend>,
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>, pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
} }
impl Compiler { impl Compiler {
pub fn session(&self) -> &Lrc<Session> { pub fn session(&self) -> &Session {
&self.sess &self.sess
} }
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> { pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
@ -492,7 +492,7 @@ 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 = Compiler {
sess: Lrc::new(sess), sess,
codegen_backend: Lrc::from(codegen_backend), codegen_backend: Lrc::from(codegen_backend),
override_queries: config.override_queries, override_queries: config.override_queries,
}; };

View file

@ -101,9 +101,10 @@ impl<'tcx> Queries<'tcx> {
} }
} }
fn session(&self) -> &Lrc<Session> { fn session(&self) -> &Session {
&self.compiler.sess &self.compiler.sess
} }
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> { fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
self.compiler.codegen_backend() self.compiler.codegen_backend()
} }
@ -238,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 {
sess: self.session().clone(),
codegen_backend: self.codegen_backend().clone(), 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(),
@ -255,7 +255,6 @@ impl<'tcx> Queries<'tcx> {
pub struct Linker { pub struct Linker {
// compilation inputs // compilation inputs
sess: Lrc<Session>,
codegen_backend: Lrc<dyn CodegenBackend>, codegen_backend: Lrc<dyn CodegenBackend>,
// compilation outputs // compilation outputs
@ -267,30 +266,25 @@ pub struct Linker {
} }
impl Linker { impl Linker {
pub fn link(self) -> Result<()> { pub fn link(self, sess: &Session) -> Result<()> {
let (codegen_results, work_products) = self.codegen_backend.join_codegen( let (codegen_results, work_products) =
self.ongoing_codegen, self.codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.prepare_outputs)?;
&self.sess,
&self.prepare_outputs,
)?;
self.sess.compile_status()?; sess.compile_status()?;
let sess = &self.sess;
let dep_graph = self.dep_graph; let dep_graph = self.dep_graph;
sess.time("serialize_work_products", || { sess.time("serialize_work_products", || {
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products) rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
}); });
let prof = self.sess.prof.clone(); let prof = sess.prof.clone();
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph)); prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
// Now that we won't touch anything in the incremental compilation directory // Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it) // any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash); rustc_incremental::finalize_session_directory(sess, self.crate_hash);
if !self if !sess
.sess
.opts .opts
.output_types .output_types
.keys() .keys()
@ -307,7 +301,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(&self.sess, codegen_results, &self.prepare_outputs) self.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().unwrap(); linker.unwrap().link(compiler.session()).unwrap();
}); });
} }