Skip to content

Commit 31e7237

Browse files
authored
Rollup merge of rust-lang#152478 - bjorn3:lto_refactors10, r=wesleywiser
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. Follow up to rust-lang#149209 Part of rust-lang/compiler-team#908
2 parents b906c96 + f49223c commit 31e7237

File tree

10 files changed

+168
-152
lines changed

10 files changed

+168
-152
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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
374374
_features: &[String],
375375
) -> TargetMachineFactoryFn<Self> {
376376
// TODO(antoyo): set opt level.
377-
Arc::new(|_| Ok(()))
377+
Arc::new(|_, _| ())
378378
}
379379
}
380380

@@ -421,14 +421,14 @@ unsafe impl Sync for SyncContext {}
421421
impl WriteBackendMethods for GccCodegenBackend {
422422
type Module = GccContext;
423423
type TargetMachine = ();
424-
type TargetMachineError = ();
425424
type ModuleBuffer = ModuleBuffer;
426425
type ThinData = ThinData;
427426
type ThinBuffer = ThinBuffer;
428427

429428
fn run_and_optimize_fat_lto(
430-
cgcx: &CodegenContext<Self>,
429+
cgcx: &CodegenContext,
431430
shared_emitter: &SharedEmitter,
431+
_tm_factory: TargetMachineFactoryFn<Self>,
432432
// FIXME(bjorn3): Limit LTO exports to these symbols
433433
_exported_symbols_for_lto: &[String],
434434
each_linked_rlib_for_lto: &[PathBuf],
@@ -438,7 +438,7 @@ impl WriteBackendMethods for GccCodegenBackend {
438438
}
439439

440440
fn run_thin_lto(
441-
cgcx: &CodegenContext<Self>,
441+
cgcx: &CodegenContext,
442442
dcx: DiagCtxtHandle<'_>,
443443
// FIXME(bjorn3): Limit LTO exports to these symbols
444444
_exported_symbols_for_lto: &[String],
@@ -458,7 +458,7 @@ impl WriteBackendMethods for GccCodegenBackend {
458458
}
459459

460460
fn optimize(
461-
_cgcx: &CodegenContext<Self>,
461+
_cgcx: &CodegenContext,
462462
_shared_emitter: &SharedEmitter,
463463
module: &mut ModuleCodegen<Self::Module>,
464464
config: &ModuleConfig,
@@ -467,15 +467,16 @@ impl WriteBackendMethods for GccCodegenBackend {
467467
}
468468

469469
fn optimize_thin(
470-
cgcx: &CodegenContext<Self>,
470+
cgcx: &CodegenContext,
471471
_shared_emitter: &SharedEmitter,
472+
_tm_factory: TargetMachineFactoryFn<Self>,
472473
thin: ThinModule<Self>,
473474
) -> ModuleCodegen<Self::Module> {
474475
back::lto::optimize_thin_module(thin, cgcx)
475476
}
476477

477478
fn codegen(
478-
cgcx: &CodegenContext<Self>,
479+
cgcx: &CodegenContext,
479480
shared_emitter: &SharedEmitter,
480481
module: ModuleCodegen<Self::Module>,
481482
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() {

0 commit comments

Comments
 (0)