Skip to content

Commit fc07615

Browse files
committed
Auto merge of #68601 - 0dvictor:split, r=tmandry
Split `join_codegen_and_link()` into two steps `join_codegen_and_link()` is split to `join_codegen()` and `link()`.
2 parents 42a0bd2 + ae51d2b commit fc07615

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

src/librustc_codegen_llvm/lib.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc::dep_graph::WorkProduct;
2929
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
3030
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig};
3131
use rustc_codegen_ssa::traits::*;
32-
use rustc_codegen_ssa::CompiledModule;
32+
use rustc_codegen_ssa::{CodegenResults, CompiledModule};
3333
use rustc_errors::{FatalError, Handler};
3434
use std::any::Any;
3535
use std::ffi::CStr;
@@ -39,7 +39,7 @@ use syntax::expand::allocator::AllocatorKind;
3939

4040
use rustc::dep_graph::DepGraph;
4141
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
42-
use rustc::session::config::{OptLevel, OutputFilenames, OutputType, PrintRequest};
42+
use rustc::session::config::{OptLevel, OutputFilenames, PrintRequest};
4343
use rustc::session::Session;
4444
use rustc::ty::{self, TyCtxt};
4545
use rustc::util::common::ErrorReported;
@@ -270,13 +270,12 @@ impl CodegenBackend for LlvmCodegenBackend {
270270
)
271271
}
272272

273-
fn join_codegen_and_link(
273+
fn join_codegen(
274274
&self,
275275
ongoing_codegen: Box<dyn Any>,
276276
sess: &Session,
277277
dep_graph: &DepGraph,
278-
outputs: &OutputFilenames,
279-
) -> Result<(), ErrorReported> {
278+
) -> Result<Box<dyn Any>, ErrorReported> {
280279
let (codegen_results, work_products) = ongoing_codegen
281280
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
282281
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
@@ -291,14 +290,18 @@ impl CodegenBackend for LlvmCodegenBackend {
291290

292291
sess.compile_status()?;
293292

294-
if !sess
295-
.opts
296-
.output_types
297-
.keys()
298-
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
299-
{
300-
return Ok(());
301-
}
293+
Ok(Box::new(codegen_results))
294+
}
295+
296+
fn link(
297+
&self,
298+
sess: &Session,
299+
codegen_results: Box<dyn Any>,
300+
outputs: &OutputFilenames,
301+
) -> Result<(), ErrorReported> {
302+
let codegen_results = codegen_results
303+
.downcast::<CodegenResults>()
304+
.expect("Expected CodegenResults, found Box<Any>");
302305

303306
if sess.opts.debugging_opts.no_link {
304307
// FIXME: use a binary format to encode the `.rlink` file

src/librustc_codegen_utils/codegen_backend.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@ pub trait CodegenBackend {
4343
/// # Panics
4444
///
4545
/// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
46-
fn join_codegen_and_link(
46+
fn join_codegen(
4747
&self,
4848
ongoing_codegen: Box<dyn Any>,
4949
sess: &Session,
5050
dep_graph: &DepGraph,
51+
) -> Result<Box<dyn Any>, ErrorReported>;
52+
53+
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
54+
///
55+
/// # Panics
56+
///
57+
/// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
58+
fn link(
59+
&self,
60+
sess: &Session,
61+
codegen_results: Box<dyn Any>,
5162
outputs: &OutputFilenames,
5263
) -> Result<(), ErrorReported>;
5364
}

src/librustc_interface/queries.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,22 @@ pub struct Linker {
310310

311311
impl Linker {
312312
pub fn link(self) -> Result<()> {
313-
let r = self
314-
.codegen_backend
315-
.join_codegen_and_link(
316-
self.ongoing_codegen,
317-
&self.sess,
318-
&self.dep_graph,
319-
&self.prepare_outputs,
320-
)
321-
.map_err(|_| ErrorReported);
313+
let codegen_results =
314+
self.codegen_backend.join_codegen(self.ongoing_codegen, &self.sess, &self.dep_graph)?;
322315
let prof = self.sess.prof.clone();
323316
let dep_graph = self.dep_graph;
324317
prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
325-
r
318+
319+
if !self
320+
.sess
321+
.opts
322+
.output_types
323+
.keys()
324+
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
325+
{
326+
return Ok(());
327+
}
328+
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
326329
}
327330
}
328331

src/test/run-make-fulldeps/hotplug_codegen_backend/the_backend.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,28 @@ impl CodegenBackend for TheBackend {
7171
Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
7272
}
7373

74-
fn join_codegen_and_link(
74+
fn join_codegen(
7575
&self,
7676
ongoing_codegen: Box<dyn Any>,
77-
sess: &Session,
77+
_sess: &Session,
7878
_dep_graph: &DepGraph,
79+
) -> Result<Box<dyn Any>, ErrorReported> {
80+
let crate_name = ongoing_codegen.downcast::<Symbol>()
81+
.expect("in join_codegen: ongoing_codegen is not a Symbol");
82+
Ok(crate_name)
83+
}
84+
85+
fn link(
86+
&self,
87+
sess: &Session,
88+
codegen_results: Box<dyn Any>,
7989
outputs: &OutputFilenames,
8090
) -> Result<(), ErrorReported> {
8191
use std::io::Write;
8292
use rustc::session::config::CrateType;
8393
use rustc_codegen_utils::link::out_filename;
84-
let crate_name = ongoing_codegen.downcast::<Symbol>()
85-
.expect("in join_codegen_and_link: ongoing_codegen is not a Symbol");
94+
let crate_name = codegen_results.downcast::<Symbol>()
95+
.expect("in link: codegen_results is not a Symbol");
8696
for &crate_type in sess.opts.crate_types.iter() {
8797
if crate_type != CrateType::Rlib {
8898
sess.fatal(&format!("Crate type is {:?}", crate_type));

0 commit comments

Comments
 (0)