Skip to content

Commit de8200c

Browse files
committed
thinlto: only build summary file if needed
If we don't do this, some versions of LLVM (at least 17, experimentally) will double-emit some error messages, which is how I noticed this. Given that it seems to be costing some extra work, let's only request the summary bitcode production if we'll actually bother writing it down, otherwise skip it.
1 parent 03d5556 commit de8200c

File tree

7 files changed

+17
-14
lines changed

7 files changed

+17
-14
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ pub(crate) fn run_thin(
229229
thin_lto(cgcx, &dcx, modules, upstream_modules, cached_modules, &symbols_below_threshold)
230230
}
231231

232-
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBuffer) {
232+
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>, emit_summary: bool) -> (String, ThinBuffer) {
233233
let name = module.name;
234-
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true);
234+
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true, emit_summary);
235235
(name, buffer)
236236
}
237237

@@ -671,9 +671,9 @@ unsafe impl Send for ThinBuffer {}
671671
unsafe impl Sync for ThinBuffer {}
672672

673673
impl ThinBuffer {
674-
pub fn new(m: &llvm::Module, is_thin: bool) -> ThinBuffer {
674+
pub fn new(m: &llvm::Module, is_thin: bool, emit_summary: bool) -> ThinBuffer {
675675
unsafe {
676-
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin);
676+
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin, emit_summary);
677677
ThinBuffer(buffer)
678678
}
679679
}

compiler/rustc_codegen_llvm/src/back/write.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,8 @@ pub(crate) unsafe fn codegen(
716716
let _timer = cgcx
717717
.prof
718718
.generic_activity_with_arg("LLVM_module_codegen_make_bitcode", &*module.name);
719-
let thin = ThinBuffer::new(llmod, config.emit_thin_lto);
719+
let thin = ThinBuffer::new(llmod, config.emit_thin_lto, config.emit_thin_lto_index);
720720
let data = thin.data();
721-
let index_data = thin.thin_link_data();
722721

723722
if let Some(bitcode_filename) = bc_out.file_name() {
724723
cgcx.prof.artifact_size(
@@ -728,7 +727,8 @@ pub(crate) unsafe fn codegen(
728727
);
729728
}
730729

731-
if let Some(thin_link_bitcode_filename) = bc_index_out.file_name() {
730+
if config.emit_thin_lto_index && let Some(thin_link_bitcode_filename) = bc_index_out.file_name() {
731+
let index_data = thin.thin_link_data();
732732
cgcx.prof.artifact_size(
733733
"llvm_bitcode_summary",
734734
thin_link_bitcode_filename.to_string_lossy(),

compiler/rustc_codegen_llvm/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ impl WriteBackendMethods for LlvmCodegenBackend {
240240
) -> Result<CompiledModule, FatalError> {
241241
back::write::codegen(cgcx, dcx, module, config)
242242
}
243-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
244-
back::lto::prepare_thin(module)
243+
fn prepare_thin(module: ModuleCodegen<Self::Module>, emit_summary: bool) -> (String, Self::ThinBuffer) {
244+
back::lto::prepare_thin(module, emit_summary)
245245
}
246246
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
247247
(module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ extern "C" {
23502350
#[allow(improper_ctypes)]
23512351
pub fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString);
23522352

2353-
pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer;
2353+
pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool, emit_summary: bool) -> &'static mut ThinLTOBuffer;
23542354
pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);
23552355
pub fn LLVMRustThinLTOBufferPtr(M: &ThinLTOBuffer) -> *const c_char;
23562356
pub fn LLVMRustThinLTOBufferLen(M: &ThinLTOBuffer) -> size_t;

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ fn execute_optimize_work_item<B: ExtraBackendMethods>(
891891
match lto_type {
892892
ComputedLtoType::No => finish_intra_module_work(cgcx, module, module_config),
893893
ComputedLtoType::Thin => {
894-
let (name, thin_buffer) = B::prepare_thin(module);
894+
let (name, thin_buffer) = B::prepare_thin(module, false);
895895
if let Some(path) = bitcode {
896896
fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
897897
panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);

compiler/rustc_codegen_ssa/src/traits/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait WriteBackendMethods: 'static + Sized + Clone {
5656
module: ModuleCodegen<Self::Module>,
5757
config: &ModuleConfig,
5858
) -> Result<CompiledModule, FatalError>;
59-
fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer);
59+
fn prepare_thin(module: ModuleCodegen<Self::Module>, want_summary: bool) -> (String, Self::ThinBuffer);
6060
fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer);
6161
}
6262

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ struct LLVMRustThinLTOBuffer {
14921492
};
14931493

14941494
extern "C" LLVMRustThinLTOBuffer*
1495-
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
1495+
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin, bool emit_summary) {
14961496
auto Ret = std::make_unique<LLVMRustThinLTOBuffer>();
14971497
{
14981498
auto OS = raw_string_ostream(Ret->data);
@@ -1510,7 +1510,10 @@ LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
15101510
PB.registerLoopAnalyses(LAM);
15111511
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
15121512
ModulePassManager MPM;
1513-
MPM.addPass(ThinLTOBitcodeWriterPass(OS, &ThinLinkOS));
1513+
// We only pass ThinLinkOS to be filled in if we want the summary,
1514+
// because otherwise LLVM does extra work and may double-emit some
1515+
// errors or warnings.
1516+
MPM.addPass(ThinLTOBitcodeWriterPass(OS, emit_summary ? &ThinLinkOS : nullptr));
15141517
MPM.run(*unwrap(M), MAM);
15151518
} else {
15161519
WriteBitcodeToFile(*unwrap(M), OS);

0 commit comments

Comments
 (0)