about summary refs log tree commit diff
path: root/src/test/codegen
diff options
context:
space:
mode:
authorErik Desjardins <erikdesjardins@users.noreply.github.com>2021-12-05 15:55:50 -0500
committerErik Desjardins <erikdesjardins@users.noreply.github.com>2021-12-05 16:07:27 -0500
commit2ff5a3e38b59dc8dc443c7459d2825130ffed4bc (patch)
treec1dcbe6f4c6c35292571a25abfee182a5f3b4217 /src/test/codegen
parent772d51f887fa407216860bf8ecf3f1a32fb795b4 (diff)
downloadrust-2ff5a3e38b59dc8dc443c7459d2825130ffed4bc.tar.gz
rust-2ff5a3e38b59dc8dc443c7459d2825130ffed4bc.zip
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.
Diffstat (limited to 'src/test/codegen')
-rw-r--r--src/test/codegen/dst-vtable-align-nonzero.rs43
1 files changed, 43 insertions, 0 deletions
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<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: 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: [[X1:%[0-9]+]] = icmp {{.+}} [[X0]]
+    // CHECK: [[X2:%[0-9]+]] = select {{.+}} [[X1]]
+    // CHECK: ret
+    &x.dst
+}
+
+// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}