Skip to content

Commit 653bb64

Browse files
committed
Remove LtoModuleCodegen
Most uses of it either contain a fat or thin lto module. Only WorkItem::LTO could contain both, but splitting that enum variant doesn't complicate things much.
1 parent d612081 commit 653bb64

File tree

7 files changed

+58
-100
lines changed

7 files changed

+58
-100
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::sync::Arc;
2424

2525
use gccjit::{Context, OutputKind};
2626
use object::read::archive::ArchiveFile;
27-
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
27+
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
2828
use rustc_codegen_ssa::back::symbol_export;
2929
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
3030
use rustc_codegen_ssa::traits::*;
@@ -176,7 +176,7 @@ pub(crate) fn run_fat(
176176
cgcx: &CodegenContext<GccCodegenBackend>,
177177
modules: Vec<FatLtoInput<GccCodegenBackend>>,
178178
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
179-
) -> Result<LtoModuleCodegen<GccCodegenBackend>, FatalError> {
179+
) -> Result<ModuleCodegen<GccContext>, FatalError> {
180180
let dcx = cgcx.create_dcx();
181181
let dcx = dcx.handle();
182182
let lto_data = prepare_lto(cgcx, dcx)?;
@@ -201,7 +201,7 @@ fn fat_lto(
201201
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
202202
tmp_path: TempDir,
203203
//symbols_below_threshold: &[String],
204-
) -> Result<LtoModuleCodegen<GccCodegenBackend>, FatalError> {
204+
) -> Result<ModuleCodegen<GccContext>, FatalError> {
205205
let _timer = cgcx.prof.generic_activity("GCC_fat_lto_build_monolithic_module");
206206
info!("going for a fat lto");
207207

@@ -334,7 +334,7 @@ fn fat_lto(
334334
// of now.
335335
module.module_llvm.temp_dir = Some(tmp_path);
336336

337-
Ok(LtoModuleCodegen::Fat(module))
337+
Ok(module)
338338
}
339339

340340
pub struct ModuleBuffer(PathBuf);
@@ -358,7 +358,7 @@ pub(crate) fn run_thin(
358358
cgcx: &CodegenContext<GccCodegenBackend>,
359359
modules: Vec<(String, ThinBuffer)>,
360360
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
361-
) -> Result<(Vec<LtoModuleCodegen<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
361+
) -> Result<(Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
362362
let dcx = cgcx.create_dcx();
363363
let dcx = dcx.handle();
364364
let lto_data = prepare_lto(cgcx, dcx)?;
@@ -427,7 +427,7 @@ fn thin_lto(
427427
tmp_path: TempDir,
428428
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
429429
//_symbols_below_threshold: &[String],
430-
) -> Result<(Vec<LtoModuleCodegen<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
430+
) -> Result<(Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
431431
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_global_analysis");
432432
info!("going for that thin, thin LTO");
433433

@@ -573,8 +573,7 @@ fn thin_lto(
573573
}*/
574574

575575
info!(" - {}: re-compiled", module_name);
576-
opt_jobs
577-
.push(LtoModuleCodegen::Thin(ThinModule { shared: shared.clone(), idx: module_index }));
576+
opt_jobs.push(ThinModule { shared: shared.clone(), idx: module_index });
578577
}
579578

580579
// Save the current ThinLTO import information for the next compilation

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use gccjit::{CType, Context, OptimizationLevel};
9797
use gccjit::{TargetInfo, Version};
9898
use rustc_ast::expand::allocator::AllocatorKind;
9999
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
100-
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
100+
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
101101
use rustc_codegen_ssa::back::write::{
102102
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
103103
};
@@ -361,15 +361,15 @@ impl WriteBackendMethods for GccCodegenBackend {
361361
cgcx: &CodegenContext<Self>,
362362
modules: Vec<FatLtoInput<Self>>,
363363
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
364-
) -> Result<LtoModuleCodegen<Self>, FatalError> {
364+
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
365365
back::lto::run_fat(cgcx, modules, cached_modules)
366366
}
367367

368368
fn run_thin_lto(
369369
cgcx: &CodegenContext<Self>,
370370
modules: Vec<(String, Self::ThinBuffer)>,
371371
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
372-
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
372+
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
373373
back::lto::run_thin(cgcx, modules, cached_modules)
374374
}
375375

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use std::{io, iter, slice};
88

99
use object::read::archive::ArchiveFile;
10-
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
10+
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
1111
use rustc_codegen_ssa::back::symbol_export;
1212
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
1313
use rustc_codegen_ssa::traits::*;
@@ -201,7 +201,7 @@ pub(crate) fn run_fat(
201201
cgcx: &CodegenContext<LlvmCodegenBackend>,
202202
modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
203203
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
204-
) -> Result<LtoModuleCodegen<LlvmCodegenBackend>, FatalError> {
204+
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
205205
let dcx = cgcx.create_dcx();
206206
let dcx = dcx.handle();
207207
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, dcx)?;
@@ -217,7 +217,7 @@ pub(crate) fn run_thin(
217217
cgcx: &CodegenContext<LlvmCodegenBackend>,
218218
modules: Vec<(String, ThinBuffer)>,
219219
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
220-
) -> Result<(Vec<LtoModuleCodegen<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
220+
) -> Result<(Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
221221
let dcx = cgcx.create_dcx();
222222
let dcx = dcx.handle();
223223
let (symbols_below_threshold, upstream_modules) = prepare_lto(cgcx, dcx)?;
@@ -248,7 +248,7 @@ fn fat_lto(
248248
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
249249
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
250250
symbols_below_threshold: &[*const libc::c_char],
251-
) -> Result<LtoModuleCodegen<LlvmCodegenBackend>, FatalError> {
251+
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
252252
let _timer = cgcx.prof.generic_activity("LLVM_fat_lto_build_monolithic_module");
253253
info!("going for a fat lto");
254254

@@ -366,7 +366,7 @@ fn fat_lto(
366366
save_temp_bitcode(cgcx, &module, "lto.after-restriction");
367367
}
368368

369-
Ok(LtoModuleCodegen::Fat(module))
369+
Ok(module)
370370
}
371371

372372
pub(crate) struct Linker<'a>(&'a mut llvm::Linker<'a>);
@@ -436,7 +436,7 @@ fn thin_lto(
436436
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
437437
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
438438
symbols_below_threshold: &[*const libc::c_char],
439-
) -> Result<(Vec<LtoModuleCodegen<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
439+
) -> Result<(Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>), FatalError> {
440440
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_global_analysis");
441441
unsafe {
442442
info!("going for that thin, thin LTO");
@@ -568,10 +568,7 @@ fn thin_lto(
568568
}
569569

570570
info!(" - {}: re-compiled", module_name);
571-
opt_jobs.push(LtoModuleCodegen::Thin(ThinModule {
572-
shared: Arc::clone(&shared),
573-
idx: module_index,
574-
}));
571+
opt_jobs.push(ThinModule { shared: Arc::clone(&shared), idx: module_index });
575572
}
576573

577574
// Save the current ThinLTO import information for the next compilation

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
3030
use llvm_util::target_config;
3131
use rustc_ast::expand::allocator::AllocatorKind;
3232
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
33-
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
33+
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
3434
use rustc_codegen_ssa::back::write::{
3535
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
3636
};
@@ -178,14 +178,14 @@ impl WriteBackendMethods for LlvmCodegenBackend {
178178
cgcx: &CodegenContext<Self>,
179179
modules: Vec<FatLtoInput<Self>>,
180180
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
181-
) -> Result<LtoModuleCodegen<Self>, FatalError> {
181+
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
182182
back::lto::run_fat(cgcx, modules, cached_modules)
183183
}
184184
fn run_thin_lto(
185185
cgcx: &CodegenContext<Self>,
186186
modules: Vec<(String, Self::ThinBuffer)>,
187187
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
188-
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
188+
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
189189
back::lto::run_thin(cgcx, modules, cached_modules)
190190
}
191191
fn optimize(

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
use std::ffi::CString;
22
use std::sync::Arc;
33

4-
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
54
use rustc_data_structures::memmap::Mmap;
6-
use rustc_errors::FatalError;
75

8-
use super::write::CodegenContext;
9-
use crate::ModuleCodegen;
10-
use crate::back::write::ModuleConfig;
116
use crate::traits::*;
127

138
pub struct ThinModule<B: WriteBackendMethods> {
@@ -42,61 +37,6 @@ pub struct ThinShared<B: WriteBackendMethods> {
4237
pub module_names: Vec<CString>,
4338
}
4439

45-
pub enum LtoModuleCodegen<B: WriteBackendMethods> {
46-
Fat(ModuleCodegen<B::Module>),
47-
Thin(ThinModule<B>),
48-
}
49-
50-
impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
51-
pub fn name(&self) -> &str {
52-
match *self {
53-
LtoModuleCodegen::Fat(_) => "everything",
54-
LtoModuleCodegen::Thin(ref m) => m.name(),
55-
}
56-
}
57-
58-
/// Optimize this module within the given codegen context.
59-
pub fn optimize(
60-
self,
61-
cgcx: &CodegenContext<B>,
62-
) -> Result<ModuleCodegen<B::Module>, FatalError> {
63-
match self {
64-
LtoModuleCodegen::Fat(mut module) => {
65-
B::optimize_fat(cgcx, &mut module)?;
66-
Ok(module)
67-
}
68-
LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin),
69-
}
70-
}
71-
72-
/// A "gauge" of how costly it is to optimize this module, used to sort
73-
/// biggest modules first.
74-
pub fn cost(&self) -> u64 {
75-
match *self {
76-
// Only one module with fat LTO, so the cost doesn't matter.
77-
LtoModuleCodegen::Fat(_) => 0,
78-
LtoModuleCodegen::Thin(ref m) => m.cost(),
79-
}
80-
}
81-
82-
/// Run autodiff on Fat LTO module
83-
pub fn autodiff(
84-
self,
85-
cgcx: &CodegenContext<B>,
86-
diff_fncs: Vec<AutoDiffItem>,
87-
config: &ModuleConfig,
88-
) -> Result<LtoModuleCodegen<B>, FatalError> {
89-
match &self {
90-
LtoModuleCodegen::Fat(module) => {
91-
B::autodiff(cgcx, &module, diff_fncs, config)?;
92-
}
93-
_ => panic!("autodiff called with non-fat LTO module"),
94-
}
95-
96-
Ok(self)
97-
}
98-
}
99-
10040
pub enum SerializedModule<M: ModuleBufferMethods> {
10141
Local(M),
10242
FromRlib(Vec<u8>),

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,16 @@ fn generate_lto_work<B: ExtraBackendMethods>(
408408

409409
if !needs_fat_lto.is_empty() {
410410
assert!(needs_thin_lto.is_empty());
411-
let mut module =
411+
let module =
412412
B::run_fat_lto(cgcx, needs_fat_lto, import_only_modules).unwrap_or_else(|e| e.raise());
413413
if cgcx.lto == Lto::Fat && !autodiff.is_empty() {
414414
let config = cgcx.config(ModuleKind::Regular);
415-
module = module.autodiff(cgcx, autodiff, config).unwrap_or_else(|e| e.raise());
415+
if let Err(err) = B::autodiff(cgcx, &module, autodiff, config) {
416+
err.raise();
417+
}
416418
}
417419
// We are adding a single work item, so the cost doesn't matter.
418-
vec![(WorkItem::LTO(module), 0)]
420+
vec![(WorkItem::FatLto(module), 0)]
419421
} else {
420422
if !autodiff.is_empty() {
421423
let dcx = cgcx.create_dcx();
@@ -428,7 +430,7 @@ fn generate_lto_work<B: ExtraBackendMethods>(
428430
.into_iter()
429431
.map(|module| {
430432
let cost = module.cost();
431-
(WorkItem::LTO(module), cost)
433+
(WorkItem::ThinLto(module), cost)
432434
})
433435
.chain(copy_jobs.into_iter().map(|wp| {
434436
(
@@ -736,15 +738,19 @@ pub(crate) enum WorkItem<B: WriteBackendMethods> {
736738
/// Copy the post-LTO artifacts from the incremental cache to the output
737739
/// directory.
738740
CopyPostLtoArtifacts(CachedModuleCodegen),
739-
/// Performs (Thin)LTO on the given module.
740-
LTO(lto::LtoModuleCodegen<B>),
741+
/// Performs fat LTO on the given module.
742+
FatLto(ModuleCodegen<B::Module>),
743+
/// Performs thin-LTO on the given module.
744+
ThinLto(lto::ThinModule<B>),
741745
}
742746

743747
impl<B: WriteBackendMethods> WorkItem<B> {
744748
fn module_kind(&self) -> ModuleKind {
745749
match *self {
746750
WorkItem::Optimize(ref m) => m.kind,
747-
WorkItem::CopyPostLtoArtifacts(_) | WorkItem::LTO(_) => ModuleKind::Regular,
751+
WorkItem::CopyPostLtoArtifacts(_) | WorkItem::FatLto(_) | WorkItem::ThinLto(_) => {
752+
ModuleKind::Regular
753+
}
748754
}
749755
}
750756

@@ -792,7 +798,8 @@ impl<B: WriteBackendMethods> WorkItem<B> {
792798
match self {
793799
WorkItem::Optimize(m) => desc("opt", "optimize module", &m.name),
794800
WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for", &m.name),
795-
WorkItem::LTO(m) => desc("lto", "LTO module", m.name()),
801+
WorkItem::FatLto(_) => desc("lto", "fat LTO module", "everything"),
802+
WorkItem::ThinLto(m) => desc("lto", "thin-LTO module", m.name()),
796803
}
797804
}
798805
}
@@ -996,12 +1003,21 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
9961003
})
9971004
}
9981005

999-
fn execute_lto_work_item<B: ExtraBackendMethods>(
1006+
fn execute_fat_lto_work_item<B: ExtraBackendMethods>(
1007+
cgcx: &CodegenContext<B>,
1008+
mut module: ModuleCodegen<B::Module>,
1009+
module_config: &ModuleConfig,
1010+
) -> Result<WorkItemResult<B>, FatalError> {
1011+
B::optimize_fat(cgcx, &mut module)?;
1012+
finish_intra_module_work(cgcx, module, module_config)
1013+
}
1014+
1015+
fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
10001016
cgcx: &CodegenContext<B>,
1001-
module: lto::LtoModuleCodegen<B>,
1017+
module: lto::ThinModule<B>,
10021018
module_config: &ModuleConfig,
10031019
) -> Result<WorkItemResult<B>, FatalError> {
1004-
let module = module.optimize(cgcx)?;
1020+
let module = B::optimize_thin(cgcx, module)?;
10051021
finish_intra_module_work(cgcx, module, module_config)
10061022
}
10071023

@@ -1842,10 +1858,16 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
18421858
);
18431859
Ok(execute_copy_from_cache_work_item(&cgcx, m, module_config))
18441860
}
1845-
WorkItem::LTO(m) => {
1861+
WorkItem::FatLto(m) => {
1862+
let _timer = cgcx
1863+
.prof
1864+
.generic_activity_with_arg("codegen_module_perform_lto", "everything");
1865+
execute_fat_lto_work_item(&cgcx, m, module_config)
1866+
}
1867+
WorkItem::ThinLto(m) => {
18461868
let _timer =
18471869
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name());
1848-
execute_lto_work_item(&cgcx, m, module_config)
1870+
execute_thin_lto_work_item(&cgcx, m, module_config)
18491871
}
18501872
})
18511873
};

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
22
use rustc_errors::{DiagCtxtHandle, FatalError};
33
use rustc_middle::dep_graph::WorkProduct;
44

5-
use crate::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
5+
use crate::back::lto::{SerializedModule, ThinModule};
66
use crate::back::write::{CodegenContext, FatLtoInput, ModuleConfig};
77
use crate::{CompiledModule, ModuleCodegen};
88

@@ -26,15 +26,15 @@ pub trait WriteBackendMethods: Clone + 'static {
2626
cgcx: &CodegenContext<Self>,
2727
modules: Vec<FatLtoInput<Self>>,
2828
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
29-
) -> Result<LtoModuleCodegen<Self>, FatalError>;
29+
) -> Result<ModuleCodegen<Self::Module>, FatalError>;
3030
/// Performs thin LTO by performing necessary global analysis and returning two
3131
/// lists, one of the modules that need optimization and another for modules that
3232
/// can simply be copied over from the incr. comp. cache.
3333
fn run_thin_lto(
3434
cgcx: &CodegenContext<Self>,
3535
modules: Vec<(String, Self::ThinBuffer)>,
3636
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
37-
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError>;
37+
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError>;
3838
fn print_pass_timings(&self);
3939
fn print_statistics(&self);
4040
fn optimize(

0 commit comments

Comments
 (0)