Skip to content

Commit c0cea75

Browse files
committed
Auto merge of #50684 - nikic:prepare-thinlto, r=nagisa
Set PrepareForThinLTO flag when using ThinLTO The LLVM PassManager has a PrepareForThinLTO flag, which is intended for use when compilation occurs in conjunction with linking by ThinLTO. The flag has two effects: * The NameAnonGlobal pass is run after all other passes, which ensures that all globals have a name. * In optimized builds, a number of late passes (mainly related to vectorization and unrolling) are disabled, on the rationale that these a) will increase codesize of the intermediate artifacts and b) will be run by ThinLTO again anyway. This patch enables the use of PrepareForThinLTO if Thin or ThinLocal linking is used. The background for this change is the CI failure in #49479, which we assume to be caused by the NameAnonGlobal pass not being run. As this changes which passes LLVM runs, this might have performance (or other) impact, so we want to land this separately.
2 parents e6db79f + a70ef4c commit c0cea75

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

src/librustc_llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,7 @@ extern "C" {
16511651
MergeFunctions: bool,
16521652
SLPVectorize: bool,
16531653
LoopVectorize: bool,
1654+
PrepareForThinLTO: bool,
16541655
PGOGenPath: *const c_char,
16551656
PGOUsePath: *const c_char);
16561657
pub fn LLVMRustAddLibraryInfo(PM: PassManagerRef,

src/librustc_trans/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn run_pass_manager(cgcx: &CodegenContext,
482482
llvm::CodeGenOptLevel::None => llvm::CodeGenOptLevel::Less,
483483
level => level,
484484
};
485-
with_llvm_pmb(llmod, config, opt_level, &mut |b| {
485+
with_llvm_pmb(llmod, config, opt_level, false, &mut |b| {
486486
if thin {
487487
if !llvm::LLVMRustPassManagerBuilderPopulateThinLTOPassManager(b, pm) {
488488
panic!("this version of LLVM does not support ThinLTO");

src/librustc_trans/back/write.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ unsafe fn optimize(cgcx: &CodegenContext,
547547
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
548548
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
549549
let opt_level = config.opt_level.unwrap_or(llvm::CodeGenOptLevel::None);
550-
with_llvm_pmb(llmod, &config, opt_level, &mut |b| {
550+
let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal;
551+
with_llvm_pmb(llmod, &config, opt_level, prepare_for_thin_lto, &mut |b| {
551552
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
552553
llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
553554
})
@@ -2042,6 +2043,7 @@ pub fn run_assembler(cgcx: &CodegenContext, handler: &Handler, assembly: &Path,
20422043
pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
20432044
config: &ModuleConfig,
20442045
opt_level: llvm::CodeGenOptLevel,
2046+
prepare_for_thin_lto: bool,
20452047
f: &mut FnMut(llvm::PassManagerBuilderRef)) {
20462048
use std::ptr;
20472049

@@ -2069,6 +2071,7 @@ pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
20692071
config.merge_functions,
20702072
config.vectorize_slp,
20712073
config.vectorize_loop,
2074+
prepare_for_thin_lto,
20722075
pgo_gen_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
20732076
pgo_use_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
20742077
);

src/rustllvm/PassWrapper.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,16 @@ extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
428428

429429
extern "C" void LLVMRustConfigurePassManagerBuilder(
430430
LLVMPassManagerBuilderRef PMBR, LLVMRustCodeGenOptLevel OptLevel,
431-
bool MergeFunctions, bool SLPVectorize, bool LoopVectorize,
431+
bool MergeFunctions, bool SLPVectorize, bool LoopVectorize, bool PrepareForThinLTO,
432432
const char* PGOGenPath, const char* PGOUsePath) {
433433
// Ignore mergefunc for now as enabling it causes crashes.
434434
// unwrap(PMBR)->MergeFunctions = MergeFunctions;
435435
unwrap(PMBR)->SLPVectorize = SLPVectorize;
436436
unwrap(PMBR)->OptLevel = fromRust(OptLevel);
437437
unwrap(PMBR)->LoopVectorize = LoopVectorize;
438+
#if LLVM_VERSION_GE(4, 0)
439+
unwrap(PMBR)->PrepareForThinLTO = PrepareForThinLTO;
440+
#endif
438441

439442
#ifdef PGO_AVAILABLE
440443
if (PGOGenPath) {

0 commit comments

Comments
 (0)