Skip to content

Commit 11a9024

Browse files
committed
Auto merge of #51007 - AstralSorcerer:master, r=nagisa
Make globals with private linkage unnamed. Fixes #50862. cc @oli-obk @nagisa
2 parents f9b9b6e + 3da7c65 commit 11a9024

File tree

16 files changed

+76
-54
lines changed

16 files changed

+76
-54
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/librustc_codegen_llvm/consts.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,22 @@ pub fn addr_of_mut(
6666
cx: &CodegenCx<'ll, '_>,
6767
cv: &'ll Value,
6868
align: Align,
69-
kind: &str,
69+
kind: Option<&str>,
7070
) -> &'ll Value {
7171
unsafe {
72-
let name = cx.generate_local_symbol_name(kind);
73-
let gv = declare::define_global(cx, &name[..], val_ty(cv)).unwrap_or_else(||{
74-
bug!("symbol `{}` is already defined", name);
75-
});
72+
let gv = match kind {
73+
Some(kind) if !cx.tcx.sess.fewer_names() => {
74+
let name = cx.generate_local_symbol_name(kind);
75+
let gv = declare::define_global(cx, &name[..], val_ty(cv)).unwrap_or_else(||{
76+
bug!("symbol `{}` is already defined", name);
77+
});
78+
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
79+
gv
80+
},
81+
_ => declare::define_private_global(cx, val_ty(cv)),
82+
};
7683
llvm::LLVMSetInitializer(gv, cv);
7784
set_global_alignment(cx, gv, align);
78-
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
7985
SetUnnamedAddr(gv, true);
8086
gv
8187
}
@@ -85,7 +91,7 @@ pub fn addr_of(
8591
cx: &CodegenCx<'ll, '_>,
8692
cv: &'ll Value,
8793
align: Align,
88-
kind: &str,
94+
kind: Option<&str>,
8995
) -> &'ll Value {
9096
if let Some(&gv) = cx.const_globals.borrow().get(&cv) {
9197
unsafe {

src/librustc_codegen_llvm/declare.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use value::Value;
3535

3636
use std::ffi::CString;
3737

38-
3938
/// Declare a global value.
4039
///
4140
/// If there’s a value with the same name already declared, the function will
@@ -170,6 +169,15 @@ pub fn define_global(cx: &CodegenCx<'ll, '_>, name: &str, ty: &'ll Type) -> Opti
170169
}
171170
}
172171

172+
/// Declare a private global
173+
///
174+
/// Use this function when you intend to define a global without a name.
175+
pub fn define_private_global(cx: &CodegenCx<'ll, '_>, ty: &'ll Type) -> &'ll Value {
176+
unsafe {
177+
llvm::LLVMRustInsertPrivateGlobal(cx.llmod, ty)
178+
}
179+
}
180+
173181
/// Declare a Rust function with an intention to define it.
174182
///
175183
/// Use this function when you intend to define a function. This function will

src/librustc_codegen_llvm/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ extern "C" {
623623
pub fn LLVMAddGlobal(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
624624
pub fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
625625
pub fn LLVMRustGetOrInsertGlobal(M: &'a Module, Name: *const c_char, T: &'a Type) -> &'a Value;
626+
pub fn LLVMRustInsertPrivateGlobal(M: &'a Module, T: &'a Type) -> &'a Value;
626627
pub fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
627628
pub fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
628629
pub fn LLVMDeleteGlobal(GlobalVar: &Value);

src/librustc_codegen_llvm/meth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn get_vtable(
106106

107107
let vtable_const = C_struct(cx, &components, false);
108108
let align = cx.data_layout().pointer_align;
109-
let vtable = consts::addr_of(cx, vtable_const, align, "vtable");
109+
let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
110110

111111
debuginfo::create_vtable_metadata(cx, ty, vtable);
112112

src/librustc_codegen_llvm/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
377377
let file_line_col = consts::addr_of(bx.cx,
378378
file_line_col,
379379
align,
380-
"panic_bounds_check_loc");
380+
Some("panic_bounds_check_loc"));
381381
(lang_items::PanicBoundsCheckFnLangItem,
382382
vec![file_line_col, index, len])
383383
}
@@ -391,7 +391,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
391391
let msg_file_line_col = consts::addr_of(bx.cx,
392392
msg_file_line_col,
393393
align,
394-
"panic_loc");
394+
Some("panic_loc"));
395395
(lang_items::PanicFnLangItem,
396396
vec![msg_file_line_col])
397397
}

src/librustc_codegen_llvm/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ pub fn scalar_to_llvm(
5858
Some(AllocType::Memory(alloc)) => {
5959
let init = const_alloc_to_llvm(cx, alloc);
6060
if alloc.runtime_mutability == Mutability::Mutable {
61-
consts::addr_of_mut(cx, init, alloc.align, "byte_str")
61+
consts::addr_of_mut(cx, init, alloc.align, None)
6262
} else {
63-
consts::addr_of(cx, init, alloc.align, "byte_str")
63+
consts::addr_of(cx, init, alloc.align, None)
6464
}
6565
}
6666
Some(AllocType::Function(fn_instance)) => {

src/librustc_codegen_llvm/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl PlaceRef<'ll, 'tcx> {
6363
offset: Size,
6464
) -> PlaceRef<'ll, 'tcx> {
6565
let init = const_alloc_to_llvm(bx.cx, alloc);
66-
let base_addr = consts::addr_of(bx.cx, init, layout.align, "byte_str");
66+
let base_addr = consts::addr_of(bx.cx, init, layout.align, None);
6767

6868
let llval = unsafe { LLVMConstInBoundsGEP(
6969
consts::bitcast(base_addr, Type::i8p(bx.cx)),

src/rustllvm/RustWrapper.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/IR/DebugInfoMetadata.h"
1313
#include "llvm/IR/DiagnosticInfo.h"
1414
#include "llvm/IR/DiagnosticPrinter.h"
15+
#include "llvm/IR/GlobalVariable.h"
1516
#include "llvm/IR/Instructions.h"
1617
#include "llvm/Object/Archive.h"
1718
#include "llvm/Object/ObjectFile.h"
@@ -116,6 +117,15 @@ LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
116117
return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
117118
}
118119

120+
extern "C" LLVMValueRef
121+
LLVMRustInsertPrivateGlobal(LLVMModuleRef M, LLVMTypeRef Ty) {
122+
return wrap(new GlobalVariable(*unwrap(M),
123+
unwrap(Ty),
124+
false,
125+
GlobalValue::PrivateLinkage,
126+
nullptr));
127+
}
128+
119129
extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
120130
return wrap(Type::getMetadataTy(*unwrap(C)));
121131
}

src/test/codegen/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
// CHECK: @STATIC = {{.*}}, align 4
2222

2323
// This checks the constants from inline_enum_const
24-
// CHECK: @byte_str.{{[0-9]+}} = {{.*}}, align 2
24+
// CHECK: @{{[0-9]+}} = {{.*}}, align 2
2525

2626
// This checks the constants from {low,high}_align_const, they share the same
2727
// constant, but the alignment differs, so the higher one should be used
28-
// CHECK: [[LOW_HIGH:@byte_str.[0-9]+]] = {{.*}}, align 4
28+
// CHECK: [[LOW_HIGH:@[0-9]+]] = {{.*}}, align 4
2929

3030
#[derive(Copy, Clone)]
3131

src/test/codegen/remap_path_prefix/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod aux_mod;
2222
include!("aux_mod.rs");
2323

2424
// Here we check that the expansion of the file!() macro is mapped.
25-
// CHECK: @byte_str.1 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>, align 1
25+
// CHECK: @0 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>, align 1
2626
pub static FILE_PATH: &'static str = file!();
2727

2828
fn main() {

src/test/run-make-fulldeps/symbols-are-reasonable/Makefile

-13
This file was deleted.

src/test/run-make-fulldeps/symbols-are-reasonable/lib.rs

-21
This file was deleted.

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)