Use a safe wrapper around an LLVM FFI function

This commit is contained in:
Oli Scherer 2025-02-24 14:45:16 +00:00
parent f16f64b15a
commit 3565603d25
5 changed files with 13 additions and 8 deletions

View file

@ -533,7 +533,7 @@ pub(crate) fn inline_asm_call<'ll>(
))
}));
let md = llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len());
let md = llvm::LLVMMetadataAsValue(&bx.llcx, md);
let md = bx.get_metadata_value(md);
llvm::LLVMSetMetadata(call, kind, md);
Some(call)

View file

@ -490,7 +490,7 @@ impl<'ll> CodegenCx<'ll, '_> {
llvm::LLVMMDStringInContext2(self.llcx, bytes.as_c_char_ptr(), bytes.len());
let data = [section, alloc];
let meta = llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len());
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
let val = self.get_metadata_value(meta);
llvm::LLVMAddNamedMetadataOperand(
self.llmod,
c"wasm.custom_sections".as_ptr(),

View file

@ -656,7 +656,7 @@ impl<'ll> SimpleCx<'ll> {
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
unsafe { llvm::LLVMMetadataAsValue(self.llcx(), metadata) }
llvm::LLVMMetadataAsValue(self.llcx(), metadata)
}
pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
@ -1225,9 +1225,14 @@ impl CodegenCx<'_, '_> {
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
pub(crate) fn set_metadata<'a>(&self, val: &'a Value, kind_id: MetadataType, md: &'a Metadata) {
pub(crate) fn set_metadata<'a>(
&self,
val: &'a Value,
kind_id: MetadataType,
md: &'ll Metadata,
) {
let node = self.get_metadata_value(md);
unsafe {
let node = llvm::LLVMMetadataAsValue(self.llcx(), md);
llvm::LLVMSetMetadata(val, kind_id as c_uint, node);
}
}

View file

@ -649,7 +649,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Metadata) -> Self::Value {
// Test the called operand using llvm.type.test intrinsic. The LowerTypeTests link-time
// optimization pass replaces calls to this intrinsic with code to test type membership.
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
let typeid = self.get_metadata_value(typeid);
self.call_intrinsic("llvm.type.test", &[pointer, typeid])
}
@ -659,7 +659,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
vtable_byte_offset: u64,
typeid: &'ll Metadata,
) -> Self::Value {
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
let typeid = self.get_metadata_value(typeid);
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
let type_checked_load =
self.call_intrinsic("llvm.type.checked.load", &[llvtable, vtable_byte_offset, typeid]);

View file

@ -1680,7 +1680,7 @@ unsafe extern "C" {
Packed: Bool,
);
pub(crate) fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
pub(crate) fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);