Skip to content

Commit

Permalink
Use safe wrappers get_visibility and set_visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Oct 26, 2024
1 parent 983d258 commit b114040
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
17 changes: 4 additions & 13 deletions compiler/rustc_codegen_llvm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,14 @@ pub(crate) unsafe fn codegen(
// __rust_alloc_error_handler_should_panic
let name = OomStrategy::SYMBOL;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility(
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
let llval = llvm::LLVMConstInt(i8, val as u64, False);
llvm::LLVMSetInitializer(ll_g, llval);

let name = NO_ALLOC_SHIM_IS_UNSTABLE;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
llvm::LLVMRustSetVisibility(
ll_g,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
let llval = llvm::LLVMConstInt(i8, 0, False);
llvm::LLVMSetInitializer(ll_g, llval);
}
Expand Down Expand Up @@ -134,10 +128,7 @@ fn create_wrapper_function(
None
};

llvm::LLVMRustSetVisibility(
llfn,
llvm::Visibility::from_generic(tcx.sess.default_visibility()),
);
llvm::set_visibility(llfn, llvm::Visibility::from_generic(tcx.sess.default_visibility()));

if tcx.sess.must_emit_unwind_tables() {
let uwtable =
Expand All @@ -151,7 +142,7 @@ fn create_wrapper_function(
// -> ! DIFlagNoReturn
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
}
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
llvm::set_visibility(callee, llvm::Visibility::Hidden);

let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr());

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
|| !cx.tcx.is_reachable_non_generic(instance_def_id))
};
if is_hidden {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
}

// MinGW: For backward compatibility we rely on the linker to decide whether it
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ impl<'ll> CodegenCx<'ll, '_> {
let g = self.declare_global(sym, llty);

if !self.tcx.is_reachable_non_generic(def_id) {
unsafe {
llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden);
}
llvm::set_visibility(g, llvm::Visibility::Hidden);
}

g
Expand All @@ -308,7 +306,7 @@ impl<'ll> CodegenCx<'ll, '_> {
llvm::set_thread_local_mode(g, self.tls_model);
}

let dso_local = unsafe { self.should_assume_dso_local(g, true) };
let dso_local = self.should_assume_dso_local(g, true);
if dso_local {
unsafe {
llvm::LLVMRustSetDSOLocal(g, true);
Expand Down Expand Up @@ -398,7 +396,7 @@ impl<'ll> CodegenCx<'ll, '_> {
llvm::set_value_name(g, b"");

let linkage = llvm::get_linkage(g);
let visibility = llvm::LLVMRustGetVisibility(g);
let visibility = llvm::get_visibility(g);

let new_g = llvm::LLVMRustGetOrInsertGlobal(
self.llmod,
Expand All @@ -408,7 +406,7 @@ impl<'ll> CodegenCx<'ll, '_> {
);

llvm::set_linkage(new_g, linkage);
llvm::LLVMRustSetVisibility(new_g, visibility);
llvm::set_visibility(new_g, visibility);

// The old global has had its name removed but is returned by
// get_static since it is in the instance cache. Provide an
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
}
}

pub fn get_visibility(llglobal: &Value) -> Visibility {
unsafe { LLVMRustGetVisibility(llglobal) }
}

pub fn set_visibility(llglobal: &Value, visibility: Visibility) {
unsafe {
LLVMRustSetVisibility(llglobal, visibility);
Expand Down
20 changes: 7 additions & 13 deletions compiler/rustc_codegen_llvm/src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
});

llvm::set_linkage(g, base::linkage_to_llvm(linkage));
llvm::set_visibility(g, base::visibility_to_llvm(visibility));
unsafe {
llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
if self.should_assume_dso_local(g, false) {
llvm::LLVMRustSetDSOLocal(g, true);
}
Expand Down Expand Up @@ -78,21 +78,15 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
&& linkage != Linkage::Private
&& self.tcx.is_compiler_builtins(LOCAL_CRATE)
{
unsafe {
llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
}
llvm::set_visibility(lldecl, llvm::Visibility::Hidden);
} else {
unsafe {
llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
}
llvm::set_visibility(lldecl, base::visibility_to_llvm(visibility));
}

debug!("predefine_fn: instance = {:?}", instance);

unsafe {
if self.should_assume_dso_local(lldecl, false) {
llvm::LLVMRustSetDSOLocal(lldecl, true);
}
if self.should_assume_dso_local(lldecl, false) {
unsafe { llvm::LLVMRustSetDSOLocal(lldecl, true) };
}

self.instances.borrow_mut().insert(instance, lldecl);
Expand All @@ -102,13 +96,13 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
impl CodegenCx<'_, '_> {
/// Whether a definition or declaration can be assumed to be local to a group of
/// libraries that form a single DSO or executable.
pub(crate) unsafe fn should_assume_dso_local(
pub(crate) fn should_assume_dso_local(
&self,
llval: &llvm::Value,
is_declaration: bool,
) -> bool {
let linkage = llvm::get_linkage(llval);
let visibility = unsafe { llvm::LLVMRustGetVisibility(llval) };
let visibility = llvm::get_visibility(llval);

if matches!(linkage, llvm::Linkage::InternalLinkage | llvm::Linkage::PrivateLinkage) {
return true;
Expand Down

0 comments on commit b114040

Please sign in to comment.