Skip to content

Commit dfa104e

Browse files
authored
Rollup merge of rust-lang#55609 - nikic:fix-51947, r=nagisa
Run name-anon-globals after LTO passes as well If we're going to emit bitcode (through ThinLTOBuffer), then we need to ensure that anon globals are named. This was already done after optimization passes, but also has to happen after LTO passes, as we always emit the final result in a ThinLTO-compatible manner. I added the test as `run-make`. The important bit is that we emit bitcode in some way (e.g. `--crate-type rlib` or `--emit=llvm-bc`). Please tell me if there is a better way to test for that. Fixes rust-lang#51947
2 parents 0708a6a + 66702fc commit dfa104e

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/librustc_codegen_llvm/back/lto.rs

+7
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ fn run_pass_manager(cgcx: &CodegenContext,
605605
}
606606
});
607607

608+
// We always generate bitcode through ThinLTOBuffers,
609+
// which do not support anonymous globals
610+
if config.bitcode_needed() {
611+
let pass = llvm::LLVMRustFindAndCreatePass("name-anon-globals\0".as_ptr() as *const _);
612+
llvm::LLVMRustAddPass(pm, pass.unwrap());
613+
}
614+
608615
if config.verify_llvm_ir {
609616
let pass = llvm::LLVMRustFindAndCreatePass("verify\0".as_ptr() as *const _);
610617
llvm::LLVMRustAddPass(pm, pass.unwrap());

src/librustc_codegen_llvm/back/write.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ impl ModuleConfig {
337337
self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
338338
sess.opts.optimize == config::OptLevel::Aggressive;
339339
}
340+
341+
pub fn bitcode_needed(&self) -> bool {
342+
self.emit_bc || self.obj_is_bitcode
343+
|| self.emit_bc_compressed || self.embed_bitcode
344+
}
340345
}
341346

342347
/// Assembler name and command used by codegen when no_integrated_as is enabled
@@ -564,8 +569,7 @@ unsafe fn optimize(cgcx: &CodegenContext,
564569
// Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need
565570
// to make sure we run LLVM's NameAnonGlobals pass when emitting bitcode; otherwise
566571
// we'll get errors in LLVM.
567-
let using_thin_buffers = config.emit_bc || config.obj_is_bitcode
568-
|| config.emit_bc_compressed || config.embed_bitcode;
572+
let using_thin_buffers = config.bitcode_needed();
569573
let mut have_name_anon_globals_pass = false;
570574
if !config.no_prepopulate_passes {
571575
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);

src/test/ui/issue-51947.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile-pass
2+
3+
#![crate_type = "lib"]
4+
#![feature(linkage)]
5+
6+
// MergeFunctions will merge these via an anonymous internal
7+
// backing function, which must be named if ThinLTO buffers are used
8+
9+
#[linkage = "weak"]
10+
pub fn fn1(a: u32, b: u32, c: u32) -> u32 {
11+
a + b + c
12+
}
13+
14+
#[linkage = "weak"]
15+
pub fn fn2(a: u32, b: u32, c: u32) -> u32 {
16+
a + b + c
17+
}

0 commit comments

Comments
 (0)