Skip to content

Commit a7dc3b0

Browse files
committed
Remove code for ThinLTO from cg_gcc
It was just a dummy implementation to workarround the fact that thin local lto is the default in rustc. By adding a thin_lto_supported thin local lto can be automatically disabled for cg_gcc, removing the need for this dummy implementation. This makes improvements to the LTO handling on the cg_ssa side a lot easier.
1 parent 7ad4e69 commit a7dc3b0

File tree

10 files changed

+64
-403
lines changed

10 files changed

+64
-403
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
196196
println!("Cranelift version: {}", cranelift_codegen::VERSION);
197197
}
198198

199+
fn thin_lto_supported(&self) -> bool {
200+
false
201+
}
202+
199203
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
200204
info!("codegen crate {}", tcx.crate_name(LOCAL_CRATE));
201205
let config = self.config.get().unwrap();

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 4 additions & 388 deletions
Large diffs are not rendered by default.

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use std::path::{Path, PathBuf};
7676
use std::sync::atomic::{AtomicBool, Ordering};
7777
use std::sync::{Arc, Mutex};
7878

79-
use back::lto::{ThinBuffer, ThinData};
8079
use gccjit::{CType, Context, OptimizationLevel};
8180
#[cfg(feature = "master")]
8281
use gccjit::{TargetInfo, Version};
@@ -87,7 +86,9 @@ use rustc_codegen_ssa::back::write::{
8786
};
8887
use rustc_codegen_ssa::base::codegen_crate;
8988
use rustc_codegen_ssa::target_features::cfg_target_feature;
90-
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
89+
use rustc_codegen_ssa::traits::{
90+
CodegenBackend, ExtraBackendMethods, ThinBufferMethods, WriteBackendMethods,
91+
};
9192
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
9293
use rustc_data_structures::fx::FxIndexMap;
9394
use rustc_data_structures::sync::IntoDynSyncSend;
@@ -278,6 +279,10 @@ impl CodegenBackend for GccCodegenBackend {
278279
}
279280
}
280281

282+
fn thin_lto_supported(&self) -> bool {
283+
false
284+
}
285+
281286
fn provide(&self, providers: &mut Providers) {
282287
providers.queries.global_backend_features =
283288
|tcx, ()| gcc_util::global_gcc_features(tcx.sess)
@@ -418,11 +423,19 @@ unsafe impl Send for SyncContext {}
418423
// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "CodegenBackend::supports_parallel()".
419424
unsafe impl Sync for SyncContext {}
420425

426+
pub struct ThinBuffer;
427+
428+
impl ThinBufferMethods for ThinBuffer {
429+
fn data(&self) -> &[u8] {
430+
&[]
431+
}
432+
}
433+
421434
impl WriteBackendMethods for GccCodegenBackend {
422435
type Module = GccContext;
423436
type TargetMachine = ();
424437
type ModuleBuffer = ModuleBuffer;
425-
type ThinData = ThinData;
438+
type ThinData = ();
426439
type ThinBuffer = ThinBuffer;
427440

428441
fn run_and_optimize_fat_lto(
@@ -438,15 +451,15 @@ impl WriteBackendMethods for GccCodegenBackend {
438451
}
439452

440453
fn run_thin_lto(
441-
cgcx: &CodegenContext,
442-
dcx: DiagCtxtHandle<'_>,
454+
_cgcx: &CodegenContext,
455+
_dcx: DiagCtxtHandle<'_>,
443456
// FIXME(bjorn3): Limit LTO exports to these symbols
444457
_exported_symbols_for_lto: &[String],
445-
each_linked_rlib_for_lto: &[PathBuf],
446-
modules: Vec<(String, Self::ThinBuffer)>,
447-
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
458+
_each_linked_rlib_for_lto: &[PathBuf],
459+
_modules: Vec<(String, Self::ThinBuffer)>,
460+
_cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
448461
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
449-
back::lto::run_thin(cgcx, dcx, each_linked_rlib_for_lto, modules, cached_modules)
462+
unreachable!()
450463
}
451464

452465
fn print_pass_timings(&self) {
@@ -467,12 +480,12 @@ impl WriteBackendMethods for GccCodegenBackend {
467480
}
468481

469482
fn optimize_thin(
470-
cgcx: &CodegenContext,
483+
_cgcx: &CodegenContext,
471484
_shared_emitter: &SharedEmitter,
472485
_tm_factory: TargetMachineFactoryFn<Self>,
473-
thin: ThinModule<Self>,
486+
_thin: ThinModule<Self>,
474487
) -> ModuleCodegen<Self::Module> {
475-
back::lto::optimize_thin_module(thin, cgcx)
488+
unreachable!()
476489
}
477490

478491
fn codegen(
@@ -484,8 +497,8 @@ impl WriteBackendMethods for GccCodegenBackend {
484497
back::write::codegen(cgcx, shared_emitter, module, config)
485498
}
486499

487-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
488-
back::lto::prepare_thin(module)
500+
fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
501+
unreachable!()
489502
}
490503

491504
fn serialize_module(_module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ impl CodegenBackend for LlvmCodegenBackend {
349349
vec![sym::unchecked_funnel_shl, sym::unchecked_funnel_shr, sym::carrying_mul_add]
350350
}
351351

352+
fn thin_lto_supported(&self) -> bool {
353+
true
354+
}
355+
352356
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
353357
Box::new(rustc_codegen_ssa::base::codegen_crate(
354358
LlvmCodegenBackend(()),

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ pub trait CodegenBackend {
8080
vec![]
8181
}
8282

83+
/// Is ThinLTO supported by this backend?
84+
fn thin_lto_supported(&self) -> bool;
85+
8386
/// Value printed by `--print=backend-has-zstd`.
8487
///
8588
/// Used by compiletest to determine whether tests involving zstd compression

compiler/rustc_interface/src/interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
463463

464464
codegen_backend.init(&sess);
465465
sess.replaced_intrinsics = FxHashSet::from_iter(codegen_backend.replaced_intrinsics());
466+
sess.thin_lto_supported = codegen_backend.thin_lto_supported();
466467

467468
let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
468469
let mut cfg = config::build_configuration(&sess, cfg);

compiler/rustc_interface/src/util.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ impl CodegenBackend for DummyCodegenBackend {
400400
vec![CrateType::Rlib, CrateType::Executable]
401401
}
402402

403+
fn thin_lto_supported(&self) -> bool {
404+
false
405+
}
406+
403407
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
404408
Box::new(CodegenResults {
405409
modules: vec![],

compiler/rustc_macros/src/diagnostics/message.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const ALLOWED_CAPITALIZED_WORDS: &[&str] = &[
101101
"NaNs",
102102
"OK",
103103
"Rust",
104+
"ThinLTO",
104105
"Unicode",
105106
"VS",
106107
// tidy-alphabetical-end

compiler/rustc_session/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,7 @@ pub(crate) struct UnexpectedBuiltinCfg {
537537
pub(crate) cfg_name: Symbol,
538538
pub(crate) controlled_by: &'static str,
539539
}
540+
541+
#[derive(Diagnostic)]
542+
#[diag("ThinLTO is not supported by the codegen backend")]
543+
pub(crate) struct ThinLtoNotSupportedByBackend;

compiler/rustc_session/src/session.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ pub struct Session {
158158
/// The names of intrinsics that the current codegen backend replaces
159159
/// with its own implementations.
160160
pub replaced_intrinsics: FxHashSet<Symbol>,
161+
162+
/// Does the codegen backend support ThinLTO?
163+
pub thin_lto_supported: bool,
161164
}
162165

163166
#[derive(Clone, Copy)]
@@ -606,6 +609,9 @@ impl Session {
606609
}
607610
config::LtoCli::Thin => {
608611
// The user explicitly asked for ThinLTO
612+
if !self.thin_lto_supported {
613+
self.dcx().emit_fatal(errors::ThinLtoNotSupportedByBackend);
614+
}
609615
return config::Lto::Thin;
610616
}
611617
}
@@ -626,6 +632,9 @@ impl Session {
626632
// a deprecated option now that `-C lto=thin` exists.
627633
if let Some(enabled) = self.opts.unstable_opts.thinlto {
628634
if enabled {
635+
if !self.thin_lto_supported {
636+
self.dcx().emit_fatal(errors::ThinLtoNotSupportedByBackend);
637+
}
629638
return config::Lto::ThinLocal;
630639
} else {
631640
return config::Lto::No;
@@ -642,7 +651,8 @@ impl Session {
642651
// optimized compiles (anything greater than O0).
643652
match self.opts.optimize {
644653
config::OptLevel::No => config::Lto::No,
645-
_ => config::Lto::ThinLocal,
654+
_ if self.thin_lto_supported => config::Lto::ThinLocal,
655+
_ => config::Lto::No,
646656
}
647657
}
648658

@@ -1088,6 +1098,7 @@ pub fn build_session(
10881098
host_filesearch,
10891099
invocation_temp,
10901100
replaced_intrinsics: FxHashSet::default(), // filled by `run_compiler`
1101+
thin_lto_supported: true, // filled by `run_compiler`
10911102
};
10921103

10931104
validate_commandline_args_with_session_available(&sess);

0 commit comments

Comments
 (0)