Skip to content

Commit 4878456

Browse files
committed
Fuse codegen into LTO optimize methods
1 parent 9e4c713 commit 4878456

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
@@ -93,7 +93,7 @@ use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetCon
9393
use rustc_data_structures::fx::FxIndexMap;
9494
use rustc_data_structures::profiling::SelfProfilerRef;
9595
use rustc_data_structures::sync::IntoDynSyncSend;
96-
use rustc_errors::DiagCtxtHandle;
96+
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
9797
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
9898
use rustc_middle::ty::TyCtxt;
9999
use rustc_middle::util::Providers;
@@ -438,7 +438,7 @@ impl WriteBackendMethods for GccCodegenBackend {
438438
type ThinData = ();
439439
type ThinBuffer = ThinBuffer;
440440

441-
fn run_and_optimize_fat_lto(
441+
fn optimize_and_codegen_fat_lto(
442442
cgcx: &CodegenContext,
443443
prof: &SelfProfilerRef,
444444
shared_emitter: &SharedEmitter,
@@ -447,7 +447,7 @@ impl WriteBackendMethods for GccCodegenBackend {
447447
_exported_symbols_for_lto: &[String],
448448
each_linked_rlib_for_lto: &[PathBuf],
449449
modules: Vec<FatLtoInput<Self>>,
450-
) -> ModuleCodegen<Self::Module> {
450+
) -> CompiledModule {
451451
back::lto::run_fat(cgcx, prof, shared_emitter, each_linked_rlib_for_lto, modules)
452452
}
453453

@@ -474,13 +474,13 @@ impl WriteBackendMethods for GccCodegenBackend {
474474
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
475475
}
476476

477-
fn optimize_thin(
477+
fn optimize_and_codegen_thin(
478478
_cgcx: &CodegenContext,
479479
_prof: &SelfProfilerRef,
480480
_shared_emitter: &SharedEmitter,
481481
_tm_factory: TargetMachineFactoryFn<Self>,
482482
_thin: ThinModule<Self>,
483-
) -> ModuleCodegen<Self::Module> {
483+
) -> CompiledModule {
484484
unreachable!()
485485
}
486486

@@ -491,7 +491,9 @@ impl WriteBackendMethods for GccCodegenBackend {
491491
module: ModuleCodegen<Self::Module>,
492492
config: &ModuleConfig,
493493
) -> CompiledModule {
494-
back::write::codegen(cgcx, prof, shared_emitter, module, config)
494+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
495+
let dcx = dcx.handle();
496+
back::write::codegen(cgcx, prof, dcx, module, config)
495497
}
496498

497499
fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {

compiler/rustc_codegen_llvm/src/back/lto.rs

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

2727
use crate::back::write::{
28-
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, save_temp_bitcode,
28+
self, CodegenDiagnosticsStage, DiagnosticHandlers, bitcode_section_name, codegen,
29+
save_temp_bitcode,
2930
};
3031
use crate::errors::{LlvmError, LtoBitcodeFromRlib};
3132
use crate::llvm::{self, build_string};
@@ -745,13 +746,13 @@ impl Drop for ThinBuffer {
745746
}
746747
}
747748

748-
pub(crate) fn optimize_thin_module(
749+
pub(crate) fn optimize_and_codegen_thin_module(
749750
cgcx: &CodegenContext,
750751
prof: &SelfProfilerRef,
751752
shared_emitter: &SharedEmitter,
752753
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
753754
thin_module: ThinModule<LlvmCodegenBackend>,
754-
) -> ModuleCodegen<ModuleLlvm> {
755+
) -> CompiledModule {
755756
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
756757
let dcx = dcx.handle();
757758

@@ -830,7 +831,7 @@ pub(crate) fn optimize_thin_module(
830831
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
831832
}
832833
}
833-
module
834+
codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
834835
}
835836

836837
/// 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
@@ -136,15 +136,15 @@ impl WriteBackendMethods for LlvmCodegenBackend {
136136
type TargetMachine = OwnedTargetMachine;
137137
type ThinData = back::lto::ThinData;
138138
type ThinBuffer = back::lto::ThinBuffer;
139-
fn run_and_optimize_fat_lto(
139+
fn optimize_and_codegen_fat_lto(
140140
cgcx: &CodegenContext,
141141
prof: &SelfProfilerRef,
142142
shared_emitter: &SharedEmitter,
143143
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
144144
exported_symbols_for_lto: &[String],
145145
each_linked_rlib_for_lto: &[PathBuf],
146146
modules: Vec<FatLtoInput<Self>>,
147-
) -> ModuleCodegen<Self::Module> {
147+
) -> CompiledModule {
148148
let mut module = back::lto::run_fat(
149149
cgcx,
150150
prof,
@@ -159,7 +159,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
159159
let dcx = dcx.handle();
160160
back::lto::run_pass_manager(cgcx, prof, dcx, &mut module, false);
161161

162-
module
162+
back::write::codegen(cgcx, prof, shared_emitter, module, &cgcx.module_config)
163163
}
164164
fn run_thin_lto(
165165
cgcx: &CodegenContext,
@@ -189,14 +189,14 @@ impl WriteBackendMethods for LlvmCodegenBackend {
189189
) {
190190
back::write::optimize(cgcx, prof, shared_emitter, module, config)
191191
}
192-
fn optimize_thin(
192+
fn optimize_and_codegen_thin(
193193
cgcx: &CodegenContext,
194194
prof: &SelfProfilerRef,
195195
shared_emitter: &SharedEmitter,
196196
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
197197
thin: ThinModule<Self>,
198-
) -> ModuleCodegen<Self::Module> {
199-
back::lto::optimize_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
198+
) -> CompiledModule {
199+
back::lto::optimize_and_codegen_thin_module(cgcx, prof, shared_emitter, tm_factory, thin)
200200
}
201201
fn codegen(
202202
cgcx: &CodegenContext,

compiler/rustc_codegen_ssa/src/back/write.rs

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

1009-
let module = B::run_and_optimize_fat_lto(
1009+
B::optimize_and_codegen_fat_lto(
10101010
cgcx,
10111011
prof,
10121012
&shared_emitter,
10131013
tm_factory,
10141014
exported_symbols_for_lto,
10151015
each_linked_rlib_for_lto,
10161016
needs_fat_lto,
1017-
);
1018-
B::codegen(cgcx, prof, &shared_emitter, module, &cgcx.module_config)
1017+
)
10191018
}
10201019

10211020
fn do_thin_lto<B: ExtraBackendMethods>(
@@ -1180,8 +1179,7 @@ fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
11801179
) -> CompiledModule {
11811180
let _timer = prof.generic_activity_with_arg("codegen_module_perform_lto", module.name());
11821181

1183-
let module = B::optimize_thin(cgcx, prof, &shared_emitter, tm_factory, module);
1184-
B::codegen(cgcx, prof, &shared_emitter, module, &cgcx.module_config)
1182+
B::optimize_and_codegen_thin(cgcx, prof, &shared_emitter, tm_factory, module)
11851183
}
11861184

11871185
/// 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
@@ -19,15 +19,15 @@ pub trait WriteBackendMethods: Clone + 'static {
1919

2020
/// Performs fat LTO by merging all modules into a single one, running autodiff
2121
/// if necessary and running any further optimizations
22-
fn run_and_optimize_fat_lto(
22+
fn optimize_and_codegen_fat_lto(
2323
cgcx: &CodegenContext,
2424
prof: &SelfProfilerRef,
2525
shared_emitter: &SharedEmitter,
2626
tm_factory: TargetMachineFactoryFn<Self>,
2727
exported_symbols_for_lto: &[String],
2828
each_linked_rlib_for_lto: &[PathBuf],
2929
modules: Vec<FatLtoInput<Self>>,
30-
) -> ModuleCodegen<Self::Module>;
30+
) -> CompiledModule;
3131
/// Performs thin LTO by performing necessary global analysis and returning two
3232
/// lists, one of the modules that need optimization and another for modules that
3333
/// can simply be copied over from the incr. comp. cache.
@@ -47,13 +47,13 @@ pub trait WriteBackendMethods: Clone + 'static {
4747
module: &mut ModuleCodegen<Self::Module>,
4848
config: &ModuleConfig,
4949
);
50-
fn optimize_thin(
50+
fn optimize_and_codegen_thin(
5151
cgcx: &CodegenContext,
5252
prof: &SelfProfilerRef,
5353
shared_emitter: &SharedEmitter,
5454
tm_factory: TargetMachineFactoryFn<Self>,
5555
thin: ThinModule<Self>,
56-
) -> ModuleCodegen<Self::Module>;
56+
) -> CompiledModule;
5757
fn codegen(
5858
cgcx: &CodegenContext,
5959
prof: &SelfProfilerRef,

0 commit comments

Comments
 (0)