Skip to content

Commit 89f63b3

Browse files
Rollup merge of #143920 - oli-obk:cg-llvm-safety, r=jieyouxu
Make more of codegen_llvm safe Best reviewed commit-by-commit.
2 parents c259c6b + 7f95f04 commit 89f63b3

File tree

15 files changed

+221
-253
lines changed

15 files changed

+221
-253
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/builder.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
302302
return;
303303
}
304304

305-
let id_str = "branch_weights";
306-
let id = unsafe {
307-
llvm::LLVMMDStringInContext2(self.cx.llcx, id_str.as_ptr().cast(), id_str.len())
308-
};
305+
let id = self.cx.create_metadata(b"branch_weights");
309306

310307
// For switch instructions with 2 targets, the `llvm.expect` intrinsic is used.
311308
// This function handles switch instructions with more than 2 targets and it needs to
@@ -637,17 +634,16 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
637634
} else if place.layout.is_llvm_immediate() {
638635
let mut const_llval = None;
639636
let llty = place.layout.llvm_type(self);
640-
unsafe {
641-
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
642-
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
643-
if let Some(init) = llvm::LLVMGetInitializer(global) {
644-
if self.val_ty(init) == llty {
645-
const_llval = Some(init);
646-
}
637+
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
638+
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
639+
if let Some(init) = llvm::LLVMGetInitializer(global) {
640+
if self.val_ty(init) == llty {
641+
const_llval = Some(init);
647642
}
648643
}
649644
}
650645
}
646+
651647
let llval = const_llval.unwrap_or_else(|| {
652648
let load = self.load(llty, place.val.llval, place.val.align);
653649
if let abi::BackendRepr::Scalar(scalar) = place.layout.backend_repr {
@@ -1721,7 +1717,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17211717
} else {
17221718
cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
17231719
};
1724-
let typeid_metadata = self.cx.typeid_metadata(typeid).unwrap();
1720+
let typeid_metadata = self.cx.create_metadata(typeid.as_bytes());
17251721
let dbg_loc = self.get_dbg_loc();
17261722

17271723
// Test whether the function pointer is associated with the type identifier using the

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ fn match_args_from_caller_to_enzyme<'ll>(
7676
outer_pos = 1;
7777
}
7878

79-
let enzyme_const = cx.create_metadata("enzyme_const".to_string()).unwrap();
80-
let enzyme_out = cx.create_metadata("enzyme_out".to_string()).unwrap();
81-
let enzyme_dup = cx.create_metadata("enzyme_dup".to_string()).unwrap();
82-
let enzyme_dupv = cx.create_metadata("enzyme_dupv".to_string()).unwrap();
83-
let enzyme_dupnoneed = cx.create_metadata("enzyme_dupnoneed".to_string()).unwrap();
84-
let enzyme_dupnoneedv = cx.create_metadata("enzyme_dupnoneedv".to_string()).unwrap();
79+
let enzyme_const = cx.create_metadata(b"enzyme_const");
80+
let enzyme_out = cx.create_metadata(b"enzyme_out");
81+
let enzyme_dup = cx.create_metadata(b"enzyme_dup");
82+
let enzyme_dupv = cx.create_metadata(b"enzyme_dupv");
83+
let enzyme_dupnoneed = cx.create_metadata(b"enzyme_dupnoneed");
84+
let enzyme_dupnoneedv = cx.create_metadata(b"enzyme_dupnoneedv");
8585

8686
while activity_pos < inputs.len() {
8787
let diff_activity = inputs[activity_pos as usize];
@@ -378,12 +378,12 @@ fn generate_enzyme_call<'ll>(
378378
let mut args = Vec::with_capacity(num_args as usize + 1);
379379
args.push(fn_to_diff);
380380

381-
let enzyme_primal_ret = cx.create_metadata("enzyme_primal_return".to_string()).unwrap();
381+
let enzyme_primal_ret = cx.create_metadata(b"enzyme_primal_return");
382382
if matches!(attrs.ret_activity, DiffActivity::Dual | DiffActivity::Active) {
383383
args.push(cx.get_metadata_value(enzyme_primal_ret));
384384
}
385385
if attrs.width > 1 {
386-
let enzyme_width = cx.create_metadata("enzyme_width".to_string()).unwrap();
386+
let enzyme_width = cx.create_metadata(b"enzyme_width");
387387
args.push(cx.get_metadata_value(enzyme_width));
388388
args.push(cx.get_const_int(cx.type_i64(), attrs.width as u64));
389389
}

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());

0 commit comments

Comments
 (0)