Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,7 @@ pub(crate) fn inline_asm_call<'ll>(
bx.const_u64(u64::from(span.lo().to_u32()) | (u64::from(span.hi().to_u32()) << 32)),
)
}));
let md = unsafe { llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len()) };
let md = bx.get_metadata_value(md);
llvm::LLVMSetMetadata(call, kind, md);
bx.cx.set_metadata_node(call, kind, &srcloc);

Some(call)
}
Expand Down
54 changes: 16 additions & 38 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::borrow::{Borrow, Cow};
use std::iter;
use std::ops::Deref;
use std::{iter, ptr};

use rustc_ast::expand::typetree::FncTree;
pub(crate) mod autodiff;
pub(crate) mod gpu_offload;

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

unsafe {
let md_node = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len() as size_t);
self.cx.set_metadata(switch, llvm::MD_prof, md_node);
}
self.cx.set_metadata_node(switch, llvm::MD_prof, &md);
}

fn invoke(
Expand Down Expand Up @@ -801,22 +798,16 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
return;
}

unsafe {
let llty = self.cx.val_ty(load);
let md = [
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
];
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
self.set_metadata(load, llvm::MD_range, md);
}
let llty = self.cx.val_ty(load);
let md = [
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
];
self.set_metadata_node(load, llvm::MD_range, &md);
}

fn nonnull_metadata(&mut self, load: &'ll Value) {
unsafe {
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
self.set_metadata(load, llvm::MD_nonnull, md);
}
self.set_metadata_node(load, llvm::MD_nonnull, &[]);
}

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

fn set_invariant_load(&mut self, load: &'ll Value) {
unsafe {
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
self.set_metadata(load, llvm::MD_invariant_load, md);
}
self.set_metadata_node(load, llvm::MD_invariant_load, &[]);
}

fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
Expand Down Expand Up @@ -1528,25 +1515,16 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
}
impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
unsafe {
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
self.set_metadata(load, llvm::MD_align, md);
}
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
self.set_metadata_node(load, llvm::MD_align, &md);
}

fn noundef_metadata(&mut self, load: &'ll Value) {
unsafe {
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
self.set_metadata(load, llvm::MD_noundef, md);
}
self.set_metadata_node(load, llvm::MD_noundef, &[]);
}

pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
unsafe {
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
self.set_metadata(inst, llvm::MD_unpredictable, md);
}
self.set_metadata_node(inst, llvm::MD_unpredictable, &[]);
}
}
impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
Expand Down
11 changes: 1 addition & 10 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,7 @@ impl<'ll> CodegenCx<'ll, '_> {
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
let alloc = self.create_metadata(bytes);
let data = [section, alloc];
let meta =
unsafe { llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len()) };
let val = self.get_metadata_value(meta);
unsafe {
llvm::LLVMAddNamedMetadataOperand(
self.llmod,
c"wasm.custom_sections".as_ptr(),
val,
)
};
self.module_add_named_metadata_node(self.llmod(), c"wasm.custom_sections", &data);
}
} else {
base::set_link_section(g, attrs);
Expand Down
71 changes: 62 additions & 9 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use smallvec::SmallVec;
use crate::back::write::to_llvm_code_model;
use crate::callee::get_fn;
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
use crate::llvm::{Metadata, MetadataKindId};
use crate::llvm::{Metadata, MetadataKindId, Module};
use crate::type_::Type;
use crate::value::Value;
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
Expand Down Expand Up @@ -495,14 +495,7 @@ pub(crate) unsafe fn create_module<'ll>(
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));

let name_metadata = cx.create_metadata(rustc_producer.as_bytes());

unsafe {
llvm::LLVMAddNamedMetadataOperand(
llmod,
c"llvm.ident".as_ptr(),
&cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
);
}
cx.module_add_named_metadata_node(llmod, c"llvm.ident", &[name_metadata]);

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

impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
/// Wrapper for `LLVMMDNodeInContext2`, i.e. `llvm::MDNode::get`.
pub(crate) fn md_node_in_context(&self, md_list: &[&'ll Metadata]) -> &'ll Metadata {
unsafe { llvm::LLVMMDNodeInContext2(self.llcx(), md_list.as_ptr(), md_list.len()) }
}

/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
pub(crate) fn set_metadata<'a>(
&self,
Expand All @@ -1012,6 +1010,61 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
let node = self.get_metadata_value(md);
llvm::LLVMSetMetadata(val, kind_id, node);
}

/// Helper method for the sequence of calls:
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
/// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
/// - `LLVMSetMetadata` (to set that node as metadata of `kind_id` for `instruction`)
pub(crate) fn set_metadata_node(
&self,
instruction: &'ll Value,
kind_id: MetadataKindId,
md_list: &[&'ll Metadata],
) {
let md = self.md_node_in_context(md_list);
self.set_metadata(instruction, kind_id, md);
}

/// Helper method for the sequence of calls:
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
/// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
/// - `LLVMAddNamedMetadataOperand` (to set that node as metadata of `kind_name` for `module`)
pub(crate) fn module_add_named_metadata_node(
&self,
module: &'ll Module,
kind_name: &CStr,
md_list: &[&'ll Metadata],
) {
let md = self.md_node_in_context(md_list);
let md_as_val = self.get_metadata_value(md);
unsafe { llvm::LLVMAddNamedMetadataOperand(module, kind_name.as_ptr(), md_as_val) };
}

/// Helper method for the sequence of calls:
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
/// - `LLVMRustGlobalAddMetadata` (to set that node as metadata of `kind_id` for `global`)
pub(crate) fn global_add_metadata_node(
&self,
global: &'ll Value,
kind_id: MetadataKindId,
md_list: &[&'ll Metadata],
) {
let md = self.md_node_in_context(md_list);
unsafe { llvm::LLVMRustGlobalAddMetadata(global, kind_id, md) };
}

/// Helper method for the sequence of calls:
/// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
/// - `LLVMGlobalSetMetadata` (to set that node as metadata of `kind_id` for `global`)
pub(crate) fn global_set_metadata_node(
&self,
global: &'ll Value,
kind_id: MetadataKindId,
md_list: &[&'ll Metadata],
) {
let md = self.md_node_in_context(md_list);
unsafe { llvm::LLVMGlobalSetMetadata(global, kind_id, md) };
}
}

impl HasDataLayout for CodegenCx<'_, '_> {
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,17 +1607,11 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());

unsafe {
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
llvm::LLVMRustGlobalAddMetadata(
vtable,
llvm::MD_type,
llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
);
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
llvm::LLVMGlobalSetMetadata(vtable, llvm::MD_vcall_visibility, vcall_visibility_metadata);
}
let type_ = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
cx.global_add_metadata_node(vtable, llvm::MD_type, &type_);

let vcall_visibility = [llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64))];
cx.global_set_metadata_node(vtable, llvm::MD_vcall_visibility, &vcall_visibility);
}

/// Creates debug information for the given vtable, which is for the
Expand Down
48 changes: 8 additions & 40 deletions compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,59 +302,27 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
let typeid_metadata = self.create_metadata(typeid);
unsafe {
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
llvm::LLVMRustGlobalAddMetadata(
function,
llvm::MD_type,
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
)
}
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
self.global_add_metadata_node(function, llvm::MD_type, &v);
}

fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
let typeid_metadata = self.create_metadata(typeid);
unsafe {
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
llvm::LLVMGlobalSetMetadata(
function,
llvm::MD_type,
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
)
}
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
self.global_set_metadata_node(function, llvm::MD_type, &v);
}

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

fn add_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
unsafe {
llvm::LLVMRustGlobalAddMetadata(
function,
llvm::MD_kcfi_type,
llvm::LLVMMDNodeInContext2(
self.llcx,
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
1,
),
)
}
let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
self.global_add_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
}

fn set_kcfi_type_metadata(&self, function: &'ll Value, kcfi_typeid: u32) {
let kcfi_type_metadata = self.const_u32(kcfi_typeid);
unsafe {
llvm::LLVMGlobalSetMetadata(
function,
llvm::MD_kcfi_type,
llvm::LLVMMDNodeInContext2(
self.llcx,
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
1,
),
)
}
let kcfi_type_metadata = [llvm::LLVMValueAsMetadata(self.const_u32(kcfi_typeid))];
self.global_set_metadata_node(function, llvm::MD_kcfi_type, &kcfi_type_metadata);
}
}
Loading