Skip to content

Commit 12f41b5

Browse files
committed
More explicit handling of the allocator shim around LTO
1 parent e0e204f commit 12f41b5

File tree

2 files changed

+27
-40
lines changed

2 files changed

+27
-40
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ pub struct CodegenContext<B: WriteBackendMethods> {
334334
pub output_filenames: Arc<OutputFilenames>,
335335
pub invocation_temp: Option<String>,
336336
pub module_config: Arc<ModuleConfig>,
337-
pub allocator_config: Arc<ModuleConfig>,
338337
pub tm_factory: TargetMachineFactoryFn<B>,
339338
pub msvc_imps_needed: bool,
340339
pub is_pe_coff: bool,
@@ -799,19 +798,12 @@ pub(crate) fn compute_per_cgu_lto_type(
799798
sess_lto: &Lto,
800799
opts: &config::Options,
801800
sess_crate_types: &[CrateType],
802-
module_kind: ModuleKind,
803801
) -> ComputedLtoType {
804802
// If the linker does LTO, we don't have to do it. Note that we
805803
// keep doing full LTO, if it is requested, as not to break the
806804
// assumption that the output will be a single module.
807805
let linker_does_lto = opts.cg.linker_plugin_lto.enabled();
808806

809-
// When we're automatically doing ThinLTO for multi-codegen-unit
810-
// builds we don't actually want to LTO the allocator module if
811-
// it shows up. This is due to various linker shenanigans that
812-
// we'll encounter later.
813-
let is_allocator = module_kind == ModuleKind::Allocator;
814-
815807
// We ignore a request for full crate graph LTO if the crate type
816808
// is only an rlib, as there is no full crate graph to process,
817809
// that'll happen later.
@@ -823,7 +815,7 @@ pub(crate) fn compute_per_cgu_lto_type(
823815
let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);
824816

825817
match sess_lto {
826-
Lto::ThinLocal if !linker_does_lto && !is_allocator => ComputedLtoType::Thin,
818+
Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,
827819
Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
828820
Lto::Fat if !is_rlib => ComputedLtoType::Fat,
829821
_ => ComputedLtoType::No,
@@ -839,23 +831,18 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
839831
let dcx = cgcx.create_dcx();
840832
let dcx = dcx.handle();
841833

842-
let module_config = match module.kind {
843-
ModuleKind::Regular => &cgcx.module_config,
844-
ModuleKind::Allocator => &cgcx.allocator_config,
845-
};
846-
847-
B::optimize(cgcx, dcx, &mut module, module_config);
834+
B::optimize(cgcx, dcx, &mut module, &cgcx.module_config);
848835

849836
// After we've done the initial round of optimizations we need to
850837
// decide whether to synchronously codegen this module or ship it
851838
// back to the coordinator thread for further LTO processing (which
852839
// has to wait for all the initial modules to be optimized).
853840

854-
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types, module.kind);
841+
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types);
855842

856843
// If we're doing some form of incremental LTO then we need to be sure to
857844
// save our module to disk first.
858-
let bitcode = if module_config.emit_pre_lto_bc {
845+
let bitcode = if cgcx.module_config.emit_pre_lto_bc {
859846
let filename = pre_lto_bitcode_filename(&module.name);
860847
cgcx.incr_comp_session_dir.as_ref().map(|path| path.join(&filename))
861848
} else {
@@ -864,7 +851,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
864851

865852
match lto_type {
866853
ComputedLtoType::No => {
867-
let module = B::codegen(cgcx, module, module_config);
854+
let module = B::codegen(cgcx, module, &cgcx.module_config);
868855
WorkItemResult::Finished(module)
869856
}
870857
ComputedLtoType::Thin => {
@@ -1245,7 +1232,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
12451232
coordinator_receive: Receiver<Message<B>>,
12461233
regular_config: Arc<ModuleConfig>,
12471234
allocator_config: Arc<ModuleConfig>,
1248-
allocator_module: Option<ModuleCodegen<B::Module>>,
1235+
mut allocator_module: Option<ModuleCodegen<B::Module>>,
12491236
coordinator_send: Sender<Message<B>>,
12501237
) -> thread::JoinHandle<Result<CompiledModules, ()>> {
12511238
let sess = tcx.sess;
@@ -1303,7 +1290,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
13031290
diag_emitter: shared_emitter.clone(),
13041291
output_filenames: Arc::clone(tcx.output_filenames(())),
13051292
module_config: regular_config,
1306-
allocator_config,
13071293
tm_factory: backend.target_machine_factory(tcx.sess, ol, backend_features),
13081294
msvc_imps_needed: msvc_imps_needed(tcx),
13091295
is_pe_coff: tcx.sess.target.is_like_windows,
@@ -1497,16 +1483,12 @@ fn start_executing_work<B: ExtraBackendMethods>(
14971483

14981484
let mut llvm_start_time: Option<VerboseTimingGuard<'_>> = None;
14991485

1500-
let compiled_allocator_module = allocator_module.and_then(|allocator_module| {
1501-
match execute_optimize_work_item(&cgcx, allocator_module) {
1502-
WorkItemResult::Finished(compiled_module) => return Some(compiled_module),
1503-
WorkItemResult::NeedsFatLto(fat_lto_input) => needs_fat_lto.push(fat_lto_input),
1504-
WorkItemResult::NeedsThinLto(name, thin_buffer) => {
1505-
needs_thin_lto.push((name, thin_buffer))
1506-
}
1507-
}
1508-
None
1509-
});
1486+
if let Some(allocator_module) = &mut allocator_module {
1487+
let dcx = cgcx.create_dcx();
1488+
let dcx = dcx.handle();
1489+
1490+
B::optimize(&cgcx, dcx, allocator_module, &allocator_config);
1491+
}
15101492

15111493
// Run the message loop while there's still anything that needs message
15121494
// processing. Note that as soon as codegen is aborted we simply want to
@@ -1733,6 +1715,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
17331715
assert!(compiled_modules.is_empty());
17341716
assert!(needs_thin_lto.is_empty());
17351717

1718+
if let Some(allocator_module) = allocator_module.take() {
1719+
needs_fat_lto.push(FatLtoInput::InMemory(allocator_module));
1720+
}
1721+
17361722
// This uses the implicit token
17371723
let module = do_fat_lto(
17381724
&cgcx,
@@ -1746,6 +1732,13 @@ fn start_executing_work<B: ExtraBackendMethods>(
17461732
assert!(compiled_modules.is_empty());
17471733
assert!(needs_fat_lto.is_empty());
17481734

1735+
if cgcx.lto != Lto::ThinLocal {
1736+
if let Some(allocator_module) = allocator_module.take() {
1737+
let (name, thin_buffer) = B::prepare_thin(allocator_module);
1738+
needs_thin_lto.push((name, thin_buffer));
1739+
}
1740+
}
1741+
17491742
compiled_modules.extend(do_thin_lto(
17501743
&cgcx,
17511744
exported_symbols_for_lto,
@@ -1762,7 +1755,8 @@ fn start_executing_work<B: ExtraBackendMethods>(
17621755

17631756
Ok(CompiledModules {
17641757
modules: compiled_modules,
1765-
allocator_module: compiled_allocator_module,
1758+
allocator_module: allocator_module
1759+
.map(|allocator_module| B::codegen(&cgcx, allocator_module, &allocator_config)),
17661760
})
17671761
})
17681762
.expect("failed to spawn coordinator thread");

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ use crate::meth::load_vtable;
4949
use crate::mir::operand::OperandValue;
5050
use crate::mir::place::PlaceRef;
5151
use crate::traits::*;
52-
use crate::{
53-
CachedModuleCodegen, CodegenLintLevels, CrateInfo, ModuleCodegen, ModuleKind, errors, meth, mir,
54-
};
52+
use crate::{CachedModuleCodegen, CodegenLintLevels, CrateInfo, ModuleCodegen, errors, meth, mir};
5553

5654
pub(crate) fn bin_op_to_icmp_predicate(op: BinOp, signed: bool) -> IntPredicate {
5755
match (op, signed) {
@@ -1132,12 +1130,7 @@ pub fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) ->
11321130
// We can re-use either the pre- or the post-thinlto state. If no LTO is
11331131
// being performed then we can use post-LTO artifacts, otherwise we must
11341132
// reuse pre-LTO artifacts
1135-
match compute_per_cgu_lto_type(
1136-
&tcx.sess.lto(),
1137-
&tcx.sess.opts,
1138-
tcx.crate_types(),
1139-
ModuleKind::Regular,
1140-
) {
1133+
match compute_per_cgu_lto_type(&tcx.sess.lto(), &tcx.sess.opts, tcx.crate_types()) {
11411134
ComputedLtoType::No => CguReuse::PostLto,
11421135
_ => CguReuse::PreLto,
11431136
}

0 commit comments

Comments
 (0)