Skip to content

Commit

Permalink
Reduce duplication of vtables
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Jul 1, 2021
1 parent 4cbba98 commit f5a1633
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ pub(crate) fn codegen_const_value<'tcx>(
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
let base_addr = match alloc_kind {
Some(GlobalAlloc::Memory(alloc)) => {
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
let data_id = data_id_for_alloc_id(
&mut fx.constants_cx,
fx.module,
Expand Down Expand Up @@ -249,12 +248,11 @@ pub(crate) fn codegen_const_value<'tcx>(
}
}

pub(crate) fn pointer_for_allocation<'tcx>(
fn pointer_for_allocation<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
alloc: &'tcx Allocation,
) -> crate::pointer::Pointer {
let alloc_id = fx.tcx.create_memory_alloc(alloc);
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
let data_id =
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);

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

fn data_id_for_alloc_id(
pub(crate) fn data_id_for_alloc_id(
cx: &mut ConstantCx,
module: &mut dyn Module,
alloc_id: AllocId,
mutability: rustc_hir::Mutability,
) -> DataId {
cx.todo.push(TodoItem::Alloc(alloc_id));
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
})
Expand Down Expand Up @@ -352,7 +351,14 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
GlobalAlloc::Memory(alloc) => alloc,
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
};
let data_id = data_id_for_alloc_id(cx, module, alloc_id, alloc.mutability);
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module
.declare_anonymous_data(
alloc.mutability == rustc_hir::Mutability::Mut,
false,
)
.unwrap()
});
(data_id, alloc, None)
}
TodoItem::Static(def_id) => {
Expand Down Expand Up @@ -415,7 +421,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
continue;
}
GlobalAlloc::Memory(target_alloc) => {
cx.todo.push(TodoItem::Alloc(reloc));
data_id_for_alloc_id(cx, module, reloc, target_alloc.mutability)
}
GlobalAlloc::Static(def_id) => {
Expand Down
19 changes: 13 additions & 6 deletions src/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! See `rustc_codegen_ssa/src/meth.rs` for reference.
use super::constant::pointer_for_allocation;
use crate::constant::data_id_for_alloc_id;
use crate::prelude::*;

fn vtable_memflags() -> MemFlags {
Expand Down Expand Up @@ -68,9 +68,16 @@ pub(crate) fn get_vtable<'tcx>(
ty: Ty<'tcx>,
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> Value {
let vtable_alloc_id = fx.tcx.vtable_allocation(ty, trait_ref);
let vtable_allocation = fx.tcx.global_alloc(vtable_alloc_id).unwrap_memory();
let vtable_ptr = pointer_for_allocation(fx, vtable_allocation);

vtable_ptr.get_addr(fx)
let alloc_id = fx.tcx.vtable_allocation(ty, trait_ref);
let data_id = data_id_for_alloc_id(
&mut fx.constants_cx,
&mut *fx.module,
alloc_id,
Mutability::Not,
);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("vtable: {:?}", alloc_id));
}
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
}

0 comments on commit f5a1633

Please sign in to comment.