Remove ModuleBuffer ThinBuffer duplication

This commit is contained in:
bjorn3
2026-02-12 15:33:17 +00:00
parent a5372d1dba
commit a086b3617e
9 changed files with 22 additions and 50 deletions
+2 -2
View File
@@ -87,7 +87,7 @@
use rustc_codegen_ssa::base::codegen_crate;
use rustc_codegen_ssa::target_features::cfg_target_feature;
use rustc_codegen_ssa::traits::{
CodegenBackend, ExtraBackendMethods, ThinBufferMethods, WriteBackendMethods,
CodegenBackend, ExtraBackendMethods, ModuleBufferMethods, WriteBackendMethods,
};
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
use rustc_data_structures::fx::FxIndexMap;
@@ -425,7 +425,7 @@ unsafe impl Sync for SyncContext {}
pub struct ThinBuffer;
impl ThinBufferMethods for ThinBuffer {
impl ModuleBufferMethods for ThinBuffer {
fn data(&self) -> &[u8] {
&[]
}
+11 -25
View File
@@ -187,7 +187,7 @@ pub(crate) fn run_thin(
dcx: DiagCtxtHandle<'_>,
exported_symbols_for_lto: &[String],
each_linked_rlib_for_lto: &[PathBuf],
modules: Vec<(String, ThinBuffer)>,
modules: Vec<(String, ModuleBuffer)>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
) -> (Vec<ThinModule<LlvmCodegenBackend>>, Vec<WorkProduct>) {
let (symbols_below_threshold, upstream_modules) =
@@ -203,9 +203,9 @@ pub(crate) fn run_thin(
thin_lto(cgcx, prof, dcx, modules, upstream_modules, cached_modules, &symbols_below_threshold)
}
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBuffer) {
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ModuleBuffer) {
let name = module.name;
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true);
let buffer = ModuleBuffer::new(module.module_llvm.llmod(), true);
(name, buffer)
}
@@ -297,7 +297,7 @@ fn fat_lto(
// way we know of to do that is to serialize them to a string and them parse
// them later. Not great but hey, that's why it's "fat" LTO, right?
for module in in_memory {
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
let buffer = ModuleBuffer::new(module.module_llvm.llmod(), false);
let llmod_id = CString::new(&module.name[..]).unwrap();
serialized_modules.push((SerializedModule::Local(buffer), llmod_id));
}
@@ -400,7 +400,7 @@ fn thin_lto(
cgcx: &CodegenContext,
prof: &SelfProfilerRef,
dcx: DiagCtxtHandle<'_>,
modules: Vec<(String, ThinBuffer)>,
modules: Vec<(String, ModuleBuffer)>,
serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
symbols_below_threshold: &[*const libc::c_char],
@@ -688,20 +688,6 @@ fn drop(&mut self) {
}
}
pub struct ModuleBuffer(Buffer);
impl ModuleBuffer {
pub(crate) fn new(m: &llvm::Module) -> ModuleBuffer {
ModuleBuffer(Buffer(unsafe { llvm::LLVMRustModuleSerialize(m) }))
}
}
impl ModuleBufferMethods for ModuleBuffer {
fn data(&self) -> &[u8] {
self.0.data()
}
}
pub struct ThinData(&'static mut llvm::ThinLTOData);
unsafe impl Send for ThinData {}
@@ -715,20 +701,20 @@ fn drop(&mut self) {
}
}
pub struct ThinBuffer {
pub struct ModuleBuffer {
data: Buffer,
}
impl ThinBuffer {
pub(crate) fn new(m: &llvm::Module, is_thin: bool) -> ThinBuffer {
impl ModuleBuffer {
pub(crate) fn new(m: &llvm::Module, is_thin: bool) -> ModuleBuffer {
unsafe {
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin);
ThinBuffer { data: Buffer(buffer) }
let buffer = llvm::LLVMRustModuleSerialize(m, is_thin);
ModuleBuffer { data: Buffer(buffer) }
}
}
}
impl ThinBufferMethods for ThinBuffer {
impl ModuleBufferMethods for ModuleBuffer {
fn data(&self) -> &[u8] {
self.data.data()
}
@@ -29,7 +29,7 @@
};
use tracing::{debug, trace};
use crate::back::lto::ThinBuffer;
use crate::back::lto::ModuleBuffer;
use crate::back::owned_target_machine::OwnedTargetMachine;
use crate::back::profiling::{
LlvmSelfProfiler, selfprofile_after_pass_callback, selfprofile_before_pass_callback,
@@ -1039,7 +1039,7 @@ pub(crate) fn codegen(
"LLVM_module_codegen_make_bitcode",
&*module.name,
);
ThinBuffer::new(llmod, cgcx.lto != Lto::Fat)
ModuleBuffer::new(llmod, cgcx.lto != Lto::Fat)
};
let data = thin.data();
let _timer = prof
+2 -2
View File
@@ -153,7 +153,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
type ModuleBuffer = back::lto::ModuleBuffer;
type TargetMachine = OwnedTargetMachine;
type ThinData = back::lto::ThinData;
type ThinBuffer = back::lto::ThinBuffer;
type ThinBuffer = back::lto::ModuleBuffer;
fn print_pass_timings(&self) {
let timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
print!("{timings}");
@@ -237,7 +237,7 @@ fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffe
back::lto::prepare_thin(module)
}
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod(), false))
}
}
+1 -2
View File
@@ -2458,11 +2458,10 @@ pub(crate) fn LLVMRustUnpackSMDiagnostic(
pub(crate) fn LLVMRustBufferPtr(p: &Buffer) -> *const u8;
pub(crate) fn LLVMRustBufferLen(p: &Buffer) -> usize;
pub(crate) fn LLVMRustBufferFree(p: &'static mut Buffer);
pub(crate) fn LLVMRustModuleSerialize(M: &Module) -> &'static mut Buffer;
pub(crate) fn LLVMRustModuleCost(M: &Module) -> u64;
pub(crate) fn LLVMRustModuleInstructionStats(M: &Module) -> u64;
pub(crate) fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut Buffer;
pub(crate) fn LLVMRustModuleSerialize(M: &Module, is_thin: bool) -> &'static mut Buffer;
pub(crate) fn LLVMRustCreateThinLTOData(
Modules: *const ThinLTOModule,
NumModules: size_t,
+1 -1
View File
@@ -48,7 +48,7 @@
ArgAbiBuilderMethods, BaseTypeCodegenMethods, DerivedTypeCodegenMethods,
LayoutTypeCodegenMethods, TypeCodegenMethods, TypeMembershipCodegenMethods,
};
pub use self::write::{ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods};
pub use self::write::{ModuleBufferMethods, WriteBackendMethods};
pub trait CodegenObject = Copy + fmt::Debug;
@@ -15,7 +15,7 @@ pub trait WriteBackendMethods: Clone + 'static {
type TargetMachine;
type ModuleBuffer: ModuleBufferMethods;
type ThinData: Send + Sync;
type ThinBuffer: ThinBufferMethods;
type ThinBuffer: ModuleBufferMethods;
/// Performs fat LTO by merging all modules into a single one, running autodiff
/// if necessary and running any further optimizations
@@ -67,10 +67,6 @@ fn codegen(
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
}
pub trait ThinBufferMethods: Send + Sync {
fn data(&self) -> &[u8];
}
pub trait ModuleBufferMethods: Send + Sync {
fn data(&self) -> &[u8];
}
@@ -1399,8 +1399,8 @@ extern "C" bool LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data,
return true;
}
extern "C" LLVMRustBuffer *LLVMRustThinLTOBufferCreate(LLVMModuleRef M,
bool is_thin) {
extern "C" LLVMRustBuffer *LLVMRustModuleSerialize(LLVMModuleRef M,
bool is_thin) {
auto Ret = std::make_unique<LLVMRustBuffer>();
{
auto OS = raw_string_ostream(Ret->data);
@@ -1539,15 +1539,6 @@ extern "C" size_t LLVMRustBufferLen(const LLVMRustBuffer *Buffer) {
return Buffer->data.length();
}
extern "C" LLVMRustBuffer *LLVMRustModuleSerialize(LLVMModuleRef M) {
auto Ret = std::make_unique<LLVMRustBuffer>();
{
auto OS = raw_string_ostream(Ret->data);
WriteBitcodeToFile(*unwrap(M), OS);
}
return Ret.release();
}
extern "C" uint64_t LLVMRustModuleCost(LLVMModuleRef M) {
auto f = unwrap(M)->functions();
return std::distance(std::begin(f), std::end(f));