Skip to content

Commit f49223c

Browse files
committed
Remove tm_factory field from CodegenContext
This is necessary to support serializing the CodegenContext to a .rlink file in the future for moving LTO to the -Zlink-only step.
1 parent 2d07e81 commit f49223c

File tree

9 files changed

+157
-119
lines changed

9 files changed

+157
-119
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct LtoData {
5050
}
5151

5252
fn prepare_lto(
53-
cgcx: &CodegenContext<GccCodegenBackend>,
53+
cgcx: &CodegenContext,
5454
each_linked_rlib_for_lto: &[PathBuf],
5555
dcx: DiagCtxtHandle<'_>,
5656
) -> LtoData {
@@ -111,7 +111,7 @@ fn save_as_file(obj: &[u8], path: &Path) -> Result<(), LtoBitcodeFromRlib> {
111111
/// Performs fat LTO by merging all modules into a single one and returning it
112112
/// for further optimization.
113113
pub(crate) fn run_fat(
114-
cgcx: &CodegenContext<GccCodegenBackend>,
114+
cgcx: &CodegenContext,
115115
shared_emitter: &SharedEmitter,
116116
each_linked_rlib_for_lto: &[PathBuf],
117117
modules: Vec<FatLtoInput<GccCodegenBackend>>,
@@ -132,7 +132,7 @@ pub(crate) fn run_fat(
132132
}
133133

134134
fn fat_lto(
135-
cgcx: &CodegenContext<GccCodegenBackend>,
135+
cgcx: &CodegenContext,
136136
_dcx: DiagCtxtHandle<'_>,
137137
modules: Vec<FatLtoInput<GccCodegenBackend>>,
138138
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
@@ -283,7 +283,7 @@ impl ModuleBufferMethods for ModuleBuffer {
283283
/// lists, one of the modules that need optimization and another for modules that
284284
/// can simply be copied over from the incr. comp. cache.
285285
pub(crate) fn run_thin(
286-
cgcx: &CodegenContext<GccCodegenBackend>,
286+
cgcx: &CodegenContext,
287287
dcx: DiagCtxtHandle<'_>,
288288
each_linked_rlib_for_lto: &[PathBuf],
289289
modules: Vec<(String, ThinBuffer)>,
@@ -345,7 +345,7 @@ pub(crate) fn prepare_thin(module: ModuleCodegen<GccContext>) -> (String, ThinBu
345345
/// all of the `LtoModuleCodegen` units returned below and destroyed once
346346
/// they all go out of scope.
347347
fn thin_lto(
348-
cgcx: &CodegenContext<GccCodegenBackend>,
348+
cgcx: &CodegenContext,
349349
_dcx: DiagCtxtHandle<'_>,
350350
modules: Vec<(String, ThinBuffer)>,
351351
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
@@ -520,11 +520,9 @@ fn thin_lto(
520520

521521
pub fn optimize_thin_module(
522522
thin_module: ThinModule<GccCodegenBackend>,
523-
_cgcx: &CodegenContext<GccCodegenBackend>,
523+
_cgcx: &CodegenContext,
524524
) -> ModuleCodegen<GccContext> {
525525
//let module_name = &thin_module.shared.module_names[thin_module.idx];
526-
/*let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, module_name.to_str().unwrap());
527-
let tm = (cgcx.tm_factory)(tm_factory_config).map_err(|e| write::llvm_err(&dcx, e))?;*/
528526

529527
// Right now the implementation we've got only works over serialized
530528
// modules, so we create a fresh new LLVM context and parse the module

compiler/rustc_codegen_gcc/src/back/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use rustc_target::spec::SplitDebuginfo;
1414

1515
use crate::base::add_pic_option;
1616
use crate::errors::CopyBitcode;
17-
use crate::{GccCodegenBackend, GccContext, LtoMode};
17+
use crate::{GccContext, LtoMode};
1818

1919
pub(crate) fn codegen(
20-
cgcx: &CodegenContext<GccCodegenBackend>,
20+
cgcx: &CodegenContext,
2121
shared_emitter: &SharedEmitter,
2222
module: ModuleCodegen<GccContext>,
2323
config: &ModuleConfig,
@@ -227,7 +227,7 @@ pub(crate) fn codegen(
227227
}
228228

229229
pub(crate) fn save_temp_bitcode(
230-
cgcx: &CodegenContext<GccCodegenBackend>,
230+
cgcx: &CodegenContext,
231231
_module: &ModuleCodegen<GccContext>,
232232
_name: &str,
233233
) {

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,9 @@ impl WriteBackendMethods for GccCodegenBackend {
426426
type ThinBuffer = ThinBuffer;
427427

428428
fn run_and_optimize_fat_lto(
429-
cgcx: &CodegenContext<Self>,
429+
cgcx: &CodegenContext,
430430
shared_emitter: &SharedEmitter,
431+
_tm_factory: TargetMachineFactoryFn<Self>,
431432
// FIXME(bjorn3): Limit LTO exports to these symbols
432433
_exported_symbols_for_lto: &[String],
433434
each_linked_rlib_for_lto: &[PathBuf],
@@ -437,7 +438,7 @@ impl WriteBackendMethods for GccCodegenBackend {
437438
}
438439

439440
fn run_thin_lto(
440-
cgcx: &CodegenContext<Self>,
441+
cgcx: &CodegenContext,
441442
dcx: DiagCtxtHandle<'_>,
442443
// FIXME(bjorn3): Limit LTO exports to these symbols
443444
_exported_symbols_for_lto: &[String],
@@ -457,7 +458,7 @@ impl WriteBackendMethods for GccCodegenBackend {
457458
}
458459

459460
fn optimize(
460-
_cgcx: &CodegenContext<Self>,
461+
_cgcx: &CodegenContext,
461462
_shared_emitter: &SharedEmitter,
462463
module: &mut ModuleCodegen<Self::Module>,
463464
config: &ModuleConfig,
@@ -466,15 +467,16 @@ impl WriteBackendMethods for GccCodegenBackend {
466467
}
467468

468469
fn optimize_thin(
469-
cgcx: &CodegenContext<Self>,
470+
cgcx: &CodegenContext,
470471
_shared_emitter: &SharedEmitter,
472+
_tm_factory: TargetMachineFactoryFn<Self>,
471473
thin: ThinModule<Self>,
472474
) -> ModuleCodegen<Self::Module> {
473475
back::lto::optimize_thin_module(thin, cgcx)
474476
}
475477

476478
fn codegen(
477-
cgcx: &CodegenContext<Self>,
479+
cgcx: &CodegenContext,
478480
shared_emitter: &SharedEmitter,
479481
module: ModuleCodegen<Self::Module>,
480482
config: &ModuleConfig,

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ 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, SharedEmitter};
12+
use rustc_codegen_ssa::back::write::{
13+
CodegenContext, FatLtoInput, SharedEmitter, TargetMachineFactoryFn,
14+
};
1315
use rustc_codegen_ssa::traits::*;
1416
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1517
use rustc_data_structures::fx::FxHashMap;
@@ -33,7 +35,7 @@ use crate::{LlvmCodegenBackend, ModuleLlvm};
3335
const THIN_LTO_KEYS_INCR_COMP_FILE_NAME: &str = "thin-lto-past-keys.bin";
3436

3537
fn prepare_lto(
36-
cgcx: &CodegenContext<LlvmCodegenBackend>,
38+
cgcx: &CodegenContext,
3739
exported_symbols_for_lto: &[String],
3840
each_linked_rlib_for_lto: &[PathBuf],
3941
dcx: DiagCtxtHandle<'_>,
@@ -123,7 +125,7 @@ fn prepare_lto(
123125

124126
fn get_bitcode_slice_from_object_data<'a>(
125127
obj: &'a [u8],
126-
cgcx: &CodegenContext<LlvmCodegenBackend>,
128+
cgcx: &CodegenContext,
127129
) -> Result<&'a [u8], LtoBitcodeFromRlib> {
128130
// We're about to assume the data here is an object file with sections, but if it's raw LLVM IR
129131
// that won't work. Fortunately, if that's what we have we can just return the object directly,
@@ -149,8 +151,9 @@ fn get_bitcode_slice_from_object_data<'a>(
149151
/// Performs fat LTO by merging all modules into a single one and returning it
150152
/// for further optimization.
151153
pub(crate) fn run_fat(
152-
cgcx: &CodegenContext<LlvmCodegenBackend>,
154+
cgcx: &CodegenContext,
153155
shared_emitter: &SharedEmitter,
156+
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
154157
exported_symbols_for_lto: &[String],
155158
each_linked_rlib_for_lto: &[PathBuf],
156159
modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
@@ -161,14 +164,22 @@ pub(crate) fn run_fat(
161164
prepare_lto(cgcx, exported_symbols_for_lto, each_linked_rlib_for_lto, dcx);
162165
let symbols_below_threshold =
163166
symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();
164-
fat_lto(cgcx, dcx, shared_emitter, modules, upstream_modules, &symbols_below_threshold)
167+
fat_lto(
168+
cgcx,
169+
dcx,
170+
shared_emitter,
171+
tm_factory,
172+
modules,
173+
upstream_modules,
174+
&symbols_below_threshold,
175+
)
165176
}
166177

167178
/// Performs thin LTO by performing necessary global analysis and returning two
168179
/// lists, one of the modules that need optimization and another for modules that
169180
/// can simply be copied over from the incr. comp. cache.
170181
pub(crate) fn run_thin(
171-
cgcx: &CodegenContext<LlvmCodegenBackend>,
182+
cgcx: &CodegenContext,
172183
dcx: DiagCtxtHandle<'_>,
173184
exported_symbols_for_lto: &[String],
174185
each_linked_rlib_for_lto: &[PathBuf],
@@ -195,9 +206,10 @@ pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBu
195206
}
196207

197208
fn fat_lto(
198-
cgcx: &CodegenContext<LlvmCodegenBackend>,
209+
cgcx: &CodegenContext,
199210
dcx: DiagCtxtHandle<'_>,
200211
shared_emitter: &SharedEmitter,
212+
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
201213
modules: Vec<FatLtoInput<LlvmCodegenBackend>>,
202214
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
203215
symbols_below_threshold: &[*const libc::c_char],
@@ -252,7 +264,7 @@ fn fat_lto(
252264
assert!(!serialized_modules.is_empty(), "must have at least one serialized module");
253265
let (buffer, name) = serialized_modules.remove(0);
254266
info!("no in-memory regular modules to choose from, parsing {:?}", name);
255-
let llvm_module = ModuleLlvm::parse(cgcx, &name, buffer.data(), dcx);
267+
let llvm_module = ModuleLlvm::parse(cgcx, tm_factory, &name, buffer.data(), dcx);
256268
ModuleCodegen::new_regular(name.into_string().unwrap(), llvm_module)
257269
}
258270
};
@@ -381,7 +393,7 @@ impl Drop for Linker<'_> {
381393
/// all of the `LtoModuleCodegen` units returned below and destroyed once
382394
/// they all go out of scope.
383395
fn thin_lto(
384-
cgcx: &CodegenContext<LlvmCodegenBackend>,
396+
cgcx: &CodegenContext,
385397
dcx: DiagCtxtHandle<'_>,
386398
modules: Vec<(String, ThinBuffer)>,
387399
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
@@ -585,7 +597,7 @@ pub(crate) fn enable_autodiff_settings(ad: &[config::AutoDiff]) {
585597
}
586598

587599
pub(crate) fn run_pass_manager(
588-
cgcx: &CodegenContext<LlvmCodegenBackend>,
600+
cgcx: &CodegenContext,
589601
dcx: DiagCtxtHandle<'_>,
590602
module: &mut ModuleCodegen<ModuleLlvm>,
591603
thin: bool,
@@ -726,8 +738,9 @@ impl Drop for ThinBuffer {
726738
}
727739

728740
pub(crate) fn optimize_thin_module(
729-
cgcx: &CodegenContext<LlvmCodegenBackend>,
741+
cgcx: &CodegenContext,
730742
shared_emitter: &SharedEmitter,
743+
tm_factory: TargetMachineFactoryFn<LlvmCodegenBackend>,
731744
thin_module: ThinModule<LlvmCodegenBackend>,
732745
) -> ModuleCodegen<ModuleLlvm> {
733746
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
@@ -740,7 +753,7 @@ pub(crate) fn optimize_thin_module(
740753
// into that context. One day, however, we may do this for upstream
741754
// crates but for locally codegened modules we may be able to reuse
742755
// that LLVM Context and Module.
743-
let module_llvm = ModuleLlvm::parse(cgcx, module_name, thin_module.data(), dcx);
756+
let module_llvm = ModuleLlvm::parse(cgcx, tm_factory, module_name, thin_module.data(), dcx);
744757
let mut module = ModuleCodegen::new_regular(thin_module.name(), module_llvm);
745758
// Given that the newly created module lacks a thinlto buffer for embedding, we need to re-add it here.
746759
if cgcx.module_config.embed_bitcode() {

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ pub(crate) fn target_machine_factory(
323323
}
324324

325325
pub(crate) fn save_temp_bitcode(
326-
cgcx: &CodegenContext<LlvmCodegenBackend>,
326+
cgcx: &CodegenContext,
327327
module: &ModuleCodegen<ModuleLlvm>,
328328
name: &str,
329329
) {
@@ -358,14 +358,14 @@ pub(crate) enum CodegenDiagnosticsStage {
358358
}
359359

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

366366
impl<'a> DiagnosticHandlers<'a> {
367367
pub(crate) fn new(
368-
cgcx: &'a CodegenContext<LlvmCodegenBackend>,
368+
cgcx: &'a CodegenContext,
369369
shared_emitter: &'a SharedEmitter,
370370
llcx: &'a llvm::Context,
371371
module: &ModuleCodegen<ModuleLlvm>,
@@ -431,7 +431,7 @@ impl<'a> Drop for DiagnosticHandlers<'a> {
431431
}
432432

433433
fn report_inline_asm(
434-
cgcx: &CodegenContext<LlvmCodegenBackend>,
434+
cgcx: &CodegenContext,
435435
msg: String,
436436
level: llvm::DiagnosticLevel,
437437
cookie: u64,
@@ -463,8 +463,7 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
463463
if user.is_null() {
464464
return;
465465
}
466-
let (cgcx, shared_emitter) =
467-
unsafe { *(user as *const (&CodegenContext<LlvmCodegenBackend>, &SharedEmitter)) };
466+
let (cgcx, shared_emitter) = unsafe { *(user as *const (&CodegenContext, &SharedEmitter)) };
468467

469468
let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
470469
let dcx = dcx.handle();
@@ -560,7 +559,7 @@ pub(crate) enum AutodiffStage {
560559
}
561560

562561
pub(crate) unsafe fn llvm_optimize(
563-
cgcx: &CodegenContext<LlvmCodegenBackend>,
562+
cgcx: &CodegenContext,
564563
dcx: DiagCtxtHandle<'_>,
565564
module: &ModuleCodegen<ModuleLlvm>,
566565
thin_lto_buffer: Option<&mut *mut llvm::ThinLTOBuffer>,
@@ -892,7 +891,7 @@ pub(crate) unsafe fn llvm_optimize(
892891

893892
// Unsafe due to LLVM calls.
894893
pub(crate) fn optimize(
895-
cgcx: &CodegenContext<LlvmCodegenBackend>,
894+
cgcx: &CodegenContext,
896895
shared_emitter: &SharedEmitter,
897896
module: &mut ModuleCodegen<ModuleLlvm>,
898897
config: &ModuleConfig,
@@ -983,7 +982,7 @@ pub(crate) fn optimize(
983982
}
984983

985984
pub(crate) fn codegen(
986-
cgcx: &CodegenContext<LlvmCodegenBackend>,
985+
cgcx: &CodegenContext,
987986
shared_emitter: &SharedEmitter,
988987
module: ModuleCodegen<ModuleLlvm>,
989988
config: &ModuleConfig,
@@ -1238,7 +1237,7 @@ fn create_section_with_flags_asm(section_name: &str, section_flags: &str, data:
12381237
asm
12391238
}
12401239

1241-
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static CStr {
1240+
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext) -> &'static CStr {
12421241
if cgcx.target_is_like_darwin {
12431242
c"__LLVM,__bitcode"
12441243
} else if cgcx.target_is_like_aix {
@@ -1250,7 +1249,7 @@ pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) ->
12501249

12511250
/// Embed the bitcode of an LLVM module for LTO in the LLVM module itself.
12521251
fn embed_bitcode(
1253-
cgcx: &CodegenContext<LlvmCodegenBackend>,
1252+
cgcx: &CodegenContext,
12541253
llcx: &llvm::Context,
12551254
llmod: &llvm::Module,
12561255
bitcode: &[u8],
@@ -1334,11 +1333,7 @@ fn embed_bitcode(
13341333
// when using MSVC linker. We do this only for data, as linker can fix up
13351334
// code references on its own.
13361335
// See #26591, #27438
1337-
fn create_msvc_imps(
1338-
cgcx: &CodegenContext<LlvmCodegenBackend>,
1339-
llcx: &llvm::Context,
1340-
llmod: &llvm::Module,
1341-
) {
1336+
fn create_msvc_imps(cgcx: &CodegenContext, llcx: &llvm::Context, llmod: &llvm::Module) {
13421337
if !cgcx.msvc_imps_needed {
13431338
return;
13441339
}

0 commit comments

Comments
 (0)