Skip to content

Commit ecb831d

Browse files
committed
Extract helper method set_metadata_node
1 parent 42b384e commit ecb831d

File tree

3 files changed

+36
-41
lines changed

3 files changed

+36
-41
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/context.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,11 @@ impl CodegenCx<'_, '_> {
10021002
}
10031003

10041004
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
1005+
/// Wrapper for `LLVMMDNodeInContext2`, i.e. `llvm::MDNode::get`.
1006+
pub(crate) fn md_node_in_context(&self, md_list: &[&'ll Metadata]) -> &'ll Metadata {
1007+
unsafe { llvm::LLVMMDNodeInContext2(self.llcx(), md_list.as_ptr(), md_list.len()) }
1008+
}
1009+
10051010
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
10061011
pub(crate) fn set_metadata<'a>(
10071012
&self,
@@ -1012,6 +1017,20 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
10121017
let node = self.get_metadata_value(md);
10131018
llvm::LLVMSetMetadata(val, kind_id, node);
10141019
}
1020+
1021+
/// Helper method for the sequence of calls:
1022+
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1023+
/// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
1024+
/// - `LLVMSetMetadata` (to set that node as metadata of `kind_id` for `instruction`)
1025+
pub(crate) fn set_metadata_node(
1026+
&self,
1027+
instruction: &'ll Value,
1028+
kind_id: MetadataKindId,
1029+
md_list: &[&'ll Metadata],
1030+
) {
1031+
let md = self.md_node_in_context(md_list);
1032+
self.set_metadata(instruction, kind_id, md);
1033+
}
10151034
}
10161035

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

0 commit comments

Comments
 (0)