Skip to content

Commit

Permalink
Auto merge of #117993 - nnethercote:streamline-Linker, r=bjorn3
Browse files Browse the repository at this point in the history
Streamline `Linker`

r? `@bjorn3`
  • Loading branch information
bors committed Nov 17, 2023
2 parents 069a4af + 9582172 commit e886137
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 64 deletions.
11 changes: 5 additions & 6 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ fn run_compiler(
}
let should_stop = print_crate_info(
&handler,
&**compiler.codegen_backend(),
compiler.codegen_backend(),
compiler.session(),
false,
);
Expand All @@ -385,12 +385,11 @@ fn run_compiler(

interface::run_compiler(config, |compiler| {
let sess = compiler.session();
let codegen_backend = compiler.codegen_backend();
let handler = EarlyErrorHandler::new(sess.opts.error_format);

let should_stop = print_crate_info(&handler, &**compiler.codegen_backend(), sess, true)
.and_then(|| {
list_metadata(&handler, sess, &*compiler.codegen_backend().metadata_loader())
})
let should_stop = print_crate_info(&handler, codegen_backend, sess, true)
.and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader()))
.and_then(|| try_process_rlink(sess, compiler));

if should_stop == Compilation::Stop {
Expand Down Expand Up @@ -482,7 +481,7 @@ fn run_compiler(

if let Some(linker) = linker {
let _timer = sess.timer("link");
linker.link()?
linker.link(sess, codegen_backend)?
}

if sess.opts.unstable_opts.print_fuel.is_some() {
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
/// Can be used to run `rustc_interface` queries.
/// Created by passing [`Config`] to [`run_compiler`].
pub struct Compiler {
pub(crate) sess: Lrc<Session>,
codegen_backend: Lrc<dyn CodegenBackend>,
sess: Session,
codegen_backend: Box<dyn CodegenBackend>,
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
}

impl Compiler {
pub fn session(&self) -> &Lrc<Session> {
pub fn session(&self) -> &Session {
&self.sess
}
pub fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {
&self.codegen_backend
pub fn codegen_backend(&self) -> &dyn CodegenBackend {
&*self.codegen_backend
}
pub fn build_output_filenames(
&self,
Expand Down Expand Up @@ -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));

let compiler = Compiler {
sess: Lrc::new(sess),
codegen_backend: Lrc::from(codegen_backend),
override_queries: config.override_queries,
};
let compiler =
Compiler { sess, codegen_backend, override_queries: config.override_queries };

rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
let r = {
Expand Down
76 changes: 29 additions & 47 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal;
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::definitions::Definitions;
use rustc_incremental::setup_dep_graph;
Expand Down Expand Up @@ -101,10 +101,11 @@ impl<'tcx> Queries<'tcx> {
}
}

fn session(&self) -> &Lrc<Session> {
&self.compiler.sess
fn session(&self) -> &Session {
&self.compiler.session()
}
fn codegen_backend(&self) -> &Lrc<dyn CodegenBackend> {

fn codegen_backend(&self) -> &dyn CodegenBackend {
self.compiler.codegen_backend()
}

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

Ok(passes::start_codegen(&**self.codegen_backend(), tcx))
Ok(passes::start_codegen(self.codegen_backend(), tcx))
})
}

Expand Down Expand Up @@ -236,67 +237,48 @@ impl<'tcx> Queries<'tcx> {
}

pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
let sess = self.session().clone();
let codegen_backend = self.codegen_backend().clone();

let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
(
if tcx.needs_crate_hash() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { None },
tcx.output_filenames(()).clone(),
tcx.dep_graph.clone(),
)
});

Ok(Linker {
sess,
codegen_backend,

dep_graph,
prepare_outputs,
crate_hash,
ongoing_codegen,
self.global_ctxt()?.enter(|tcx| {
Ok(Linker {
dep_graph: tcx.dep_graph.clone(),
output_filenames: tcx.output_filenames(()).clone(),
crate_hash: if tcx.needs_crate_hash() {
Some(tcx.crate_hash(LOCAL_CRATE))
} else {
None
},
ongoing_codegen,
})
})
}
}

pub struct Linker {
// compilation inputs
sess: Lrc<Session>,
codegen_backend: Lrc<dyn CodegenBackend>,

// compilation outputs
dep_graph: DepGraph,
prepare_outputs: Arc<OutputFilenames>,
output_filenames: Arc<OutputFilenames>,
// Only present when incr. comp. is enabled.
crate_hash: Option<Svh>,
ongoing_codegen: Box<dyn Any>,
}

impl Linker {
pub fn link(self) -> Result<()> {
let (codegen_results, work_products) = self.codegen_backend.join_codegen(
self.ongoing_codegen,
&self.sess,
&self.prepare_outputs,
)?;
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
let (codegen_results, work_products) =
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames)?;

self.sess.compile_status()?;
sess.compile_status()?;

let sess = &self.sess;
let dep_graph = self.dep_graph;
sess.time("serialize_work_products", || {
rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
rustc_incremental::save_work_product_index(sess, &self.dep_graph, work_products)
});

let prof = self.sess.prof.clone();
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
let prof = sess.prof.clone();
prof.generic_activity("drop_dep_graph").run(move || drop(self.dep_graph));

// Now that we won't touch anything in the incremental compilation directory
// 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
.sess
if !sess
.opts
.output_types
.keys()
Expand All @@ -306,14 +288,14 @@ impl Linker {
}

if sess.opts.unstable_opts.no_link {
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
return Ok(());
}

let _timer = sess.prof.verbose_generic_activity("link_crate");
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
codegen_backend.link(sess, codegen_results, &self.output_filenames)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-make-fulldeps/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
let ongoing_codegen = queries.ongoing_codegen()?;
queries.linker(ongoing_codegen)
});
linker.unwrap().link().unwrap();
linker.unwrap().link(compiler.session(), compiler.codegen_backend()).unwrap();
});
}

0 comments on commit e886137

Please sign in to comment.