Skip to content

Commit 2206d93

Browse files
authored
Rollup merge of #149209 - lto_refactors8, r=jackh726
Move LTO to OngoingCodegen::join This will make it easier to in the future move all this code to link_binary. Follow up to #147810 Part of rust-lang/compiler-team#908
2 parents d37351b + b13bb4b commit 2206d93

13 files changed

Lines changed: 326 additions & 179 deletions

File tree

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ use std::sync::atomic::Ordering;
2626
use gccjit::{Context, OutputKind};
2727
use object::read::archive::ArchiveFile;
2828
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
29-
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
29+
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput, SharedEmitter};
3030
use rustc_codegen_ssa::traits::*;
3131
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
3232
use rustc_data_structures::memmap::Mmap;
33-
use rustc_errors::DiagCtxtHandle;
33+
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
3434
use rustc_log::tracing::info;
3535
use rustc_middle::bug;
3636
use rustc_middle::dep_graph::WorkProduct;
@@ -112,10 +112,11 @@ fn save_as_file(obj: &[u8], path: &Path) -> Result<(), LtoBitcodeFromRlib> {
112112
/// for further optimization.
113113
pub(crate) fn run_fat(
114114
cgcx: &CodegenContext<GccCodegenBackend>,
115+
shared_emitter: &SharedEmitter,
115116
each_linked_rlib_for_lto: &[PathBuf],
116117
modules: Vec<FatLtoInput<GccCodegenBackend>>,
117118
) -> ModuleCodegen<GccContext> {
118-
let dcx = cgcx.create_dcx();
119+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
119120
let dcx = dcx.handle();
120121
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
121122
/*let symbols_below_threshold =
@@ -283,14 +284,13 @@ impl ModuleBufferMethods for ModuleBuffer {
283284
/// can simply be copied over from the incr. comp. cache.
284285
pub(crate) fn run_thin(
285286
cgcx: &CodegenContext<GccCodegenBackend>,
287+
dcx: DiagCtxtHandle<'_>,
286288
each_linked_rlib_for_lto: &[PathBuf],
287289
modules: Vec<(String, ThinBuffer)>,
288290
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
289291
) -> (Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>) {
290-
let dcx = cgcx.create_dcx();
291-
let dcx = dcx.handle();
292292
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
293-
if cgcx.opts.cg.linker_plugin_lto.enabled() {
293+
if cgcx.use_linker_plugin_lto {
294294
unreachable!(
295295
"We should never reach this case if the LTO step \
296296
is deferred to the linker"
@@ -522,8 +522,6 @@ pub fn optimize_thin_module(
522522
thin_module: ThinModule<GccCodegenBackend>,
523523
_cgcx: &CodegenContext<GccCodegenBackend>,
524524
) -> ModuleCodegen<GccContext> {
525-
//let dcx = cgcx.create_dcx();
526-
527525
//let module_name = &thin_module.shared.module_names[thin_module.idx];
528526
/*let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, module_name.to_str().unwrap());
529527
let tm = (cgcx.tm_factory)(tm_factory_config).map_err(|e| write::llvm_err(&dcx, e))?;*/

compiler/rustc_codegen_gcc/src/back/write.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ 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::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
5+
use rustc_codegen_ssa::back::write::{
6+
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, SharedEmitter,
7+
};
68
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
9+
use rustc_errors::DiagCtxt;
710
use rustc_fs_util::link_or_copy;
811
use rustc_log::tracing::debug;
912
use rustc_session::config::OutputType;
@@ -15,10 +18,11 @@ use crate::{GccCodegenBackend, GccContext, LtoMode};
1518

1619
pub(crate) fn codegen(
1720
cgcx: &CodegenContext<GccCodegenBackend>,
21+
shared_emitter: &SharedEmitter,
1822
module: ModuleCodegen<GccContext>,
1923
config: &ModuleConfig,
2024
) -> CompiledModule {
21-
let dcx = cgcx.create_dcx();
25+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
2226
let dcx = dcx.handle();
2327

2428
let _timer = cgcx.prof.generic_activity_with_arg("GCC_module_codegen", &*module.name);

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use gccjit::{TargetInfo, Version};
8484
use rustc_ast::expand::allocator::AllocatorMethod;
8585
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
8686
use rustc_codegen_ssa::back::write::{
87-
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
87+
CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn,
8888
};
8989
use rustc_codegen_ssa::base::codegen_crate;
9090
use rustc_codegen_ssa::target_features::cfg_target_feature;
@@ -435,23 +435,25 @@ impl WriteBackendMethods for GccCodegenBackend {
435435

436436
fn run_and_optimize_fat_lto(
437437
cgcx: &CodegenContext<Self>,
438+
shared_emitter: &SharedEmitter,
438439
// FIXME(bjorn3): Limit LTO exports to these symbols
439440
_exported_symbols_for_lto: &[String],
440441
each_linked_rlib_for_lto: &[PathBuf],
441442
modules: Vec<FatLtoInput<Self>>,
442443
) -> ModuleCodegen<Self::Module> {
443-
back::lto::run_fat(cgcx, each_linked_rlib_for_lto, modules)
444+
back::lto::run_fat(cgcx, shared_emitter, each_linked_rlib_for_lto, modules)
444445
}
445446

446447
fn run_thin_lto(
447448
cgcx: &CodegenContext<Self>,
449+
dcx: DiagCtxtHandle<'_>,
448450
// FIXME(bjorn3): Limit LTO exports to these symbols
449451
_exported_symbols_for_lto: &[String],
450452
each_linked_rlib_for_lto: &[PathBuf],
451453
modules: Vec<(String, Self::ThinBuffer)>,
452454
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
453455
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
454-
back::lto::run_thin(cgcx, each_linked_rlib_for_lto, modules, cached_modules)
456+
back::lto::run_thin(cgcx, dcx, each_linked_rlib_for_lto, modules, cached_modules)
455457
}
456458

457459
fn print_pass_timings(&self) {
@@ -464,7 +466,7 @@ impl WriteBackendMethods for GccCodegenBackend {
464466

465467
fn optimize(
466468
_cgcx: &CodegenContext<Self>,
467-
_dcx: DiagCtxtHandle<'_>,
469+
_shared_emitter: &SharedEmitter,
468470
module: &mut ModuleCodegen<Self::Module>,
469471
config: &ModuleConfig,
470472
) {
@@ -473,17 +475,19 @@ impl WriteBackendMethods for GccCodegenBackend {
473475

474476
fn optimize_thin(
475477
cgcx: &CodegenContext<Self>,
478+
_shared_emitter: &SharedEmitter,
476479
thin: ThinModule<Self>,
477480
) -> ModuleCodegen<Self::Module> {
478481
back::lto::optimize_thin_module(thin, cgcx)
479482
}
480483

481484
fn codegen(
482485
cgcx: &CodegenContext<Self>,
486+
shared_emitter: &SharedEmitter,
483487
module: ModuleCodegen<Self::Module>,
484488
config: &ModuleConfig,
485489
) -> CompiledModule {
486-
back::write::codegen(cgcx, module, config)
490+
back::write::codegen(cgcx, shared_emitter, module, config)
487491
}
488492

489493
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use std::{io, iter, slice};
99
use object::read::archive::ArchiveFile;
1010
use object::{Object, ObjectSection};
1111
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
12-
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
12+
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput, SharedEmitter};
1313
use rustc_codegen_ssa::traits::*;
1414
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::memmap::Mmap;
17-
use rustc_errors::DiagCtxtHandle;
17+
use rustc_errors::{DiagCtxt, DiagCtxtHandle};
1818
use rustc_hir::attrs::SanitizerSet;
1919
use rustc_middle::bug;
2020
use rustc_middle::dep_graph::WorkProduct;
@@ -150,36 +150,36 @@ fn get_bitcode_slice_from_object_data<'a>(
150150
/// for further optimization.
151151
pub(crate) fn run_fat(
152152
cgcx: &CodegenContext<LlvmCodegenBackend>,
153+
shared_emitter: &SharedEmitter,
153154
exported_symbols_for_lto: &[String],
154155
each_linked_rlib_for_lto: &[PathBuf],
155156
modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
156157
) -> ModuleCodegen<ModuleLlvm> {
157-
let dcx = cgcx.create_dcx();
158+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
158159
let dcx = dcx.handle();
159160
let (symbols_below_threshold, upstream_modules) =
160161
prepare_lto(cgcx, exported_symbols_for_lto, each_linked_rlib_for_lto, dcx);
161162
let symbols_below_threshold =
162163
symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
163-
fat_lto(cgcx, dcx, modules, upstream_modules, &symbols_below_threshold)
164+
fat_lto(cgcx, dcx, shared_emitter, modules, upstream_modules, &symbols_below_threshold)
164165
}
165166

166167
/// Performs thin LTO by performing necessary global analysis and returning two
167168
/// lists, one of the modules that need optimization and another for modules that
168169
/// can simply be copied over from the incr. comp. cache.
169170
pub(crate) fn run_thin(
170171
cgcx: &CodegenContext<LlvmCodegenBackend>,
172+
dcx: DiagCtxtHandle<'_>,
171173
exported_symbols_for_lto: &[String],
172174
each_linked_rlib_for_lto: &[PathBuf],
173175
modules: Vec<(String, ThinBuffer)>,
174176
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
175177
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>) {
176-
let dcx = cgcx.create_dcx();
177-
let dcx = dcx.handle();
178178
let (symbols_below_threshold, upstream_modules) =
179179
prepare_lto(cgcx, exported_symbols_for_lto, each_linked_rlib_for_lto, dcx);
180180
let symbols_below_threshold =
181181
symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
182-
if cgcx.opts.cg.linker_plugin_lto.enabled() {
182+
if cgcx.use_linker_plugin_lto {
183183
unreachable!(
184184
"We should never reach this case if the LTO step \
185185
is deferred to the linker"
@@ -197,6 +197,7 @@ pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBu
197197
fn fat_lto(
198198
cgcx: &CodegenContext<LlvmCodegenBackend>,
199199
dcx: DiagCtxtHandle<'_>,
200+
shared_emitter: &SharedEmitter,
200201
modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
201202
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
202203
symbols_below_threshold: &[*const libc::c_char],
@@ -265,8 +266,13 @@ fn fat_lto(
265266
// The linking steps below may produce errors and diagnostics within LLVM
266267
// which we'd like to handle and print, so set up our diagnostic handlers
267268
// (which get unregistered when they go out of scope below).
268-
let _handler =
269-
DiagnosticHandlers::new(cgcx, dcx, llcx, &module, CodegenDiagnosticsStage::LTO);
269+
let _handler = DiagnosticHandlers::new(
270+
cgcx,
271+
shared_emitter,
272+
llcx,
273+
&module,
274+
CodegenDiagnosticsStage::LTO,
275+
);
270276

271277
// For all other modules we codegened we'll need to link them into our own
272278
// bitcode. All modules were codegened in their own LLVM context, however,
@@ -720,10 +726,11 @@ impl Drop for ThinBuffer {
720726
}
721727

722728
pub(crate) fn optimize_thin_module(
723-
thin_module: ThinModule<LlvmCodegenBackend>,
724729
cgcx: &CodegenContext<LlvmCodegenBackend>,
730+
shared_emitter: &SharedEmitter,
731+
thin_module: ThinModule<LlvmCodegenBackend>,
725732
) -> ModuleCodegen<ModuleLlvm> {
726-
let dcx = cgcx.create_dcx();
733+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
727734
let dcx = dcx.handle();
728735

729736
let module_name = &thin_module.shared.module_names[thin_module.idx];

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use libc::{c_char, c_int, c_void, size_t};
99
use rustc_codegen_ssa::back::link::ensure_removed;
1010
use rustc_codegen_ssa::back::versioned_llvm_target;
1111
use rustc_codegen_ssa::back::write::{
12-
BitcodeSection, CodegenContext, EmitObj, InlineAsmError, ModuleConfig,
12+
BitcodeSection, CodegenContext, EmitObj, InlineAsmError, ModuleConfig, SharedEmitter,
1313
TargetMachineFactoryConfig, TargetMachineFactoryFn,
1414
};
1515
use rustc_codegen_ssa::base::wants_wasm_eh;
@@ -18,7 +18,7 @@ use rustc_codegen_ssa::traits::*;
1818
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind};
1919
use rustc_data_structures::profiling::SelfProfilerRef;
2020
use rustc_data_structures::small_c_str::SmallCStr;
21-
use rustc_errors::{DiagCtxtHandle, Level};
21+
use rustc_errors::{DiagCtxt, DiagCtxtHandle, Level};
2222
use rustc_fs_util::{link_or_copy, path_to_c_string};
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_session::Session;
@@ -356,15 +356,15 @@ pub(crate) enum CodegenDiagnosticsStage {
356356
}
357357

358358
pub(crate) struct DiagnosticHandlers<'a> {
359-
data: *mut (&'a CodegenContext<LlvmCodegenBackend>, DiagCtxtHandle<'a>),
359+
data: *mut (&'a CodegenContext<LlvmCodegenBackend>, &'a SharedEmitter),
360360
llcx: &'a llvm::Context,
361361
old_handler: Option<&'a llvm::DiagnosticHandler>,
362362
}
363363

364364
impl<'a> DiagnosticHandlers<'a> {
365365
pub(crate) fn new(
366366
cgcx: &'a CodegenContext<LlvmCodegenBackend>,
367-
dcx: DiagCtxtHandle<'a>,
367+
shared_emitter: &'a SharedEmitter,
368368
llcx: &'a llvm::Context,
369369
module: &ModuleCodegen<ModuleLlvm>,
370370
stage: CodegenDiagnosticsStage,
@@ -398,8 +398,8 @@ impl<'a> DiagnosticHandlers<'a> {
398398
})
399399
.and_then(|dir| dir.to_str().and_then(|p| CString::new(p).ok()));
400400

401-
let pgo_available = cgcx.opts.cg.profile_use.is_some();
402-
let data = Box::into_raw(Box::new((cgcx, dcx)));
401+
let pgo_available = cgcx.module_config.pgo_use.is_some();
402+
let data = Box::into_raw(Box::new((cgcx, shared_emitter)));
403403
unsafe {
404404
let old_handler = llvm::LLVMRustContextGetDiagnosticHandler(llcx);
405405
llvm::LLVMRustContextConfigureDiagnosticHandler(
@@ -461,12 +461,16 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
461461
if user.is_null() {
462462
return;
463463
}
464-
let (cgcx, dcx) =
465-
unsafe { *(user as *const (&CodegenContext<LlvmCodegenBackend>, DiagCtxtHandle<'_>)) };
464+
let (cgcx, shared_emitter) =
465+
unsafe { *(user as *const (&CodegenContext<LlvmCodegenBackend>, &SharedEmitter)) };
466+
467+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
468+
let dcx = dcx.handle();
466469

467470
match unsafe { llvm::diagnostic::Diagnostic::unpack(info) } {
468471
llvm::diagnostic::InlineAsm(inline) => {
469-
cgcx.diag_emitter.inline_asm_error(report_inline_asm(
472+
// FIXME use dcx
473+
shared_emitter.inline_asm_error(report_inline_asm(
470474
cgcx,
471475
inline.message,
472476
inline.level,
@@ -776,7 +780,7 @@ pub(crate) unsafe fn llvm_optimize(
776780
&*module.module_llvm.tm.raw(),
777781
to_pass_builder_opt_level(opt_level),
778782
opt_stage,
779-
cgcx.opts.cg.linker_plugin_lto.enabled(),
783+
cgcx.use_linker_plugin_lto,
780784
config.no_prepopulate_passes,
781785
config.verify_llvm_ir,
782786
config.lint_llvm_ir,
@@ -887,14 +891,18 @@ pub(crate) unsafe fn llvm_optimize(
887891
// Unsafe due to LLVM calls.
888892
pub(crate) fn optimize(
889893
cgcx: &CodegenContext<LlvmCodegenBackend>,
890-
dcx: DiagCtxtHandle<'_>,
894+
shared_emitter: &SharedEmitter,
891895
module: &mut ModuleCodegen<ModuleLlvm>,
892896
config: &ModuleConfig,
893897
) {
894898
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &*module.name);
895899

900+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
901+
let dcx = dcx.handle();
902+
896903
let llcx = &*module.module_llvm.llcx;
897-
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
904+
let _handlers =
905+
DiagnosticHandlers::new(cgcx, shared_emitter, llcx, module, CodegenDiagnosticsStage::Opt);
898906

899907
if config.emit_no_opt_bc {
900908
let out = cgcx.output_filenames.temp_path_ext_for_cgu(
@@ -911,7 +919,7 @@ pub(crate) fn optimize(
911919
let opt_stage = match cgcx.lto {
912920
Lto::Fat => llvm::OptStage::PreLinkFatLTO,
913921
Lto::Thin | Lto::ThinLocal => llvm::OptStage::PreLinkThinLTO,
914-
_ if cgcx.opts.cg.linker_plugin_lto.enabled() => llvm::OptStage::PreLinkThinLTO,
922+
_ if cgcx.use_linker_plugin_lto => llvm::OptStage::PreLinkThinLTO,
915923
_ => llvm::OptStage::PreLinkNoLTO,
916924
};
917925

@@ -974,19 +982,26 @@ pub(crate) fn optimize(
974982

975983
pub(crate) fn codegen(
976984
cgcx: &CodegenContext<LlvmCodegenBackend>,
985+
shared_emitter: &SharedEmitter,
977986
module: ModuleCodegen<ModuleLlvm>,
978987
config: &ModuleConfig,
979988
) -> CompiledModule {
980-
let dcx = cgcx.create_dcx();
989+
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
990+
991+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
981992
let dcx = dcx.handle();
982993

983-
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
984994
{
985995
let llmod = module.module_llvm.llmod();
986996
let llcx = &*module.module_llvm.llcx;
987997
let tm = &*module.module_llvm.tm;
988-
let _handlers =
989-
DiagnosticHandlers::new(cgcx, dcx, llcx, &module, CodegenDiagnosticsStage::Codegen);
998+
let _handlers = DiagnosticHandlers::new(
999+
cgcx,
1000+
shared_emitter,
1001+
llcx,
1002+
&module,
1003+
CodegenDiagnosticsStage::Codegen,
1004+
);
9901005

9911006
if cgcx.msvc_imps_needed {
9921007
create_msvc_imps(cgcx, llcx, llmod);

0 commit comments

Comments
 (0)