Skip to content

Commit 2cf94b9

Browse files
committed
Ensure fat LTO doesn't merge everything into the allocator module
1 parent 9239d14 commit 2cf94b9

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use cranelift_object::{ObjectBuilder, ObjectModule};
1212
use rustc_codegen_ssa::assert_module_sources::CguReuse;
1313
use rustc_codegen_ssa::back::link::ensure_removed;
1414
use rustc_codegen_ssa::base::determine_cgu_reuse;
15-
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, errors as ssa_errors};
15+
use rustc_codegen_ssa::{
16+
CodegenResults, CompiledModule, CrateInfo, ModuleKind, errors as ssa_errors,
17+
};
1618
use rustc_data_structures::profiling::SelfProfilerRef;
1719
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1820
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
@@ -361,6 +363,7 @@ fn emit_cgu(
361363
invocation_temp,
362364
prof,
363365
product.object,
366+
ModuleKind::Regular,
364367
name.clone(),
365368
producer,
366369
)?;
@@ -369,6 +372,7 @@ fn emit_cgu(
369372
module_regular,
370373
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
371374
name: format!("{name}.asm"),
375+
kind: ModuleKind::Regular,
372376
object: Some(global_asm_object_file),
373377
dwarf_object: None,
374378
bytecode: None,
@@ -385,6 +389,7 @@ fn emit_module(
385389
invocation_temp: Option<&str>,
386390
prof: &SelfProfilerRef,
387391
mut object: cranelift_object::object::write::Object<'_>,
392+
kind: ModuleKind,
388393
name: String,
389394
producer_str: &str,
390395
) -> Result<CompiledModule, String> {
@@ -425,6 +430,7 @@ fn emit_module(
425430

426431
Ok(CompiledModule {
427432
name,
433+
kind,
428434
object: Some(tmp_file),
429435
dwarf_object: None,
430436
bytecode: None,
@@ -479,6 +485,7 @@ fn reuse_workproduct_for_cgu(
479485
Ok(ModuleCodegenResult {
480486
module_regular: CompiledModule {
481487
name: cgu.name().to_string(),
488+
kind: ModuleKind::Regular,
482489
object: Some(obj_out_regular),
483490
dwarf_object: None,
484491
bytecode: None,
@@ -488,6 +495,7 @@ fn reuse_workproduct_for_cgu(
488495
},
489496
module_global_asm: source_file_global_asm.map(|source_file| CompiledModule {
490497
name: cgu.name().to_string(),
498+
kind: ModuleKind::Regular,
491499
object: Some(obj_out_global_asm),
492500
dwarf_object: None,
493501
bytecode: None,
@@ -643,6 +651,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
643651
tcx.sess.invocation_temp.as_deref(),
644652
&tcx.sess.prof,
645653
product.object,
654+
ModuleKind::Allocator,
646655
"allocator_shim".to_owned(),
647656
&crate::debuginfo::producer(tcx.sess),
648657
) {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use object::{Object, ObjectSection};
1111
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
1212
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
1313
use rustc_codegen_ssa::traits::*;
14-
use rustc_codegen_ssa::{ModuleCodegen, looks_like_rust_object_file};
14+
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::memmap::Mmap;
1717
use rustc_errors::DiagCtxtHandle;
@@ -225,9 +225,15 @@ fn fat_lto(
225225
// All the other modules will be serialized and reparsed into the new
226226
// context, so this hopefully avoids serializing and parsing the largest
227227
// codegen unit.
228+
//
229+
// Additionally use a regular module as the base here to ensure that various
230+
// file copy operations in the backend work correctly. The only other kind
231+
// of module here should be an allocator one, and if your crate is smaller
232+
// than the allocator module then the size doesn't really matter anyway.
228233
let costliest_module = in_memory
229234
.iter()
230235
.enumerate()
236+
.filter(|&(_, module)| module.kind == ModuleKind::Regular)
231237
.map(|(i, module)| {
232238
let cost = unsafe { llvm::LLVMRustModuleCost(module.module_llvm.llmod()) };
233239
(cost, i)

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
490490

491491
let _timer = sess.timer("copy_all_cgu_workproducts_to_incr_comp_cache_dir");
492492

493-
for module in &compiled_modules.modules {
493+
for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {
494494
let mut files = Vec::new();
495495
if let Some(object_file_path) = &module.object {
496496
files.push((OutputType::Object.extension(), object_file_path.as_path()));
@@ -960,6 +960,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
960960

961961
WorkItemResult::Finished(CompiledModule {
962962
links_from_incr_cache,
963+
kind: ModuleKind::Regular,
963964
name: module.name,
964965
object,
965966
dwarf_object,

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl<M> ModuleCodegen<M> {
120120

121121
CompiledModule {
122122
name: self.name,
123+
kind: self.kind,
123124
object,
124125
dwarf_object,
125126
bytecode,
@@ -133,6 +134,7 @@ impl<M> ModuleCodegen<M> {
133134
#[derive(Debug, Encodable, Decodable)]
134135
pub struct CompiledModule {
135136
pub name: String,
137+
pub kind: ModuleKind,
136138
pub object: Option<PathBuf>,
137139
pub dwarf_object: Option<PathBuf>,
138140
pub bytecode: Option<PathBuf>,

0 commit comments

Comments
 (0)