about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-10-06 13:32:57 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-03-12 05:50:11 +0000
commitd4b30aa96c12191f3ee35d48b4827c5a2c7a712b (patch)
treee0baa24a887994c3bcc427d0ba0eef147907f673 /compiler/rustc_codegen_llvm
parentdd1e27120d253017c85e40871b23e91faab56abd (diff)
downloadrust-d4b30aa96c12191f3ee35d48b4827c5a2c7a712b.tar.gz
rust-d4b30aa96c12191f3ee35d48b4827c5a2c7a712b.zip
Reduce some duplicate work that is being done around statics
Diffstat (limited to 'compiler/rustc_codegen_llvm')
-rw-r--r--compiler/rustc_codegen_llvm/src/consts.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs
index f8fca39ec3c..95f14780e57 100644
--- a/compiler/rustc_codegen_llvm/src/consts.rs
+++ b/compiler/rustc_codegen_llvm/src/consts.rs
@@ -225,9 +225,20 @@ impl<'ll> CodegenCx<'ll, '_> {
         }
     }
 
+    #[instrument(level = "debug", skip(self))]
     pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
         let instance = Instance::mono(self.tcx, def_id);
-        if let Some(&g) = self.instances.borrow().get(&instance) {
+        trace!(?instance);
+        let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
+        trace!(?ty);
+        let llty = self.layout_of(ty).llvm_type(self);
+        self.get_static_inner(def_id, llty)
+    }
+
+    #[instrument(level = "debug", skip(self, llty))]
+    pub(crate) fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
+        if let Some(&g) = self.instances.borrow().get(&Instance::mono(self.tcx, def_id)) {
+            trace!("used cached value");
             return g;
         }
 
@@ -239,12 +250,10 @@ impl<'ll> CodegenCx<'ll, '_> {
                  statics defined in the same CGU, but did not for `{def_id:?}`"
         );
 
-        let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
-        let sym = self.tcx.symbol_name(instance).name;
+        let sym = self.tcx.symbol_name(Instance::mono(self.tcx, def_id)).name;
         let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
 
-        debug!("get_static: sym={} instance={:?} fn_attrs={:?}", sym, instance, fn_attrs);
-        let llty = self.layout_of(ty).llvm_type(self);
+        debug!(?sym, ?fn_attrs);
 
         let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
             if let Some(g) = self.get_declared_value(sym) {
@@ -331,7 +340,7 @@ impl<'ll> CodegenCx<'ll, '_> {
             }
         }
 
-        self.instances.borrow_mut().insert(instance, g);
+        self.instances.borrow_mut().insert(Instance::mono(self.tcx, def_id), g);
         g
     }
 }
@@ -367,13 +376,14 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
             };
             let alloc = alloc.inner();
 
-            let g = self.get_static(def_id);
-
             let val_llty = self.val_ty(v);
 
             let instance = Instance::mono(self.tcx, def_id);
             let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
             let llty = self.layout_of(ty).llvm_type(self);
+
+            let g = self.get_static_inner(def_id, llty);
+
             let g = if val_llty == llty {
                 g
             } else {