Skip to content

Commit 71289c3

Browse files
committed
Auto merge of #145955 - bjorn3:lto_refactors4, r=nnethercote
Rework how the codegen coordinator code handles the allocator shim Continuing from #144503 this centralizes most handling of the allocator shim to a single 4 line block in the codegen coordinator. The allocator shim is small enough that making it go through the main codegen loop and spawning a worker thread for it is wasted effort.
2 parents 45b9d13 + 319fe23 commit 71289c3

File tree

7 files changed

+97
-155
lines changed

7 files changed

+97
-155
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ 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::{
16-
CodegenResults, CompiledModule, CrateInfo, ModuleKind, errors as ssa_errors,
17-
};
15+
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, errors as ssa_errors};
1816
use rustc_data_structures::profiling::SelfProfilerRef;
1917
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2018
use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
@@ -363,7 +361,6 @@ fn emit_cgu(
363361
invocation_temp,
364362
prof,
365363
product.object,
366-
ModuleKind::Regular,
367364
name.clone(),
368365
producer,
369366
)?;
@@ -372,7 +369,6 @@ fn emit_cgu(
372369
module_regular,
373370
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
374371
name: format!("{name}.asm"),
375-
kind: ModuleKind::Regular,
376372
object: Some(global_asm_object_file),
377373
dwarf_object: None,
378374
bytecode: None,
@@ -389,7 +385,6 @@ fn emit_module(
389385
invocation_temp: Option<&str>,
390386
prof: &SelfProfilerRef,
391387
mut object: cranelift_object::object::write::Object<'_>,
392-
kind: ModuleKind,
393388
name: String,
394389
producer_str: &str,
395390
) -> Result<CompiledModule, String> {
@@ -430,7 +425,6 @@ fn emit_module(
430425

431426
Ok(CompiledModule {
432427
name,
433-
kind,
434428
object: Some(tmp_file),
435429
dwarf_object: None,
436430
bytecode: None,
@@ -485,7 +479,6 @@ fn reuse_workproduct_for_cgu(
485479
Ok(ModuleCodegenResult {
486480
module_regular: CompiledModule {
487481
name: cgu.name().to_string(),
488-
kind: ModuleKind::Regular,
489482
object: Some(obj_out_regular),
490483
dwarf_object: None,
491484
bytecode: None,
@@ -495,7 +488,6 @@ fn reuse_workproduct_for_cgu(
495488
},
496489
module_global_asm: source_file_global_asm.map(|source_file| CompiledModule {
497490
name: cgu.name().to_string(),
498-
kind: ModuleKind::Regular,
499491
object: Some(obj_out_global_asm),
500492
dwarf_object: None,
501493
bytecode: None,
@@ -651,7 +643,6 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
651643
tcx.sess.invocation_temp.as_deref(),
652644
&tcx.sess.prof,
653645
product.object,
654-
ModuleKind::Allocator,
655646
"allocator_shim".to_owned(),
656647
&crate::debuginfo::producer(tcx.sess),
657648
) {

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn fat_lto(
204204
let path = tmp_path.path().to_path_buf().join(&module.name);
205205
let path = path.to_str().expect("path");
206206
let context = &module.module_llvm.context;
207-
let config = cgcx.config(module.kind);
207+
let config = &cgcx.module_config;
208208
// NOTE: we need to set the optimization level here in order for LTO to do its job.
209209
context.set_optimization_level(to_gcc_opt_level(config.opt_level));
210210
context.add_command_line_option("-flto=auto");

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 7 additions & 15 deletions
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, ModuleKind, looks_like_rust_object_file};
14+
use rustc_codegen_ssa::{ModuleCodegen, looks_like_rust_object_file};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::memmap::Mmap;
1717
use rustc_errors::DiagCtxtHandle;
@@ -43,9 +43,7 @@ fn prepare_lto(
4343
.map(|symbol| CString::new(symbol.to_owned()).unwrap())
4444
.collect::<Vec<CString>>();
4545

46-
if cgcx.regular_module_config.instrument_coverage
47-
|| cgcx.regular_module_config.pgo_gen.enabled()
48-
{
46+
if cgcx.module_config.instrument_coverage || cgcx.module_config.pgo_gen.enabled() {
4947
// These are weak symbols that point to the profile version and the
5048
// profile name, which need to be treated as exported so LTO doesn't nix
5149
// them.
@@ -55,15 +53,15 @@ fn prepare_lto(
5553
symbols_below_threshold.extend(PROFILER_WEAK_SYMBOLS.iter().map(|&sym| sym.to_owned()));
5654
}
5755

58-
if cgcx.regular_module_config.sanitizer.contains(SanitizerSet::MEMORY) {
56+
if cgcx.module_config.sanitizer.contains(SanitizerSet::MEMORY) {
5957
let mut msan_weak_symbols = Vec::new();
6058

6159
// Similar to profiling, preserve weak msan symbol during LTO.
62-
if cgcx.regular_module_config.sanitizer_recover.contains(SanitizerSet::MEMORY) {
60+
if cgcx.module_config.sanitizer_recover.contains(SanitizerSet::MEMORY) {
6361
msan_weak_symbols.push(c"__msan_keep_going");
6462
}
6563

66-
if cgcx.regular_module_config.sanitizer_memory_track_origins != 0 {
64+
if cgcx.module_config.sanitizer_memory_track_origins != 0 {
6765
msan_weak_symbols.push(c"__msan_track_origins");
6866
}
6967

@@ -227,15 +225,9 @@ fn fat_lto(
227225
// All the other modules will be serialized and reparsed into the new
228226
// context, so this hopefully avoids serializing and parsing the largest
229227
// codegen unit.
230-
//
231-
// Additionally use a regular module as the base here to ensure that various
232-
// file copy operations in the backend work correctly. The only other kind
233-
// of module here should be an allocator one, and if your crate is smaller
234-
// than the allocator module then the size doesn't really matter anyway.
235228
let costliest_module = in_memory
236229
.iter()
237230
.enumerate()
238-
.filter(|&(_, module)| module.kind == ModuleKind::Regular)
239231
.map(|(i, module)| {
240232
let cost = unsafe { llvm::LLVMRustModuleCost(module.module_llvm.llmod()) };
241233
(cost, i)
@@ -583,7 +575,7 @@ pub(crate) fn run_pass_manager(
583575
thin: bool,
584576
) {
585577
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_lto_optimize", &*module.name);
586-
let config = cgcx.config(module.kind);
578+
let config = &cgcx.module_config;
587579

588580
// Now we have one massive module inside of llmod. Time to run the
589581
// LTO-specific optimization passes that LLVM provides.
@@ -745,7 +737,7 @@ pub(crate) fn optimize_thin_module(
745737
let module_llvm = ModuleLlvm::parse(cgcx, module_name, thin_module.data(), dcx);
746738
let mut module = ModuleCodegen::new_regular(thin_module.name(), module_llvm);
747739
// Given that the newly created module lacks a thinlto buffer for embedding, we need to re-add it here.
748-
if cgcx.config(ModuleKind::Regular).embed_bitcode() {
740+
if cgcx.module_config.embed_bitcode() {
749741
module.thin_lto_buffer = Some(thin_module.data().to_vec());
750742
}
751743
{

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ffi::CString;
22
use std::sync::Arc;
33

4+
use rustc_ast::expand::allocator::AllocatorKind;
45
use rustc_data_structures::memmap::Mmap;
56
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
67
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportLevel};
@@ -95,6 +96,19 @@ pub(super) fn exported_symbols_for_lto(
9596
.filter_map(|&(s, info): &(ExportedSymbol<'_>, SymbolExportInfo)| {
9697
if info.level.is_below_threshold(export_threshold) || info.used {
9798
Some(symbol_name_for_instance_in_crate(tcx, s, cnum))
99+
} else if export_threshold == SymbolExportLevel::C
100+
&& info.rustc_std_internal_symbol
101+
&& let Some(AllocatorKind::Default) = allocator_kind_for_codegen(tcx)
102+
{
103+
// Export the __rdl_* exports for usage by the allocator shim when not using
104+
// #[global_allocator]. Most of the conditions above are only used to avoid
105+
// unnecessary expensive symbol_name_for_instance_in_crate calls.
106+
let sym = symbol_name_for_instance_in_crate(tcx, s, cnum);
107+
if sym.contains("__rdl_") || sym.contains("__rg_oom") {
108+
Some(sym)
109+
} else {
110+
None
111+
}
98112
} else {
99113
None
100114
}

0 commit comments

Comments
 (0)