1
Fork 0

Auto merge of #105446 - erikdesjardins:vt-size, r=nikic

Add 0..=isize::MAX range metadata to size loads from vtables

This is the (much belated) size counterpart to #91569.

Inspired by https://rust-lang.zulipchat.com/#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/Range.20metadata.20for.20.60size_of_val.60.20and.20other.20isize.3A.3AMAX.20limits. This could help optimize layout computations based on the size of a dyn trait. Though, admittedly, adding this to vtables wouldn't be as beneficial as adding it to slice len, which is used much more often.

Miri detects this UB already: b7cc99142a/compiler/rustc_const_eval/src/interpret/traits.rs (L119-L121)
(In fact Miri goes further, [assuming a 48-bit address space on 64-bit platforms](9db224fc90/compiler/rustc_abi/src/lib.rs (L312-L331)), but I don't think we can assume that in an optimization.)
This commit is contained in:
bors 2022-12-18 22:01:39 +00:00
commit 2b094b1ede
5 changed files with 76 additions and 4 deletions

View file

@ -29,6 +29,9 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
.get_usize(bx, vtable);
// Size is always <= isize::MAX.
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
bx.range_metadata(size, WrappingRange { start: 0, end: size_bound });
// Alignment is always nonzero.
bx.range_metadata(align, WrappingRange { start: 1, end: !0 });

View file

@ -110,10 +110,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
_ => bug!(),
};
let value = meth::VirtualIndex::from_index(idx).get_usize(bx, vtable);
if name == sym::vtable_align {
match name {
// Size is always <= isize::MAX.
sym::vtable_size => {
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
bx.range_metadata(value, WrappingRange { start: 0, end: size_bound });
},
// Alignment is always nonzero.
bx.range_metadata(value, WrappingRange { start: 1, end: !0 });
};
sym::vtable_align => bx.range_metadata(value, WrappingRange { start: 1, end: !0 }),
_ => {}
}
value
}
sym::pref_align_of