Skip to content

Commit dede102

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 b0af276 + 6da2d6e commit dede102

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
@@ -500,6 +500,7 @@ impl<'a, 'gcc, 'tcx> Deref for Builder<'a, 'gcc, 'tcx> {
500500

501501
impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
502502
type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
503+
type Metadata = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Metadata;
503504
type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
504505
type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
505506
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;
@@ -677,26 +678,19 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
677678

678679
unsafe {
679680
let llty = self.cx.val_ty(load);
680-
let v = [
681-
self.cx.const_uint_big(llty, range.start),
682-
self.cx.const_uint_big(llty, range.end.wrapping_add(1)),
681+
let md = [
682+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.start)),
683+
llvm::LLVMValueAsMetadata(self.cx.const_uint_big(llty, range.end.wrapping_add(1))),
683684
];
684-
685-
llvm::LLVMSetMetadata(
686-
load,
687-
llvm::MD_range as c_uint,
688-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
689-
);
685+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
686+
self.set_metadata(load, llvm::MD_range, md);
690687
}
691688
}
692689

693690
fn nonnull_metadata(&mut self, load: &'ll Value) {
694691
unsafe {
695-
llvm::LLVMSetMetadata(
696-
load,
697-
llvm::MD_nonnull as c_uint,
698-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
699-
);
692+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
693+
self.set_metadata(load, llvm::MD_nonnull, md);
700694
}
701695
}
702696

@@ -744,9 +738,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
744738
// *always* point to a metadata value of the integer 1.
745739
//
746740
// [1]: https://llvm.org/docs/LangRef.html#store-instruction
747-
let one = self.cx.const_i32(1);
748-
let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
749-
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
741+
let one = llvm::LLVMValueAsMetadata(self.cx.const_i32(1));
742+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
743+
self.set_metadata(store, llvm::MD_nontemporal, md);
750744
}
751745
}
752746
store
@@ -1210,11 +1204,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12101204

12111205
fn set_invariant_load(&mut self, load: &'ll Value) {
12121206
unsafe {
1213-
llvm::LLVMSetMetadata(
1214-
load,
1215-
llvm::MD_invariant_load as c_uint,
1216-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1217-
);
1207+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1208+
self.set_metadata(load, llvm::MD_invariant_load, md);
12181209
}
12191210
}
12201211

@@ -1343,33 +1334,23 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13431334

13441335
fn align_metadata(&mut self, load: &'ll Value, align: Align) {
13451336
unsafe {
1346-
let v = [self.cx.const_u64(align.bytes())];
1347-
1348-
llvm::LLVMSetMetadata(
1349-
load,
1350-
llvm::MD_align as c_uint,
1351-
llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1352-
);
1337+
let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
1338+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
1339+
self.set_metadata(load, llvm::MD_align, md);
13531340
}
13541341
}
13551342

13561343
fn noundef_metadata(&mut self, load: &'ll Value) {
13571344
unsafe {
1358-
llvm::LLVMSetMetadata(
1359-
load,
1360-
llvm::MD_noundef as c_uint,
1361-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1362-
);
1345+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1346+
self.set_metadata(load, llvm::MD_noundef, md);
13631347
}
13641348
}
13651349

13661350
pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
13671351
unsafe {
1368-
llvm::LLVMSetMetadata(
1369-
inst,
1370-
llvm::MD_unpredictable as c_uint,
1371-
llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1372-
);
1352+
let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
1353+
self.set_metadata(inst, llvm::MD_unpredictable, md);
13731354
}
13741355
}
13751356

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::{c_uint, 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::*;
@@ -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};
@@ -403,17 +403,17 @@ pub(crate) unsafe fn create_module<'ll>(
403403
let rustc_producer =
404404
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
405405
let name_metadata = unsafe {
406-
llvm::LLVMMDStringInContext(
406+
llvm::LLVMMDStringInContext2(
407407
llcx,
408408
rustc_producer.as_ptr().cast(),
409-
rustc_producer.as_bytes().len() as c_uint,
409+
rustc_producer.as_bytes().len(),
410410
)
411411
};
412412
unsafe {
413413
llvm::LLVMAddNamedMetadataOperand(
414414
llmod,
415415
c"llvm.ident".as_ptr(),
416-
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
416+
&llvm::LLVMMetadataAsValue(llcx, llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
417417
);
418418
}
419419

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

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

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

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

+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;
@@ -616,18 +616,20 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
616616
}
617617
}
618618

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

625626
fn type_checked_load(
626627
&mut self,
627628
llvtable: &'ll Value,
628629
vtable_byte_offset: u64,
629-
typeid: &'ll Value,
630+
typeid: &'ll Metadata,
630631
) -> Self::Value {
632+
let typeid = unsafe { llvm::LLVMMetadataAsValue(&self.llcx, typeid) };
631633
let vtable_byte_offset = self.const_i32(vtable_byte_offset as i32);
632634
let type_checked_load =
633635
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
@@ -912,17 +912,7 @@ unsafe extern "C" {
912912
pub fn LLVMGetPoison(Ty: &Type) -> &Value;
913913

914914
// Operations on metadata
915-
// FIXME: deprecated, replace with LLVMMDStringInContext2
916-
pub fn LLVMMDStringInContext(C: &Context, Str: *const c_char, SLen: c_uint) -> &Value;
917-
918915
pub fn LLVMMDStringInContext2(C: &Context, Str: *const c_char, SLen: size_t) -> &Metadata;
919-
920-
// FIXME: deprecated, replace with LLVMMDNodeInContext2
921-
pub fn LLVMMDNodeInContext<'a>(
922-
C: &'a Context,
923-
Vals: *const &'a Value,
924-
Count: c_uint,
925-
) -> &'a Value;
926916
pub fn LLVMMDNodeInContext2<'a>(
927917
C: &'a Context,
928918
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)