Skip to content
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use rustc_codegen_ssa::traits::{ExtraBackendMethods, WriteBackendMethods};
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind};
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mono::{MonoItem, MonoItemData, Visibility};
use rustc_session::Session;
use rustc_session::config::{OptLevel, OutputFilenames, OutputType};
use rustc_session::{BorrowedIncrCompSession, Session};
use rustc_span::Symbol;

use crate::base::CodegenedFunction;
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::{CompiledModules, CrateInfo, TargetConfig, back};
use rustc_data_structures::unord::UnordSet;
use rustc_log::tracing::info;
use rustc_middle::dep_graph::WorkProductMap;
use rustc_middle::dep_graph::IncrCompSession;
use rustc_session::config::{NATIVE_CPU, OutputFilenames};
use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{Arch, CfgAbi, Env, Os};

Expand Down Expand Up @@ -239,10 +239,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: Option<&mut IncrCompSession>,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, WorkProductMap) {
) -> CompiledModules {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<driver::aot::AotDriver>>()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub(crate) fn codegen(

EmitObj::Bitcode => {
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
if let Err(err) = link_or_copy(&bc_out, &obj_out) {
if let Err(err) = link_or_copy(&bc_out, &obj_out, true /* allow_overwrite */) {
dcx.emit_err(CopyBitcode { err });
}

Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ use rustc_codegen_ssa::{CompiledModule, CompiledModules, CrateInfo, ModuleCodege
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OptLevel, OutputFilenames};
use rustc_session::{
BorrowedIncrCompSession, CodegenBackendInit, EarlySession, IncrCompSession, Session,
};
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{RelocModel, TargetTuple};
use tempfile::TempDir;
Expand Down Expand Up @@ -305,10 +303,10 @@ impl CodegenBackend for GccCodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: Option<&mut IncrCompSession>,
_outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, WorkProductMap) {
) -> CompiledModules {
ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
Expand Down
14 changes: 6 additions & 8 deletions compiler/rustc_codegen_llvm/src/back/llvm_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use rustc_codegen_ssa::{CompiledModule, CompiledModules, CrateInfo, ModuleCodege
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
use rustc_session::{
BorrowedIncrCompSession, CodegenBackendInit, EarlySession, IncrCompSession, Session,
};
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::{Symbol, sym};
use rustc_target::spec::{RelocModel, TlsModel};

Expand Down Expand Up @@ -368,11 +366,11 @@ impl CodegenBackend for LlvmCodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: Option<&mut IncrCompSession>,
outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, WorkProductMap) {
let (compiled_modules, work_products) = ongoing_codegen
) -> CompiledModules {
let compiled_modules = ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
.join(sess, incr_comp_session, crate_info);
Expand Down Expand Up @@ -412,7 +410,7 @@ impl CodegenBackend for LlvmCodegenBackend {
}
}

(compiled_modules, work_products)
compiled_modules
}

fn link(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
use rustc_middle::dep_graph::WorkProduct;
use rustc_session::{BorrowedIncrCompSession, config};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct};
use rustc_session::config;
use rustc_span::bug;
use rustc_structures::SanitizerSet;
use tracing::{debug, info};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ pub(crate) fn codegen(

EmitObj::Bitcode => {
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
if let Err(err) = link_or_copy(&bc_out, &obj_out) {
if let Err(err) = link_or_copy(&bc_out, &obj_out, true /* allow_overwrite */) {
dcx.emit_err(CopyBitcode { err });
}

Expand Down
89 changes: 44 additions & 45 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use rustc_incremental::{
};
use rustc_macros::{Decodable, Encodable};
use rustc_metadata::fs::copy_to_stdout;
use rustc_middle::dep_graph::{WorkProduct, WorkProductMap};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct};
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::{
self, Lto, OptLevel, OutFileName, OutputFilenames, OutputType, Passes, SwitchWithOptPath,
};
use rustc_session::{BorrowedIncrCompSession, IncrCompSession, Session};
use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, FileName, InnerSpan, Span, SyntaxContext, bug};
use rustc_structures::CrateType;
Expand Down Expand Up @@ -457,14 +457,11 @@ pub(crate) fn start_async_codegen<B: WriteBackendMethods>(

fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: &mut IncrCompSession,
compiled_modules: &CompiledModules,
) -> WorkProductMap {
let mut work_products = WorkProductMap::default();

if sess.opts.incremental.is_none() || sess.opts.unstable_opts.disable_incr_comp_backend_caching
{
return work_products;
) {
if sess.opts.unstable_opts.disable_incr_comp_backend_caching {
return;
}

let _timer = sess.timer("copy_all_cgu_workproducts_to_incr_comp_cache_dir");
Expand All @@ -489,16 +486,13 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
if let Some(path) = &module.bytecode {
files.push((OutputType::Bitcode.extension(), path.as_path()));
}
let (id, product) = copy_cgu_workproduct_to_incr_comp_cache_dir(
copy_cgu_workproduct_to_incr_comp_cache_dir(
sess,
incr_comp_session.unwrap(),
incr_comp_session,
&module.name,
files.as_slice(),
);
work_products.insert(id, product);
}

work_products
}

pub fn produce_final_output_artifacts(
Expand Down Expand Up @@ -892,7 +886,11 @@ fn execute_copy_from_cache_work_item(
source_file_in_incr_comp_dir,
output_path.display()
);
match link_or_copy(&source_file_in_incr_comp_dir, &output_path) {
match link_or_copy(
&source_file_in_incr_comp_dir,
&output_path,
true, // allow_overwrite
) {
Ok(_) => Some(output_path),
Err(error) => {
dcx.emit_err(diagnostics::CopyPathBuf {
Expand Down Expand Up @@ -2116,9 +2114,9 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
pub fn join(
self,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: Option<&mut IncrCompSession>,
crate_info: &CrateInfo,
) -> (CompiledModules, WorkProductMap) {
) -> CompiledModules {
self.shared_emitter_main.check(sess, true);

let maybe_lto_modules = sess.time("join_worker_thread", || match self.coordinator.join() {
Expand All @@ -2137,25 +2135,26 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
let (shared_emitter, shared_emitter_main) = SharedEmitter::new();

// Catch fatal errors to ensure shared_emitter_main.check() can emit the actual diagnostics
let compilation_output = catch_fatal_errors(|| match maybe_lto_modules {
let compiled_modules = catch_fatal_errors(|| match maybe_lto_modules {
MaybeLtoModules::NoLto(compiled_modules) => {
drop(shared_emitter);

let work_products = copy_all_cgu_workproducts_to_incr_comp_cache_dir(
sess,
incr_comp_session,
&compiled_modules,
);
if let Some(incr_comp_session) = incr_comp_session {
copy_all_cgu_workproducts_to_incr_comp_cache_dir(
sess,
incr_comp_session,
&compiled_modules,
);
}

(compiled_modules, work_products)
compiled_modules
}
MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => {
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);

// FIXME cache local pre-LTO bitcode
let work_products = WorkProductMap::default();

let compiled_modules = CompiledModules {
CompiledModules {
modules: vec![do_fat_lto(
sess,
&cgcx,
Expand All @@ -2166,9 +2165,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
needs_fat_lto,
)],
allocator_module: None,
};

(compiled_modules, work_products)
}
}
MaybeLtoModules::ThinLto { cgcx, needs_thin_lto } => {
let tm_factory = self.backend.target_machine_factory(sess, cgcx.opt_level);
Expand All @@ -2177,7 +2174,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {
modules: do_thin_lto::<B>(
&cgcx,
&sess.prof,
incr_comp_session.map(|incr_comp_session| incr_comp_session.borrow()),
incr_comp_session.as_deref().map(IncrCompSession::borrow),
shared_emitter,
tm_factory,
&crate_info.exported_symbols_for_lto,
Expand All @@ -2190,22 +2187,24 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {

// FIXME include pre-LTO bitcode in workproduct tracking
// FIXME add separate incr comp session for post-LTO outputs to use during link step
let work_products = copy_all_cgu_workproducts_to_incr_comp_cache_dir(
sess,
incr_comp_session,
&compiled_modules,
);
if let Some(incr_comp_session) = incr_comp_session {
copy_all_cgu_workproducts_to_incr_comp_cache_dir(
sess,
incr_comp_session,
&compiled_modules,
);
}

(compiled_modules, work_products)
compiled_modules
}
});

shared_emitter_main.check(sess, true);

sess.dcx().abort_if_errors();

let (mut compiled_modules, work_products) =
compilation_output.expect("fatal error emitted but not sent to SharedEmitter");
let mut compiled_modules =
compiled_modules.expect("fatal error emitted but not sent to SharedEmitter");

// Regardless of what order these modules completed in, report them to
// the backend in the same order every time to ensure that we're handing
Expand All @@ -2214,7 +2213,7 @@ impl<B: WriteBackendMethods> OngoingCodegen<B> {

produce_final_output_artifacts(sess, &compiled_modules, &self.output_filenames);

(compiled_modules, work_products)
compiled_modules
}

pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) {
Expand Down Expand Up @@ -2258,19 +2257,19 @@ pub(crate) fn submit_post_lto_module_to_llvm<B: WriteBackendMethods>(
}

pub(crate) fn submit_pre_lto_module_to_llvm<B: WriteBackendMethods>(
tcx: TyCtxt<'_>,
sess: &Session,
incr_comp_session: &IncrCompSession,
coordinator: &Coordinator<B>,
module: CachedModuleCodegen,
) {
let filename = pre_lto_bitcode_filename(&module.name);
let old_bitcode_path =
in_old_incr_comp_dir_sess(tcx.incr_comp_session.unwrap(), &filename).unwrap();
let bitcode_path = in_incr_comp_dir_sess(tcx.incr_comp_session.unwrap(), &filename);
let old_bitcode_path = in_old_incr_comp_dir_sess(incr_comp_session, &filename).unwrap();
let bitcode_path = in_incr_comp_dir_sess(incr_comp_session, &filename);

match link_or_copy(&old_bitcode_path, &bitcode_path) {
match link_or_copy(&old_bitcode_path, &bitcode_path, false /* allow_overwrite */) {
Ok(_) => {}
Err(error) => {
tcx.sess.dcx().emit_err(diagnostics::CopyPathBuf {
sess.dcx().emit_err(diagnostics::CopyPathBuf {
source_file: old_bitcode_path,
output_path: bitcode_path,
error,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,8 @@ pub fn codegen_crate<
}
CguReuse::PreLto => {
submit_pre_lto_module_to_llvm(
tcx,
tcx.sess,
tcx.incr_comp_session.unwrap(),
&ongoing_codegen.coordinator,
CachedModuleCodegen {
name: cgu.name().to_string(),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use rustc_ast::expand::allocator::AllocatorMethod;
use rustc_data_structures::sync::{DynSend, DynSync};
use rustc_metadata::EncodedMetadata;
use rustc_metadata::creader::MetadataLoaderDyn;
use rustc_middle::dep_graph::WorkProductMap;
use rustc_middle::dep_graph::IncrCompSession;
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
use rustc_session::config::{OutputFilenames, PrintRequest};
use rustc_session::{CodegenBackendInit, EarlySession, IncrCompSession, Session};
use rustc_session::{CodegenBackendInit, EarlySession, Session};
use rustc_span::Symbol;
use rustc_structures::CrateType;

Expand Down Expand Up @@ -115,10 +115,10 @@ pub trait CodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
incr_comp_session: Option<&IncrCompSession>,
incr_comp_session: Option<&mut IncrCompSession>,
outputs: &OutputFilenames,
crate_info: &CrateInfo,
) -> (CompiledModules, WorkProductMap);
) -> CompiledModules;

/// This is called on the returned [`CompiledModules`] from [`join_codegen`](Self::join_codegen).
fn link(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/traits/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::path::PathBuf;

use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::DiagCtxtHandle;
use rustc_middle::dep_graph::WorkProduct;
use rustc_session::{BorrowedIncrCompSession, Session, config};
use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct};
use rustc_session::{Session, config};

use crate::back::lto::ThinModule;
use crate::back::write::{
Expand Down
Loading
Loading