Skip to content

Commit 7a01155

Browse files
authored
Rollup merge of rust-lang#147259 - Zalathar:node-in-context, r=nnethercote
cg_llvm: Use helper methods for all calls to `LLVMMDNodeInContext2` Originally I was only planning on extracting an `md_node_in_context` method, but then I noticed that all callers of `LLVMMDNodeInContext2` could be covered by a small number of additional helper methods. There should be no change in compiler output.
2 parents 6fe05b5 + 8ef9821 commit 7a01155

File tree

6 files changed

+93
-111
lines changed

6 files changed

+93
-111
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,7 @@ pub(crate) fn inline_asm_call<'ll>(
538538
bx.const_u64(u64::from(span.lo().to_u32()) | (u64::from(span.hi().to_u32()) << 32)),
539539
)
540540
}));
541-
let md = unsafe { llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len()) };
542-
let md = bx.get_metadata_value(md);
543-
llvm::LLVMSetMetadata(call, kind, md);
541+
bx.cx.set_metadata_node(call, kind, &srcloc);
544542

545543
Some(call)
546544
}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::borrow::{Borrow, Cow};
2+
use std::iter;
23
use std::ops::Deref;
3-
use std::{iter, ptr};
44

55
use rustc_ast::expand::typetree::FncTree;
66
pub(crate) mod autodiff;
77
pub(crate) mod gpu_offload;
88

9-
use libc::{c_char, c_uint, size_t};
9+
use libc::{c_char, c_uint};
1010
use rustc_abi as abi;
1111
use rustc_abi::{Align, Size, WrappingRange};
1212
use rustc_codegen_ssa::MemFlags;
@@ -396,10 +396,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
396396
md.push(weight(is_cold));
397397
}
398398

399-
unsafe {
400-
let md_node = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len() as size_t);
401-
self.cx.set_metadata(switch, llvm::MD_prof, md_node);
402-
}
399+
self.cx.set_metadata_node(switch, llvm::MD_prof, &md);
403400
}
404401

405402
fn invoke(
@@ -801,22 +798,16 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
801798
return;
802799
}
803800

804-
unsafe {
805-
let llty = self.cx.val_ty(load);
806-
let md = [
807-
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
808-
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
809-
];
810-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
811-
self.set_metadata(load, llvm::MD_range, md);
812-
}
801+
let llty = self.cx.val_ty(load);
802+
let md = [
803+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
804+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
805+
];
806+
self.set_metadata_node(load, llvm::MD_range, &md);
813807
}
814808

815809
fn nonnull_metadata(&mut self, load: &'ll Value) {
816-
unsafe {
817-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
818-
self.set_metadata(load, llvm::MD_nonnull, md);
819-
}
810+
self.set_metadata_node(load, llvm::MD_nonnull, &[]);
820811
}
821812

822813
fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
@@ -865,8 +856,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
865856
//
866857
// [1]: https://llvm.org/docs/LangRef.html#store-instruction
867858
let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
868-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
869-
self.set_metadata(store, llvm::MD_nontemporal, md);
859+
self.set_metadata_node(store, llvm::MD_nontemporal, &[one]);
870860
}
871861
}
872862
store
@@ -1381,10 +1371,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
13811371
}
13821372

13831373
fn set_invariant_load(&mut self, load: &'ll Value) {
1384-
unsafe {
1385-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1386-
self.set_metadata(load, llvm::MD_invariant_load, md);
1387-
}
1374+
self.set_metadata_node(load, llvm::MD_invariant_load, &[]);
13881375
}
13891376

13901377
fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
@@ -1528,25 +1515,16 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
15281515
}
15291516
impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
15301517
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
1531-
unsafe {
1532-
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1533-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
1534-
self.set_metadata(load, llvm::MD_align, md);
1535-
}
1518+
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1519+
self.set_metadata_node(load, llvm::MD_align, &md);
15361520
}
15371521

15381522
fn noundef_metadata(&mut self, load: &'ll Value) {
1539-
unsafe {
1540-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1541-
self.set_metadata(load, llvm::MD_noundef, md);
1542-
}
1523+
self.set_metadata_node(load, llvm::MD_noundef, &[]);
15431524
}
15441525

15451526
pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
1546-
unsafe {
1547-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1548-
self.set_metadata(inst, llvm::MD_unpredictable, md);
1549-
}
1527+
self.set_metadata_node(inst, llvm::MD_unpredictable, &[]);
15501528
}
15511529
}
15521530
impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,7 @@ impl<'ll> CodegenCx<'ll, '_> {
494494
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
495495
let alloc = self.create_metadata(bytes);
496496
let data = [section, alloc];
497-
let meta =
498-
unsafe { llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len()) };
499-
let val = self.get_metadata_value(meta);
500-
unsafe {
501-
llvm::LLVMAddNamedMetadataOperand(
502-
self.llmod,
503-
c"wasm.custom_sections".as_ptr(),
504-
val,
505-
)
506-
};
497+
self.module_add_named_metadata_node(self.llmod(), c"wasm.custom_sections", &data);
507498
}
508499
} else {
509500
base::set_link_section(g, attrs);

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use smallvec::SmallVec;
3434
use crate::back::write::to_llvm_code_model;
3535
use crate::callee::get_fn;
3636
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
37-
use crate::llvm::{Metadata, MetadataKindId};
37+
use crate::llvm::{Metadata, MetadataKindId, Module};
3838
use crate::type_::Type;
3939
use crate::value::Value;
4040
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
@@ -495,14 +495,7 @@ pub(crate) unsafe fn create_module<'ll>(
495495
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
496496

497497
let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
498-
499-
unsafe {
500-
llvm::LLVMAddNamedMetadataOperand(
501-
llmod,
502-
c"llvm.ident".as_ptr(),
503-
&cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
504-
);
505-
}
498+
cx.module_add_named_metadata_node(llmod, c"llvm.ident", &[name_metadata]);
506499

507500
// Emit RISC-V specific target-abi metadata
508501
// to workaround lld as the LTO plugin not
@@ -1002,6 +995,11 @@ impl CodegenCx<'_, '_> {
1002995
}
1003996

1004997
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
998+
/// Wrapper for `LLVMMDNodeInContext2`, i.e. `llvm::MDNode::get`.
999+
pub(crate) fn md_node_in_context(&self, md_list: &[&'ll Metadata]) -> &'ll Metadata {
1000+
unsafe { llvm::LLVMMDNodeInContext2(self.llcx(), md_list.as_ptr(), md_list.len()) }
1001+
}
1002+
10051003
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
10061004
pub(crate) fn set_metadata<'a>(
10071005
&self,
@@ -1012,6 +1010,61 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
10121010
let node = self.get_metadata_value(md);
10131011
llvm::LLVMSetMetadata(val, kind_id, node);
10141012
}
1013+
1014+
/// Helper method for the sequence of calls:
1015+
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1016+
/// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
1017+
/// - `LLVMSetMetadata` (to set that node as metadata of `kind_id` for `instruction`)
1018+
pub(crate) fn set_metadata_node(
1019+
&self,
1020+
instruction: &'ll Value,
1021+
kind_id: MetadataKindId,
1022+
md_list: &[&'ll Metadata],
1023+
) {
1024+
let md = self.md_node_in_context(md_list);
1025+
self.set_metadata(instruction, kind_id, md);
1026+
}
1027+
1028+
/// Helper method for the sequence of calls:
1029+
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1030+
/// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
1031+
/// - `LLVMAddNamedMetadataOperand` (to set that node as metadata of `kind_name` for `module`)
1032+
pub(crate) fn module_add_named_metadata_node(
1033+
&self,
1034+
module: &'ll Module,
1035+
kind_name: &CStr,
1036+
md_list: &[&'ll Metadata],
1037+
) {
1038+
let md = self.md_node_in_context(md_list);
1039+
let md_as_val = self.get_metadata_value(md);
1040+
unsafe { llvm::LLVMAddNamedMetadataOperand(module, kind_name.as_ptr(), md_as_val) };
1041+
}
1042+
1043+
/// Helper method for the sequence of calls:
1044+
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1045+
/// - `LLVMRustGlobalAddMetadata` (to set that node as metadata of `kind_id` for `global`)
1046+
pub(crate) fn global_add_metadata_node(
1047+
&self,
1048+
global: &'ll Value,
1049+
kind_id: MetadataKindId,
1050+
md_list: &[&'ll Metadata],
1051+
) {
1052+
let md = self.md_node_in_context(md_list);
1053+
unsafe { llvm::LLVMRustGlobalAddMetadata(global, kind_id, md) };
1054+
}
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+
}
10151068
}
10161069

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

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,17 +1607,11 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
16071607
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
16081608
let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());
16091609

1610-
unsafe {
1611-
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1612-
llvm::LLVMRustGlobalAddMetadata(
1613-
vtable,
1614-
llvm::MD_type,
1615-
llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
1616-
);
1617-
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1618-
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1619-
llvm::LLVMGlobalSetMetadata(vtable, llvm::MD_vcall_visibility, vcall_visibility_metadata);
1620-
}
1610+
let type_ = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1611+
cx.global_add_metadata_node(vtable, llvm::MD_type, &type_);
1612+
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);
16211615
}
16221616

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

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -302,59 +302,27 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
302302
impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
303303
fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
304304
let typeid_metadata = self.create_metadata(typeid);
305-
unsafe {
306-
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
307-
llvm::LLVMRustGlobalAddMetadata(
308-
function,
309-
llvm::MD_type,
310-
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
311-
)
312-
}
305+
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
306+
self.global_add_metadata_node(function, llvm::MD_type, &v);
313307
}
314308

315309
fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
316310
let typeid_metadata = self.create_metadata(typeid);
317-
unsafe {
318-
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
319-
llvm::LLVMGlobalSetMetadata(
320-
function,
321-
llvm::MD_type,
322-
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
323-
)
324-
}
311+
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
312+
self.global_set_metadata_node(function, llvm::MD_type, &v);
325313
}
326314

327315
fn typeid_metadata(&self, typeid: &[u8]) -> Option<&'ll Metadata> {
328316
Some(self.create_metadata(typeid))
329317
}
330318

331319
fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
332-
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
333-
unsafe {
334-
llvm::LLVMRustGlobalAddMetadata(
335-
function,
336-
llvm::MD_kcfi_type,
337-
llvm::LLVMMDNodeInContext2(
338-
self.llcx,
339-
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
340-
1,
341-
),
342-
)
343-
}
320+
let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
321+
self.global_add_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
344322
}
345323

346324
fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
347-
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
348-
unsafe {
349-
llvm::LLVMGlobalSetMetadata(
350-
function,
351-
llvm::MD_kcfi_type,
352-
llvm::LLVMMDNodeInContext2(
353-
self.llcx,
354-
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
355-
1,
356-
),
357-
)
358-
}
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);
359327
}
360328
}

0 commit comments

Comments
 (0)