diff --git a/Cargo.lock b/Cargo.lock index d21a126cf3fd6..74292387d5732 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3790,6 +3790,7 @@ dependencies = [ "rustc_fs_util", "rustc_hashes", "rustc_hir", + "rustc_incremental", "rustc_index", "rustc_lint_defs", "rustc_llvm", diff --git a/compiler/rustc_codegen_cranelift/src/driver/aot.rs b/compiler/rustc_codegen_cranelift/src/driver/aot.rs index d380182754cf5..6fb353b345d96 100644 --- a/compiler/rustc_codegen_cranelift/src/driver/aot.rs +++ b/compiler/rustc_codegen_cranelift/src/driver/aot.rs @@ -16,10 +16,10 @@ use rustc_codegen_ssa::back::write::{ CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput, }; use rustc_codegen_ssa::traits::{ExtraBackendMethods, WriteBackendMethods}; -use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind}; +use rustc_codegen_ssa::{CachedModuleCodegen, CompiledModule, ModuleCodegen, ModuleKind}; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_errors::{DiagCtxt, DiagCtxtHandle}; -use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct}; +use rustc_middle::dep_graph::IncrCompSession; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mono::{MonoItem, MonoItemData, Visibility}; use rustc_session::Session; @@ -337,13 +337,13 @@ impl WriteBackendMethods for AotDriver { fn run_thin_lto( _cgcx: &CodegenContext, - _prof: &SelfProfilerRef, - _incr_comp_session: Option<&BorrowedIncrCompSession>, + _sess: &Session, + _incr_comp_session: Option<&IncrCompSession>, _dcx: rustc_errors::DiagCtxtHandle<'_>, _exported_symbols_for_lto: &[String], _each_linked_rlib_for_lto: &[PathBuf], _modules: Vec>, - ) -> (Vec>, Vec) { + ) -> (Vec>, Vec) { unreachable!() } diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs index b9ae15ab2a2a3..c05ee60129dc3 100644 --- a/compiler/rustc_codegen_cranelift/src/lib.rs +++ b/compiler/rustc_codegen_cranelift/src/lib.rs @@ -43,7 +43,7 @@ use rustc_codegen_ssa::{CompiledModules, CrateInfo, TargetConfig, back}; use rustc_data_structures::unord::UnordSet; use rustc_log::tracing::info; use rustc_middle::dep_graph::IncrCompSession; -use rustc_session::config::{NATIVE_CPU, OutputFilenames}; +use rustc_session::config::NATIVE_CPU; use rustc_session::{CodegenBackendInit, EarlySession, Session}; use rustc_span::{Symbol, sym}; use rustc_target::spec::{Arch, CfgAbi, Env, Os}; @@ -240,13 +240,24 @@ impl CodegenBackend for CraneliftCodegenBackend { ongoing_codegen: Box, sess: &Session, incr_comp_session: Option<&mut IncrCompSession>, - _outputs: &OutputFilenames, - crate_info: &CrateInfo, - ) -> CompiledModules { + ) -> Box { ongoing_codegen .downcast::>() .unwrap() - .join(sess, incr_comp_session, crate_info) + .join(sess, incr_comp_session) + } + + fn perform_lto( + &self, + pending_lto: Box, + sess: &Session, + incr_comp_session: Option<&mut IncrCompSession>, + crate_info: &CrateInfo, + ) -> CompiledModules { + pending_lto + .downcast::>() + .unwrap() + .perform_lto(sess, incr_comp_session, crate_info) } } diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 42357abe353a4..56e7d5e3f5fe4 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -85,13 +85,15 @@ use rustc_codegen_ssa::back::write::{ use rustc_codegen_ssa::base::codegen_crate; use rustc_codegen_ssa::target_features::{self, internal_target_features}; use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods}; -use rustc_codegen_ssa::{CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig}; +use rustc_codegen_ssa::{ + CachedModuleCodegen, CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig, +}; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::IntoDynSyncSend; use rustc_errors::{DiagCtxt, DiagCtxtHandle}; -use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct}; +use rustc_middle::dep_graph::IncrCompSession; use rustc_middle::ty::TyCtxt; -use rustc_session::config::{OptLevel, OutputFilenames}; +use rustc_session::config::OptLevel; use rustc_session::{CodegenBackendInit, EarlySession, Session}; use rustc_span::{Symbol, sym}; use rustc_target::spec::{RelocModel, TargetTuple}; @@ -322,13 +324,24 @@ impl CodegenBackend for GccCodegenBackend { ongoing_codegen: Box, sess: &Session, incr_comp_session: Option<&mut IncrCompSession>, - _outputs: &OutputFilenames, - crate_info: &CrateInfo, - ) -> CompiledModules { + ) -> Box { ongoing_codegen .downcast::>() .expect("Expected GccCodegenBackend's OngoingCodegen, found Box") - .join(sess, incr_comp_session, crate_info) + .join(sess, incr_comp_session) + } + + fn perform_lto( + &self, + pending_lto: Box, + sess: &Session, + incr_comp_session: Option<&mut IncrCompSession>, + crate_info: &CrateInfo, + ) -> CompiledModules { + pending_lto + .downcast::>() + .expect("Expected GccCodegenBackend's PendingLto, found Box") + .perform_lto(sess, incr_comp_session, crate_info) } } @@ -436,14 +449,14 @@ impl WriteBackendMethods for GccCodegenBackend { fn run_thin_lto( _cgcx: &CodegenContext, - _prof: &SelfProfilerRef, - _incr_comp_session: Option<&BorrowedIncrCompSession>, + _sess: &Session, + _incr_comp_session: Option<&IncrCompSession>, _dcx: DiagCtxtHandle<'_>, // FIXME(bjorn3): Limit LTO exports to these symbols _exported_symbols_for_lto: &[String], _each_linked_rlib_for_lto: &[PathBuf], _modules: Vec>, - ) -> (Vec>, Vec) { + ) -> (Vec>, Vec) { unreachable!() } diff --git a/compiler/rustc_codegen_llvm/Cargo.toml b/compiler/rustc_codegen_llvm/Cargo.toml index ce6731d4ef701..b70802b1d8a83 100644 --- a/compiler/rustc_codegen_llvm/Cargo.toml +++ b/compiler/rustc_codegen_llvm/Cargo.toml @@ -28,6 +28,7 @@ rustc_errors = { path = "../rustc_errors" } rustc_fs_util = { path = "../rustc_fs_util" } rustc_hashes = { path = "../rustc_hashes" } rustc_hir = { path = "../rustc_hir" } +rustc_incremental = { path = "../rustc_incremental" } rustc_index = { path = "../rustc_index" } rustc_lint_defs = { path = "../rustc_lint_defs" } rustc_llvm = { path = "../rustc_llvm" } diff --git a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs index b4fcb1ced00a5..fedb2d57aa166 100644 --- a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs +++ b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs @@ -17,12 +17,13 @@ use rustc_codegen_ssa::back::write::{ }; use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods}; use rustc_codegen_ssa::{ - CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig, target_features, + CachedModuleCodegen, CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, TargetConfig, + target_features, }; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_errors::{DiagCtxt, DiagCtxtHandle}; use rustc_metadata::EncodedMetadata; -use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct}; +use rustc_middle::dep_graph::IncrCompSession; use rustc_middle::ty::TyCtxt; use rustc_session::config::{LtoCli, OptLevel, OutputFilenames, PrintKind, PrintRequest}; use rustc_session::{CodegenBackendInit, EarlySession, Session}; @@ -119,16 +120,16 @@ impl WriteBackendMethods for LlvmCodegenBackend { } fn run_thin_lto( cgcx: &CodegenContext, - prof: &SelfProfilerRef, - incr_comp_session: Option<&BorrowedIncrCompSession>, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, dcx: DiagCtxtHandle<'_>, exported_symbols_for_lto: &[String], each_linked_rlib_for_lto: &[PathBuf], modules: Vec>, - ) -> (Vec>, Vec) { + ) -> (Vec>, Vec) { back::lto::run_thin( cgcx, - prof, + sess, incr_comp_session, dcx, exported_symbols_for_lto, @@ -409,14 +410,27 @@ impl CodegenBackend for LlvmCodegenBackend { ongoing_codegen: Box, sess: &Session, incr_comp_session: Option<&mut IncrCompSession>, - outputs: &OutputFilenames, - crate_info: &CrateInfo, - ) -> CompiledModules { - let compiled_modules = ongoing_codegen + ) -> Box { + ongoing_codegen .downcast::>() .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box") - .join(sess, incr_comp_session, crate_info); + .join(sess, incr_comp_session) + } + + fn perform_lto( + &self, + ongoing_codegen: Box, + sess: &Session, + incr_comp_session: Option<&mut IncrCompSession>, + crate_info: &CrateInfo, + ) -> CompiledModules { + ongoing_codegen + .downcast::>() + .expect("Expected LlvmCodegenBackend's PendingLto, found Box") + .perform_lto(sess, incr_comp_session, crate_info) + } + fn post_codegen_metrics(&self, sess: &Session, outputs: &OutputFilenames) { if sess.opts.unstable_opts.llvm_time_trace { sess.time("llvm_dump_timing_file", || { let file_name = outputs.with_extension("llvm_timings.json"); @@ -451,8 +465,6 @@ impl CodegenBackend for LlvmCodegenBackend { )); } } - - compiled_modules } fn link( diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 1ab2777469e46..f88569286039a 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -13,13 +13,14 @@ use rustc_codegen_ssa::back::write::{ CodegenContext, FatLtoInput, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput, }; use rustc_codegen_ssa::traits::*; -use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind}; +use rustc_codegen_ssa::{CachedModuleCodegen, CompiledModule, ModuleCodegen, ModuleKind}; 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::{BorrowedIncrCompSession, WorkProduct}; -use rustc_session::config; +use rustc_incremental::load_work_product_index; +use rustc_middle::dep_graph::{IncrCompSession, WorkProductId, WorkProductMap}; +use rustc_session::{Session, config}; use rustc_span::bug; use rustc_structures::SanitizerSet; use tracing::{debug, info}; @@ -185,13 +186,13 @@ pub(crate) fn run_fat( /// can simply be copied over from the incr. comp. cache. pub(crate) fn run_thin( cgcx: &CodegenContext, - prof: &SelfProfilerRef, - incr_comp_session: Option<&BorrowedIncrCompSession>, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, dcx: DiagCtxtHandle<'_>, exported_symbols_for_lto: &[String], each_linked_rlib_for_lto: &[PathBuf], modules: Vec>, -) -> (Vec>, Vec) { +) -> (Vec>, Vec) { let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, exported_symbols_for_lto, each_linked_rlib_for_lto, dcx); let symbols_below_threshold = @@ -202,7 +203,7 @@ pub(crate) fn run_thin( is deferred to the linker" ); } - thin_lto(prof, incr_comp_session, dcx, modules, upstream_modules, &symbols_below_threshold) + thin_lto(sess, incr_comp_session, dcx, modules, upstream_modules, &symbols_below_threshold) } fn fat_lto( @@ -369,22 +370,36 @@ fn fat_lto( /// all of the `LtoModuleCodegen` units returned below and destroyed once /// they all go out of scope. fn thin_lto( - prof: &SelfProfilerRef, - incr_comp_session: Option<&BorrowedIncrCompSession>, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, dcx: DiagCtxtHandle<'_>, modules: Vec>, serialized_modules: Vec<(SerializedModule, CString)>, symbols_below_threshold: &[*const libc::c_char], -) -> (Vec>, Vec) { - let _timer = prof.generic_activity("LLVM_thin_lto_global_analysis"); +) -> (Vec>, Vec) { + let _timer = sess.prof.generic_activity("LLVM_thin_lto_global_analysis"); unsafe { info!("going for that thin, thin LTO"); + // Try to load the previous session's dep graph and work products. + let prev_work_products = incr_comp_session.map(|incr_comp_session| { + load_work_product_index(sess, &incr_comp_session) + .unwrap_or_else(|()| WorkProductMap::default()) + }); + let green_modules: FxHashMap<_, _> = modules .iter() .filter_map(|module| { - if let ThinLtoInput::Green { wp, .. } = module { - Some((wp.cgu_name.clone(), wp.clone())) + if let ThinLtoInput::Green { name, .. } = module { + Some(( + name.clone(), + CachedModuleCodegen { + name: name.clone(), + source: prev_work_products.as_ref().unwrap() + [&WorkProductId::from_cgu_name(name)] + .clone(), + }, + )) } else { None } @@ -398,9 +413,9 @@ fn thin_lto( for (i, module) in modules.into_iter().enumerate() { let (name, buffer) = match module { - ThinLtoInput::Red { name, buffer } => (name, buffer), - ThinLtoInput::Green { wp, bitcode_path } => { - (wp.cgu_name, SerializedModule::from_file(&bitcode_path)) + ThinLtoInput::Red { name, path: _, buffer } => (name, buffer), + ThinLtoInput::Green { name: cgu_name, bitcode_path } => { + (cgu_name, SerializedModule::from_file(&bitcode_path)) } }; info!("local module: {} - {}", i, name); @@ -461,7 +476,7 @@ fn thin_lto( info!("thin LTO data created"); - let new_key_map_path = incr_comp_session.as_ref().map(|incr_comp_session| { + let new_key_map_path = incr_comp_session.map(|incr_comp_session| { incr_comp_session.new_session_directory.join(THIN_LTO_KEYS_INCR_COMP_FILE_NAME) }); @@ -508,8 +523,7 @@ fn thin_lto( // If a module exists in both the current and the previous session, // and has the same LTO cache key in both sessions, then we can re-use it if prev_key_map.keys.get(module_name) == curr_key_map.keys.get(module_name) { - let work_product = green_modules[module_name].clone(); - copy_jobs.push(work_product); + copy_jobs.push(green_modules[module_name].clone()); info!(" - {}: re-used", module_name); continue; } diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index ca9927d09d67d..6fa212756daac 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::marker::PhantomData; use std::num::NonZero; use std::panic::AssertUnwindSafe; @@ -15,12 +16,10 @@ use rustc_errors::{ catch_fatal_errors, }; use rustc_fs_util::link_or_copy; -use rustc_incremental::{ - copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir_sess, in_old_incr_comp_dir_sess, -}; +use rustc_incremental::{copy_cgu_workproduct_to_incr_comp_cache_dir, in_old_incr_comp_dir_sess}; use rustc_macros::{Decodable, Encodable}; use rustc_metadata::fs::copy_to_stdout; -use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession, WorkProduct}; +use rustc_middle::dep_graph::{BorrowedIncrCompSession, IncrCompSession}; use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustc_session::config::{ @@ -354,18 +353,18 @@ pub struct CodegenContext { fn generate_thin_lto_work( cgcx: &CodegenContext, - prof: &SelfProfilerRef, - incr_comp_session: Option<&BorrowedIncrCompSession>, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, dcx: DiagCtxtHandle<'_>, exported_symbols_for_lto: &[String], each_linked_rlib_for_lto: &[PathBuf], needs_thin_lto: Vec>, ) -> Vec<(ThinLtoWorkItem, u64)> { - let _prof_timer = prof.generic_activity("codegen_thin_generate_lto_work"); + let _prof_timer = sess.prof.generic_activity("codegen_thin_generate_lto_work"); let (lto_modules, copy_jobs) = B::run_thin_lto( cgcx, - prof, + sess, incr_comp_session, dcx, exported_symbols_for_lto, @@ -378,12 +377,9 @@ fn generate_thin_lto_work( let cost = module.cost(); (ThinLtoWorkItem::ThinLto(module), cost) }) - .chain(copy_jobs.into_iter().map(|wp| { + .chain(copy_jobs.into_iter().map(|module| { ( - ThinLtoWorkItem::CopyPostLtoArtifacts(CachedModuleCodegen { - name: wp.cgu_name.clone(), - source: wp, - }), + ThinLtoWorkItem::CopyPostLtoArtifacts(module), 0, // copying is very cheap ) })) @@ -392,6 +388,10 @@ fn generate_thin_lto_work( enum MaybeLtoModules { NoLto(CompiledModules), + Lto(LtoModules), +} + +enum LtoModules { FatLto { cgcx: CodegenContext, needs_fat_lto: Vec>, @@ -764,7 +764,7 @@ pub(crate) enum WorkItemResult { /// The backend has finished compiling a CGU, which now needs to go through /// thin LTO. - NeedsThinLto(String, B::ModuleBuffer), + NeedsThinLto(String, Option, B::ModuleBuffer), } pub enum FatLtoInput { @@ -773,8 +773,16 @@ pub enum FatLtoInput { } pub enum ThinLtoInput { - Red { name: String, buffer: SerializedModule }, - Green { wp: WorkProduct, bitcode_path: PathBuf }, + Red { + name: String, + path: Option, + buffer: SerializedModule, + }, + Green { + /// Contains only post-LTO artifacts + name: String, + bitcode_path: PathBuf, + }, } /// Actual LTO type we end up choosing based on multiple factors. @@ -814,7 +822,6 @@ pub(crate) fn compute_per_cgu_lto_type( fn execute_optimize_work_item( cgcx: &CodegenContext, prof: &SelfProfilerRef, - new_incr_comp_session_dir: Option<&Path>, shared_emitter: SharedEmitter, mut module: ModuleCodegen, ) -> WorkItemResult { @@ -833,8 +840,7 @@ fn execute_optimize_work_item( // If we're doing some form of incremental LTO then we need to be sure to // save our module to disk first. let bitcode = if cgcx.module_config.emit_pre_lto_bc { - let filename = pre_lto_bitcode_filename(&module.name); - new_incr_comp_session_dir.map(|path| path.join(&filename)) + Some(cgcx.output_filenames.temp_path_ext_for_cgu(PRE_LTO_BC_EXT, &module.name)) } else { None }; @@ -846,12 +852,12 @@ fn execute_optimize_work_item( } ComputedLtoType::Thin => { let thin_buffer = B::serialize_module(module.module_llvm, true); - if let Some(path) = bitcode { + if let Some(path) = &bitcode { fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| { panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e); }); } - WorkItemResult::NeedsThinLto(module.name, thin_buffer) + WorkItemResult::NeedsThinLto(module.name, bitcode, thin_buffer) } ComputedLtoType::Fat => match bitcode { Some(path) => { @@ -986,8 +992,8 @@ fn do_fat_lto( fn do_thin_lto( cgcx: &CodegenContext, - prof: &SelfProfilerRef, - incr_comp_session: Option, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, shared_emitter: SharedEmitter, tm_factory: TargetMachineFactoryFn, exported_symbols_for_lto: &[String], @@ -995,7 +1001,7 @@ fn do_thin_lto( needs_thin_lto: Vec>, stack_size: usize, ) -> Vec { - let _timer = prof.verbose_generic_activity("LLVM_thinlto"); + let _timer = sess.prof.verbose_generic_activity("LLVM_thinlto"); let dcx = DiagCtxt::new(Box::new(shared_emitter.clone())); let dcx = dcx.handle(); @@ -1030,8 +1036,8 @@ fn do_thin_lto( // we don't worry about tokens. for (i, (work, cost)) in generate_thin_lto_work::( cgcx, - prof, - incr_comp_session.as_ref(), + sess, + incr_comp_session, dcx, &exported_symbols_for_lto, &each_linked_rlib_for_lto, @@ -1080,7 +1086,7 @@ fn do_thin_lto( { spawn_thin_lto_work( &cgcx, - prof, + &sess.prof, incr_comp_session .as_ref() .and_then(|incr_comp_session| { @@ -1169,7 +1175,7 @@ pub(crate) enum Message { /// Similar to `CodegenDone`, but for reusing a pre-LTO artifact /// Sent from the main thread. - AddImportOnlyModule { bitcode_path: PathBuf, work_product: WorkProduct }, + AddImportOnlyModule { bitcode_path: PathBuf, name: String }, /// The frontend has finished generating everything for all codegen units. /// Sent from the main thread. @@ -1681,10 +1687,11 @@ fn start_executing_work( assert!(needs_thin_lto.is_empty()); needs_fat_lto.push(fat_lto_input); } - Ok(WorkItemResult::NeedsThinLto(name, thin_buffer)) => { + Ok(WorkItemResult::NeedsThinLto(name, path, thin_buffer)) => { assert!(needs_fat_lto.is_empty()); needs_thin_lto.push(ThinLtoInput::Red { name, + path, buffer: SerializedModule::Local(thin_buffer), }); } @@ -1700,10 +1707,10 @@ fn start_executing_work( } } - Message::AddImportOnlyModule { bitcode_path, work_product } => { + Message::AddImportOnlyModule { bitcode_path, name } => { assert_eq!(codegen_state, Ongoing); assert_eq!(main_thread_state, MainThreadState::Codegenning); - lto_import_only_modules.push((bitcode_path, work_product)); + lto_import_only_modules.push((bitcode_path, name)); main_thread_state = MainThreadState::Idle; } } @@ -1729,17 +1736,17 @@ fn start_executing_work( needs_fat_lto.push(FatLtoInput::InMemory(allocator_module)); } - for (bitcode_path, wp) in lto_import_only_modules { - needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, bitcode_path }) + for (bitcode_path, name) in lto_import_only_modules { + needs_fat_lto.push(FatLtoInput::Serialized { name, bitcode_path }) } - return Ok(MaybeLtoModules::FatLto { cgcx, needs_fat_lto }); + return Ok(MaybeLtoModules::Lto(LtoModules::FatLto { cgcx, needs_fat_lto })); } else if !needs_thin_lto.is_empty() || !lto_import_only_modules.is_empty() { assert!(compiled_modules.is_empty()); assert!(needs_fat_lto.is_empty()); - for (bitcode_path, wp) in lto_import_only_modules { - needs_thin_lto.push(ThinLtoInput::Green { wp, bitcode_path }) + for (bitcode_path, name) in lto_import_only_modules { + needs_thin_lto.push(ThinLtoInput::Green { name, bitcode_path }) } let allocator_module = if let Some(allocator_module) = allocator_module { @@ -1755,6 +1762,7 @@ fn start_executing_work( let thin_buffer = B::serialize_module(allocator_module.module_llvm, true); needs_thin_lto.push(ThinLtoInput::Red { name: allocator_module.name, + path: None, buffer: SerializedModule::Local(thin_buffer), }); @@ -1764,7 +1772,11 @@ fn start_executing_work( None }; - return Ok(MaybeLtoModules::ThinLto { cgcx, needs_thin_lto, allocator_module }); + return Ok(MaybeLtoModules::Lto(LtoModules::ThinLto { + cgcx, + needs_thin_lto, + allocator_module, + })); } Ok(MaybeLtoModules::NoLto(CompiledModules { @@ -1857,8 +1869,6 @@ fn spawn_work<'a, B: WriteBackendMethods>( let old_incr_comp_session_dir = incr_comp_session .and_then(|incr_comp_session| incr_comp_session.old_session_directory.clone()); - let new_incr_comp_session_dir = - incr_comp_session.map(|incr_comp_session| incr_comp_session.new_session_directory.clone()); let cgcx = cgcx.clone(); let prof = prof.clone(); @@ -1868,13 +1878,7 @@ fn spawn_work<'a, B: WriteBackendMethods>( let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) }; let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work { - WorkItem::Optimize(m) => execute_optimize_work_item( - &cgcx, - &prof, - new_incr_comp_session_dir.as_deref(), - shared_emitter, - m, - ), + WorkItem::Optimize(m) => execute_optimize_work_item(&cgcx, &prof, shared_emitter, m), WorkItem::CopyPostLtoArtifacts(m) => { WorkItemResult::Finished(execute_copy_from_cache_work_item( &cgcx, @@ -2111,8 +2115,7 @@ impl OngoingCodegen { self, sess: &Session, incr_comp_session: Option<&mut IncrCompSession>, - crate_info: &CrateInfo, - ) -> CompiledModules { + ) -> Box { self.shared_emitter_main.check(sess, true); let maybe_lto_modules = sess.time("join_worker_thread", || match self.coordinator.join() { @@ -2128,13 +2131,9 @@ impl OngoingCodegen { sess.dcx().abort_if_errors(); - let (shared_emitter, shared_emitter_main) = SharedEmitter::new(); - // Catch fatal errors to ensure shared_emitter_main.check() can emit the actual diagnostics - let compiled_modules = catch_fatal_errors(|| match maybe_lto_modules { - MaybeLtoModules::NoLto(compiled_modules) => { - drop(shared_emitter); - + match maybe_lto_modules { + MaybeLtoModules::NoLto(mut compiled_modules) => { if let Some(incr_comp_session) = incr_comp_session { copy_all_cgu_workproducts_to_incr_comp_cache_dir( sess, @@ -2143,12 +2142,99 @@ impl OngoingCodegen { ); } - compiled_modules + // 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 + // out deterministic results. + compiled_modules.modules.sort_by(|a, b| a.name.cmp(&b.name)); + + produce_final_output_artifacts(sess, &compiled_modules, &self.output_filenames); + + Box::new(compiled_modules) as Box } - MaybeLtoModules::FatLto { cgcx, needs_fat_lto } => { - let tm_factory = self.backend.target_machine_factory(sess); + MaybeLtoModules::Lto(lto_modules) => { + match <o_modules { + LtoModules::FatLto { cgcx: _, needs_fat_lto: _ } => { + // FIXME cache local pre-LTO bitcode + } + LtoModules::ThinLto { cgcx: _, needs_thin_lto, allocator_module: _ } => { + if let Some(incr_comp_session) = incr_comp_session { + for module in needs_thin_lto { + match module { + ThinLtoInput::Green { name, bitcode_path } => { + copy_cgu_workproduct_to_incr_comp_cache_dir( + sess, + incr_comp_session, + name, + &[(PRE_LTO_BC_EXT, bitcode_path)], + ); + } + ThinLtoInput::Red { name, path, buffer: _ } => { + if let Some(path) = path { + copy_cgu_workproduct_to_incr_comp_cache_dir( + sess, + incr_comp_session, + name, + &[(PRE_LTO_BC_EXT, path)], + ); + } + } + } + } + } + } + } + + Box::new(PendingLto { + backend: self.backend, + output_filenames: self.output_filenames, + lto_modules, + }) + } + } + } + + pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) { + self.wait_for_signal_to_codegen_item(); + self.check_for_errors(tcx.sess); + drop(self.coordinator.sender.send(Message::CodegenComplete::)); + } - // FIXME cache local pre-LTO bitcode + pub(crate) fn check_for_errors(&self, sess: &Session) { + self.shared_emitter_main.check(sess, false); + } + + pub(crate) fn wait_for_signal_to_codegen_item(&self) { + match self.codegen_worker_receive.recv() { + Ok(CguMessage) => { + // Ok to proceed. + } + Err(_) => { + // One of the LLVM threads must have panicked, fall through so + // error handling can be reached. + } + } + } +} + +pub struct PendingLto { + backend: B, + output_filenames: Arc, + lto_modules: LtoModules, +} + +impl PendingLto { + pub fn perform_lto( + self, + sess: &Session, + incr_comp_session: Option<&mut IncrCompSession>, + crate_info: &CrateInfo, + ) -> CompiledModules { + let (shared_emitter, shared_emitter_main) = SharedEmitter::new(); + + // Catch fatal errors to ensure shared_emitter_main.check() can emit the actual diagnostics + let compiled_modules = catch_fatal_errors(|| match self.lto_modules { + LtoModules::FatLto { cgcx, needs_fat_lto } => { + let tm_factory = self.backend.target_machine_factory(sess); CompiledModules { modules: vec![do_fat_lto( @@ -2163,14 +2249,14 @@ impl OngoingCodegen { allocator_module: None, } } - MaybeLtoModules::ThinLto { cgcx, needs_thin_lto, allocator_module } => { + LtoModules::ThinLto { cgcx, needs_thin_lto, allocator_module } => { let tm_factory = self.backend.target_machine_factory(sess); let compiled_modules = CompiledModules { modules: do_thin_lto::( &cgcx, - &sess.prof, - incr_comp_session.as_deref().map(IncrCompSession::borrow), + &sess, + incr_comp_session.as_deref(), shared_emitter, tm_factory, &crate_info.exported_symbols_for_lto, @@ -2181,8 +2267,6 @@ impl OngoingCodegen { allocator_module, }; - // FIXME include pre-LTO bitcode in workproduct tracking - // FIXME add separate incr comp session for post-LTO outputs to use during link step if let Some(incr_comp_session) = incr_comp_session { copy_all_cgu_workproducts_to_incr_comp_cache_dir( sess, @@ -2211,28 +2295,6 @@ impl OngoingCodegen { compiled_modules } - - pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) { - self.wait_for_signal_to_codegen_item(); - self.check_for_errors(tcx.sess); - drop(self.coordinator.sender.send(Message::CodegenComplete::)); - } - - pub(crate) fn check_for_errors(&self, sess: &Session) { - self.shared_emitter_main.check(sess, false); - } - - pub(crate) fn wait_for_signal_to_codegen_item(&self) { - match self.codegen_worker_receive.recv() { - Ok(CguMessage) => { - // Ok to proceed. - } - Err(_) => { - // One of the LLVM threads must have panicked, fall through so - // error handling can be reached. - } - } - } } pub(crate) fn submit_codegened_module_to_llvm( @@ -2254,13 +2316,14 @@ pub(crate) fn submit_post_lto_module_to_llvm( pub(crate) fn submit_pre_lto_module_to_llvm( sess: &Session, + output_filenames: &OutputFilenames, incr_comp_session: &IncrCompSession, coordinator: &Coordinator, module: CachedModuleCodegen, ) { let filename = pre_lto_bitcode_filename(&module.name); 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); + let bitcode_path = output_filenames.temp_path_ext_for_cgu(PRE_LTO_BC_EXT, &module.name); match link_or_copy(&old_bitcode_path, &bitcode_path, false /* allow_overwrite */) { Ok(_) => {} @@ -2278,7 +2341,7 @@ pub(crate) fn submit_pre_lto_module_to_llvm( drop( coordinator .sender - .send(Message::AddImportOnlyModule:: { bitcode_path, work_product: module.source }), + .send(Message::AddImportOnlyModule:: { bitcode_path, name: module.name }), ); } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 1dffc5fab8689..30bfef7943076 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -765,6 +765,7 @@ pub fn codegen_crate< let regular_module_config = ModuleConfig::new(ModuleKind::Regular, tcx, no_builtins); let bitcode_needed = regular_module_config.bitcode_needed(); let allocator_module_config = ModuleConfig::new(ModuleKind::Allocator, tcx, no_builtins); + let output_filenames = tcx.output_filenames(()); let ongoing_codegen = start_async_codegen( backend.clone(), @@ -871,6 +872,7 @@ pub fn codegen_crate< CguReuse::PreLto => { submit_pre_lto_module_to_llvm( tcx.sess, + output_filenames, tcx.incr_comp_session.unwrap(), &ongoing_codegen.coordinator, CachedModuleCodegen { diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index bd4f3892071c9..456b523e53990 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -137,7 +137,8 @@ impl CompiledModule { } } -pub(crate) struct CachedModuleCodegen { +#[derive(Clone)] +pub struct CachedModuleCodegen { pub name: String, pub source: WorkProduct, } diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index 2f28f86333230..847e84ef89891 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -103,20 +103,25 @@ pub trait CodegenBackend { fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box; - /// This is called on the returned `Box` from [`codegen_crate`](Self::codegen_crate) - /// - /// # Panics - /// - /// Panics when the passed `Box` was not returned by [`codegen_crate`](Self::codegen_crate). + /// This is called on the returned `Box` from [`codegen_crate`](Self::codegen_crate). fn join_codegen( &self, ongoing_codegen: Box, sess: &Session, incr_comp_session: Option<&mut IncrCompSession>, - outputs: &OutputFilenames, + ) -> Box; + + /// This is called on the returned `Box` from [`join_codegen`](Self::join_codegen). + fn perform_lto( + &self, + pending_lto: Box, + sess: &Session, + incr_comp_session: Option<&mut IncrCompSession>, crate_info: &CrateInfo, ) -> CompiledModules; + fn post_codegen_metrics(&self, _sess: &Session, _outputs: &OutputFilenames) {} + /// This is called on the returned [`CompiledModules`] from [`join_codegen`](Self::join_codegen). fn link( &self, diff --git a/compiler/rustc_codegen_ssa/src/traits/write.rs b/compiler/rustc_codegen_ssa/src/traits/write.rs index d2ee9fad4c18f..154a821328aa6 100644 --- a/compiler/rustc_codegen_ssa/src/traits/write.rs +++ b/compiler/rustc_codegen_ssa/src/traits/write.rs @@ -4,14 +4,14 @@ use std::path::PathBuf; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_errors::DiagCtxtHandle; -use rustc_middle::dep_graph::{BorrowedIncrCompSession, WorkProduct}; +use rustc_middle::dep_graph::IncrCompSession; use rustc_session::Session; use crate::back::lto::ThinModule; use crate::back::write::{ CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn, ThinLtoInput, }; -use crate::{CompiledModule, ModuleCodegen}; +use crate::{CachedModuleCodegen, CompiledModule, ModuleCodegen}; pub trait WriteBackendMethods: Clone + 'static { type Module: Send; @@ -45,13 +45,13 @@ pub trait WriteBackendMethods: Clone + 'static { /// can simply be copied over from the incr. comp. cache. fn run_thin_lto( cgcx: &CodegenContext, - prof: &SelfProfilerRef, - incr_comp_session: Option<&BorrowedIncrCompSession>, + sess: &Session, + incr_comp_session: Option<&IncrCompSession>, dcx: DiagCtxtHandle<'_>, exported_symbols_for_lto: &[String], each_linked_rlib_for_lto: &[PathBuf], modules: Vec>, - ) -> (Vec>, Vec); + ) -> (Vec>, Vec); fn optimize( cgcx: &CodegenContext, prof: &SelfProfilerRef, diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index 92603d6ad3f07..a3a212e6afd40 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -12,7 +12,8 @@ mod persist; pub use persist::{ copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir_sess, - in_old_incr_comp_dir_sess, load_query_result_cache, setup_dep_graph, + in_old_incr_comp_dir_sess, load_query_result_cache, load_work_product_index, + prepare_session_directory, setup_dep_graph, }; use rustc_middle::util::Providers; diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index 11d45339c16b1..fd97bc97ee427 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -111,7 +111,7 @@ use rustc_middle::dep_graph::{IncrCompSession, WorkProduct}; use rustc_session::output::OutputType; use rustc_session::{Session, StableCrateId}; use rustc_span::{Symbol, bug}; -use tracing::debug; +use tracing::{debug, warn}; use crate::diagnostics; @@ -218,10 +218,11 @@ pub fn in_incr_comp_dir_sess(incr_comp_session: &IncrCompSession, file_name: &st /// The garbage collection will take care of it. /// /// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph -pub(crate) fn prepare_session_directory( +pub fn prepare_session_directory( sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId, + suffix: &'static str, ) -> IncrCompSession { assert!(sess.opts.incremental.is_some()); @@ -229,8 +230,8 @@ pub(crate) fn prepare_session_directory( debug!("prepare_session_directory"); - // {incr-comp-dir}/{crate-name-and-disambiguator} - let crate_dir = crate_path(sess, crate_name, stable_crate_id); + // {incr-comp-dir}/{crate-name}-{disambiguator}-{suffix} + let crate_dir = crate_path(sess, crate_name, stable_crate_id, suffix); debug!("crate-dir: {}", crate_dir.display()); create_dir(sess, &crate_dir, "crate"); @@ -247,7 +248,7 @@ pub(crate) fn prepare_session_directory( // Generate a session directory of the form: // - // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working + // {incr-comp-dir}/{crate-name}-{disambiguator}-{prefix}/s-{timestamp}-{random}-{suffix}-working let new_session_dir = generate_session_dir_path(&crate_dir); debug!("session-dir: {}", new_session_dir.display()); @@ -268,7 +269,18 @@ pub(crate) fn prepare_session_directory( None }; - IncrCompSession::new(old_session_directory, new_session_directory) + let incr_comp_session = IncrCompSession::new(old_session_directory, new_session_directory); + + sess.time("incr_comp_garbage_collect_session_directories", || { + if let Err(e) = garbage_collect_session_directories(sess, &incr_comp_session) { + warn!( + "Error while trying to garbage collect incremental compilation \ + cache directory: {e}", + ); + } + }); + + incr_comp_session } /// This function finalizes and thus 'publishes' the session directory by @@ -536,11 +548,16 @@ fn string_to_timestamp(s: &str) -> Result { Ok(UNIX_EPOCH + duration) } -fn crate_path(sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId) -> PathBuf { +fn crate_path( + sess: &Session, + crate_name: Symbol, + stable_crate_id: StableCrateId, + suffix: &str, +) -> PathBuf { let incr_dir = sess.opts.incremental.as_ref().unwrap().clone(); - let crate_name = - format!("{crate_name}-{}", stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE)); + let crate_id = stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE); + let crate_name = format!("{crate_name}-{crate_id}-{suffix}"); incr_dir.join(crate_name) } @@ -549,7 +566,7 @@ fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool { } /// Runs garbage collection for the current session. -pub(crate) fn garbage_collect_session_directories( +fn garbage_collect_session_directories( sess: &Session, incr_comp_session: &IncrCompSession, ) -> io::Result<()> { diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 63c950b27de08..c91a2c87f2f20 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -10,7 +10,7 @@ use rustc_serialize::{Decodable, Encodable}; use rustc_session::config::IncrementalStateAssertion; use rustc_session::{Session, StableCrateId}; use rustc_span::Symbol; -use tracing::{debug, warn}; +use tracing::debug; use super::file_format; use super::fs::*; @@ -75,7 +75,8 @@ fn load_dep_graph( } } -fn load_work_product_index( +/// Attempt to load the work product index from disk +pub fn load_work_product_index( sess: &Session, incr_comp_session: &IncrCompSession, ) -> Result { @@ -177,24 +178,16 @@ pub fn setup_dep_graph( } // `load_dep_graph` can only be called after `prepare_session_directory`. - let mut incr_comp_session = prepare_session_directory(sess, crate_name, stable_crate_id); + let mut incr_comp_session = + prepare_session_directory(sess, crate_name, stable_crate_id, "main"); // Try to load the previous session's dep graph and work products. let load_result = load_dep_graph(sess, &incr_comp_session); - sess.time("incr_comp_garbage_collect_session_directories", || { - if let Err(e) = garbage_collect_session_directories(sess, &incr_comp_session) { - warn!( - "Error while trying to garbage collect incremental compilation \ - cache directory: {e}", - ); - } - }); - // Emit a fatal error if `-Zassert-incr-state` is present and unsatisfied. maybe_assert_incr_state(sess, load_result.is_ok()); let (prev_graph, prev_work_products) = match load_result { - Ok((prev_graph, prev_work_products)) => (prev_graph, prev_work_products), + Ok(prev) => prev, Err(()) => { invalidate_old_session_dir(sess, &mut incr_comp_session); Default::default() diff --git a/compiler/rustc_incremental/src/persist/mod.rs b/compiler/rustc_incremental/src/persist/mod.rs index e101d8df8e2e7..0a6336256f63e 100644 --- a/compiler/rustc_incremental/src/persist/mod.rs +++ b/compiler/rustc_incremental/src/persist/mod.rs @@ -9,7 +9,10 @@ mod load; mod save; mod work_product; -pub use fs::{finalize_session_directory, in_incr_comp_dir_sess, in_old_incr_comp_dir_sess}; -pub use load::{load_query_result_cache, setup_dep_graph}; +pub use fs::{ + finalize_session_directory, in_incr_comp_dir_sess, in_old_incr_comp_dir_sess, + prepare_session_directory, +}; +pub use load::{load_query_result_cache, load_work_product_index, setup_dep_graph}; pub(crate) use save::save_dep_graph; pub use work_product::copy_cgu_workproduct_to_incr_comp_cache_dir; diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index ae5286705cc34..ac22cf340ed1c 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -6,12 +6,13 @@ use rustc_codegen_ssa::{CompiledModules, CrateInfo}; use rustc_data_structures::svh::Svh; use rustc_errors::timings::TimingSection; use rustc_hir::def_id::LOCAL_CRATE; +use rustc_incremental::prepare_session_directory; use rustc_metadata::EncodedMetadata; use rustc_middle::dep_graph::{DepGraph, IncrCompSession, WorkProduct}; use rustc_middle::ty::TyCtxt; -use rustc_session::Session; use rustc_session::config::{self, OutputFilenames}; use rustc_session::output::OutputType; +use rustc_session::{Session, StableCrateId}; use crate::diagnostics::FailedWritingFile; use crate::passes; @@ -19,6 +20,7 @@ use crate::passes; pub struct Linker { dep_graph: DepGraph, output_filenames: Arc, + local_crate_id: StableCrateId, // Only present when incr. comp. is enabled. crate_hash: Option, crate_info: CrateInfo, @@ -37,6 +39,7 @@ impl Linker { Linker { dep_graph: tcx.dep_graph.clone(), output_filenames: Arc::clone(tcx.output_filenames(())), + local_crate_id: tcx.stable_crate_id(LOCAL_CRATE), crate_hash: if tcx.sess.opts.incremental.is_some() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { @@ -54,18 +57,12 @@ impl Linker { mut incr_comp_session: Option, codegen_backend: &dyn CodegenBackend, ) { - let compiled_modules = sess.time("finish_ongoing_codegen", || { - match self.ongoing_codegen.downcast::() { + let maybe_pending_lto = sess.time("finish_ongoing_codegen", || { + if self.ongoing_codegen.is::() { // This was a check only build - Ok(compiled_modules) => *compiled_modules, - - Err(ongoing_codegen) => codegen_backend.join_codegen( - ongoing_codegen, - sess, - incr_comp_session.as_mut(), - &self.output_filenames, - &self.crate_info, - ), + self.ongoing_codegen + } else { + codegen_backend.join_codegen(self.ongoing_codegen, sess, incr_comp_session.as_mut()) } }); @@ -119,6 +116,45 @@ impl Linker { return; } + let compiled_modules = sess.time("maybe_perform_lto", || { + match maybe_pending_lto.downcast::() { + // This was a check only build + Ok(compiled_modules) => *compiled_modules, + + Err(pending_lto) => { + let mut lto_incr_comp_session = sess.opts.incremental.is_some().then(|| { + prepare_session_directory( + sess, + self.crate_info.local_crate_name, + self.local_crate_id, + "lto", + ) + }); + + let compiled_modules = codegen_backend.perform_lto( + pending_lto, + sess, + lto_incr_comp_session.as_mut(), + &self.crate_info, + ); + + if let Some(lto_incr_comp_session) = lto_incr_comp_session { + // 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( + sess, + lto_incr_comp_session, + self.crate_hash.unwrap(), + ); + } + + compiled_modules + } + } + }); + + codegen_backend.post_codegen_metrics(sess, &self.output_filenames); + if sess.opts.unstable_opts.no_link { let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT); CompiledModules::serialize_rlink( diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 5ae4adc311eae..7b8d9471029f2 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -482,13 +482,21 @@ impl CodegenBackend for DummyCodegenBackend { fn join_codegen( &self, - ongoing_codegen: Box, + _ongoing_codegen: Box, + _sess: &Session, + _incr_comp_session: Option<&mut IncrCompSession>, + ) -> Box { + unreachable!("Linker::link should skip join_codegen for CompiledModules") + } + + fn perform_lto( + &self, + _pending_lto: Box, _sess: &Session, _incr_comp_session: Option<&mut IncrCompSession>, - _outputs: &OutputFilenames, _crate_info: &CrateInfo, ) -> CompiledModules { - *ongoing_codegen.downcast().unwrap() + unreachable!("Linker::link should skip perform_lto for CompiledModules") } fn link( diff --git a/src/tools/miri/src/bin/miri.rs b/src/tools/miri/src/bin/miri.rs index 0336afe234583..adc68d642200b 100644 --- a/src/tools/miri/src/bin/miri.rs +++ b/src/tools/miri/src/bin/miri.rs @@ -260,10 +260,18 @@ impl CodegenBackend for MiriCodegenBackend { ongoing_codegen: Box, sess: &Session, incr_comp_session: Option<&mut rustc_middle::dep_graph::IncrCompSession>, - outputs: &rustc_session::config::OutputFilenames, + ) -> Box { + self.dummy.join_codegen(ongoing_codegen, sess, incr_comp_session) + } + + fn perform_lto( + &self, + pending_lto: Box, + sess: &Session, + incr_comp_session: Option<&mut rustc_middle::dep_graph::IncrCompSession>, crate_info: &CrateInfo, ) -> CompiledModules { - self.dummy.join_codegen(ongoing_codegen, sess, incr_comp_session, outputs, crate_info) + self.dummy.perform_lto(pending_lto, sess, incr_comp_session, crate_info) } fn link( diff --git a/tests/ui-fulldeps/codegen-backend/auxiliary/the_backend.rs b/tests/ui-fulldeps/codegen-backend/auxiliary/the_backend.rs index 5cfcb5df1cb56..5ebda8ee1bf65 100644 --- a/tests/ui-fulldeps/codegen-backend/auxiliary/the_backend.rs +++ b/tests/ui-fulldeps/codegen-backend/auxiliary/the_backend.rs @@ -40,14 +40,22 @@ impl CodegenBackend for TheBackend { ongoing_codegen: Box, _sess: &Session, _incr_comp_session: Option<&mut IncrCompSession>, - _outputs: &OutputFilenames, - _crate_info: &CrateInfo, - ) -> CompiledModules { - *ongoing_codegen + ) -> Box { + ongoing_codegen .downcast::() .expect("in join_codegen: ongoing_codegen is not a CompiledModules") } + fn perform_lto( + &self, + _pending_lto: Box, + _sess: &Session, + _incr_comp_session: Option<&mut IncrCompSession>, + _crate_info: &CrateInfo, + ) -> CompiledModules { + unreachable!() + } + fn link( &self, sess: &Session,