Skip to content

Commit 60de210

Browse files
committed
LLVMMDNodeInContext2
1 parent 473ae00 commit 60de210

File tree

12 files changed

+61
-79
lines changed

12 files changed

+61
-79
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,15 @@ pub(crate) fn inline_asm_call<'ll>(
504504
// due to the asm template string coming from a macro. LLVM will
505505
// default to the first srcloc for lines that don't have an
506506
// associated srcloc.
507-
srcloc.push(bx.const_i32(0));
507+
srcloc.push(llvm::LLVMValueAsMetadata(bx.const_i32(0)));
508508
}
509-
srcloc.extend(line_spans.iter().map(|span| bx.const_i32(span.lo().to_u32() as i32)));
510-
let md = llvm::LLVMMDNodeInContext(bx.llcx, srcloc.as_ptr(), srcloc.len() as u32);
509+
srcloc.extend(
510+
line_spans
511+
.iter()
512+
.map(|span| llvm::LLVMValueAsMetadata(bx.const_i32(span.lo().to_u32() as i32))),
513+
);
514+
let md = llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len());
515+
let md = llvm::LLVMMetadataAsValue(&bx.llcx, md);
511516
llvm::LLVMSetMetadata(call, kind, md);
512517

513518
Some(call)

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const UNNAMED: *const c_char = c"".as_ptr();
5656

5757
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5858
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
59+
type Metadata = <CodegenCx<'ll, 'tcx> as BackendTypes>::Metadata;
5960
type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
6061
type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
6162
type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
@@ -684,25 +685,21 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
684685
unsafe {
685686
let llty = self.cx.val_ty(load);
686687
let v = [
687-
self.cx.const_uint_big(llty, range.start),
688-
self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
688+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
689+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
689690
];
690691

691-
llvm::LLVMSetMetadata(
692-
load,
693-
llvm::MD_range as c_uint,
694-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
695-
);
692+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, v.as_ptr(), v.len());
693+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
694+
llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint, md);
696695
}
697696
}
698697

699698
fn nonnull_metadata(&mut self, load: &'ll Value) {
700699
unsafe {
701-
llvm::LLVMSetMetadata(
702-
load,
703-
llvm::MD_nonnull as c_uint,
704-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
705-
);
700+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
701+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
702+
llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint, md);
706703
}
707704
}
708705

@@ -750,8 +747,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
750747
// *always* point to a metadata value of the integer 1.
751748
//
752749
// [1]: https://llvm.org/docs/LangRef.html#store-instruction
753-
let one = self.cx.const_i32(1);
754-
let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
750+
let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
751+
let node = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
752+
let node = llvm::LLVMMetadataAsValue(&self.llcx, node);
755753
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
756754
}
757755
}
@@ -1216,11 +1214,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12161214

12171215
fn set_invariant_load(&mut self, load: &'ll Value) {
12181216
unsafe {
1219-
llvm::LLVMSetMetadata(
1220-
load,
1221-
llvm::MD_invariant_load as c_uint,
1222-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1223-
);
1217+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1218+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1219+
llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint, md);
12241220
}
12251221
}
12261222

@@ -1355,33 +1351,26 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13551351

13561352
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
13571353
unsafe {
1358-
let v = [self.cx.const_u64(align.bytes())];
1359-
1360-
llvm::LLVMSetMetadata(
1361-
load,
1362-
llvm::MD_align as c_uint,
1363-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1364-
);
1354+
let v = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1355+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, v.as_ptr(), v.len());
1356+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1357+
llvm::LLVMSetMetadata(load, llvm::MD_align as c_uint, md);
13651358
}
13661359
}
13671360

13681361
fn noundef_metadata(&mut self, load: &'ll Value) {
13691362
unsafe {
1370-
llvm::LLVMSetMetadata(
1371-
load,
1372-
llvm::MD_noundef as c_uint,
1373-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1374-
);
1363+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1364+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1365+
llvm::LLVMSetMetadata(load, llvm::MD_noundef as c_uint, md);
13751366
}
13761367
}
13771368

13781369
pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
13791370
unsafe {
1380-
llvm::LLVMSetMetadata(
1381-
inst,
1382-
llvm::MD_unpredictable as c_uint,
1383-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1384-
);
1371+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1372+
let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
1373+
llvm::LLVMSetMetadata(inst, llvm::MD_unpredictable as c_uint, md);
13851374
}
13861375
}
13871376

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing::debug;
1414

1515
use crate::consts::const_alloc_to_llvm;
1616
pub(crate) use crate::context::CodegenCx;
17-
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True};
17+
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, OperandBundleDef, True};
1818
use crate::type_::Type;
1919
use crate::value::Value;
2020

@@ -79,6 +79,7 @@ impl<'ll> Funclet<'ll> {
7979

8080
impl<'ll> BackendTypes for CodegenCx<'ll, '_> {
8181
type Value = &'ll Value;
82+
type Metadata = &'ll Metadata;
8283
// FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
8384
type Function = &'ll Value;
8485

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cell::{Cell, RefCell};
33
use std::ffi::CStr;
44
use std::str;
55

6-
use libc::c_uint;
76
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
87
use rustc_codegen_ssa::errors as ssa_errors;
98
use rustc_codegen_ssa::traits::*;
@@ -413,17 +412,17 @@ pub(crate) unsafe fn create_module<'ll>(
413412
let rustc_producer =
414413
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
415414
let name_metadata = unsafe {
416-
llvm::LLVMMDStringInContext(
415+
llvm::LLVMMDStringInContext2(
417416
llcx,
418417
rustc_producer.as_ptr().cast(),
419-
rustc_producer.as_bytes().len() as c_uint,
418+
rustc_producer.as_bytes().len(),
420419
)
421420
};
422421
unsafe {
423422
llvm::LLVMAddNamedMetadataOperand(
424423
llmod,
425424
c"llvm.ident".as_ptr(),
426-
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
425+
&llvm::LLVMMetadataAsValue(llcx, llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
427426
);
428427
}
429428

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,20 +1545,16 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
15451545
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
15461546

15471547
unsafe {
1548-
let typeid = llvm::LLVMMDStringInContext(
1548+
let typeid = llvm::LLVMMDStringInContext2(
15491549
cx.llcx,
15501550
trait_ref_typeid.as_ptr() as *const c_char,
1551-
trait_ref_typeid.as_bytes().len() as c_uint,
1551+
trait_ref_typeid.as_bytes().len(),
15521552
);
1553-
let v = [cx.const_usize(0), typeid];
1553+
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
15541554
llvm::LLVMRustGlobalAddMetadata(
15551555
vtable,
15561556
llvm::MD_type as c_uint,
1557-
llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
1558-
cx.llcx,
1559-
v.as_ptr(),
1560-
v.len() as c_uint,
1561-
)),
1557+
llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
15621558
);
15631559
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
15641560
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::debug;
2020
use crate::abi::{Abi, FnAbi, FnAbiLlvmExt, LlvmType, PassMode};
2121
use crate::builder::Builder;
2222
use crate::context::CodegenCx;
23-
use crate::llvm;
23+
use crate::llvm::{self, Metadata};
2424
use crate::type_::Type;
2525
use crate::type_of::LayoutLlvmExt;
2626
use crate::va_arg::emit_va_arg;
@@ -614,18 +614,20 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
614614
}
615615
}
616616

617-
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value {
617+
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Metadata) -> Self::Value {
618618
// Test the called operand using llvm.type.test intrinsic. The LowerTypeTests link-time
619619
// optimization pass replaces calls to this intrinsic with code to test type membership.
620+
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
620621
self.call_intrinsic("llvm.type.test", &[pointer, typeid])
621622
}
622623

623624
fn type_checked_load(
624625
&mut self,
625626
llvtable: &'ll Value,
626627
vtable_byte_offset: u64,
627-
typeid: &'ll Value,
628+
typeid: &'ll Metadata,
628629
) -> Self::Value {
630+
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
629631
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
630632
let type_checked_load =
631633
self.call_intrinsic("llvm.type.checked.load", &[llvtable, vtable_byte_offset, typeid]);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,12 @@ unsafe extern "C" {
912912
pub fn LLVMGetPoison(Ty: &Type) -> &Value;
913913

914914
// Operations on metadata
915-
// FIXME: deprecated, replace with LLVMMDStringInContext2
915+
#[deprecated = "replace with LLVMMDStringInContext2"]
916916
pub fn LLVMMDStringInContext(C: &Context, Str: *const c_char, SLen: c_uint) -> &Value;
917917

918918
pub fn LLVMMDStringInContext2(C: &Context, Str: *const c_char, SLen: size_t) -> &Metadata;
919919

920-
// FIXME: deprecated, replace with LLVMMDNodeInContext2
920+
#[deprecated = "replace with LLVMMDNodeInContext2"]
921921
pub fn LLVMMDNodeInContext<'a>(
922922
C: &'a Context,
923923
Vals: *const &'a Value,

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_target::abi::{AddressSpace, Align, Integer, Size};
1313
use crate::abi::{FnAbiLlvmExt, LlvmType};
1414
use crate::context::CodegenCx;
1515
pub(crate) use crate::llvm::Type;
16-
use crate::llvm::{Bool, False, True};
16+
use crate::llvm::{Bool, False, Metadata, True};
1717
use crate::type_of::LayoutLlvmExt;
1818
use crate::value::Value;
1919
use crate::{common, llvm};
@@ -283,43 +283,31 @@ impl<'ll, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
283283
impl<'ll, 'tcx> TypeMembershipMethods<'tcx> for CodegenCx<'ll, 'tcx> {
284284
fn add_type_metadata(&self, function: &'ll Value, typeid: String) {
285285
let typeid_metadata = self.typeid_metadata(typeid).unwrap();
286-
let v = [self.const_usize(0), typeid_metadata];
287286
unsafe {
287+
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
288288
llvm::LLVMRustGlobalAddMetadata(
289289
function,
290290
llvm::MD_type as c_uint,
291-
llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
292-
self.llcx,
293-
v.as_ptr(),
294-
v.len() as c_uint,
295-
)),
291+
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
296292
)
297293
}
298294
}
299295

300296
fn set_type_metadata(&self, function: &'ll Value, typeid: String) {
301297
let typeid_metadata = self.typeid_metadata(typeid).unwrap();
302-
let v = [self.const_usize(0), typeid_metadata];
303298
unsafe {
299+
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
304300
llvm::LLVMGlobalSetMetadata(
305301
function,
306302
llvm::MD_type as c_uint,
307-
llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
308-
self.llcx,
309-
v.as_ptr(),
310-
v.len() as c_uint,
311-
)),
303+
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
312304
)
313305
}
314306
}
315307

316-
fn typeid_metadata(&self, typeid: String) -> Option<&'ll Value> {
308+
fn typeid_metadata(&self, typeid: String) -> Option<&'ll Metadata> {
317309
Some(unsafe {
318-
llvm::LLVMMDStringInContext(
319-
self.llcx,
320-
typeid.as_ptr() as *const c_char,
321-
typeid.len() as c_uint,
322-
)
310+
llvm::LLVMMDStringInContext2(self.llcx, typeid.as_ptr() as *const c_char, typeid.len())
323311
})
324312
}
325313

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::{CodegenResults, ModuleCodegen};
2323

2424
pub trait BackendTypes {
2525
type Value: CodegenObject;
26+
type Metadata: CodegenObject;
2627
type Function: CodegenObject;
2728

2829
type BasicBlock: Copy;

compiler/rustc_codegen_ssa/src/traits/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
2424
fn assume(&mut self, val: Self::Value);
2525
fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value;
2626
/// Trait method used to test whether a given pointer is associated with a type identifier.
27-
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value;
27+
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Metadata) -> Self::Value;
2828
/// Trait method used to load a function while testing if it is associated with a type
2929
/// identifier.
3030
fn type_checked_load(
3131
&mut self,
3232
llvtable: Self::Value,
3333
vtable_byte_offset: u64,
34-
typeid: Self::Value,
34+
typeid: Self::Metadata,
3535
) -> Self::Value;
3636
/// Trait method used to inject `va_start` on the "spoofed" `VaListImpl` in
3737
/// Rust defined C-variadic functions.

compiler/rustc_codegen_ssa/src/traits/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub trait HasCodegen<'tcx>:
8989
type CodegenCx: CodegenMethods<'tcx>
9090
+ BackendTypes<
9191
Value = Self::Value,
92+
Metadata = Self::Metadata,
9293
Function = Self::Function,
9394
BasicBlock = Self::BasicBlock,
9495
Type = Self::Type,

compiler/rustc_codegen_ssa/src/traits/type_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
149149
pub trait TypeMembershipMethods<'tcx>: Backend<'tcx> {
150150
fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
151151
fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
152-
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Value> {
152+
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Metadata> {
153153
None
154154
}
155155
fn add_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}

0 commit comments

Comments
 (0)