Skip to content

Commit 4cbfcf1

Browse files
committed
Auto merge of #130389 - Luv-Ray:LLVMMDNodeInContext2, r=nikic
llvm: replace some deprecated functions `LLVMMDStringInContext` and `LLVMMDNodeInContext` are deprecated, replace them with `LLVMMDStringInContext2` and `LLVMMDNodeInContext2`. Also replace `Value` with `Metadata` in some function signatures for better consistency.
2 parents 11e760b + d7ebf9e commit 4cbfcf1

File tree

14 files changed

+66
-91
lines changed

14 files changed

+66
-91
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ impl<'a, 'gcc, 'tcx> Deref for Builder<'a, 'gcc, 'tcx> {
503503

504504
impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
505505
type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
506+
type Metadata = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Metadata;
506507
type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
507508
type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
508509
type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;

compiler/rustc_codegen_gcc/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
414414

415415
impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> {
416416
type Value = RValue<'gcc>;
417+
type Metadata = RValue<'gcc>;
417418
type Function = RValue<'gcc>;
418419

419420
type BasicBlock = Block<'gcc>;

compiler/rustc_codegen_llvm/src/asm.rs

+8-3
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

+20-39
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;
@@ -623,26 +624,19 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
623624

624625
unsafe {
625626
let llty = self.cx.val_ty(load);
626-
let v = [
627-
self.cx.const_uint_big(llty, range.start),
628-
self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
627+
let md = [
628+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
629+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
629630
];
630-
631-
llvm::LLVMSetMetadata(
632-
load,
633-
llvm::MD_range as c_uint,
634-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
635-
);
631+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
632+
self.set_metadata(load, llvm::MD_range, md);
636633
}
637634
}
638635

639636
fn nonnull_metadata(&mut self, load: &'ll Value) {
640637
unsafe {
641-
llvm::LLVMSetMetadata(
642-
load,
643-
llvm::MD_nonnull as c_uint,
644-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
645-
);
638+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
639+
self.set_metadata(load, llvm::MD_nonnull, md);
646640
}
647641
}
648642

@@ -690,9 +684,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
690684
// *always* point to a metadata value of the integer 1.
691685
//
692686
// [1]: https://llvm.org/docs/LangRef.html#store-instruction
693-
let one = self.cx.const_i32(1);
694-
let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
695-
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
687+
let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
688+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
689+
self.set_metadata(store, llvm::MD_nontemporal, md);
696690
}
697691
}
698692
store
@@ -1157,11 +1151,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11571151

11581152
fn set_invariant_load(&mut self, load: &'ll Value) {
11591153
unsafe {
1160-
llvm::LLVMSetMetadata(
1161-
load,
1162-
llvm::MD_invariant_load as c_uint,
1163-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1164-
);
1154+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1155+
self.set_metadata(load, llvm::MD_invariant_load, md);
11651156
}
11661157
}
11671158

@@ -1290,33 +1281,23 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
12901281

12911282
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
12921283
unsafe {
1293-
let v = [self.cx.const_u64(align.bytes())];
1294-
1295-
llvm::LLVMSetMetadata(
1296-
load,
1297-
llvm::MD_align as c_uint,
1298-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1299-
);
1284+
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1285+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
1286+
self.set_metadata(load, llvm::MD_align, md);
13001287
}
13011288
}
13021289

13031290
fn noundef_metadata(&mut self, load: &'ll Value) {
13041291
unsafe {
1305-
llvm::LLVMSetMetadata(
1306-
load,
1307-
llvm::MD_noundef as c_uint,
1308-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1309-
);
1292+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1293+
self.set_metadata(load, llvm::MD_noundef, md);
13101294
}
13111295
}
13121296

13131297
pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
13141298
unsafe {
1315-
llvm::LLVMSetMetadata(
1316-
inst,
1317-
llvm::MD_unpredictable as c_uint,
1318-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1319-
);
1299+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1300+
self.set_metadata(inst, llvm::MD_unpredictable, md);
13201301
}
13211302
}
13221303

compiler/rustc_codegen_llvm/src/common.rs

+2-1
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

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::borrow::Borrow;
22
use std::cell::{Cell, RefCell};
3-
use std::ffi::CStr;
3+
use std::ffi::{CStr, c_uint};
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::*;
@@ -31,6 +30,7 @@ use smallvec::SmallVec;
3130
use crate::back::write::to_llvm_code_model;
3231
use crate::callee::get_fn;
3332
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
33+
use crate::llvm::{Metadata, MetadataType};
3434
use crate::type_::Type;
3535
use crate::value::Value;
3636
use crate::{attributes, coverageinfo, debuginfo, llvm, llvm_util};
@@ -404,17 +404,17 @@ pub(crate) unsafe fn create_module<'ll>(
404404
let rustc_producer =
405405
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
406406
let name_metadata = unsafe {
407-
llvm::LLVMMDStringInContext(
407+
llvm::LLVMMDStringInContext2(
408408
llcx,
409409
rustc_producer.as_ptr().cast(),
410-
rustc_producer.as_bytes().len() as c_uint,
410+
rustc_producer.as_bytes().len(),
411411
)
412412
};
413413
unsafe {
414414
llvm::LLVMAddNamedMetadataOperand(
415415
llmod,
416416
c"llvm.ident".as_ptr(),
417-
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
417+
&llvm::LLVMMetadataAsValue(llcx, llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
418418
);
419419
}
420420

@@ -1119,6 +1119,14 @@ impl CodegenCx<'_, '_> {
11191119
name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
11201120
name
11211121
}
1122+
1123+
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
1124+
pub(crate) fn set_metadata<'a>(&self, val: &'a Value, kind_id: MetadataType, md: &'a Metadata) {
1125+
unsafe {
1126+
let node = llvm::LLVMMetadataAsValue(&self.llcx, md);
1127+
llvm::LLVMSetMetadata(val, kind_id as c_uint, node);
1128+
}
1129+
}
11221130
}
11231131

11241132
impl HasDataLayout for CodegenCx<'_, '_> {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1547,20 +1547,16 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
15471547
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
15481548

15491549
unsafe {
1550-
let typeid = llvm::LLVMMDStringInContext(
1550+
let typeid = llvm::LLVMMDStringInContext2(
15511551
cx.llcx,
15521552
trait_ref_typeid.as_ptr() as *const c_char,
1553-
trait_ref_typeid.as_bytes().len() as c_uint,
1553+
trait_ref_typeid.as_bytes().len(),
15541554
);
1555-
let v = [cx.const_usize(0), typeid];
1555+
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
15561556
llvm::LLVMRustGlobalAddMetadata(
15571557
vtable,
15581558
llvm::MD_type as c_uint,
1559-
llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
1560-
cx.llcx,
1561-
v.as_ptr(),
1562-
v.len() as c_uint,
1563-
)),
1559+
llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
15641560
);
15651561
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
15661562
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);

compiler/rustc_codegen_llvm/src/intrinsic.rs

+5-3
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;
@@ -613,18 +613,20 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
613613
}
614614
}
615615

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

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

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-10
Original file line numberDiff line numberDiff line change
@@ -885,17 +885,7 @@ unsafe extern "C" {
885885
pub fn LLVMGetPoison(Ty: &Type) -> &Value;
886886

887887
// Operations on metadata
888-
// FIXME: deprecated, replace with LLVMMDStringInContext2
889-
pub fn LLVMMDStringInContext(C: &Context, Str: *const c_char, SLen: c_uint) -> &Value;
890-
891888
pub fn LLVMMDStringInContext2(C: &Context, Str: *const c_char, SLen: size_t) -> &Metadata;
892-
893-
// FIXME: deprecated, replace with LLVMMDNodeInContext2
894-
pub fn LLVMMDNodeInContext<'a>(
895-
C: &'a Context,
896-
Vals: *const &'a Value,
897-
Count: c_uint,
898-
) -> &'a Value;
899889
pub fn LLVMMDNodeInContext2<'a>(
900890
C: &'a Context,
901891
Vals: *const &'a Metadata,

compiler/rustc_codegen_llvm/src/type_.rs

+7-19
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> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
283283
impl<'ll, 'tcx> TypeMembershipCodegenMethods<'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

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::{CodegenResults, ModuleCodegen};
2121

2222
pub trait BackendTypes {
2323
type Value: CodegenObject;
24+
type Metadata: CodegenObject;
2425
type Function: CodegenObject;
2526

2627
type BasicBlock: Copy;

compiler/rustc_codegen_ssa/src/traits/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub trait BuilderMethods<'a, 'tcx>:
5151
type CodegenCx: CodegenMethods<
5252
'tcx,
5353
Value = Self::Value,
54+
Metadata = Self::Metadata,
5455
Function = Self::Function,
5556
BasicBlock = Self::BasicBlock,
5657
Type = Self::Type,

compiler/rustc_codegen_ssa/src/traits/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub trait IntrinsicCallBuilderMethods<'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/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes {
152152
pub trait TypeMembershipCodegenMethods<'tcx>: BackendTypes {
153153
fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
154154
fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
155-
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Value> {
155+
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Metadata> {
156156
None
157157
}
158158
fn add_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}

0 commit comments

Comments
 (0)