about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuv-Ray <zhuoxun.yang777@outlook.com>2024-09-19 18:45:23 +0800
committerLuv-Ray <zhuoxun.yang777@outlook.com>2024-09-19 18:45:23 +0800
commit632342a13513dfdbb53fcbd14ac2e493e36ed6ff (patch)
treebab54a974852c696c0070635af58781dc830d23a
parentb7c5656713158e826189db21eeea051347548e07 (diff)
downloadrust-632342a13513dfdbb53fcbd14ac2e493e36ed6ff.tar.gz
rust-632342a13513dfdbb53fcbd14ac2e493e36ed6ff.zip
wrap `LLVMSetMetadata`
-rw-r--r--compiler/rustc_codegen_llvm/src/builder.rs31
-rw-r--r--compiler/rustc_codegen_llvm/src/context.rs11
2 files changed, 22 insertions, 20 deletions
diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs
index e4efe83079b..70d81c6c5a8 100644
--- a/compiler/rustc_codegen_llvm/src/builder.rs
+++ b/compiler/rustc_codegen_llvm/src/builder.rs
@@ -678,22 +678,20 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
 
         unsafe {
             let llty = self.cx.val_ty(load);
-            let v = [
+            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, v.as_ptr(), v.len());
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint, md);
+            let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, md.as_ptr(), md.len());
+            self.set_metadata(load, llvm::MD_range as c_uint, md);
         }
     }
 
     fn nonnull_metadata(&mut self, load: &'ll Value) {
         unsafe {
             let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint, md);
+            self.set_metadata(load, llvm::MD_nonnull as c_uint, md);
         }
     }
 
@@ -742,9 +740,8 @@ 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 node = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
-                    let node = llvm::LLVMMetadataAsValue(&self.llcx, node);
-                    llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
+                    let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, &one, 1);
+                    self.set_metadata(store, llvm::MD_nontemporal as c_uint, md);
                 }
             }
             store
@@ -1209,8 +1206,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);
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint, md);
+            self.set_metadata(load, llvm::MD_invariant_load as c_uint, md);
         }
     }
 
@@ -1339,26 +1335,23 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
 
     fn align_metadata(&mut self, load: &'ll Value, align: Align) {
         unsafe {
-            let v = [llvm::LLVMValueAsMetadata(self.cx.const_u64(align.bytes()))];
-            let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, v.as_ptr(), v.len());
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(load, llvm::MD_align as c_uint, md);
+            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 as c_uint, md);
         }
     }
 
     fn noundef_metadata(&mut self, load: &'ll Value) {
         unsafe {
             let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(load, llvm::MD_noundef as c_uint, md);
+            self.set_metadata(load, llvm::MD_noundef as c_uint, md);
         }
     }
 
     pub(crate) fn set_unpredictable(&mut self, inst: &'ll Value) {
         unsafe {
             let md = llvm::LLVMMDNodeInContext2(self.cx.llcx, ptr::null(), 0);
-            let md = llvm::LLVMMetadataAsValue(&self.llcx, md);
-            llvm::LLVMSetMetadata(inst, llvm::MD_unpredictable as c_uint, md);
+            self.set_metadata(inst, llvm::MD_unpredictable as c_uint, md);
         }
     }
 
diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs
index 469f6843f35..02fa1657f40 100644
--- a/compiler/rustc_codegen_llvm/src/context.rs
+++ b/compiler/rustc_codegen_llvm/src/context.rs
@@ -1,6 +1,6 @@
 use std::borrow::Borrow;
 use std::cell::{Cell, RefCell};
-use std::ffi::CStr;
+use std::ffi::{c_uint, CStr};
 use std::str;
 
 use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
@@ -30,6 +30,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;
 use crate::type_::Type;
 use crate::value::Value;
 use crate::{attributes, coverageinfo, debuginfo, llvm, llvm_util};
@@ -585,6 +586,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
             llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
         }
     }
+
+    /// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
+    pub(crate) fn set_metadata<'a>(&self, val: &'a Value, kind_id: c_uint, md: &'a Metadata) {
+        unsafe {
+            let node = llvm::LLVMMetadataAsValue(&self.llcx, md);
+            llvm::LLVMSetMetadata(val, kind_id, node);
+        }
+    }
 }
 
 impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {