diff --git a/compiler/rustc_codegen_gcc/src/back/lto.rs b/compiler/rustc_codegen_gcc/src/back/lto.rs index 401d4c244d5a0..6fd9cc96328ff 100644 --- a/compiler/rustc_codegen_gcc/src/back/lto.rs +++ b/compiler/rustc_codegen_gcc/src/back/lto.rs @@ -144,9 +144,12 @@ fn fat_lto( for module in modules { match module { FatLtoInput::InMemory(m) => in_memory.push(m), - FatLtoInput::Serialized { name, buffer } => { + FatLtoInput::Serialized { name, bitcode_path } => { info!("pushing serialized module {:?}", name); - serialized_modules.push((buffer, CString::new(name).unwrap())); + serialized_modules.push(( + SerializedModule::from_file(&bitcode_path), + CString::new(name).unwrap(), + )); } } } diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 09863961c9d69..63c09fe940279 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -223,9 +223,12 @@ fn fat_lto( for module in modules { match module { FatLtoInput::InMemory(m) => in_memory.push(m), - FatLtoInput::Serialized { name, buffer } => { + FatLtoInput::Serialized { name, bitcode_path } => { info!("pushing serialized module {:?}", name); - serialized_modules.push((buffer, CString::new(name).unwrap())); + serialized_modules.push(( + SerializedModule::from_file(&bitcode_path), + CString::new(name).unwrap(), + )); } } } @@ -396,7 +399,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, buffer } => (wp.cgu_name, buffer), + ThinLtoInput::Green { wp, bitcode_path } => { + (wp.cgu_name, SerializedModule::from_file(&bitcode_path)) + } }; info!("local module: {} - {}", i, name); let cname = CString::new(name.as_bytes()).unwrap(); diff --git a/compiler/rustc_codegen_ssa/src/back/lto.rs b/compiler/rustc_codegen_ssa/src/back/lto.rs index cd380abb75e0e..ddfcd8a85f6b8 100644 --- a/compiler/rustc_codegen_ssa/src/back/lto.rs +++ b/compiler/rustc_codegen_ssa/src/back/lto.rs @@ -1,4 +1,6 @@ use std::ffi::CString; +use std::fs; +use std::path::Path; use std::sync::Arc; use rustc_data_structures::memmap::Mmap; @@ -49,6 +51,19 @@ pub enum SerializedModule { } impl SerializedModule { + pub fn from_file(bc_path: &Path) -> Self { + let file = fs::File::open(&bc_path).unwrap_or_else(|e| { + panic!("failed to open LTO bitcode file `{}`: {}", bc_path.display(), e) + }); + + let mmap = unsafe { + Mmap::map(file).unwrap_or_else(|e| { + panic!("failed to mmap LTO bitcode file `{}`: {}", bc_path.display(), e) + }) + }; + SerializedModule::FromUncompressedFile(mmap) + } + pub fn data(&self) -> &[u8] { match *self { SerializedModule::Local(ref m) => m.data(), diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 82d7e23602e25..5601a950fbdb5 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -8,7 +8,6 @@ use std::{assert_matches, fs, io, mem, str, thread}; use rustc_abi::Size; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::jobserver::{self, Acquired}; -use rustc_data_structures::memmap::Mmap; use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard}; use rustc_errors::emitter::Emitter; use rustc_errors::{ @@ -35,9 +34,8 @@ use rustc_span::{FileName, InnerSpan, Span, SpanData}; use rustc_target::spec::{MergeFunctions, SanitizerSet}; use tracing::debug; -use super::link::{self, ensure_removed}; -use super::lto::{self, SerializedModule}; -use crate::back::lto::check_lto_allowed; +use crate::back::link::{self, ensure_removed}; +use crate::back::lto::{self, SerializedModule, check_lto_allowed}; use crate::errors::ErrorCreatingRemarkDir; use crate::traits::*; use crate::{ @@ -774,13 +772,13 @@ pub(crate) enum WorkItemResult { } pub enum FatLtoInput { - Serialized { name: String, buffer: SerializedModule }, + Serialized { name: String, bitcode_path: PathBuf }, InMemory(ModuleCodegen), } pub enum ThinLtoInput { Red { name: String, buffer: SerializedModule }, - Green { wp: WorkProduct, buffer: SerializedModule }, + Green { wp: WorkProduct, bitcode_path: PathBuf }, } /// Actual LTO type we end up choosing based on multiple factors. @@ -866,7 +864,7 @@ fn execute_optimize_work_item( }); WorkItemResult::NeedsFatLto(FatLtoInput::Serialized { name: module.name, - buffer: SerializedModule::Local(buffer), + bitcode_path: path, }) } None => WorkItemResult::NeedsFatLto(FatLtoInput::InMemory(module)), @@ -1166,10 +1164,7 @@ pub(crate) enum Message { /// Similar to `CodegenDone`, but for reusing a pre-LTO artifact /// Sent from the main thread. - AddImportOnlyModule { - module_data: SerializedModule, - work_product: WorkProduct, - }, + AddImportOnlyModule { bitcode_path: PathBuf, work_product: WorkProduct }, /// The frontend has finished generating everything for all codegen units. /// Sent from the main thread. @@ -1729,10 +1724,10 @@ fn start_executing_work( } } - Message::AddImportOnlyModule { module_data, work_product } => { + Message::AddImportOnlyModule { bitcode_path, work_product } => { assert_eq!(codegen_state, Ongoing); assert_eq!(main_thread_state, MainThreadState::Codegenning); - lto_import_only_modules.push((module_data, work_product)); + lto_import_only_modules.push((bitcode_path, work_product)); main_thread_state = MainThreadState::Idle; } } @@ -1758,8 +1753,8 @@ fn start_executing_work( needs_fat_lto.push(FatLtoInput::InMemory(allocator_module)); } - for (module, wp) in lto_import_only_modules { - needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, buffer: module }) + for (bitcode_path, wp) in lto_import_only_modules { + needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, bitcode_path }) } return Ok(MaybeLtoModules::FatLto { @@ -1772,8 +1767,8 @@ fn start_executing_work( assert!(compiled_modules.is_empty()); assert!(needs_fat_lto.is_empty()); - for (buffer, wp) in lto_import_only_modules { - needs_thin_lto.push(ThinLtoInput::Green { wp, buffer }) + for (bitcode_path, wp) in lto_import_only_modules { + needs_thin_lto.push(ThinLtoInput::Green { wp, bitcode_path }) } if cgcx.lto == Lto::ThinLocal { @@ -2133,14 +2128,14 @@ impl Drop for Coordinator { } pub struct OngoingCodegen { - pub backend: B, - pub output_filenames: Arc, + backend: B, + output_filenames: Arc, // Field order below is intended to terminate the coordinator thread before two fields below // drop and prematurely close channels used by coordinator thread. See `Coordinator`'s // `Drop` implementation for more info. - pub coordinator: Coordinator, - pub codegen_worker_receive: Receiver, - pub shared_emitter_main: SharedEmitterMain, + pub(crate) coordinator: Coordinator, + codegen_worker_receive: Receiver, + shared_emitter_main: SharedEmitterMain, } impl OngoingCodegen { @@ -2285,20 +2280,13 @@ pub(crate) fn submit_pre_lto_module_to_llvm( module: CachedModuleCodegen, ) { let filename = pre_lto_bitcode_filename(&module.name); - let bc_path = in_incr_comp_dir_sess(tcx.sess, &filename); - let file = fs::File::open(&bc_path) - .unwrap_or_else(|e| panic!("failed to open bitcode file `{}`: {}", bc_path.display(), e)); - - let mmap = unsafe { - Mmap::map(file).unwrap_or_else(|e| { - panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e) - }) - }; + let bitcode_path = in_incr_comp_dir_sess(tcx.sess, &filename); // Schedule the module to be loaded - drop(coordinator.sender.send(Message::AddImportOnlyModule:: { - module_data: SerializedModule::FromUncompressedFile(mmap), - work_product: module.source, - })); + drop( + coordinator + .sender + .send(Message::AddImportOnlyModule:: { bitcode_path, work_product: module.source }), + ); } fn pre_lto_bitcode_filename(module_name: &str) -> String {