about summary refs log tree commit diff
path: root/tests/codegen-llvm/dst-vtable-align-nonzero.rs
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-07-21 14:34:12 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-07-22 14:28:48 +0200
commita27f3e3fd1e4d16160f8885b6b06665b5319f56c (patch)
treeb033935392cbadf6f85d2dbddf433a88e323aeeb /tests/codegen-llvm/dst-vtable-align-nonzero.rs
parented93c1783b404d728d4809973a0550eb33cd293f (diff)
downloadrust-a27f3e3fd1e4d16160f8885b6b06665b5319f56c.tar.gz
rust-a27f3e3fd1e4d16160f8885b6b06665b5319f56c.zip
Rename `tests/codegen` into `tests/codegen-llvm`
Diffstat (limited to 'tests/codegen-llvm/dst-vtable-align-nonzero.rs')
-rw-r--r--tests/codegen-llvm/dst-vtable-align-nonzero.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/codegen-llvm/dst-vtable-align-nonzero.rs b/tests/codegen-llvm/dst-vtable-align-nonzero.rs
new file mode 100644
index 00000000000..1404bd64f50
--- /dev/null
+++ b/tests/codegen-llvm/dst-vtable-align-nonzero.rs
@@ -0,0 +1,67 @@
+//@ compile-flags: -Copt-level=3 -Z merge-functions=disabled
+
+#![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.
+
+pub trait Trait {
+    fn f(&self);
+}
+
+pub struct WrapperWithAlign1<T: ?Sized> {
+    x: u8,
+    y: T,
+}
+
+pub struct WrapperWithAlign2<T: ?Sized> {
+    x: u16,
+    y: T,
+}
+
+pub struct Struct<W: ?Sized> {
+    _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<dyn Trait>>,
+) -> &WrapperWithAlign1<dyn Trait> {
+    // CHECK: load [[USIZE:i[0-9]+]], {{.+}} !range [[RANGE_META:![0-9]+]]
+    // CHECK-NOT: llvm.umax
+    // 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<dyn Trait>>,
+) -> &WrapperWithAlign2<dyn Trait> {
+    // CHECK: [[X0:%[0-9]+]] = load [[USIZE]], {{.+}} !range [[RANGE_META]]
+    // CHECK: {{icmp|llvm.umax}}
+    // CHECK: ret
+    &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]]
+    core::intrinsics::vtable_align(vtable)
+}
+
+// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}