Skip to content

Commit

Permalink
Rollup merge of rust-lang#55609 - nikic:fix-51947, r=nagisa
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kennytm authored Nov 7, 2018
2 parents 0708a6a + 66702fc commit dfa104e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/librustc_codegen_llvm/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,13 @@ fn run_pass_manager(cgcx: &CodegenContext,
}
});

// We always generate bitcode through ThinLTOBuffers,
// which do not support anonymous globals
if config.bitcode_needed() {
let pass = llvm::LLVMRustFindAndCreatePass("name-anon-globals\0".as_ptr() as *const _);
llvm::LLVMRustAddPass(pm, pass.unwrap());
}

if config.verify_llvm_ir {
let pass = llvm::LLVMRustFindAndCreatePass("verify\0".as_ptr() as *const _);
llvm::LLVMRustAddPass(pm, pass.unwrap());
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ impl ModuleConfig {
self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
sess.opts.optimize == config::OptLevel::Aggressive;
}

pub fn bitcode_needed(&self) -> bool {
self.emit_bc || self.obj_is_bitcode
|| self.emit_bc_compressed || self.embed_bitcode
}
}

/// Assembler name and command used by codegen when no_integrated_as is enabled
Expand Down Expand Up @@ -564,8 +569,7 @@ unsafe fn optimize(cgcx: &CodegenContext,
// Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need
// to make sure we run LLVM's NameAnonGlobals pass when emitting bitcode; otherwise
// we'll get errors in LLVM.
let using_thin_buffers = config.emit_bc || config.obj_is_bitcode
|| config.emit_bc_compressed || config.embed_bitcode;
let using_thin_buffers = config.bitcode_needed();
let mut have_name_anon_globals_pass = false;
if !config.no_prepopulate_passes {
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/issue-51947.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// compile-pass

#![crate_type = "lib"]
#![feature(linkage)]

// MergeFunctions will merge these via an anonymous internal
// backing function, which must be named if ThinLTO buffers are used

#[linkage = "weak"]
pub fn fn1(a: u32, b: u32, c: u32) -> u32 {
a + b + c
}

#[linkage = "weak"]
pub fn fn2(a: u32, b: u32, c: u32) -> u32 {
a + b + c
}

0 comments on commit dfa104e

Please sign in to comment.