Skip to content

Commit 2ff5a3e

Browse files
Attach range metadata to alignment loads from vtables
...because alignment is always nonzero. This helps eliminate redundant runtime alignment checks, when a DST is a field of a struct whose remaining fields have alignment 1.
1 parent 772d51f commit 2ff5a3e

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

compiler/rustc_codegen_ssa/src/glue.rs

Lines changed: 11 additions & 7 deletions
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);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![crate_type = "lib"]
2+
3+
// This test checks that we annotate alignment loads from vtables with nonzero range metadata,
4+
// and that this allows LLVM to eliminate redundant `align >= 1` checks.
5+
6+
pub trait Trait {
7+
fn f(&self);
8+
}
9+
10+
pub struct WrapperWithAlign1<T: ?Sized> { x: u8, y: T }
11+
12+
pub struct WrapperWithAlign2<T: ?Sized> { x: u16, y: T }
13+
14+
pub struct Struct<W: ?Sized> {
15+
_field: i8,
16+
dst: W,
17+
}
18+
19+
// CHECK-LABEL: @eliminates_runtime_check_when_align_1
20+
#[no_mangle]
21+
pub fn eliminates_runtime_check_when_align_1(
22+
x: &Struct<WrapperWithAlign1<dyn Trait>>
23+
) -> &WrapperWithAlign1<dyn Trait> {
24+
// CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
25+
// CHECK-NOT: icmp
26+
// CHECK-NOT: select
27+
// CHECK: ret
28+
&x.dst
29+
}
30+
31+
// CHECK-LABEL: @does_not_eliminate_runtime_check_when_align_2
32+
#[no_mangle]
33+
pub fn does_not_eliminate_runtime_check_when_align_2(
34+
x: &Struct<WrapperWithAlign2<dyn Trait>>
35+
) -> &WrapperWithAlign2<dyn Trait> {
36+
// CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]]
37+
// CHECK: [[X1:%[0-9]+]] = icmp {{.+}} [[X0]]
38+
// CHECK: [[X2:%[0-9]+]] = select {{.+}} [[X1]]
39+
// CHECK: ret
40+
&x.dst
41+
}
42+
43+
// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}

0 commit comments

Comments
 (0)