Skip to content

Commit

Permalink
Set dso_local for more items
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobo1239 committed May 18, 2021
1 parent 46985d5 commit f7ed4a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
24 changes: 24 additions & 0 deletions compiler/rustc_codegen_llvm/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,27 @@ pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
Visibility::Protected => llvm::Visibility::Protected,
}
}

pub fn linkage_from_llvm(linkage: llvm::Linkage) -> Linkage {
match linkage {
llvm::Linkage::ExternalLinkage => Linkage::External,
llvm::Linkage::AvailableExternallyLinkage => Linkage::AvailableExternally,
llvm::Linkage::LinkOnceAnyLinkage => Linkage::LinkOnceAny,
llvm::Linkage::LinkOnceODRLinkage => Linkage::LinkOnceODR,
llvm::Linkage::WeakAnyLinkage => Linkage::WeakAny,
llvm::Linkage::WeakODRLinkage => Linkage::WeakODR,
llvm::Linkage::AppendingLinkage => Linkage::Appending,
llvm::Linkage::InternalLinkage => Linkage::Internal,
llvm::Linkage::PrivateLinkage => Linkage::Private,
llvm::Linkage::ExternalWeakLinkage => Linkage::ExternalWeak,
llvm::Linkage::CommonLinkage => Linkage::Common,
}
}

pub fn visibility_from_llvm(linkage: llvm::Visibility) -> Visibility {
match linkage {
llvm::Visibility::Default => Visibility::Default,
llvm::Visibility::Hidden => Visibility::Hidden,
llvm::Visibility::Protected => Visibility::Protected,
}
}
13 changes: 13 additions & 0 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_middle::mir::mono::MonoItem;
use rustc_middle::ty::{self, Instance, Ty};
use rustc_middle::{bug, span_bug};
use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive, Scalar, Size};
use rustc_target::spec::RelocModel;
use tracing::debug;

pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
Expand Down Expand Up @@ -282,6 +283,12 @@ impl CodegenCx<'ll, 'tcx> {
}
}

if self.tcx.sess.relocation_model() == RelocModel::Static {
unsafe {
llvm::LLVMRustSetDSOLocal(g, true);
}
}

self.instances.borrow_mut().insert(instance, g);
g
}
Expand Down Expand Up @@ -363,6 +370,12 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
set_global_alignment(&self, g, self.align_of(ty));
llvm::LLVMSetInitializer(g, v);

let linkage = base::linkage_from_llvm(llvm::LLVMRustGetLinkage(g));
let visibility = base::visibility_from_llvm(llvm::LLVMRustGetVisibility(g));
if self.should_assume_dso_local(linkage, visibility) {
llvm::LLVMRustSetDSOLocal(g, true);
}

// As an optimization, all shared statics which do not have interior
// mutability are placed into read-only memory.
if !is_mutable && self.type_is_freeze(ty) {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum CallConv {
}

/// LLVMRustLinkage
#[derive(PartialEq)]
#[derive(Copy, Clone, PartialEq)]
#[repr(C)]
pub enum Linkage {
ExternalLinkage = 0,
Expand All @@ -72,6 +72,7 @@ pub enum Linkage {

// LLVMRustVisibility
#[repr(C)]
#[derive(Copy, Clone)]
pub enum Visibility {
Default = 0,
Hidden = 1,
Expand Down
33 changes: 33 additions & 0 deletions src/test/assembly/static-relocation-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ trait Sized {}
#[lang="copy"]
trait Copy {}

#[lang="sync"]
trait Sync {}

#[lang = "drop_in_place"]
fn drop_in_place<T>(_: *mut T) {}

impl Copy for u8 {}
impl Sync for u8 {}

#[no_mangle]
pub static PIERIS: u8 = 42;

extern "C" {
static EXOCHORDA: *mut u8;

fn chaenomeles();
}

Expand All @@ -42,3 +54,24 @@ pub fn peach() -> u8 {
*(banana as *mut u8)
}
}

// CHECK-LABEL: mango:
// x64: movq EXOCHORDA{{(\(%[a-z0-9]+\))?}}, %[[REG:[a-z0-9]+]]
// x64-NEXT: movb (%[[REG]]), %{{[a-z0-9]+}}
// A64: adrp [[REG2:[a-z0-9]+]], EXOCHORDA
// A64-NEXT: ldr {{[a-z0-9]+}}, {{\[}}[[REG2]], :lo12:EXOCHORDA]
#[no_mangle]
pub fn mango() -> u8 {
unsafe {
*EXOCHORDA
}
}

// CHECK-LABEL: orange:
// x64: mov{{l|absq}} $PIERIS, %{{[a-z0-9]+}}
// A64: adrp [[REG2:[a-z0-9]+]], PIERIS
// A64-NEXT: add {{[a-z0-9]+}}, [[REG2]], :lo12:PIERIS
#[no_mangle]
pub fn orange() -> &'static u8 {
&PIERIS
}

0 comments on commit f7ed4a7

Please sign in to comment.