Avoid a bunch of unnecessary unsafe blocks in cg_llvm

This commit is contained in:
Oli Scherer
2025-07-09 09:12:42 +00:00
parent ad635e5d06
commit d3d51b4fdb
8 changed files with 52 additions and 63 deletions

View File

@@ -879,9 +879,7 @@ pub(crate) fn codegen(
.generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name);
let thin_bc =
module.thin_lto_buffer.as_deref().expect("cannot find embedded bitcode");
unsafe {
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
}
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
}
}
@@ -945,7 +943,7 @@ pub(crate) fn codegen(
// binaries. So we must clone the module to produce the asm output
// if we are also producing object code.
let llmod = if let EmitObj::ObjectCode(_) = config.emit_obj {
unsafe { llvm::LLVMCloneModule(llmod) }
llvm::LLVMCloneModule(llmod)
} else {
llmod
};
@@ -1073,7 +1071,7 @@ pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) ->
}
/// Embed the bitcode of an LLVM module for LTO in the LLVM module itself.
unsafe fn embed_bitcode(
fn embed_bitcode(
cgcx: &CodegenContext<LlvmCodegenBackend>,
llcx: &llvm::Context,
llmod: &llvm::Module,
@@ -1115,43 +1113,40 @@ unsafe fn embed_bitcode(
// Unfortunately, LLVM provides no way to set custom section flags. For ELF
// and COFF we emit the sections using module level inline assembly for that
// reason (see issue #90326 for historical background).
unsafe {
if cgcx.target_is_like_darwin
|| cgcx.target_is_like_aix
|| cgcx.target_arch == "wasm32"
|| cgcx.target_arch == "wasm64"
{
// We don't need custom section flags, create LLVM globals.
let llconst = common::bytes_in_context(llcx, bitcode);
let llglobal =
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
llvm::set_initializer(llglobal, llconst);
llvm::set_section(llglobal, bitcode_section_name(cgcx));
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
if cgcx.target_is_like_darwin
|| cgcx.target_is_like_aix
|| cgcx.target_arch == "wasm32"
|| cgcx.target_arch == "wasm64"
{
// We don't need custom section flags, create LLVM globals.
let llconst = common::bytes_in_context(llcx, bitcode);
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
llvm::set_initializer(llglobal, llconst);
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
let llglobal =
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
llvm::set_initializer(llglobal, llconst);
let section = if cgcx.target_is_like_darwin {
c"__LLVM,__cmdline"
} else if cgcx.target_is_like_aix {
c".info"
} else {
c".llvmcmd"
};
llvm::set_section(llglobal, section);
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
llvm::set_section(llglobal, bitcode_section_name(cgcx));
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
llvm::set_initializer(llglobal, llconst);
let section = if cgcx.target_is_like_darwin {
c"__LLVM,__cmdline"
} else if cgcx.target_is_like_aix {
c".info"
} else {
// We need custom section flags, so emit module-level inline assembly.
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
llvm::append_module_inline_asm(llmod, &asm);
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
llvm::append_module_inline_asm(llmod, &asm);
}
c".llvmcmd"
};
llvm::set_section(llglobal, section);
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
} else {
// We need custom section flags, so emit module-level inline assembly.
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
llvm::append_module_inline_asm(llmod, &asm);
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
llvm::append_module_inline_asm(llmod, &asm);
}
}

View File

@@ -215,10 +215,10 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
bug!("symbol `{}` is already defined", sym);
});
llvm::set_initializer(g, sc);
unsafe {
llvm::LLVMSetGlobalConstant(g, True);
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
}
llvm::set_global_constant(g, true);
llvm::set_unnamed_address(g, llvm::UnnamedAddr::Global);
llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
// Cast to default address space if globals are in a different addrspace
let g = self.const_pointercast(g, self.type_ptr());

View File

@@ -19,11 +19,10 @@ use tracing::{debug, instrument, trace};
use crate::common::{AsCCharPtr, CodegenCx};
use crate::errors::SymbolAlreadyDefined;
use crate::llvm::{self, True};
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use crate::{base, debuginfo};
use crate::{base, debuginfo, llvm};
pub(crate) fn const_alloc_to_llvm<'ll>(
cx: &CodegenCx<'ll, '_>,
@@ -247,7 +246,7 @@ impl<'ll> CodegenCx<'ll, '_> {
};
llvm::set_initializer(gv, cv);
set_global_alignment(self, gv, align);
llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
llvm::set_unnamed_address(gv, llvm::UnnamedAddr::Global);
gv
}
@@ -272,9 +271,8 @@ impl<'ll> CodegenCx<'ll, '_> {
return gv;
}
let gv = self.static_addr_of_mut(cv, align, kind);
unsafe {
llvm::LLVMSetGlobalConstant(gv, True);
}
llvm::set_global_constant(gv, true);
self.const_globals.borrow_mut().insert(cv, gv);
gv
}
@@ -465,7 +463,7 @@ impl<'ll> CodegenCx<'ll, '_> {
// Forward the allocation's mutability (picked by the const interner) to LLVM.
if alloc.mutability.is_not() {
llvm::LLVMSetGlobalConstant(g, llvm::True);
llvm::set_global_constant(g, true);
}
debuginfo::build_global_var_di_node(self, def_id, g);

View File

@@ -75,7 +75,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
llvm::set_section(section_var, c".debug_gdb_scripts");
llvm::set_initializer(section_var, cx.const_bytes(section_contents));
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
llvm::set_unnamed_address(section_var, llvm::UnnamedAddr::Global);
llvm::set_linkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
// This should make sure that the whole section is not larger than
// the string it contains. Otherwise we get a warning from GDB.

View File

@@ -1630,7 +1630,7 @@ pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
// When full debuginfo is enabled, we want to try and prevent vtables from being
// merged. Otherwise debuggers will have a hard time mapping from dyn pointer
// to concrete type.
llvm::SetUnnamedAddress(vtable, llvm::UnnamedAddr::No);
llvm::set_unnamed_address(vtable, llvm::UnnamedAddr::No);
let vtable_name =
compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);

View File

@@ -49,7 +49,7 @@ pub(crate) fn declare_simple_fn<'ll>(
};
llvm::SetFunctionCallConv(llfn, callconv);
llvm::SetUnnamedAddress(llfn, unnamed);
llvm::set_unnamed_address(llfn, unnamed);
llvm::set_visibility(llfn, visibility);
llfn

View File

@@ -1009,7 +1009,7 @@ unsafe extern "C" {
ModuleID: *const c_char,
C: &Context,
) -> &Module;
pub(crate) fn LLVMCloneModule(M: &Module) -> &Module;
pub(crate) safe fn LLVMCloneModule(M: &Module) -> &Module;
/// Data layout. See Module::getDataLayout.
pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
@@ -1179,7 +1179,7 @@ unsafe extern "C" {
pub(crate) fn LLVMIsThreadLocal(GlobalVar: &Value) -> Bool;
pub(crate) fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
pub(crate) fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
pub(crate) fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
pub(crate) safe fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
pub(crate) safe fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
// Operations on attributes
@@ -1718,7 +1718,7 @@ unsafe extern "C" {
pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
pub(crate) safe fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
pub(crate) fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;

View File

@@ -217,10 +217,8 @@ pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
set_comdat(llmod, val, &name);
}
pub(crate) fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
unsafe {
LLVMSetUnnamedAddress(global, unnamed);
}
pub(crate) fn set_unnamed_address(global: &Value, unnamed: UnnamedAddr) {
LLVMSetUnnamedAddress(global, unnamed);
}
pub(crate) fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
@@ -260,9 +258,7 @@ pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
}
pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
unsafe {
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
}
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
}
pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {