Add -Z combine_cgu
flag
Introduce a compiler option to let rustc combines all regular CGUs into a single one at the end of compilation. Part of Issue #64191
This commit is contained in:
parent
e82584a77d
commit
c81b43d8ac
6 changed files with 90 additions and 15 deletions
|
@ -346,14 +346,14 @@ fn fat_lto(
|
|||
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: serialized_bitcode })
|
||||
}
|
||||
|
||||
struct Linker<'a>(&'a mut llvm::Linker<'a>);
|
||||
crate struct Linker<'a>(&'a mut llvm::Linker<'a>);
|
||||
|
||||
impl Linker<'a> {
|
||||
fn new(llmod: &'a llvm::Module) -> Self {
|
||||
crate fn new(llmod: &'a llvm::Module) -> Self {
|
||||
unsafe { Linker(llvm::LLVMRustLinkerNew(llmod)) }
|
||||
}
|
||||
|
||||
fn add(&mut self, bytecode: &[u8]) -> Result<(), ()> {
|
||||
crate fn add(&mut self, bytecode: &[u8]) -> Result<(), ()> {
|
||||
unsafe {
|
||||
if llvm::LLVMRustLinkerAdd(
|
||||
self.0,
|
||||
|
|
|
@ -617,6 +617,31 @@ unsafe fn add_sanitizer_passes(config: &ModuleConfig, passes: &mut Vec<&'static
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn link(
|
||||
cgcx: &CodegenContext<LlvmCodegenBackend>,
|
||||
diag_handler: &Handler,
|
||||
mut modules: Vec<ModuleCodegen<ModuleLlvm>>,
|
||||
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
|
||||
use super::lto::{Linker, ModuleBuffer};
|
||||
// Sort the modules by name to ensure to ensure deterministic behavior.
|
||||
modules.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
let (first, elements) =
|
||||
modules.split_first().expect("Bug! modules must contain at least one module.");
|
||||
|
||||
let mut linker = Linker::new(first.module_llvm.llmod());
|
||||
for module in elements {
|
||||
let _timer =
|
||||
cgcx.prof.generic_activity_with_arg("LLVM_link_module", format!("{:?}", module.name));
|
||||
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
|
||||
linker.add(&buffer.data()).map_err(|()| {
|
||||
let msg = format!("failed to serialize module {:?}", module.name);
|
||||
llvm_err(&diag_handler, &msg)
|
||||
})?;
|
||||
}
|
||||
drop(linker);
|
||||
Ok(modules.remove(0))
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn codegen(
|
||||
cgcx: &CodegenContext<LlvmCodegenBackend>,
|
||||
diag_handler: &Handler,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue