Skip to content

Commit 4a7fb97

Browse files
committed
Auto merge of rust-lang#91569 - erikdesjardins:vt-align, r=nikic
Attach range metadata to alignment loads from vtables ...because alignment is always nonzero[0]. This helps eliminate redundant runtime alignment checks, when a DST is a field of a struct whose remaining fields have alignment 1. Fixes rust-lang#91438. --- [0]: The [reference](https://doc.rust-lang.org/reference/type-layout.html) says that alignment must be at least 1. And in practice, the alignment field for all vtables is generated here: https://github.com/rust-lang/rust/blob/772d51f887fa407216860bf8ecf3f1a32fb795b4/compiler/rustc_middle/src/ty/vtable.rs#L68-L90 and is nonzero because [`Align::bytes()`](https://github.com/rust-lang/rust/blob/772d51f887fa407216860bf8ecf3f1a32fb795b4/compiler/rustc_target/src/abi/mod.rs#L547-L549) is always nonzero.
2 parents f7fd79a + 94f0833 commit 4a7fb97

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

compiler/rustc_codegen_ssa/src/glue.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::common::IntPredicate;
66
use crate::meth;
77
use crate::traits::*;
88
use rustc_middle::ty::{self, Ty};
9+
use rustc_target::abi::WrappingRange;
910

1011
pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
1112
bx: &mut Bx,
@@ -21,14 +22,17 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2122
}
2223
match t.kind() {
2324
ty::Dynamic(..) => {
24-
// load size/align from vtable
25+
// Load size/align from vtable.
2526
let vtable = info.unwrap();
26-
(
27-
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE)
28-
.get_usize(bx, vtable),
29-
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
30-
.get_usize(bx, vtable),
31-
)
27+
let size = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE)
28+
.get_usize(bx, vtable);
29+
let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
30+
.get_usize(bx, vtable);
31+
32+
// Alignment is always nonzero.
33+
bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
34+
35+
(size, align)
3236
}
3337
ty::Slice(_) | ty::Str => {
3438
let unit = layout.field(bx, 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// This test checks that we annotate alignment loads from vtables with nonzero range metadata,
6+
// and that this allows LLVM to eliminate redundant `align >= 1` checks.
7+
8+
pub trait Trait {
9+
fn f(&self);
10+
}
11+
12+
pub struct WrapperWithAlign1<T: ?Sized> { x: u8, y: T }
13+
14+
pub struct WrapperWithAlign2<T: ?Sized> { x: u16, y: T }
15+
16+
pub struct Struct<W: ?Sized> {
17+
_field: i8,
18+
dst: W,
19+
}
20+
21+
// CHECK-LABEL: @eliminates_runtime_check_when_align_1
22+
#[no_mangle]
23+
pub fn eliminates_runtime_check_when_align_1(
24+
x: &Struct<WrapperWithAlign1<dyn Trait>>
25+
) -> &WrapperWithAlign1<dyn Trait> {
26+
// CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
27+
// CHECK-NOT: icmp
28+
// CHECK-NOT: select
29+
// CHECK: ret
30+
&x.dst
31+
}
32+
33+
// CHECK-LABEL: @does_not_eliminate_runtime_check_when_align_2
34+
#[no_mangle]
35+
pub fn does_not_eliminate_runtime_check_when_align_2(
36+
x: &Struct<WrapperWithAlign2<dyn Trait>>
37+
) -> &WrapperWithAlign2<dyn Trait> {
38+
// CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]]
39+
// CHECK: [[X1:%[0-9]+]] = icmp {{.+}} [[X0]]
40+
// CHECK: [[X2:%[0-9]+]] = select {{.+}} [[X1]]
41+
// CHECK: ret
42+
&x.dst
43+
}
44+
45+
// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}

0 commit comments

Comments
 (0)