Skip to content

Commit 7ff6135

Browse files
authored
Rollup merge of #143388 - bjorn3:lto_refactors, r=compiler-errors
Various refactors to the LTO handling code In particular reducing the sharing of code paths between fat and thin-LTO and making the fat LTO implementation more self-contained. This also moves some autodiff handling out of cg_ssa into cg_llvm given that Enzyme only works with LLVM anyway and an implementation for another backend may do things entirely differently. This will also make it a bit easier to split LTO handling out of the coordinator thread main loop into a separate loop, which should reduce the complexity of the coordinator thread.
2 parents 48117e8 + bf4daef commit 7ff6135

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

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

src/back/write.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ use crate::{GccCodegenBackend, GccContext};
1616

1717
pub(crate) fn codegen(
1818
cgcx: &CodegenContext<GccCodegenBackend>,
19-
dcx: DiagCtxtHandle<'_>,
2019
module: ModuleCodegen<GccContext>,
2120
config: &ModuleConfig,
2221
) -> Result<CompiledModule, FatalError> {
22+
let dcx = cgcx.create_dcx();
23+
let dcx = dcx.handle();
24+
2325
let _timer = cgcx.prof.generic_activity_with_arg("GCC_module_codegen", &*module.name);
2426
{
2527
let context = &module.module_llvm.context;

src/lib.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use gccjit::{CType, Context, OptimizationLevel};
9393
use gccjit::{TargetInfo, Version};
9494
use rustc_ast::expand::allocator::AllocatorKind;
9595
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
96-
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
96+
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
9797
use rustc_codegen_ssa::back::write::{
9898
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
9999
};
@@ -353,19 +353,24 @@ impl WriteBackendMethods for GccCodegenBackend {
353353
type ThinData = ThinData;
354354
type ThinBuffer = ThinBuffer;
355355

356-
fn run_fat_lto(
356+
fn run_and_optimize_fat_lto(
357357
cgcx: &CodegenContext<Self>,
358358
modules: Vec<FatLtoInput<Self>>,
359359
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
360-
) -> Result<LtoModuleCodegen<Self>, FatalError> {
360+
diff_fncs: Vec<AutoDiffItem>,
361+
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
362+
if !diff_fncs.is_empty() {
363+
unimplemented!();
364+
}
365+
361366
back::lto::run_fat(cgcx, modules, cached_modules)
362367
}
363368

364369
fn run_thin_lto(
365370
cgcx: &CodegenContext<Self>,
366371
modules: Vec<(String, Self::ThinBuffer)>,
367372
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
368-
) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
373+
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
369374
back::lto::run_thin(cgcx, modules, cached_modules)
370375
}
371376

@@ -387,14 +392,6 @@ impl WriteBackendMethods for GccCodegenBackend {
387392
Ok(())
388393
}
389394

390-
fn optimize_fat(
391-
_cgcx: &CodegenContext<Self>,
392-
_module: &mut ModuleCodegen<Self::Module>,
393-
) -> Result<(), FatalError> {
394-
// TODO(antoyo)
395-
Ok(())
396-
}
397-
398395
fn optimize_thin(
399396
cgcx: &CodegenContext<Self>,
400397
thin: ThinModule<Self>,
@@ -404,11 +401,10 @@ impl WriteBackendMethods for GccCodegenBackend {
404401

405402
fn codegen(
406403
cgcx: &CodegenContext<Self>,
407-
dcx: DiagCtxtHandle<'_>,
408404
module: ModuleCodegen<Self::Module>,
409405
config: &ModuleConfig,
410406
) -> Result<CompiledModule, FatalError> {
411-
back::write::codegen(cgcx, dcx, module, config)
407+
back::write::codegen(cgcx, module, config)
412408
}
413409

414410
fn prepare_thin(
@@ -429,15 +425,6 @@ impl WriteBackendMethods for GccCodegenBackend {
429425
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
430426
back::write::link(cgcx, dcx, modules)
431427
}
432-
433-
fn autodiff(
434-
_cgcx: &CodegenContext<Self>,
435-
_module: &ModuleCodegen<Self::Module>,
436-
_diff_functions: Vec<AutoDiffItem>,
437-
_config: &ModuleConfig,
438-
) -> Result<(), FatalError> {
439-
unimplemented!()
440-
}
441428
}
442429

443430
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit

0 commit comments

Comments
 (0)