Skip to content

Commit 541b5a5

Browse files
committed
Ensure the allocator shim never participates in LTO
Making it participate in LTO would be incorrect if you compile a crate as both a dylib (which needs it) and rlib (which must not include it) in the same rustc invocation. With linker plugin LTO, the allocator shim will still participate in LTO as it is safe to do so in that case.
1 parent 28919a6 commit 541b5a5

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,19 +798,12 @@ pub(crate) fn compute_per_cgu_lto_type(
798798
sess_lto: &Lto,
799799
opts: &config::Options,
800800
sess_crate_types: &[CrateType],
801-
module_kind: ModuleKind,
802801
) -> ComputedLtoType {
803802
// If the linker does LTO, we don't have to do it. Note that we
804803
// keep doing full LTO, if it is requested, as not to break the
805804
// assumption that the output will be a single module.
806805
let linker_does_lto = opts.cg.linker_plugin_lto.enabled();
807806

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

824817
match sess_lto {
825-
Lto::ThinLocal if !linker_does_lto && !is_allocator => ComputedLtoType::Thin,
818+
Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,
826819
Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
827820
Lto::Fat if !is_rlib => ComputedLtoType::Fat,
828821
_ => ComputedLtoType::No,
@@ -844,7 +837,16 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
844837
// back to the coordinator thread for further LTO processing (which
845838
// has to wait for all the initial modules to be optimized).
846839

847-
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types, module.kind);
840+
// When we're automatically doing ThinLTO for multi-codegen-unit
841+
// builds we don't actually want to LTO the allocator modules if
842+
// it shows up. This is due to various linker shenanigans that
843+
// we'll encounter later.
844+
if module.kind == ModuleKind::Allocator {
845+
let module = B::codegen(cgcx, module, module_config);
846+
return WorkItemResult::Finished(module);
847+
}
848+
849+
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types);
848850

849851
// If we're doing some form of incremental LTO then we need to be sure to
850852
// save our module to disk first.

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ use crate::meth::load_vtable;
4646
use crate::mir::operand::OperandValue;
4747
use crate::mir::place::PlaceRef;
4848
use crate::traits::*;
49-
use crate::{
50-
CachedModuleCodegen, CodegenLintLevels, CrateInfo, ModuleCodegen, ModuleKind, errors, meth, mir,
51-
};
49+
use crate::{CachedModuleCodegen, CodegenLintLevels, CrateInfo, ModuleCodegen, errors, meth, mir};
5250

5351
pub(crate) fn bin_op_to_icmp_predicate(op: BinOp, signed: bool) -> IntPredicate {
5452
match (op, signed) {
@@ -1135,12 +1133,7 @@ pub fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) ->
11351133
// We can re-use either the pre- or the post-thinlto state. If no LTO is
11361134
// being performed then we can use post-LTO artifacts, otherwise we must
11371135
// reuse pre-LTO artifacts
1138-
match compute_per_cgu_lto_type(
1139-
&tcx.sess.lto(),
1140-
&tcx.sess.opts,
1141-
tcx.crate_types(),
1142-
ModuleKind::Regular,
1143-
) {
1136+
match compute_per_cgu_lto_type(&tcx.sess.lto(), &tcx.sess.opts, tcx.crate_types()) {
11441137
ComputedLtoType::No => CguReuse::PostLto,
11451138
_ => CguReuse::PreLto,
11461139
}

0 commit comments

Comments
 (0)