Skip to content

Commit 035891f

Browse files
committed
Remove SharedEmitter from CodegenContext
1 parent e1e4357 commit 035891f

File tree

9 files changed

+165
-96
lines changed

9 files changed

+165
-96
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 5 additions & 7 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,12 +284,11 @@ 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);
293293
if cgcx.use_linker_plugin_lto {
294294
unreachable!(
@@ -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
@@ -82,7 +82,7 @@ use gccjit::{TargetInfo, Version};
8282
use rustc_ast::expand::allocator::AllocatorMethod;
8383
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
8484
use rustc_codegen_ssa::back::write::{
85-
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
85+
CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryFn,
8686
};
8787
use rustc_codegen_ssa::base::codegen_crate;
8888
use rustc_codegen_ssa::target_features::cfg_target_feature;
@@ -371,23 +371,25 @@ impl WriteBackendMethods for GccCodegenBackend {
371371

372372
fn run_and_optimize_fat_lto(
373373
cgcx: &CodegenContext<Self>,
374+
shared_emitter: &SharedEmitter,
374375
// FIXME(bjorn3): Limit LTO exports to these symbols
375376
_exported_symbols_for_lto: &[String],
376377
each_linked_rlib_for_lto: &[PathBuf],
377378
modules: Vec<FatLtoInput<Self>>,
378379
) -> ModuleCodegen<Self::Module> {
379-
back::lto::run_fat(cgcx, each_linked_rlib_for_lto, modules)
380+
back::lto::run_fat(cgcx, shared_emitter, each_linked_rlib_for_lto, modules)
380381
}
381382

382383
fn run_thin_lto(
383384
cgcx: &CodegenContext<Self>,
385+
dcx: DiagCtxtHandle<'_>,
384386
// FIXME(bjorn3): Limit LTO exports to these symbols
385387
_exported_symbols_for_lto: &[String],
386388
each_linked_rlib_for_lto: &[PathBuf],
387389
modules: Vec<(String, Self::ThinBuffer)>,
388390
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
389391
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
390-
back::lto::run_thin(cgcx, each_linked_rlib_for_lto, modules, cached_modules)
392+
back::lto::run_thin(cgcx, dcx, each_linked_rlib_for_lto, modules, cached_modules)
391393
}
392394

393395
fn print_pass_timings(&self) {
@@ -400,7 +402,7 @@ impl WriteBackendMethods for GccCodegenBackend {
400402

401403
fn optimize(
402404
_cgcx: &CodegenContext<Self>,
403-
_dcx: DiagCtxtHandle<'_>,
405+
_shared_emitter: &SharedEmitter,
404406
module: &mut ModuleCodegen<Self::Module>,
405407
config: &ModuleConfig,
406408
) {
@@ -409,17 +411,19 @@ impl WriteBackendMethods for GccCodegenBackend {
409411

410412
fn optimize_thin(
411413
cgcx: &CodegenContext<Self>,
414+
_shared_emitter: &SharedEmitter,
412415
thin: ThinModule<Self>,
413416
) -> ModuleCodegen<Self::Module> {
414417
back::lto::optimize_thin_module(thin, cgcx)
415418
}
416419

417420
fn codegen(
418421
cgcx: &CodegenContext<Self>,
422+
shared_emitter: &SharedEmitter,
419423
module: ModuleCodegen<Self::Module>,
420424
config: &ModuleConfig,
421425
) -> CompiledModule {
422-
back::write::codegen(cgcx, module, config)
426+
back::write::codegen(cgcx, shared_emitter, module, config)
423427
}
424428

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

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 17 additions & 10 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,31 +150,31 @@ 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 =
@@ -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,
@@ -730,10 +736,11 @@ impl Drop for ThinBuffer {
730736
}
731737

732738
pub(crate) fn optimize_thin_module(
733-
thin_module: ThinModule<LlvmCodegenBackend>,
734739
cgcx: &CodegenContext<LlvmCodegenBackend>,
740+
shared_emitter: &SharedEmitter,
741+
thin_module: ThinModule<LlvmCodegenBackend>,
735742
) -> ModuleCodegen<ModuleLlvm> {
736-
let dcx = cgcx.create_dcx();
743+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
737744
let dcx = dcx.handle();
738745

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

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ 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;
1616
use rustc_codegen_ssa::traits::*;
1717
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, ModuleKind};
1818
use rustc_data_structures::profiling::SelfProfilerRef;
1919
use rustc_data_structures::small_c_str::SmallCStr;
20-
use rustc_errors::{DiagCtxtHandle, Level};
20+
use rustc_errors::{DiagCtxt, DiagCtxtHandle, Level};
2121
use rustc_fs_util::{link_or_copy, path_to_c_string};
2222
use rustc_middle::ty::TyCtxt;
2323
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,
@@ -399,7 +399,7 @@ impl<'a> DiagnosticHandlers<'a> {
399399
.and_then(|dir| dir.to_str().and_then(|p| CString::new(p).ok()));
400400

401401
let pgo_available = cgcx.module_config.pgo_use.is_some();
402-
let data = Box::into_raw(Box::new((cgcx, dcx)));
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,
@@ -777,14 +781,18 @@ pub(crate) unsafe fn llvm_optimize(
777781
// Unsafe due to LLVM calls.
778782
pub(crate) fn optimize(
779783
cgcx: &CodegenContext<LlvmCodegenBackend>,
780-
dcx: DiagCtxtHandle<'_>,
784+
shared_emitter: &SharedEmitter,
781785
module: &mut ModuleCodegen<ModuleLlvm>,
782786
config: &ModuleConfig,
783787
) {
784788
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &*module.name);
785789

790+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
791+
let dcx = dcx.handle();
792+
786793
let llcx = &*module.module_llvm.llcx;
787-
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
794+
let _handlers =
795+
DiagnosticHandlers::new(cgcx, shared_emitter, llcx, module, CodegenDiagnosticsStage::Opt);
788796

789797
if config.emit_no_opt_bc {
790798
let out = cgcx.output_filenames.temp_path_ext_for_cgu(
@@ -865,19 +873,26 @@ pub(crate) fn optimize(
865873

866874
pub(crate) fn codegen(
867875
cgcx: &CodegenContext<LlvmCodegenBackend>,
876+
shared_emitter: &SharedEmitter,
868877
module: ModuleCodegen<ModuleLlvm>,
869878
config: &ModuleConfig,
870879
) -> CompiledModule {
871-
let dcx = cgcx.create_dcx();
880+
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
881+
882+
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
872883
let dcx = dcx.handle();
873884

874-
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &*module.name);
875885
{
876886
let llmod = module.module_llvm.llmod();
877887
let llcx = &*module.module_llvm.llcx;
878888
let tm = &*module.module_llvm.tm;
879-
let _handlers =
880-
DiagnosticHandlers::new(cgcx, dcx, llcx, &module, CodegenDiagnosticsStage::Codegen);
889+
let _handlers = DiagnosticHandlers::new(
890+
cgcx,
891+
shared_emitter,
892+
llcx,
893+
&module,
894+
CodegenDiagnosticsStage::Codegen,
895+
);
881896

882897
if cgcx.msvc_imps_needed {
883898
create_msvc_imps(cgcx, llcx, llmod);

0 commit comments

Comments
 (0)