Skip to content

Commit 2b094b1

Browse files
committed
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: https://github.com/rust-lang/rust/blob/b7cc99142ad0cfe47e2fe9f7a82eaf5b672c0573/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](https://github.com/rust-lang/rust/blob/9db224fc908059986c179fc6ec433944e9cfce50/compiler/rustc_abi/src/lib.rs#L312-L331), but I don't think we can assume that in an optimization.)
2 parents d0dc9ef + e01d944 commit 2b094b1

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

compiler/rustc_abi/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,18 @@ impl Integer {
774774
}
775775
}
776776

777+
/// Returns the largest signed value that can be represented by this Integer.
778+
#[inline]
779+
pub fn signed_max(self) -> i128 {
780+
match self {
781+
I8 => i8::MAX as i128,
782+
I16 => i16::MAX as i128,
783+
I32 => i32::MAX as i128,
784+
I64 => i64::MAX as i128,
785+
I128 => i128::MAX,
786+
}
787+
}
788+
777789
/// Finds the smallest Integer type which can represent the signed value.
778790
#[inline]
779791
pub fn fit_signed(x: i128) -> Integer {

compiler/rustc_codegen_ssa/src/glue.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2929
let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
3030
.get_usize(bx, vtable);
3131

32+
// Size is always <= isize::MAX.
33+
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
34+
bx.range_metadata(size, WrappingRange { start: 0, end: size_bound });
3235
// Alignment is always nonzero.
3336
bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
3437

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
110110
_ => bug!(),
111111
};
112112
let value = meth::VirtualIndex::from_index(idx).get_usize(bx, vtable);
113-
if name == sym::vtable_align {
113+
match name {
114+
// Size is always <= isize::MAX.
115+
sym::vtable_size => {
116+
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
117+
bx.range_metadata(value, WrappingRange { start: 0, end: size_bound });
118+
},
114119
// Alignment is always nonzero.
115-
bx.range_metadata(value, WrappingRange { start: 1, end: !0 });
116-
};
120+
sym::vtable_align => bx.range_metadata(value, WrappingRange { start: 1, end: !0 }),
121+
_ => {}
122+
}
117123
value
118124
}
119125
sym::pref_align_of

src/test/codegen/dst-vtable-align-nonzero.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// compile-flags: -O
1+
// compile-flags: -O -Z merge-functions=disabled
22

33
#![crate_type = "lib"]
4+
#![feature(core_intrinsics)]
45

56
// This test checks that we annotate alignment loads from vtables with nonzero range metadata,
67
// and that this allows LLVM to eliminate redundant `align >= 1` checks.
@@ -42,4 +43,19 @@ pub fn does_not_eliminate_runtime_check_when_align_2(
4243
&x.dst
4344
}
4445

46+
// CHECK-LABEL: @align_load_from_align_of_val
47+
#[no_mangle]
48+
pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize {
49+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
50+
core::mem::align_of_val(x)
51+
}
52+
53+
// CHECK-LABEL: @align_load_from_vtable_align_intrinsic
54+
#[no_mangle]
55+
pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize {
56+
let (data, vtable): (*const (), *const ()) = core::mem::transmute(x);
57+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
58+
core::intrinsics::vtable_align(vtable)
59+
}
60+
4561
// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// compile-flags: -O -Z merge-functions=disabled
2+
3+
#![crate_type = "lib"]
4+
#![feature(core_intrinsics)]
5+
6+
// Check that we annotate size loads from vtables with 0..(isize::MAX + 1) range metadata.
7+
8+
pub trait Trait {
9+
fn f(&self);
10+
}
11+
12+
// Note that rustc uses inclusive bounds, but LLVM uses exclusive bounds for range metadata.
13+
// CHECK-LABEL: @generate_exclusive_bound
14+
#[no_mangle]
15+
pub fn generate_exclusive_bound() -> usize {
16+
// CHECK: ret [[USIZE:i[0-9]+]] [[EXCLUSIVE_BOUND:[-0-9]+]]
17+
isize::MAX as usize + 1
18+
}
19+
20+
// CHECK-LABEL: @size_load_from_size_of_val
21+
#[no_mangle]
22+
pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize {
23+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META:![0-9]+]]
24+
core::mem::size_of_val(x)
25+
}
26+
27+
// CHECK-LABEL: @size_load_from_vtable_size_intrinsic
28+
#[no_mangle]
29+
pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize {
30+
let (data, vtable): (*const (), *const ()) = core::mem::transmute(x);
31+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
32+
core::intrinsics::vtable_size(vtable)
33+
}
34+
35+
// CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]}

0 commit comments

Comments
 (0)