From 2ff5a3e38b59dc8dc443c7459d2825130ffed4bc Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Sun, 5 Dec 2021 15:55:50 -0500 Subject: [PATCH 1/2] 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. --- compiler/rustc_codegen_ssa/src/glue.rs | 18 ++++---- src/test/codegen/dst-vtable-align-nonzero.rs | 43 ++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/test/codegen/dst-vtable-align-nonzero.rs diff --git a/compiler/rustc_codegen_ssa/src/glue.rs b/compiler/rustc_codegen_ssa/src/glue.rs index cf217b52c86..2c4e6bbe9a5 100644 --- a/compiler/rustc_codegen_ssa/src/glue.rs +++ b/compiler/rustc_codegen_ssa/src/glue.rs @@ -6,6 +6,7 @@ use crate::common::IntPredicate; use crate::meth; use crate::traits::*; use rustc_middle::ty::{self, Ty}; +use rustc_target::abi::WrappingRange; pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( bx: &mut Bx, @@ -21,14 +22,17 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( } match t.kind() { ty::Dynamic(..) => { - // load size/align from vtable + // Load size/align from vtable. let vtable = info.unwrap(); - ( - meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE) - .get_usize(bx, vtable), - meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN) - .get_usize(bx, vtable), - ) + let size = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE) + .get_usize(bx, vtable); + let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN) + .get_usize(bx, vtable); + + // Alignment is always nonzero. + bx.range_metadata(align, WrappingRange { start: 1, end: !0 }); + + (size, align) } ty::Slice(_) | ty::Str => { let unit = layout.field(bx, 0); diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs new file mode 100644 index 00000000000..00f82885d65 --- /dev/null +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -0,0 +1,43 @@ +#![crate_type = "lib"] + +// This test checks that we annotate alignment loads from vtables with nonzero range metadata, +// and that this allows LLVM to eliminate redundant `align >= 1` checks. + +pub trait Trait { + fn f(&self); +} + +pub struct WrapperWithAlign1 { x: u8, y: T } + +pub struct WrapperWithAlign2 { x: u16, y: T } + +pub struct Struct { + _field: i8, + dst: W, +} + +// CHECK-LABEL: @eliminates_runtime_check_when_align_1 +#[no_mangle] +pub fn eliminates_runtime_check_when_align_1( + x: &Struct> +) -> &WrapperWithAlign1 { + // CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]] + // CHECK-NOT: icmp + // CHECK-NOT: select + // CHECK: ret + &x.dst +} + +// CHECK-LABEL: @does_not_eliminate_runtime_check_when_align_2 +#[no_mangle] +pub fn does_not_eliminate_runtime_check_when_align_2( + x: &Struct> +) -> &WrapperWithAlign2 { + // CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]] + // CHECK: [[X1:%[0-9]+]] = icmp {{.+}} [[X0]] + // CHECK: [[X2:%[0-9]+]] = select {{.+}} [[X1]] + // CHECK: ret + &x.dst +} + +// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0} From 94f08334e0839689ff1c959ffaefa4ca19bd26f5 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Wed, 8 Dec 2021 22:40:23 -0500 Subject: [PATCH 2/2] add // compile-flags: -O to test that depends on opts --- src/test/codegen/dst-vtable-align-nonzero.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs index 00f82885d65..021c7b19f89 100644 --- a/src/test/codegen/dst-vtable-align-nonzero.rs +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -1,3 +1,5 @@ +// compile-flags: -O + #![crate_type = "lib"] // This test checks that we annotate alignment loads from vtables with nonzero range metadata,