Skip to content

Commit f5a1633

Browse files
committed
Reduce duplication of vtables
1 parent 4cbba98 commit f5a1633

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/constant.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ pub(crate) fn codegen_const_value<'tcx>(
190190
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
191191
let base_addr = match alloc_kind {
192192
Some(GlobalAlloc::Memory(alloc)) => {
193-
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
194193
let data_id = data_id_for_alloc_id(
195194
&mut fx.constants_cx,
196195
fx.module,
@@ -249,12 +248,11 @@ pub(crate) fn codegen_const_value<'tcx>(
249248
}
250249
}
251250

252-
pub(crate) fn pointer_for_allocation<'tcx>(
251+
fn pointer_for_allocation<'tcx>(
253252
fx: &mut FunctionCx<'_, '_, 'tcx>,
254253
alloc: &'tcx Allocation,
255254
) -> crate::pointer::Pointer {
256255
let alloc_id = fx.tcx.create_memory_alloc(alloc);
257-
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
258256
let data_id =
259257
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);
260258

@@ -266,12 +264,13 @@ pub(crate) fn pointer_for_allocation<'tcx>(
266264
crate::pointer::Pointer::new(global_ptr)
267265
}
268266

269-
fn data_id_for_alloc_id(
267+
pub(crate) fn data_id_for_alloc_id(
270268
cx: &mut ConstantCx,
271269
module: &mut dyn Module,
272270
alloc_id: AllocId,
273271
mutability: rustc_hir::Mutability,
274272
) -> DataId {
273+
cx.todo.push(TodoItem::Alloc(alloc_id));
275274
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
276275
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
277276
})
@@ -352,7 +351,14 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
352351
GlobalAlloc::Memory(alloc) => alloc,
353352
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
354353
};
355-
let data_id = data_id_for_alloc_id(cx, module, alloc_id, alloc.mutability);
354+
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
355+
module
356+
.declare_anonymous_data(
357+
alloc.mutability == rustc_hir::Mutability::Mut,
358+
false,
359+
)
360+
.unwrap()
361+
});
356362
(data_id, alloc, None)
357363
}
358364
TodoItem::Static(def_id) => {
@@ -415,7 +421,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
415421
continue;
416422
}
417423
GlobalAlloc::Memory(target_alloc) => {
418-
cx.todo.push(TodoItem::Alloc(reloc));
419424
data_id_for_alloc_id(cx, module, reloc, target_alloc.mutability)
420425
}
421426
GlobalAlloc::Static(def_id) => {

src/vtable.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! See `rustc_codegen_ssa/src/meth.rs` for reference.
44
5-
use super::constant::pointer_for_allocation;
5+
use crate::constant::data_id_for_alloc_id;
66
use crate::prelude::*;
77

88
fn vtable_memflags() -> MemFlags {
@@ -68,9 +68,16 @@ pub(crate) fn get_vtable<'tcx>(
6868
ty: Ty<'tcx>,
6969
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
7070
) -> Value {
71-
let vtable_alloc_id = fx.tcx.vtable_allocation(ty, trait_ref);
72-
let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory();
73-
let vtable_ptr = pointer_for_allocation(fx, vtable_allocation);
74-
75-
vtable_ptr.get_addr(fx)
71+
let alloc_id = fx.tcx.vtable_allocation(ty, trait_ref);
72+
let data_id = data_id_for_alloc_id(
73+
&mut fx.constants_cx,
74+
&mut *fx.module,
75+
alloc_id,
76+
Mutability::Not,
77+
);
78+
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
79+
if fx.clif_comments.enabled() {
80+
fx.add_comment(local_data_id, format!("vtable: {:?}", alloc_id));
81+
}
82+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
7683
}

0 commit comments

Comments
 (0)