Skip to content

Commit 566b47c

Browse files
committed
Reduce some duplicate work that is being done around statics
1 parent 205cfa1 commit 566b47c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,20 @@ impl<'ll> CodegenCx<'ll, '_> {
225225
}
226226
}
227227

228+
#[instrument(level = "debug", skip(self))]
228229
pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
229230
let instance = Instance::mono(self.tcx, def_id);
231+
trace!(?instance);
232+
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
233+
trace!(?ty);
234+
let llty = self.layout_of(ty).llvm_type(self);
235+
self.get_static_inner(def_id, llty)
236+
}
237+
238+
#[instrument(level = "debug", skip(self, llty))]
239+
pub(crate) fn get_static_inner(&self, def_id: DefId, llty: &'ll Type) -> &'ll Value {
230240
if let Some(&g) = self.statics.borrow().get(&def_id) {
241+
trace!("used cached value");
231242
return g;
232243
}
233244

@@ -239,12 +250,10 @@ impl<'ll> CodegenCx<'ll, '_> {
239250
statics defined in the same CGU, but did not for `{def_id:?}`"
240251
);
241252

242-
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
243-
let sym = self.tcx.symbol_name(instance).name;
253+
let sym = self.tcx.symbol_name(Instance::mono(self.tcx, def_id)).name;
244254
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
245255

246-
debug!("get_static: sym={} instance={:?} fn_attrs={:?}", sym, instance, fn_attrs);
247-
let llty = self.layout_of(ty).llvm_type(self);
256+
debug!(?sym, ?fn_attrs);
248257

249258
let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
250259
if let Some(g) = self.get_declared_value(sym) {
@@ -367,13 +376,14 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
367376
};
368377
let alloc = alloc.inner();
369378

370-
let g = self.get_static(def_id);
371-
372379
let val_llty = self.val_ty(v);
373380

374381
let instance = Instance::mono(self.tcx, def_id);
375382
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
376383
let llty = self.layout_of(ty).llvm_type(self);
384+
385+
let g = self.get_static_inner(def_id, llty);
386+
377387
let g = if val_llty == llty {
378388
g
379389
} else {

0 commit comments

Comments
 (0)