From 3a78ab473d98cc6099af19c8396dac8091b901f5 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 18 Sep 2024 16:06:20 +1000 Subject: [PATCH] Remove `LLVMMDNodeInContext`. Because it's deprecated in LLVM. Use `LLVMMDNodeInContext2` instead. --- compiler/rustc_codegen_llvm/src/asm.rs | 18 +++++-- compiler/rustc_codegen_llvm/src/builder.rs | 53 ++++++++++++++++----- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 7 --- compiler/rustc_codegen_llvm/src/type_.rs | 22 ++++----- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/asm.rs b/compiler/rustc_codegen_llvm/src/asm.rs index 1d91c3fb17ddc..cc098cb5b8d8a 100644 --- a/compiler/rustc_codegen_llvm/src/asm.rs +++ b/compiler/rustc_codegen_llvm/src/asm.rs @@ -504,11 +504,21 @@ pub(crate) fn inline_asm_call<'ll>( // due to the asm template string coming from a macro. LLVM will // default to the first srcloc for lines that don't have an // associated srcloc. - srcloc.push(bx.const_i32(0)); + srcloc.push(llvm::LLVMValueAsMetadata(bx.const_i32(0))); } - srcloc.extend(line_spans.iter().map(|span| bx.const_i32(span.lo().to_u32() as i32))); - let md = llvm::LLVMMDNodeInContext(bx.llcx, srcloc.as_ptr(), srcloc.len() as u32); - llvm::LLVMSetMetadata(call, kind, md); + srcloc.extend( + line_spans + .iter() + .map(|span| llvm::LLVMValueAsMetadata(bx.const_i32(span.lo().to_u32() as i32))), + ); + llvm::LLVMSetMetadata( + call, + kind, + llvm::LLVMMetadataAsValue( + bx.llcx, + llvm::LLVMMDNodeInContext2(bx.llcx, srcloc.as_ptr(), srcloc.len()), + ), + ); Some(call) } else { diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 40783825cae57..2c028ad8db1d6 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -683,15 +683,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { unsafe { let llty = self.cx.val_ty(load); - let v = [ - self.cx.const_uint_big(llty, range.start), - self.cx.const_uint_big(llty, range.end.wrapping_add(1)), + 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))), ]; llvm::LLVMSetMetadata( load, llvm::MD_range as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len()), + ), ); } } @@ -701,7 +704,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { llvm::LLVMSetMetadata( load, llvm::MD_nonnull as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0), + ), ); } } @@ -750,9 +756,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { // *always* point to a metadata value of the integer 1. // // [1]: https://llvm.org/docs/LangRef.html#store-instruction - let one = self.cx.const_i32(1); - let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1); - llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node); + llvm::LLVMSetMetadata( + store, + llvm::MD_nontemporal as c_uint, + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2( + self.cx.llcx, + &llvm::LLVMValueAsMetadata(self.cx.const_i32(1)), + 1, + ), + ), + ); } } store @@ -1219,7 +1234,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { llvm::LLVMSetMetadata( load, llvm::MD_invariant_load as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0), + ), ); } } @@ -1355,12 +1373,15 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { fn align_metadata(&mut self, load: &'ll Value, align: Align) { unsafe { - let v = [self.cx.const_u64(align.bytes())]; + let md = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))]; llvm::LLVMSetMetadata( load, llvm::MD_align as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len()), + ), ); } } @@ -1370,7 +1391,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { llvm::LLVMSetMetadata( load, llvm::MD_noundef as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0), + ), ); } } @@ -1380,7 +1404,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { llvm::LLVMSetMetadata( inst, llvm::MD_unpredictable as c_uint, - llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0), + llvm::LLVMMetadataAsValue( + self.cx.llcx, + llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0), + ), ); } } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index c3dd2d373665f..2c0e7f6ffbb74 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -913,13 +913,6 @@ unsafe extern "C" { // Operations on metadata pub fn LLVMMDStringInContext2(C: &Context, Str: *const c_char, SLen: size_t) -> &Metadata; - - // FIXME: deprecated, replace with LLVMMDNodeInContext2 - pub fn LLVMMDNodeInContext<'a>( - C: &'a Context, - Vals: *const &'a Value, - Count: c_uint, - ) -> &'a Value; pub fn LLVMMDNodeInContext2<'a>( C: &'a Context, Vals: *const &'a Metadata, diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 4a27990a20c8a..6f0cb739286e9 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -283,32 +283,30 @@ impl<'ll, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> { impl<'ll, 'tcx> TypeMembershipMethods<'tcx> for CodegenCx<'ll, 'tcx> { fn add_type_metadata(&self, function: &'ll Value, typeid: String) { let typeid_metadata = self.typeid_metadata(typeid).unwrap(); - let v = [self.const_usize(0), typeid_metadata]; unsafe { + let meta = [ + llvm::LLVMValueAsMetadata(self.const_usize(0)), + llvm::LLVMValueAsMetadata(typeid_metadata), + ]; llvm::LLVMRustGlobalAddMetadata( function, llvm::MD_type as c_uint, - llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext( - self.llcx, - v.as_ptr(), - v.len() as c_uint, - )), + llvm::LLVMMDNodeInContext2(self.llcx, meta.as_ptr(), meta.len()), ) } } fn set_type_metadata(&self, function: &'ll Value, typeid: String) { let typeid_metadata = self.typeid_metadata(typeid).unwrap(); - let v = [self.const_usize(0), typeid_metadata]; unsafe { + let meta = [ + llvm::LLVMValueAsMetadata(self.const_usize(0)), + llvm::LLVMValueAsMetadata(typeid_metadata), + ]; llvm::LLVMGlobalSetMetadata( function, llvm::MD_type as c_uint, - llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext( - self.llcx, - v.as_ptr(), - v.len() as c_uint, - )), + llvm::LLVMMDNodeInContext2(self.llcx, meta.as_ptr(), meta.len()), ) } }