Skip to content

Commit 23785c2

Browse files
Rollup merge of rust-lang#96587 - bjorn3:refactor_backend_write, r=michaelwoerister
Refactor the WriteBackendMethods and ExtraBackendMethods traits The new interface is slightly less confusing and is easier to implement for non-LLVM backends.
2 parents 3ff71e9 + 78c65a5 commit 23785c2

File tree

8 files changed

+48
-71
lines changed

8 files changed

+48
-71
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,12 @@ impl CodegenBackend for GccCodegenBackend {
139139
}
140140

141141
impl ExtraBackendMethods for GccCodegenBackend {
142-
fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Module {
143-
GccContext {
142+
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) -> Self::Module {
143+
let mut mods = GccContext {
144144
context: Context::default(),
145-
}
146-
}
147-
148-
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
149-
unsafe { allocator::codegen(tcx, mods, module_name, kind, has_alloc_error_handler) }
145+
};
146+
unsafe { allocator::codegen(tcx, &mut mods, module_name, kind, has_alloc_error_handler); }
147+
mods
150148
}
151149

152150
fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<Self::Module>, u64) {
@@ -213,7 +211,7 @@ impl WriteBackendMethods for GccCodegenBackend {
213211
unimplemented!();
214212
}
215213
};
216-
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: vec![] })
214+
Ok(LtoModuleCodegen::Fat { module, _serialized_bitcode: vec![] })
217215
}
218216

219217
fn run_thin_lto(_cgcx: &CodegenContext<Self>, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
@@ -229,7 +227,12 @@ impl WriteBackendMethods for GccCodegenBackend {
229227
Ok(())
230228
}
231229

232-
unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: &mut ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
230+
fn optimize_fat(_cgcx: &CodegenContext<Self>, _module: &mut ModuleCodegen<Self::Module>) -> Result<(), FatalError> {
231+
// TODO(antoyo)
232+
Ok(())
233+
}
234+
235+
unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
233236
unimplemented!();
234237
}
235238

@@ -245,11 +248,6 @@ impl WriteBackendMethods for GccCodegenBackend {
245248
unimplemented!();
246249
}
247250

248-
fn run_lto_pass_manager(_cgcx: &CodegenContext<Self>, _module: &ModuleCodegen<Self::Module>, _config: &ModuleConfig, _thin: bool) -> Result<(), FatalError> {
249-
// TODO(antoyo)
250-
Ok(())
251-
}
252-
253251
fn run_link(cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
254252
back::write::link(cgcx, diag_handler, modules)
255253
}

compiler/rustc_codegen_llvm/src/back/lto.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::llvm::{self, build_string, False, True};
66
use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm};
77
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
88
use rustc_codegen_ssa::back::symbol_export;
9-
use rustc_codegen_ssa::back::write::{
10-
CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig,
11-
};
9+
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, TargetMachineFactoryConfig};
1210
use rustc_codegen_ssa::traits::*;
1311
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
1412
use rustc_data_structures::fx::FxHashMap;
@@ -353,7 +351,7 @@ fn fat_lto(
353351
}
354352
}
355353

356-
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: serialized_bitcode })
354+
Ok(LtoModuleCodegen::Fat { module, _serialized_bitcode: serialized_bitcode })
357355
}
358356

359357
crate struct Linker<'a>(&'a mut llvm::Linker<'a>);
@@ -578,11 +576,11 @@ fn thin_lto(
578576
pub(crate) fn run_pass_manager(
579577
cgcx: &CodegenContext<LlvmCodegenBackend>,
580578
diag_handler: &Handler,
581-
module: &ModuleCodegen<ModuleLlvm>,
582-
config: &ModuleConfig,
579+
module: &mut ModuleCodegen<ModuleLlvm>,
583580
thin: bool,
584581
) -> Result<(), FatalError> {
585582
let _timer = cgcx.prof.extra_verbose_generic_activity("LLVM_lto_optimize", &*module.name);
583+
let config = cgcx.config(module.kind);
586584

587585
// Now we have one massive module inside of llmod. Time to run the
588586
// LTO-specific optimization passes that LLVM provides.
@@ -726,7 +724,7 @@ impl Drop for ThinBuffer {
726724
}
727725

728726
pub unsafe fn optimize_thin_module(
729-
thin_module: &mut ThinModule<LlvmCodegenBackend>,
727+
thin_module: ThinModule<LlvmCodegenBackend>,
730728
cgcx: &CodegenContext<LlvmCodegenBackend>,
731729
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
732730
let diag_handler = cgcx.create_diag_handler();
@@ -743,7 +741,7 @@ pub unsafe fn optimize_thin_module(
743741
// that LLVM Context and Module.
744742
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
745743
let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &diag_handler)? as *const _;
746-
let module = ModuleCodegen {
744+
let mut module = ModuleCodegen {
747745
module_llvm: ModuleLlvm { llmod_raw, llcx, tm },
748746
name: thin_module.name().to_string(),
749747
kind: ModuleKind::Regular,
@@ -859,8 +857,7 @@ pub unsafe fn optimize_thin_module(
859857
// little differently.
860858
{
861859
info!("running thin lto passes over {}", module.name);
862-
let config = cgcx.config(module.kind);
863-
run_pass_manager(cgcx, &diag_handler, &module, config, true)?;
860+
run_pass_manager(cgcx, &diag_handler, &mut module, true)?;
864861
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
865862
}
866863
}

compiler/rustc_codegen_llvm/src/lib.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,18 @@ impl Drop for TimeTraceProfiler {
104104
}
105105

106106
impl ExtraBackendMethods for LlvmCodegenBackend {
107-
fn new_metadata(&self, tcx: TyCtxt<'_>, mod_name: &str) -> ModuleLlvm {
108-
ModuleLlvm::new_metadata(tcx, mod_name)
109-
}
110-
111107
fn codegen_allocator<'tcx>(
112108
&self,
113109
tcx: TyCtxt<'tcx>,
114-
module_llvm: &mut ModuleLlvm,
115110
module_name: &str,
116111
kind: AllocatorKind,
117112
has_alloc_error_handler: bool,
118-
) {
119-
unsafe { allocator::codegen(tcx, module_llvm, module_name, kind, has_alloc_error_handler) }
113+
) -> ModuleLlvm {
114+
let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
115+
unsafe {
116+
allocator::codegen(tcx, &mut module_llvm, module_name, kind, has_alloc_error_handler);
117+
}
118+
module_llvm
120119
}
121120
fn compile_codegen_unit(
122121
&self,
@@ -210,9 +209,16 @@ impl WriteBackendMethods for LlvmCodegenBackend {
210209
) -> Result<(), FatalError> {
211210
back::write::optimize(cgcx, diag_handler, module, config)
212211
}
212+
fn optimize_fat(
213+
cgcx: &CodegenContext<Self>,
214+
module: &mut ModuleCodegen<Self::Module>,
215+
) -> Result<(), FatalError> {
216+
let diag_handler = cgcx.create_diag_handler();
217+
back::lto::run_pass_manager(cgcx, &diag_handler, module, false)
218+
}
213219
unsafe fn optimize_thin(
214220
cgcx: &CodegenContext<Self>,
215-
thin: &mut ThinModule<Self>,
221+
thin: ThinModule<Self>,
216222
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
217223
back::lto::optimize_thin_module(thin, cgcx)
218224
}
@@ -230,15 +236,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
230236
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
231237
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
232238
}
233-
fn run_lto_pass_manager(
234-
cgcx: &CodegenContext<Self>,
235-
module: &ModuleCodegen<Self::Module>,
236-
config: &ModuleConfig,
237-
thin: bool,
238-
) -> Result<(), FatalError> {
239-
let diag_handler = cgcx.create_diag_handler();
240-
back::lto::run_pass_manager(cgcx, &diag_handler, module, config, thin)
241-
}
242239
}
243240

244241
unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis

compiler/rustc_codegen_ssa/src/back/lto.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct ThinShared<B: WriteBackendMethods> {
4242

4343
pub enum LtoModuleCodegen<B: WriteBackendMethods> {
4444
Fat {
45-
module: Option<ModuleCodegen<B::Module>>,
45+
module: ModuleCodegen<B::Module>,
4646
_serialized_bitcode: Vec<SerializedModule<B::ModuleBuffer>>,
4747
},
4848

@@ -64,19 +64,15 @@ impl<B: WriteBackendMethods> LtoModuleCodegen<B> {
6464
/// It's intended that the module returned is immediately code generated and
6565
/// dropped, and then this LTO module is dropped.
6666
pub unsafe fn optimize(
67-
&mut self,
67+
self,
6868
cgcx: &CodegenContext<B>,
6969
) -> Result<ModuleCodegen<B::Module>, FatalError> {
70-
match *self {
71-
LtoModuleCodegen::Fat { ref mut module, .. } => {
72-
let module = module.take().unwrap();
73-
{
74-
let config = cgcx.config(module.kind);
75-
B::run_lto_pass_manager(cgcx, &module, config, false)?;
76-
}
70+
match self {
71+
LtoModuleCodegen::Fat { mut module, .. } => {
72+
B::optimize_fat(cgcx, &mut module)?;
7773
Ok(module)
7874
}
79-
LtoModuleCodegen::Thin(ref mut thin) => B::optimize_thin(cgcx, thin),
75+
LtoModuleCodegen::Thin(thin) => B::optimize_thin(cgcx, thin),
8076
}
8177
}
8278

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
889889

890890
fn execute_lto_work_item<B: ExtraBackendMethods>(
891891
cgcx: &CodegenContext<B>,
892-
mut module: lto::LtoModuleCodegen<B>,
892+
module: lto::LtoModuleCodegen<B>,
893893
module_config: &ModuleConfig,
894894
) -> Result<WorkItemResult<B>, FatalError> {
895895
let module = unsafe { module.optimize(cgcx)? };

compiler/rustc_codegen_ssa/src/base.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
575575
} else if let Some(kind) = tcx.allocator_kind(()) {
576576
let llmod_id =
577577
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
578-
let mut module_llvm = backend.new_metadata(tcx, &llmod_id);
579-
tcx.sess.time("write_allocator_module", || {
580-
backend.codegen_allocator(
581-
tcx,
582-
&mut module_llvm,
583-
&llmod_id,
584-
kind,
585-
tcx.lang_items().oom().is_some(),
586-
)
578+
let module_llvm = tcx.sess.time("write_allocator_module", || {
579+
backend.codegen_allocator(tcx, &llmod_id, kind, tcx.lang_items().oom().is_some())
587580
});
588581

589582
Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator })

compiler/rustc_codegen_ssa/src/traits/backend.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ pub trait CodegenBackend {
114114
}
115115

116116
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
117-
fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
118117
fn codegen_allocator<'tcx>(
119118
&self,
120119
tcx: TyCtxt<'tcx>,
121-
module_llvm: &mut Self::Module,
122120
module_name: &str,
123121
kind: AllocatorKind,
124122
has_alloc_error_handler: bool,
125-
);
123+
) -> Self::Module;
126124
/// This generates the codegen unit and returns it along with
127125
/// a `u64` giving an estimate of the unit's processing cost.
128126
fn compile_codegen_unit(

compiler/rustc_codegen_ssa/src/traits/write.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
4141
module: &ModuleCodegen<Self::Module>,
4242
config: &ModuleConfig,
4343
) -> Result<(), FatalError>;
44+
fn optimize_fat(
45+
cgcx: &CodegenContext<Self>,
46+
llmod: &mut ModuleCodegen<Self::Module>,
47+
) -> Result<(), FatalError>;
4448
unsafe fn optimize_thin(
4549
cgcx: &CodegenContext<Self>,
46-
thin: &mut ThinModule<Self>,
50+
thin: ThinModule<Self>,
4751
) -> Result<ModuleCodegen<Self::Module>, FatalError>;
4852
unsafe fn codegen(
4953
cgcx: &CodegenContext<Self>,
@@ -53,12 +57,6 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
5357
) -> Result<CompiledModule, FatalError>;
5458
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer);
5559
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
56-
fn run_lto_pass_manager(
57-
cgcx: &CodegenContext<Self>,
58-
llmod: &ModuleCodegen<Self::Module>,
59-
config: &ModuleConfig,
60-
thin: bool,
61-
) -> Result<(), FatalError>;
6260
}
6361

6462
pub trait ThinBufferMethods: Send + Sync {

0 commit comments

Comments
 (0)