Skip to content

Commit 3565603

Browse files
committed
Use a safe wrapper around an LLVM FFI function
1 parent f16f64b commit 3565603

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

Diff for: compiler/rustc_codegen_llvm/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ pub(crate) fn inline_asm_call<'ll>(
533533
))
534534
}));
535535
let md = llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len());
536-
let md = llvm::LLVMMetadataAsValue(&bx.llcx, md);
536+
let md = bx.get_metadata_value(md);
537537
llvm::LLVMSetMetadata(call, kind, md);
538538

539539
Some(call)

Diff for: compiler/rustc_codegen_llvm/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'ll> CodegenCx<'ll, '_> {
490490
llvm::LLVMMDStringInContext2(self.llcx, bytes.as_c_char_ptr(), bytes.len());
491491
let data = [section, alloc];
492492
let meta = llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len());
493-
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
493+
let val = self.get_metadata_value(meta);
494494
llvm::LLVMAddNamedMetadataOperand(
495495
self.llmod,
496496
c"wasm.custom_sections".as_ptr(),

Diff for: compiler/rustc_codegen_llvm/src/context.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ impl<'ll> SimpleCx<'ll> {
656656

657657
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
658658
pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
659-
unsafe { llvm::LLVMMetadataAsValue(self.llcx(), metadata) }
659+
llvm::LLVMMetadataAsValue(self.llcx(), metadata)
660660
}
661661

662662
pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
@@ -1225,9 +1225,14 @@ impl CodegenCx<'_, '_> {
12251225

12261226
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
12271227
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
1228-
pub(crate) fn set_metadata<'a>(&self, val: &'a Value, kind_id: MetadataType, md: &'a Metadata) {
1228+
pub(crate) fn set_metadata<'a>(
1229+
&self,
1230+
val: &'a Value,
1231+
kind_id: MetadataType,
1232+
md: &'ll Metadata,
1233+
) {
1234+
let node = self.get_metadata_value(md);
12291235
unsafe {
1230-
let node = llvm::LLVMMetadataAsValue(self.llcx(), md);
12311236
llvm::LLVMSetMetadata(val, kind_id as c_uint, node);
12321237
}
12331238
}

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
649649
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Metadata) -> Self::Value {
650650
// Test the called operand using llvm.type.test intrinsic. The LowerTypeTests link-time
651651
// optimization pass replaces calls to this intrinsic with code to test type membership.
652-
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
652+
let typeid = self.get_metadata_value(typeid);
653653
self.call_intrinsic("llvm.type.test", &[pointer, typeid])
654654
}
655655

@@ -659,7 +659,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
659659
vtable_byte_offset: u64,
660660
typeid: &'ll Metadata,
661661
) -> Self::Value {
662-
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
662+
let typeid = self.get_metadata_value(typeid);
663663
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
664664
let type_checked_load =
665665
self.call_intrinsic("llvm.type.checked.load", &[llvtable, vtable_byte_offset, typeid]);

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ unsafe extern "C" {
16801680
Packed: Bool,
16811681
);
16821682

1683-
pub(crate) fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
1683+
pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
16841684

16851685
pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
16861686

0 commit comments

Comments
 (0)