Skip to content

Commit ecd2f10

Browse files
committed
Remove last remaining ModuleBuffer/ThinBuffer duplication
1 parent bddd8f8 commit ecd2f10

5 files changed

Lines changed: 16 additions & 37 deletions

File tree

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ use rustc_codegen_ssa::back::write::{
8686
};
8787
use rustc_codegen_ssa::base::codegen_crate;
8888
use rustc_codegen_ssa::target_features::cfg_target_feature;
89-
use rustc_codegen_ssa::traits::{
90-
CodegenBackend, ExtraBackendMethods, ModuleBufferMethods, WriteBackendMethods,
91-
};
89+
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
9290
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
9391
use rustc_data_structures::fx::FxIndexMap;
9492
use rustc_data_structures::profiling::SelfProfilerRef;
@@ -423,20 +421,11 @@ unsafe impl Send for SyncContext {}
423421
// FIXME(antoyo): that shouldn't be Sync. Parallel compilation is currently disabled with "CodegenBackend::supports_parallel()".
424422
unsafe impl Sync for SyncContext {}
425423

426-
pub struct ThinBuffer;
427-
428-
impl ModuleBufferMethods for ThinBuffer {
429-
fn data(&self) -> &[u8] {
430-
&[]
431-
}
432-
}
433-
434424
impl WriteBackendMethods for GccCodegenBackend {
435425
type Module = GccContext;
436426
type TargetMachine = ();
437427
type ModuleBuffer = ModuleBuffer;
438428
type ThinData = ();
439-
type ThinBuffer = ThinBuffer;
440429

441430
fn run_and_optimize_fat_lto(
442431
cgcx: &CodegenContext,
@@ -458,7 +447,7 @@ impl WriteBackendMethods for GccCodegenBackend {
458447
// FIXME(bjorn3): Limit LTO exports to these symbols
459448
_exported_symbols_for_lto: &[String],
460449
_each_linked_rlib_for_lto: &[PathBuf],
461-
_modules: Vec<(String, Self::ThinBuffer)>,
450+
_modules: Vec<(String, Self::ModuleBuffer)>,
462451
_cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
463452
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
464453
unreachable!()
@@ -502,11 +491,7 @@ impl WriteBackendMethods for GccCodegenBackend {
502491
back::write::codegen(cgcx, prof, shared_emitter, module, config)
503492
}
504493

505-
fn prepare_thin(_module: Self::Module) -> Self::ThinBuffer {
506-
unreachable!()
507-
}
508-
509-
fn serialize_module(_module: Self::Module) -> Self::ModuleBuffer {
494+
fn serialize_module(_module: Self::Module, _is_thin: bool) -> Self::ModuleBuffer {
510495
unimplemented!();
511496
}
512497
}

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ impl WriteBackendMethods for LlvmCodegenBackend {
153153
type ModuleBuffer = back::lto::ModuleBuffer;
154154
type TargetMachine = OwnedTargetMachine;
155155
type ThinData = back::lto::ThinData;
156-
type ThinBuffer = back::lto::ModuleBuffer;
157156
fn print_pass_timings(&self) {
158157
let timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
159158
print!("{timings}");
@@ -193,7 +192,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
193192
dcx: DiagCtxtHandle<'_>,
194193
exported_symbols_for_lto: &[String],
195194
each_linked_rlib_for_lto: &[PathBuf],
196-
modules: Vec<(String, Self::ThinBuffer)>,
195+
modules: Vec<(String, Self::ModuleBuffer)>,
197196
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
198197
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
199198
back::lto::run_thin(
@@ -233,11 +232,8 @@ impl WriteBackendMethods for LlvmCodegenBackend {
233232
) -> CompiledModule {
234233
back::write::codegen(cgcx, prof, shared_emitter, module, config)
235234
}
236-
fn prepare_thin(module: Self::Module) -> Self::ThinBuffer {
237-
back::lto::ModuleBuffer::new(module.llmod(), true)
238-
}
239-
fn serialize_module(module: Self::Module) -> Self::ModuleBuffer {
240-
back::lto::ModuleBuffer::new(module.llmod(), false)
235+
fn serialize_module(module: Self::Module, is_thin: bool) -> Self::ModuleBuffer {
236+
back::lto::ModuleBuffer::new(module.llmod(), is_thin)
241237
}
242238
}
243239

compiler/rustc_codegen_ssa/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<B: WriteBackendMethods> ThinModule<B> {
4242

4343
pub struct ThinShared<B: WriteBackendMethods> {
4444
pub data: B::ThinData,
45-
pub thin_buffers: Vec<B::ThinBuffer>,
45+
pub thin_buffers: Vec<B::ModuleBuffer>,
4646
pub serialized_modules: Vec<SerializedModule<B::ModuleBuffer>>,
4747
pub module_names: Vec<CString>,
4848
}

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn generate_thin_lto_work<B: ExtraBackendMethods>(
364364
dcx: DiagCtxtHandle<'_>,
365365
exported_symbols_for_lto: &[String],
366366
each_linked_rlib_for_lto: &[PathBuf],
367-
needs_thin_lto: Vec<(String, B::ThinBuffer)>,
367+
needs_thin_lto: Vec<(String, B::ModuleBuffer)>,
368368
import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
369369
) -> Vec<(ThinLtoWorkItem<B>, u64)> {
370370
let _prof_timer = prof.generic_activity("codegen_thin_generate_lto_work");
@@ -418,7 +418,7 @@ enum MaybeLtoModules<B: WriteBackendMethods> {
418418
cgcx: CodegenContext,
419419
exported_symbols_for_lto: Arc<Vec<String>>,
420420
each_linked_rlib_file_for_lto: Vec<PathBuf>,
421-
needs_thin_lto: Vec<(String, <B as WriteBackendMethods>::ThinBuffer)>,
421+
needs_thin_lto: Vec<(String, <B as WriteBackendMethods>::ModuleBuffer)>,
422422
lto_import_only_modules:
423423
Vec<(SerializedModule<<B as WriteBackendMethods>::ModuleBuffer>, WorkProduct)>,
424424
},
@@ -796,7 +796,7 @@ pub(crate) enum WorkItemResult<B: WriteBackendMethods> {
796796

797797
/// The backend has finished compiling a CGU, which now needs to go through
798798
/// thin LTO.
799-
NeedsThinLto(String, B::ThinBuffer),
799+
NeedsThinLto(String, B::ModuleBuffer),
800800
}
801801

802802
pub enum FatLtoInput<B: WriteBackendMethods> {
@@ -871,7 +871,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
871871
WorkItemResult::Finished(module)
872872
}
873873
ComputedLtoType::Thin => {
874-
let thin_buffer = B::prepare_thin(module.module_llvm);
874+
let thin_buffer = B::serialize_module(module.module_llvm, true);
875875
if let Some(path) = bitcode {
876876
fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
877877
panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
@@ -881,7 +881,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
881881
}
882882
ComputedLtoType::Fat => match bitcode {
883883
Some(path) => {
884-
let buffer = B::serialize_module(module.module_llvm);
884+
let buffer = B::serialize_module(module.module_llvm, false);
885885
fs::write(&path, buffer.data()).unwrap_or_else(|e| {
886886
panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
887887
});
@@ -1023,7 +1023,7 @@ fn do_thin_lto<B: ExtraBackendMethods>(
10231023
tm_factory: TargetMachineFactoryFn<B>,
10241024
exported_symbols_for_lto: Arc<Vec<String>>,
10251025
each_linked_rlib_for_lto: Vec<PathBuf>,
1026-
needs_thin_lto: Vec<(String, <B as WriteBackendMethods>::ThinBuffer)>,
1026+
needs_thin_lto: Vec<(String, <B as WriteBackendMethods>::ModuleBuffer)>,
10271027
lto_import_only_modules: Vec<(
10281028
SerializedModule<<B as WriteBackendMethods>::ModuleBuffer>,
10291029
WorkProduct,
@@ -1807,7 +1807,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
18071807
));
18081808
} else {
18091809
if let Some(allocator_module) = allocator_module.take() {
1810-
let thin_buffer = B::prepare_thin(allocator_module.module_llvm);
1810+
let thin_buffer = B::serialize_module(allocator_module.module_llvm, true);
18111811
needs_thin_lto.push((allocator_module.name, thin_buffer));
18121812
}
18131813

compiler/rustc_codegen_ssa/src/traits/write.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub trait WriteBackendMethods: Clone + 'static {
1515
type TargetMachine;
1616
type ModuleBuffer: ModuleBufferMethods;
1717
type ThinData: Send + Sync;
18-
type ThinBuffer: ModuleBufferMethods;
1918

2019
/// Performs fat LTO by merging all modules into a single one, running autodiff
2120
/// if necessary and running any further optimizations
@@ -37,7 +36,7 @@ pub trait WriteBackendMethods: Clone + 'static {
3736
dcx: DiagCtxtHandle<'_>,
3837
exported_symbols_for_lto: &[String],
3938
each_linked_rlib_for_lto: &[PathBuf],
40-
modules: Vec<(String, Self::ThinBuffer)>,
39+
modules: Vec<(String, Self::ModuleBuffer)>,
4140
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
4241
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>);
4342
fn print_pass_timings(&self);
@@ -63,8 +62,7 @@ pub trait WriteBackendMethods: Clone + 'static {
6362
module: ModuleCodegen<Self::Module>,
6463
config: &ModuleConfig,
6564
) -> CompiledModule;
66-
fn prepare_thin(module: Self::Module) -> Self::ThinBuffer;
67-
fn serialize_module(module: Self::Module) -> Self::ModuleBuffer;
65+
fn serialize_module(module: Self::Module, is_thin: bool) -> Self::ModuleBuffer;
6866
}
6967

7068
pub trait ModuleBufferMethods: Send + Sync {

0 commit comments

Comments
 (0)