Skip to content

Commit b8d0319

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 d7daac0 commit b8d0319

File tree

7 files changed

+54
-409
lines changed

7 files changed

+54
-409
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

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

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 28 additions & 18 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::profiling::SelfProfilerRef;
@@ -177,8 +178,6 @@ pub struct GccCodegenBackend {
177178
lto_supported: Arc<AtomicBool>,
178179
}
179180

180-
static LTO_SUPPORTED: AtomicBool = AtomicBool::new(false);
181-
182181
fn load_libgccjit_if_needed(libgccjit_target_lib_file: &Path) {
183182
if gccjit::is_loaded() {
184183
// Do not load a libgccjit second time.
@@ -249,7 +248,6 @@ impl CodegenBackend for GccCodegenBackend {
249248
#[cfg(feature = "master")]
250249
{
251250
let lto_supported = gccjit::is_lto_supported();
252-
LTO_SUPPORTED.store(lto_supported, Ordering::SeqCst);
253251
self.lto_supported.store(lto_supported, Ordering::SeqCst);
254252

255253
gccjit::set_global_personality_function_name(b"rust_eh_personality\0");
@@ -279,6 +277,10 @@ impl CodegenBackend for GccCodegenBackend {
279277
}
280278
}
281279

280+
fn thin_lto_supported(&self) -> bool {
281+
false
282+
}
283+
282284
fn provide(&self, providers: &mut Providers) {
283285
providers.queries.global_backend_features =
284286
|tcx, ()| gcc_util::global_gcc_features(tcx.sess)
@@ -419,11 +421,19 @@ unsafe impl Send for SyncContext {}
419421
// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "CodegenBackend::supports_parallel()".
420422
unsafe impl Sync for SyncContext {}
421423

424+
pub struct ThinBuffer;
425+
426+
impl ThinBufferMethods for ThinBuffer {
427+
fn data(&self) -> &[u8] {
428+
&[]
429+
}
430+
}
431+
422432
impl WriteBackendMethods for GccCodegenBackend {
423433
type Module = GccContext;
424434
type TargetMachine = ();
425435
type ModuleBuffer = ModuleBuffer;
426-
type ThinData = ThinData;
436+
type ThinData = ();
427437
type ThinBuffer = ThinBuffer;
428438

429439
fn run_and_optimize_fat_lto(
@@ -440,16 +450,16 @@ impl WriteBackendMethods for GccCodegenBackend {
440450
}
441451

442452
fn run_thin_lto(
443-
cgcx: &CodegenContext,
444-
prof: &SelfProfilerRef,
445-
dcx: DiagCtxtHandle<'_>,
453+
_cgcx: &CodegenContext,
454+
_prof: &SelfProfilerRef,
455+
_dcx: DiagCtxtHandle<'_>,
446456
// FIXME(bjorn3): Limit LTO exports to these symbols
447457
_exported_symbols_for_lto: &[String],
448-
each_linked_rlib_for_lto: &[PathBuf],
449-
modules: Vec<(String, Self::ThinBuffer)>,
450-
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)>,
451461
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
452-
back::lto::run_thin(cgcx, prof, dcx, each_linked_rlib_for_lto, modules, cached_modules)
462+
unreachable!()
453463
}
454464

455465
fn print_pass_timings(&self) {
@@ -471,13 +481,13 @@ impl WriteBackendMethods for GccCodegenBackend {
471481
}
472482

473483
fn optimize_thin(
474-
cgcx: &CodegenContext,
484+
_cgcx: &CodegenContext,
475485
_prof: &SelfProfilerRef,
476486
_shared_emitter: &SharedEmitter,
477487
_tm_factory: TargetMachineFactoryFn<Self>,
478-
thin: ThinModule<Self>,
488+
_thin: ThinModule<Self>,
479489
) -> ModuleCodegen<Self::Module> {
480-
back::lto::optimize_thin_module(thin, cgcx)
490+
unreachable!()
481491
}
482492

483493
fn codegen(
@@ -490,8 +500,8 @@ impl WriteBackendMethods for GccCodegenBackend {
490500
back::write::codegen(cgcx, prof, shared_emitter, module, config)
491501
}
492502

493-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
494-
back::lto::prepare_thin(module)
503+
fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
504+
unreachable!()
495505
}
496506

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

compiler/rustc_codegen_ssa/src/traits/backend.rs

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

83+
/// Is ThinLTO supported by this backend?
84+
fn thin_lto_supported(&self) -> bool {
85+
true
86+
}
87+
8388
/// Value printed by `--print=backend-has-zstd`.
8489
///
8590
/// 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_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: 11 additions & 0 deletions
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,10 +609,17 @@ 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
}
612618

619+
if !self.thin_lto_supported {
620+
return config::Lto::No;
621+
}
622+
613623
// Ok at this point the target doesn't require anything and the user
614624
// hasn't asked for anything. Our next decision is whether or not
615625
// we enable "auto" ThinLTO where we use multiple codegen units and
@@ -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)