Skip to content

Commit 632342a

Browse files
committed
wrap LLVMSetMetadata
1 parent b7c5656 commit 632342a

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -678,22 +678,20 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
678678

679679
unsafe {
680680
let llty = self.cx.val_ty(load);
681-
let v = [
681+
let md = [
682682
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
683683
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
684684
];
685685

686-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, v.as_ptr(), v.len());
687-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
688-
llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint, md);
686+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
687+
self.set_metadata(load, llvm::MD_range as c_uint, md);
689688
}
690689
}
691690

692691
fn nonnull_metadata(&mut self, load: &'ll Value) {
693692
unsafe {
694693
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
695-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
696-
llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint, md);
694+
self.set_metadata(load, llvm::MD_nonnull as c_uint, md);
697695
}
698696
}
699697

@@ -742,9 +740,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
742740
//
743741
// [1]: https://llvm.org/docs/LangRef.html#store-instruction
744742
let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
745-
let node = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
746-
let node = llvm::LLVMMetadataAsValue(&self.llcx, node);
747-
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
743+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
744+
self.set_metadata(store, llvm::MD_nontemporal as c_uint, md);
748745
}
749746
}
750747
store
@@ -1209,8 +1206,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12091206
fn set_invariant_load(&mut self, load: &'ll Value) {
12101207
unsafe {
12111208
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1212-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1213-
llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint, md);
1209+
self.set_metadata(load, llvm::MD_invariant_load as c_uint, md);
12141210
}
12151211
}
12161212

@@ -1339,26 +1335,23 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13391335

13401336
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
13411337
unsafe {
1342-
let v = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1343-
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, v.as_ptr(), v.len());
1344-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1345-
llvm::LLVMSetMetadata(load, llvm::MD_align as c_uint, md);
1338+
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1339+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
1340+
self.set_metadata(load, llvm::MD_align as c_uint, md);
13461341
}
13471342
}
13481343

13491344
fn noundef_metadata(&mut self, load: &'ll Value) {
13501345
unsafe {
13511346
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1352-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1353-
llvm::LLVMSetMetadata(load, llvm::MD_noundef as c_uint, md);
1347+
self.set_metadata(load, llvm::MD_noundef as c_uint, md);
13541348
}
13551349
}
13561350

13571351
pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
13581352
unsafe {
13591353
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1360-
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1361-
llvm::LLVMSetMetadata(inst, llvm::MD_unpredictable as c_uint, md);
1354+
self.set_metadata(inst, llvm::MD_unpredictable as c_uint, md);
13621355
}
13631356
}
13641357

compiler/rustc_codegen_llvm/src/context.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Borrow;
22
use std::cell::{Cell, RefCell};
3-
use std::ffi::CStr;
3+
use std::ffi::{c_uint, CStr};
44
use std::str;
55

66
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
@@ -30,6 +30,7 @@ use smallvec::SmallVec;
3030
use crate::back::write::to_llvm_code_model;
3131
use crate::callee::get_fn;
3232
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
33+
use crate::llvm::Metadata;
3334
use crate::type_::Type;
3435
use crate::value::Value;
3536
use crate::{attributes, coverageinfo, debuginfo, llvm, llvm_util};
@@ -585,6 +586,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
585586
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
586587
}
587588
}
589+
590+
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
591+
pub(crate) fn set_metadata<'a>(&self, val: &'a Value, kind_id: c_uint, md: &'a Metadata) {
592+
unsafe {
593+
let node = llvm::LLVMMetadataAsValue(&self.llcx, md);
594+
llvm::LLVMSetMetadata(val, kind_id, node);
595+
}
596+
}
588597
}
589598

590599
impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {

0 commit comments

Comments
 (0)