Skip to content

Commit d3d51b4

Browse files
committed
Avoid a bunch of unnecessary unsafe blocks in cg_llvm
1 parent ad635e5 commit d3d51b4

File tree

8 files changed

+54
-65
lines changed

8 files changed

+54
-65
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,7 @@ pub(crate) fn codegen(
879879
.generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name);
880880
let thin_bc =
881881
module.thin_lto_buffer.as_deref().expect("cannot find embedded bitcode");
882-
unsafe {
883-
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
884-
}
882+
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
885883
}
886884
}
887885

@@ -945,7 +943,7 @@ pub(crate) fn codegen(
945943
// binaries. So we must clone the module to produce the asm output
946944
// if we are also producing object code.
947945
let llmod = if let EmitObj::ObjectCode(_) = config.emit_obj {
948-
unsafe { llvm::LLVMCloneModule(llmod) }
946+
llvm::LLVMCloneModule(llmod)
949947
} else {
950948
llmod
951949
};
@@ -1073,7 +1071,7 @@ pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) ->
10731071
}
10741072

10751073
/// Embed the bitcode of an LLVM module for LTO in the LLVM module itself.
1076-
unsafe fn embed_bitcode(
1074+
fn embed_bitcode(
10771075
cgcx: &CodegenContext<LlvmCodegenBackend>,
10781076
llcx: &llvm::Context,
10791077
llmod: &llvm::Module,
@@ -1115,43 +1113,40 @@ unsafe fn embed_bitcode(
11151113
// Unfortunately, LLVM provides no way to set custom section flags. For ELF
11161114
// and COFF we emit the sections using module level inline assembly for that
11171115
// reason (see issue #90326 for historical background).
1118-
unsafe {
1119-
if cgcx.target_is_like_darwin
1120-
|| cgcx.target_is_like_aix
1121-
|| cgcx.target_arch == "wasm32"
1122-
|| cgcx.target_arch == "wasm64"
1123-
{
1124-
// We don't need custom section flags, create LLVM globals.
1125-
let llconst = common::bytes_in_context(llcx, bitcode);
1126-
let llglobal =
1127-
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1128-
llvm::set_initializer(llglobal, llconst);
1129-
1130-
llvm::set_section(llglobal, bitcode_section_name(cgcx));
1131-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1132-
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
1133-
1134-
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1135-
let llglobal =
1136-
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1137-
llvm::set_initializer(llglobal, llconst);
1138-
let section = if cgcx.target_is_like_darwin {
1139-
c"__LLVM,__cmdline"
1140-
} else if cgcx.target_is_like_aix {
1141-
c".info"
1142-
} else {
1143-
c".llvmcmd"
1144-
};
1145-
llvm::set_section(llglobal, section);
1146-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1116+
1117+
if cgcx.target_is_like_darwin
1118+
|| cgcx.target_is_like_aix
1119+
|| cgcx.target_arch == "wasm32"
1120+
|| cgcx.target_arch == "wasm64"
1121+
{
1122+
// We don't need custom section flags, create LLVM globals.
1123+
let llconst = common::bytes_in_context(llcx, bitcode);
1124+
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1125+
llvm::set_initializer(llglobal, llconst);
1126+
1127+
llvm::set_section(llglobal, bitcode_section_name(cgcx));
1128+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1129+
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
1130+
1131+
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1132+
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1133+
llvm::set_initializer(llglobal, llconst);
1134+
let section = if cgcx.target_is_like_darwin {
1135+
c"__LLVM,__cmdline"
1136+
} else if cgcx.target_is_like_aix {
1137+
c".info"
11471138
} else {
1148-
// We need custom section flags, so emit module-level inline assembly.
1149-
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
1150-
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
1151-
llvm::append_module_inline_asm(llmod, &asm);
1152-
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
1153-
llvm::append_module_inline_asm(llmod, &asm);
1154-
}
1139+
c".llvmcmd"
1140+
};
1141+
llvm::set_section(llglobal, section);
1142+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1143+
} else {
1144+
// We need custom section flags, so emit module-level inline assembly.
1145+
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
1146+
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
1147+
llvm::append_module_inline_asm(llmod, &asm);
1148+
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
1149+
llvm::append_module_inline_asm(llmod, &asm);
11551150
}
11561151
}
11571152

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
215215
bug!("symbol `{}` is already defined", sym);
216216
});
217217
llvm::set_initializer(g, sc);
218-
unsafe {
219-
llvm::LLVMSetGlobalConstant(g, True);
220-
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
221-
}
218+
219+
llvm::set_global_constant(g, true);
220+
llvm::set_unnamed_address(g, llvm::UnnamedAddr::Global);
221+
222222
llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
223223
// Cast to default address space if globals are in a different addrspace
224224
let g = self.const_pointercast(g, self.type_ptr());

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ use tracing::{debug, instrument, trace};
1919

2020
use crate::common::{AsCCharPtr, CodegenCx};
2121
use crate::errors::SymbolAlreadyDefined;
22-
use crate::llvm::{self, True};
2322
use crate::type_::Type;
2423
use crate::type_of::LayoutLlvmExt;
2524
use crate::value::Value;
26-
use crate::{base, debuginfo};
25+
use crate::{base, debuginfo, llvm};
2726

2827
pub(crate) fn const_alloc_to_llvm<'ll>(
2928
cx: &CodegenCx<'ll, '_>,
@@ -247,7 +246,7 @@ impl<'ll> CodegenCx<'ll, '_> {
247246
};
248247
llvm::set_initializer(gv, cv);
249248
set_global_alignment(self, gv, align);
250-
llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
249+
llvm::set_unnamed_address(gv, llvm::UnnamedAddr::Global);
251250
gv
252251
}
253252

@@ -272,9 +271,8 @@ impl<'ll> CodegenCx<'ll, '_> {
272271
return gv;
273272
}
274273
let gv = self.static_addr_of_mut(cv, align, kind);
275-
unsafe {
276-
llvm::LLVMSetGlobalConstant(gv, True);
277-
}
274+
llvm::set_global_constant(gv, true);
275+
278276
self.const_globals.borrow_mut().insert(cv, gv);
279277
gv
280278
}
@@ -465,7 +463,7 @@ impl<'ll> CodegenCx<'ll, '_> {
465463

466464
// Forward the allocation's mutability (picked by the const interner) to LLVM.
467465
if alloc.mutability.is_not() {
468-
llvm::LLVMSetGlobalConstant(g, llvm::True);
466+
llvm::set_global_constant(g, true);
469467
}
470468

471469
debuginfo::build_global_var_di_node(self, def_id, g);

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
7575
llvm::set_section(section_var, c".debug_gdb_scripts");
7676
llvm::set_initializer(section_var, cx.const_bytes(section_contents));
7777
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
78-
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
78+
llvm::set_unnamed_address(section_var, llvm::UnnamedAddr::Global);
7979
llvm::set_linkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
8080
// This should make sure that the whole section is not larger than
8181
// the string it contains. Otherwise we get a warning from GDB.

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
16301630
// When full debuginfo is enabled, we want to try and prevent vtables from being
16311631
// merged. Otherwise debuggers will have a hard time mapping from dyn pointer
16321632
// to concrete type.
1633-
llvm::SetUnnamedAddress(vtable, llvm::UnnamedAddr::No);
1633+
llvm::set_unnamed_address(vtable, llvm::UnnamedAddr::No);
16341634

16351635
let vtable_name =
16361636
compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn declare_simple_fn<'ll>(
4949
};
5050

5151
llvm::SetFunctionCallConv(llfn, callconv);
52-
llvm::SetUnnamedAddress(llfn, unnamed);
52+
llvm::set_unnamed_address(llfn, unnamed);
5353
llvm::set_visibility(llfn, visibility);
5454

5555
llfn

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ unsafe extern "C" {
10091009
ModuleID: *const c_char,
10101010
C: &Context,
10111011
) -> &Module;
1012-
pub(crate) fn LLVMCloneModule(M: &Module) -> &Module;
1012+
pub(crate) safe fn LLVMCloneModule(M: &Module) -> &Module;
10131013

10141014
/// Data layout. See Module::getDataLayout.
10151015
pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
@@ -1179,7 +1179,7 @@ unsafe extern "C" {
11791179
pub(crate) fn LLVMIsThreadLocal(GlobalVar: &Value) -> Bool;
11801180
pub(crate) fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
11811181
pub(crate) fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
1182-
pub(crate) fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
1182+
pub(crate) safe fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
11831183
pub(crate) safe fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
11841184

11851185
// Operations on attributes
@@ -1718,7 +1718,7 @@ unsafe extern "C" {
17181718

17191719
pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
17201720

1721-
pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
1721+
pub(crate) safe fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
17221722

17231723
pub(crate) fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;
17241724

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,8 @@ pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
217217
set_comdat(llmod, val, &name);
218218
}
219219

220-
pub(crate) fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
221-
unsafe {
222-
LLVMSetUnnamedAddress(global, unnamed);
223-
}
220+
pub(crate) fn set_unnamed_address(global: &Value, unnamed: UnnamedAddr) {
221+
LLVMSetUnnamedAddress(global, unnamed);
224222
}
225223

226224
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) {
260258
}
261259

262260
pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
263-
unsafe {
264-
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
265-
}
261+
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
266262
}
267263

268264
pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {

0 commit comments

Comments
 (0)