Skip to content

Commit 9ee49bb

Browse files
committed
Rollup merge of rust-lang#55016 - oli-obk:vtables💥_vtables_everywhere, r=RalfJung
Deduplicate some code and compile-time values around vtables r? @RalfJung
2 parents 43ac030 + b1d3111 commit 9ee49bb

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

‎src/librustc/ty/query/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,16 @@ define_queries! { <'tcx>
369369
-> Lrc<specialization_graph::Graph>,
370370
[] fn is_object_safe: ObjectSafety(DefId) -> bool,
371371

372-
// Get the ParameterEnvironment for a given item; this environment
373-
// will be in "user-facing" mode, meaning that it is suitabe for
374-
// type-checking etc, and it does not normalize specializable
375-
// associated types. This is almost always what you want,
376-
// unless you are doing MIR optimizations, in which case you
377-
// might want to use `reveal_all()` method to change modes.
372+
/// Get the ParameterEnvironment for a given item; this environment
373+
/// will be in "user-facing" mode, meaning that it is suitabe for
374+
/// type-checking etc, and it does not normalize specializable
375+
/// associated types. This is almost always what you want,
376+
/// unless you are doing MIR optimizations, in which case you
377+
/// might want to use `reveal_all()` method to change modes.
378378
[] fn param_env: ParamEnv(DefId) -> ty::ParamEnv<'tcx>,
379379

380-
// Trait selection queries. These are best used by invoking `ty.moves_by_default()`,
381-
// `ty.is_copy()`, etc, since that will prune the environment where possible.
380+
/// Trait selection queries. These are best used by invoking `ty.moves_by_default()`,
381+
/// `ty.is_copy()`, etc, since that will prune the environment where possible.
382382
[] fn is_copy_raw: is_copy_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
383383
[] fn is_sized_raw: is_sized_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
384384
[] fn is_freeze_raw: is_freeze_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,

‎src/librustc_codegen_llvm/meth.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ pub fn get_vtable(
9494
});
9595

9696
let (size, align) = cx.size_and_align_of(ty);
97+
// /////////////////////////////////////////////////////////////////////////////////////////////
98+
// If you touch this code, be sure to also make the corresponding changes to
99+
// `get_vtable` in rust_mir/interpret/traits.rs
100+
// /////////////////////////////////////////////////////////////////////////////////////////////
97101
let components: Vec<_> = [
98102
callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
99103
C_usize(cx, size.bytes()),

‎src/librustc_mir/interpret/cast.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
327327
}
328328
(_, &ty::Dynamic(ref data, _)) => {
329329
// Initial cast from sized to dyn trait
330-
let trait_ref = data.principal().with_self_ty(
331-
*self.tcx,
332-
src_pointee_ty,
333-
);
334-
let trait_ref = self.tcx.erase_regions(&trait_ref);
335-
let vtable = self.get_vtable(src_pointee_ty, trait_ref)?;
330+
let vtable = self.get_vtable(src_pointee_ty, data.principal())?;
336331
let ptr = self.read_value(src)?.to_scalar_ptr()?;
337332
let val = Value::new_dyn_trait(ptr, vtable);
338333
self.write_value(val, dest)

‎src/librustc_mir/interpret/eval_context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc::mir::interpret::{
2727
EvalResult, EvalErrorKind,
2828
truncate, sign_extend,
2929
};
30+
use rustc_data_structures::fx::FxHashMap;
3031

3132
use syntax::source_map::{self, Span};
3233

@@ -50,6 +51,9 @@ pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
5051

5152
/// The virtual call stack.
5253
pub(crate) stack: Vec<Frame<'mir, 'tcx, M::PointerTag>>,
54+
55+
/// A cache for deduplicating vtables
56+
pub(super) vtables: FxHashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), AllocId>,
5357
}
5458

5559
/// A stack frame.
@@ -209,6 +213,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
209213
param_env,
210214
memory: Memory::new(tcx, memory_data),
211215
stack: Vec::new(),
216+
vtables: FxHashMap::default(),
212217
}
213218
}
214219

‎src/librustc_mir/interpret/traits.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,32 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
2424
pub fn get_vtable(
2525
&mut self,
2626
ty: Ty<'tcx>,
27-
trait_ref: ty::PolyTraitRef<'tcx>,
27+
poly_trait_ref: ty::PolyExistentialTraitRef<'tcx>,
2828
) -> EvalResult<'tcx, Pointer<M::PointerTag>> {
29-
debug!("get_vtable(trait_ref={:?})", trait_ref);
29+
debug!("get_vtable(trait_ref={:?})", poly_trait_ref);
3030

31-
// FIXME: Cache this!
31+
let (ty, poly_trait_ref) = self.tcx.erase_regions(&(ty, poly_trait_ref));
3232

33-
let layout = self.layout_of(trait_ref.self_ty())?;
33+
if let Some(&vtable) = self.vtables.get(&(ty, poly_trait_ref)) {
34+
return Ok(Pointer::from(vtable).with_default_tag());
35+
}
36+
37+
let trait_ref = poly_trait_ref.with_self_ty(*self.tcx, ty);
38+
let trait_ref = self.tcx.erase_regions(&trait_ref);
39+
40+
let methods = self.tcx.vtable_methods(trait_ref);
41+
42+
let layout = self.layout_of(ty)?;
3443
assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
3544
let size = layout.size.bytes();
3645
let align = layout.align.abi();
3746

3847
let ptr_size = self.pointer_size();
3948
let ptr_align = self.tcx.data_layout.pointer_align;
40-
let methods = self.tcx.vtable_methods(trait_ref);
49+
// /////////////////////////////////////////////////////////////////////////////////////////
50+
// If you touch this code, be sure to also make the corresponding changes to
51+
// `get_vtable` in rust_codegen_llvm/meth.rs
52+
// /////////////////////////////////////////////////////////////////////////////////////////
4153
let vtable = self.memory.allocate(
4254
ptr_size * (3 + methods.len() as u64),
4355
ptr_align,
@@ -64,6 +76,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6476
}
6577

6678
self.memory.mark_immutable(vtable.alloc_id)?;
79+
assert!(self.vtables.insert((ty, poly_trait_ref), vtable.alloc_id).is_none());
6780

6881
Ok(vtable)
6982
}

0 commit comments

Comments
 (0)