about summary refs log tree commit diff
path: root/src/test/codegen
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-06-15 12:02:03 +0900
committerGitHub <noreply@github.com>2022-06-15 12:02:03 +0900
commit2722c2aa33d947789904694fcde4bb556e278952 (patch)
treedbb096fb235cfcfd2dec11c22e48e0ee271ef318 /src/test/codegen
parentbb4805118a6274a83e21c78bb96e3cbd9e9dca36 (diff)
parent50f6a9ed87e47c7a8ff6aefcde01a33821e80e20 (diff)
downloadrust-2722c2aa33d947789904694fcde4bb556e278952.tar.gz
rust-2722c2aa33d947789904694fcde4bb556e278952.zip
Rollup merge of #98078 - erikdesjardins:uncheckedsize, r=petrochenkov
Use unchecked mul to compute slice sizes

This allows LLVM to realize that `slice.len() > 0` iff `slice.len() * size_of::<T>() > 0`, allowing a branch on the latter to be folded into the former when dropping vecs and boxed slices, in some cases.

Fixes (partially) #96497
Diffstat (limited to 'src/test/codegen')
-rw-r--r--src/test/codegen/issue-96497-slice-size-nowrap.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/test/codegen/issue-96497-slice-size-nowrap.rs b/src/test/codegen/issue-96497-slice-size-nowrap.rs
new file mode 100644
index 00000000000..a5dbef93460
--- /dev/null
+++ b/src/test/codegen/issue-96497-slice-size-nowrap.rs
@@ -0,0 +1,29 @@
+// This test case checks that LLVM is aware that computing the size of a slice cannot wrap.
+// The possibility of wrapping results in an additional branch when dropping boxed slices
+// in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218
+
+// compile-flags: -O
+// min-llvm-version: 14.0
+
+#![crate_type="lib"]
+
+// CHECK-LABEL: @simple_size_of_nowrap
+#[no_mangle]
+pub fn simple_size_of_nowrap(x: &[u32]) -> usize {
+    // Make sure the shift used to compute the size has a nowrap flag.
+
+    // CHECK: [[A:%.*]] = shl nsw {{.*}}, 2
+    // CHECK-NEXT: ret {{.*}} [[A]]
+    core::mem::size_of_val(x)
+}
+
+// CHECK-LABEL: @drop_write
+#[no_mangle]
+pub fn drop_write(mut x: Box<[u32]>) {
+    // Check that this write is optimized out.
+    // This depends on the size calculation not wrapping,
+    // since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0.
+
+    // CHECK-NOT: store i32 42
+    x[1] = 42;
+}