From a99e97af97f5f86e1892ec2bf633105384ed8bee Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Thu, 8 Dec 2022 01:30:07 -0500 Subject: Add 0..=isize::MAX range metadata to size loads from vtables --- src/test/codegen/dst-vtable-align-nonzero.rs | 18 ++++++++++++++ src/test/codegen/dst-vtable-size-range.rs | 37 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/test/codegen/dst-vtable-size-range.rs (limited to 'src/test/codegen') diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs index 14c4c3f30f9..7ebb4173d56 100644 --- a/src/test/codegen/dst-vtable-align-nonzero.rs +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -1,6 +1,7 @@ // compile-flags: -O #![crate_type = "lib"] +#![feature(core_intrinsics)] // 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. @@ -42,4 +43,21 @@ pub fn does_not_eliminate_runtime_check_when_align_2( &x.dst } +// CHECK-LABEL: @align_load_from_align_of_val +#[no_mangle] +pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize { + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + core::mem::align_of_val(x) +} + +// CHECK-LABEL: @align_load_from_vtable_align_intrinsic +#[no_mangle] +pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize { + let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + let align = core::intrinsics::vtable_align(vtable); + // make this function unique so it doesn't get merged with the previous + align + 1 +} + // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0} diff --git a/src/test/codegen/dst-vtable-size-range.rs b/src/test/codegen/dst-vtable-size-range.rs new file mode 100644 index 00000000000..cec5876b348 --- /dev/null +++ b/src/test/codegen/dst-vtable-size-range.rs @@ -0,0 +1,37 @@ +// compile-flags: -O + +#![crate_type = "lib"] +#![feature(core_intrinsics)] + +// Check that we annotate size loads from vtables with 0..(isize::MAX + 1) range metadata. + +pub trait Trait { + fn f(&self); +} + +// Note that rustc uses inclusive bounds, but LLVM uses exclusive bounds for range metadata. +// CHECK-LABEL: @generate_exclusive_bound +#[no_mangle] +pub fn generate_exclusive_bound() -> usize { + // CHECK: ret [[USIZE:i[0-9]+]] [[EXCLUSIVE_BOUND:[-0-9]+]] + isize::MAX as usize + 1 +} + +// CHECK-LABEL: @size_load_from_size_of_val +#[no_mangle] +pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize { + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META:![0-9]+]] + core::mem::size_of_val(x) +} + +// CHECK-LABEL: @size_load_from_vtable_size_intrinsic +#[no_mangle] +pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize { + let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + let size = core::intrinsics::vtable_size(vtable); + // make this function unique so it doesn't get merged with the previous + size + 1 +} + +// CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]} -- cgit 1.4.1-3-g733a5 From e01d944c6c33a76cdbd1a257743d8c41a8203e89 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Thu, 8 Dec 2022 19:33:14 -0500 Subject: disable mergefunc instead of making fns unique --- src/test/codegen/dst-vtable-align-nonzero.rs | 6 ++---- src/test/codegen/dst-vtable-size-range.rs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) (limited to 'src/test/codegen') diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs index 7ebb4173d56..54f6e7f992f 100644 --- a/src/test/codegen/dst-vtable-align-nonzero.rs +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -1,4 +1,4 @@ -// compile-flags: -O +// compile-flags: -O -Z merge-functions=disabled #![crate_type = "lib"] #![feature(core_intrinsics)] @@ -55,9 +55,7 @@ pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize { pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize { let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] - let align = core::intrinsics::vtable_align(vtable); - // make this function unique so it doesn't get merged with the previous - align + 1 + core::intrinsics::vtable_align(vtable) } // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0} diff --git a/src/test/codegen/dst-vtable-size-range.rs b/src/test/codegen/dst-vtable-size-range.rs index cec5876b348..671c8abdebd 100644 --- a/src/test/codegen/dst-vtable-size-range.rs +++ b/src/test/codegen/dst-vtable-size-range.rs @@ -1,4 +1,4 @@ -// compile-flags: -O +// compile-flags: -O -Z merge-functions=disabled #![crate_type = "lib"] #![feature(core_intrinsics)] @@ -29,9 +29,7 @@ pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize { pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize { let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] - let size = core::intrinsics::vtable_size(vtable); - // make this function unique so it doesn't get merged with the previous - size + 1 + core::intrinsics::vtable_size(vtable) } // CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]} -- cgit 1.4.1-3-g733a5