Skip to content

Commit eff0d4c

Browse files
committed
Fuse codegen into LTO optimize methods
1 parent 22d4bb2 commit eff0d4c

7 files changed

Lines changed: 36 additions & 40 deletions

File tree

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ use object::read::archive::ArchiveFile;
2626
use rustc_codegen_ssa::back::lto::SerializedModule;
2727
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput, SharedEmitter};
2828
use rustc_codegen_ssa::traits::*;
29-
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
29+
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind, looks_like_rust_object_file};
3030
use rustc_data_structures::memmap::Mmap;
3131
use rustc_data_structures::profiling::SelfProfilerRef;
3232
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
3333
use rustc_log::tracing::info;
3434
use rustc_session::config::Lto;
3535
use tempfile::{TempDir, tempdir};
3636

37-
use crate::back::write::save_temp_bitcode;
37+
use crate::back::write::{codegen, save_temp_bitcode};
3838
use crate::errors::LtoBitcodeFromRlib;
3939
use crate::{GccCodegenBackend, GccContext, LtoMode, to_gcc_opt_level};
4040

@@ -112,7 +112,7 @@ pub(crate) fn run_fat(
112112
shared_emitter: &SharedEmitter,
113113
each_linked_rlib_for_lto: &[PathBuf],
114114
modules: Vec<FatLtoInput<GccCodegenBackend>>,
115-
) -> ModuleCodegen<GccContext> {
115+
) -> CompiledModule {
116116
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
117117
let dcx = dcx.handle();
118118
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
@@ -132,12 +132,12 @@ pub(crate) fn run_fat(
132132
fn fat_lto(
133133
cgcx: &CodegenContext,
134134
prof: &SelfProfilerRef,
135-
_dcx: DiagCtxtHandle<'_>,
135+
dcx: DiagCtxtHandle<'_>,
136136
modules: Vec<FatLtoInput<GccCodegenBackend>>,
137137
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
138138
tmp_path: TempDir,
139139
//symbols_below_threshold: &[String],
140-
) -> ModuleCodegen<GccContext> {
140+
) -> CompiledModule {
141141
let _timer = prof.generic_activity("GCC_fat_lto_build_monolithic_module");
142142
info!("going for a fat lto");
143143

@@ -260,7 +260,7 @@ fn fat_lto(
260260
// of now.
261261
module.module_llvm.temp_dir = Some(tmp_path);
262262

263-
module
263+
codegen(cgcx, prof, dcx, module, &cgcx.module_config)
264264
}
265265

266266
pub struct ModuleBuffer(PathBuf);

compiler/rustc_codegen_gcc/src/back/write.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ use std::{env, fs};
22

33
use gccjit::{Context, OutputKind};
44
use rustc_codegen_ssa::back::link::ensure_removed;
5-
use rustc_codegen_ssa::back::write::{
6-
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, SharedEmitter,
7-
};
5+
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
86
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
97
use rustc_data_structures::profiling::SelfProfilerRef;
10-
use rustc_errors::DiagCtxt;
8+
use rustc_errors::DiagCtxtHandle;
119
use rustc_fs_util::link_or_copy;
1210
use rustc_log::tracing::debug;
1311
use rustc_session::config::OutputType;
@@ -20,13 +18,10 @@ use crate::{GccContext, LtoMode};
2018
pub(crate) fn codegen(
2119
cgcx: &CodegenContext,
2220
prof: &SelfProfilerRef,
23-
shared_emitter: &SharedEmitter,
21+
dcx: DiagCtxtHandle<'_>,
2422
module: ModuleCodegen<GccContext>,
2523
config: &ModuleConfig,
2624
) -> CompiledModule {
27-
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
28-
let dcx = dcx.handle();
29-
3025
let _timer = prof.generic_activity_with_arg("GCC_module_codegen", &*module.name);
3126
{
3227
let context = &module.module_llvm.context;

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use rustc_codegen_ssa::{CompiledModule, CompiledModules, CrateInfo, ModuleCodege
9292
use rustc_data_structures::fx::FxIndexMap;
9393
use rustc_data_structures::profiling::SelfProfilerRef;
9494
use rustc_data_structures::sync::IntoDynSyncSend;
95-
use rustc_errors::DiagCtxtHandle;
95+
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
9696
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
9797
use rustc_middle::ty::TyCtxt;
9898
use rustc_middle::util::Providers;
@@ -429,7 +429,7 @@ impl WriteBackendMethods for GccCodegenBackend {
429429
type ModuleBuffer = ModuleBuffer;
430430
type ThinData = ();
431431

432-
fn run_and_optimize_fat_lto(
432+
fn optimize_and_codegen_fat_lto(
433433
cgcx: &CodegenContext,
434434
prof: &SelfProfilerRef,
435435
shared_emitter: &SharedEmitter,
@@ -438,7 +438,7 @@ impl WriteBackendMethods for GccCodegenBackend {
438438
_exported_symbols_for_lto: &[String],
439439
each_linked_rlib_for_lto: &[PathBuf],
440440
modules: Vec<FatLtoInput<Self>>,
441-
) -> ModuleCodegen<Self::Module> {
441+
) -> CompiledModule {
442442
back::lto::run_fat(cgcx, prof, shared_emitter, each_linked_rlib_for_lto, modules)
443443
}
444444

@@ -465,13 +465,13 @@ impl WriteBackendMethods for GccCodegenBackend {
465465
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
466466
}
467467

468-
fn optimize_thin(
468+
fn optimize_and_codegen_thin(
469469
_cgcx: &CodegenContext,
470470
_prof: &SelfProfilerRef,
471471
_shared_emitter: &SharedEmitter,
472472
_tm_factory: TargetMachineFactoryFn<Self>,
473473
_thin: ThinModule<Self>,
474-
) -> ModuleCodegen<Self::Module> {
474+
) -> CompiledModule {
475475
unreachable!()
476476
}
477477

@@ -482,7 +482,9 @@ impl WriteBackendMethods for GccCodegenBackend {
482482
module: ModuleCodegen<Self::Module>,
483483
config: &ModuleConfig,
484484
) -> CompiledModule {
485-
back::write::codegen(cgcx, prof, shared_emitter, module, config)
485+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
486+
let dcx = dcx.handle();
487+
back::write::codegen(cgcx, prof, dcx, module, config)
486488
}
487489

488490
fn serialize_module(_module: Self::Module, _is_thin: bool) -> Self::ModuleBuffer {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_codegen_ssa::back::write::{
1212
CodegenContext, FatLtoInput, SharedEmitter, TargetMachineFactoryFn,
1313
};
1414
use rustc_codegen_ssa::traits::*;
15-
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
15+
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1616
use rustc_data_structures::fx::FxHashMap;
1717
use rustc_data_structures::memmap::Mmap;
1818
use rustc_data_structures::profiling::SelfProfilerRef;
@@ -24,7 +24,8 @@ use rustc_session::config::{self, Lto};
2424
use tracing::{debug, info};
2525

2626
use crate::back::write::{
27-
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, save_temp_bitcode,
27+
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, codegen,
28+
save_temp_bitcode,
2829
};
2930
use crate::errors::{LlvmError, LtoBitcodeFromRlib};
3031
use crate::llvm::{self, build_string};
@@ -709,13 +710,13 @@ impl ModuleBufferMethods for ModuleBuffer {
709710
}
710711
}
711712

712-
pub(crate) fn optimize_thin_module(
713+
pub(crate) fn optimize_and_codegen_thin_module(
713714
cgcx: &CodegenContext,
714715
prof: &SelfProfilerRef,
715716
shared_emitter: &SharedEmitter,
716717
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
717718
thin_module: ThinModule<LlvmCodegenBackend>,
718-
) -> ModuleCodegen<ModuleLlvm> {
719+
) -> CompiledModule {
719720
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
720721
let dcx = dcx.handle();
721722

@@ -794,7 +795,7 @@ pub(crate) fn optimize_thin_module(
794795
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
795796
}
796797
}
797-
module
798+
codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
798799
}
799800

800801
/// Maps LLVM module identifiers to their corresponding LLVM LTO cache keys

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ impl WriteBackendMethods for LlvmCodegenBackend {
135135
type ModuleBuffer = back::lto::ModuleBuffer;
136136
type TargetMachine = OwnedTargetMachine;
137137
type ThinData = back::lto::ThinData;
138-
fn run_and_optimize_fat_lto(
138+
fn optimize_and_codegen_fat_lto(
139139
cgcx: &CodegenContext,
140140
prof: &SelfProfilerRef,
141141
shared_emitter: &SharedEmitter,
142142
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
143143
exported_symbols_for_lto: &[String],
144144
each_linked_rlib_for_lto: &[PathBuf],
145145
modules: Vec<FatLtoInput<Self>>,
146-
) -> ModuleCodegen<Self::Module> {
146+
) -> CompiledModule {
147147
let mut module = back::lto::run_fat(
148148
cgcx,
149149
prof,
@@ -158,7 +158,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
158158
let dcx = dcx.handle();
159159
back::lto::run_pass_manager(cgcx, prof, dcx, &mut module, false);
160160

161-
module
161+
back::write::codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
162162
}
163163
fn run_thin_lto(
164164
cgcx: &CodegenContext,
@@ -188,14 +188,14 @@ impl WriteBackendMethods for LlvmCodegenBackend {
188188
) {
189189
back::write::optimize(cgcx, prof, shared_emitter, module, config)
190190
}
191-
fn optimize_thin(
191+
fn optimize_and_codegen_thin(
192192
cgcx: &CodegenContext,
193193
prof: &SelfProfilerRef,
194194
shared_emitter: &SharedEmitter,
195195
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
196196
thin: ThinModule<Self>,
197-
) -> ModuleCodegen<Self::Module> {
198-
back::lto::optimize_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
197+
) -> CompiledModule {
198+
back::lto::optimize_and_codegen_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
199199
}
200200
fn codegen(
201201
cgcx: &CodegenContext,

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,16 +988,15 @@ fn do_fat_lto<B: ExtraBackendMethods>(
988988
needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, buffer: module })
989989
}
990990

991-
let module = B::run_and_optimize_fat_lto(
991+
B::optimize_and_codegen_fat_lto(
992992
cgcx,
993993
prof,
994994
&shared_emitter,
995995
tm_factory,
996996
exported_symbols_for_lto,
997997
each_linked_rlib_for_lto,
998998
needs_fat_lto,
999-
);
1000-
B::codegen(cgcx, prof, &shared_emitter, module, &cgcx.module_config)
999+
)
10011000
}
10021001

10031002
fn do_thin_lto<B: ExtraBackendMethods>(
@@ -1162,8 +1161,7 @@ fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
11621161
) -> CompiledModule {
11631162
let _timer = prof.generic_activity_with_arg("codegen_module_perform_lto", module.name());
11641163

1165-
let module = B::optimize_thin(cgcx, prof, &shared_emitter, tm_factory, module);
1166-
B::codegen(cgcx, prof, &shared_emitter, module, &cgcx.module_config)
1164+
B::optimize_and_codegen_thin(cgcx, prof, &shared_emitter, tm_factory, module)
11671165
}
11681166

11691167
/// Messages sent to the coordinator.

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ pub trait WriteBackendMethods: Clone + 'static {
1818

1919
/// Performs fat LTO by merging all modules into a single one, running autodiff
2020
/// if necessary and running any further optimizations
21-
fn run_and_optimize_fat_lto(
21+
fn optimize_and_codegen_fat_lto(
2222
cgcx: &CodegenContext,
2323
prof: &SelfProfilerRef,
2424
shared_emitter: &SharedEmitter,
2525
tm_factory: TargetMachineFactoryFn<Self>,
2626
exported_symbols_for_lto: &[String],
2727
each_linked_rlib_for_lto: &[PathBuf],
2828
modules: Vec<FatLtoInput<Self>>,
29-
) -> ModuleCodegen<Self::Module>;
29+
) -> CompiledModule;
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.
@@ -46,13 +46,13 @@ pub trait WriteBackendMethods: Clone + 'static {
4646
module: &mut ModuleCodegen<Self::Module>,
4747
config: &ModuleConfig,
4848
);
49-
fn optimize_thin(
49+
fn optimize_and_codegen_thin(
5050
cgcx: &CodegenContext,
5151
prof: &SelfProfilerRef,
5252
shared_emitter: &SharedEmitter,
5353
tm_factory: TargetMachineFactoryFn<Self>,
5454
thin: ThinModule<Self>,
55-
) -> ModuleCodegen<Self::Module>;
55+
) -> CompiledModule;
5656
fn codegen(
5757
cgcx: &CodegenContext,
5858
prof: &SelfProfilerRef,

0 commit comments

Comments
 (0)