Skip to content

Commit 8ef9821

Browse files
committed
Extract helper method global_set_metadata_node
1 parent 6a58f80 commit 8ef9821

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,19 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
10521052
let md = self.md_node_in_context(md_list);
10531053
unsafe { llvm::LLVMRustGlobalAddMetadata(global, kind_id, md) };
10541054
}
1055+
1056+
/// Helper method for the sequence of calls:
1057+
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1058+
/// - `LLVMGlobalSetMetadata` (to set that node as metadata of `kind_id` for `global`)
1059+
pub(crate) fn global_set_metadata_node(
1060+
&self,
1061+
global: &'ll Value,
1062+
kind_id: MetadataKindId,
1063+
md_list: &[&'ll Metadata],
1064+
) {
1065+
let md = self.md_node_in_context(md_list);
1066+
unsafe { llvm::LLVMGlobalSetMetadata(global, kind_id, md) };
1067+
}
10551068
}
10561069

10571070
impl HasDataLayout for CodegenCx<'_, '_> {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,11 +1610,8 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
16101610
let type_ = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
16111611
cx.global_add_metadata_node(vtable, llvm::MD_type, &type_);
16121612

1613-
unsafe {
1614-
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1615-
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1616-
llvm::LLVMGlobalSetMetadata(vtable, llvm::MD_vcall_visibility, vcall_visibility_metadata);
1617-
}
1613+
let vcall_visibility = [llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64))];
1614+
cx.global_set_metadata_node(vtable, llvm::MD_vcall_visibility, &vcall_visibility);
16181615
}
16191616

16201617
/// Creates debug information for the given vtable, which is for the

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,8 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
308308

309309
fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
310310
let typeid_metadata = self.create_metadata(typeid);
311-
unsafe {
312-
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
313-
llvm::LLVMGlobalSetMetadata(
314-
function,
315-
llvm::MD_type,
316-
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
317-
)
318-
}
311+
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
312+
self.global_set_metadata_node(function, llvm::MD_type, &v);
319313
}
320314

321315
fn typeid_metadata(&self, typeid: &[u8]) -> Option<&'ll Metadata> {
@@ -328,17 +322,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
328322
}
329323

330324
fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
331-
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
332-
unsafe {
333-
llvm::LLVMGlobalSetMetadata(
334-
function,
335-
llvm::MD_kcfi_type,
336-
llvm::LLVMMDNodeInContext2(
337-
self.llcx,
338-
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
339-
1,
340-
),
341-
)
342-
}
325+
let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
326+
self.global_set_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
343327
}
344328
}

0 commit comments

Comments
 (0)