Skip to content

Commit

Permalink
Auto merge of #68601 - 0dvictor:split, r=tmandry
Browse files Browse the repository at this point in the history
Split `join_codegen_and_link()` into two steps

`join_codegen_and_link()` is split to `join_codegen()` and `link()`.
  • Loading branch information
bors committed Feb 4, 2020
2 parents 42a0bd2 + ae51d2b commit fc07615
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
29 changes: 16 additions & 13 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc::dep_graph::WorkProduct;
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::CompiledModule;
use rustc_codegen_ssa::{CodegenResults, CompiledModule};
use rustc_errors::{FatalError, Handler};
use std::any::Any;
use std::ffi::CStr;
Expand All @@ -39,7 +39,7 @@ use syntax::expand::allocator::AllocatorKind;

use rustc::dep_graph::DepGraph;
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
use rustc::session::config::{OptLevel, OutputFilenames, OutputType, PrintRequest};
use rustc::session::config::{OptLevel, OutputFilenames, PrintRequest};
use rustc::session::Session;
use rustc::ty::{self, TyCtxt};
use rustc::util::common::ErrorReported;
Expand Down Expand Up @@ -270,13 +270,12 @@ impl CodegenBackend for LlvmCodegenBackend {
)
}

fn join_codegen_and_link(
fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
dep_graph: &DepGraph,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported> {
) -> Result<Box<dyn Any>, ErrorReported> {
let (codegen_results, work_products) = ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
Expand All @@ -291,14 +290,18 @@ impl CodegenBackend for LlvmCodegenBackend {

sess.compile_status()?;

if !sess
.opts
.output_types
.keys()
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
{
return Ok(());
}
Ok(Box::new(codegen_results))
}

fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported> {
let codegen_results = codegen_results
.downcast::<CodegenResults>()
.expect("Expected CodegenResults, found Box<Any>");

if sess.opts.debugging_opts.no_link {
// FIXME: use a binary format to encode the `.rlink` file
Expand Down
13 changes: 12 additions & 1 deletion src/librustc_codegen_utils/codegen_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,22 @@ pub trait CodegenBackend {
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
fn join_codegen_and_link(
fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
dep_graph: &DepGraph,
) -> Result<Box<dyn Any>, ErrorReported>;

/// This is called on the returned `Box<dyn Any>` from `join_codegen`
///
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported>;
}
23 changes: 13 additions & 10 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,22 @@ pub struct Linker {

impl Linker {
pub fn link(self) -> Result<()> {
let r = self
.codegen_backend
.join_codegen_and_link(
self.ongoing_codegen,
&self.sess,
&self.dep_graph,
&self.prepare_outputs,
)
.map_err(|_| ErrorReported);
let codegen_results =
self.codegen_backend.join_codegen(self.ongoing_codegen, &self.sess, &self.dep_graph)?;
let prof = self.sess.prof.clone();
let dep_graph = self.dep_graph;
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
r

if !self
.sess
.opts
.output_types
.keys()
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
{
return Ok(());
}
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
}
}

Expand Down
18 changes: 14 additions & 4 deletions src/test/run-make-fulldeps/hotplug_codegen_backend/the_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,28 @@ impl CodegenBackend for TheBackend {
Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
}

fn join_codegen_and_link(
fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
_sess: &Session,
_dep_graph: &DepGraph,
) -> Result<Box<dyn Any>, ErrorReported> {
let crate_name = ongoing_codegen.downcast::<Symbol>()
.expect("in join_codegen: ongoing_codegen is not a Symbol");
Ok(crate_name)
}

fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported> {
use std::io::Write;
use rustc::session::config::CrateType;
use rustc_codegen_utils::link::out_filename;
let crate_name = ongoing_codegen.downcast::<Symbol>()
.expect("in join_codegen_and_link: ongoing_codegen is not a Symbol");
let crate_name = codegen_results.downcast::<Symbol>()
.expect("in link: codegen_results is not a Symbol");
for &crate_type in sess.opts.crate_types.iter() {
if crate_type != CrateType::Rlib {
sess.fatal(&format!("Crate type is {:?}", crate_type));
Expand Down

0 comments on commit fc07615

Please sign in to comment.