Skip to content

Commit 3da7c65

Browse files
Make sure we prepare for thin LTO whenever we are emitting bitcode
Emitting LLVM bitcode uses ThinLTOBuffers, so we need to prepare for thin LTO or we will likely cause errors in LLVM.
1 parent 02190f3 commit 3da7c65

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/librustc_codegen_llvm/back/write.rs

+31
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,23 @@ unsafe fn optimize(cgcx: &CodegenContext,
541541
};
542542

543543
if config.verify_llvm_ir { assert!(addpass("verify")); }
544+
545+
// Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need
546+
// to make sure we run LLVM's NameAnonGlobals pass when emitting bitcode; otherwise
547+
// we'll get errors in LLVM.
548+
let using_thin_buffers = llvm::LLVMRustThinLTOAvailable() && (config.emit_bc
549+
|| config.obj_is_bitcode || config.emit_bc_compressed || config.embed_bitcode);
550+
let mut have_name_anon_globals_pass = false;
544551
if !config.no_prepopulate_passes {
545552
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
546553
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
547554
let opt_level = config.opt_level.unwrap_or(llvm::CodeGenOptLevel::None);
548555
let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal;
556+
have_name_anon_globals_pass = have_name_anon_globals_pass || prepare_for_thin_lto;
557+
if using_thin_buffers && !prepare_for_thin_lto {
558+
assert!(addpass("name-anon-globals"));
559+
have_name_anon_globals_pass = true;
560+
}
549561
with_llvm_pmb(llmod, &config, opt_level, prepare_for_thin_lto, &mut |b| {
550562
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
551563
llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
@@ -557,6 +569,9 @@ unsafe fn optimize(cgcx: &CodegenContext,
557569
diag_handler.warn(&format!("unknown pass `{}`, ignoring",
558570
pass));
559571
}
572+
if pass == "name-anon-globals" {
573+
have_name_anon_globals_pass = true;
574+
}
560575
}
561576

562577
for pass in &cgcx.plugin_passes {
@@ -565,6 +580,22 @@ unsafe fn optimize(cgcx: &CodegenContext,
565580
`{}` but LLVM does not \
566581
recognize it", pass));
567582
}
583+
if pass == "name-anon-globals" {
584+
have_name_anon_globals_pass = true;
585+
}
586+
}
587+
588+
if using_thin_buffers && !have_name_anon_globals_pass {
589+
// As described above, this will probably cause an error in LLVM
590+
if config.no_prepopulate_passes {
591+
diag_handler.err("The current compilation is going to use thin LTO buffers \
592+
without running LLVM's NameAnonGlobals pass. \
593+
This will likely cause errors in LLVM. Consider adding \
594+
-C passes=name-anon-globals to the compiler command line.");
595+
} else {
596+
bug!("We are using thin LTO buffers without running the NameAnonGlobals pass. \
597+
This will likely cause errors in LLVM and shoud never happen.");
598+
}
568599
}
569600
}
570601

src/test/run-pass-fulldeps/myriad-closures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// See https://github.com/rust-lang/rust/issues/34793 for more information.
1515

1616
// Make sure we don't optimize anything away:
17-
// compile-flags: -C no-prepopulate-passes
17+
// compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
1818

1919
// Expand something exponentially
2020
macro_rules! go_bacterial {

src/test/run-pass/issue-38226.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// Need -Cno-prepopulate-passes to really disable inlining, otherwise the faulty
1717
// code gets optimized out:
18-
// compile-flags: -Cno-prepopulate-passes
18+
// compile-flags: -Cno-prepopulate-passes -Cpasses=name-anon-globals
1919

2020
extern crate issue_38226_aux;
2121

src/test/ui/feature-gate-unwind-attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -C no-prepopulate-passes
11+
// compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
1212

1313
#![crate_type = "lib"]
1414

0 commit comments

Comments
 (0)