Skip to content

Commit eea81b5

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 7a01c7f commit eea81b5

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
@@ -809,19 +809,12 @@ pub(crate) fn compute_per_cgu_lto_type(
809809
sess_lto: &Lto,
810810
opts: &config::Options,
811811
sess_crate_types: &[CrateType],
812-
module_kind: ModuleKind,
813812
) -> ComputedLtoType {
814813
// If the linker does LTO, we don't have to do it. Note that we
815814
// keep doing full LTO, if it is requested, as not to break the
816815
// assumption that the output will be a single module.
817816
let linker_does_lto = opts.cg.linker_plugin_lto.enabled();
818817

819-
// When we're automatically doing ThinLTO for multi-codegen-unit
820-
// builds we don't actually want to LTO the allocator modules if
821-
// it shows up. This is due to various linker shenanigans that
822-
// we'll encounter later.
823-
let is_allocator = module_kind == ModuleKind::Allocator;
824-
825818
// We ignore a request for full crate graph LTO if the crate type
826819
// is only an rlib, as there is no full crate graph to process,
827820
// that'll happen later.
@@ -833,7 +826,7 @@ pub(crate) fn compute_per_cgu_lto_type(
833826
let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);
834827

835828
match sess_lto {
836-
Lto::ThinLocal if !linker_does_lto && !is_allocator => ComputedLtoType::Thin,
829+
Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,
837830
Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
838831
Lto::Fat if !is_rlib => ComputedLtoType::Fat,
839832
_ => ComputedLtoType::No,
@@ -855,7 +848,16 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
855848
// back to the coordinator thread for further LTO processing (which
856849
// has to wait for all the initial modules to be optimized).
857850

858-
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types, module.kind);
851+
// When we're automatically doing ThinLTO for multi-codegen-unit
852+
// builds we don't actually want to LTO the allocator modules if
853+
// it shows up. This is due to various linker shenanigans that
854+
// we'll encounter later.
855+
if module.kind == ModuleKind::Allocator {
856+
let module = B::codegen(cgcx, module, module_config);
857+
return WorkItemResult::Finished(module);
858+
}
859+
860+
let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types);
859861

860862
// If we're doing some form of incremental LTO then we need to be sure to
861863
// 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)