summaryrefslogtreecommitdiffstats
path: root/tests/codegen/issue-96497-slice-size-nowrap.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:58 +0000
commita4b7ed7a42c716ab9f05e351f003d589124fd55d (patch)
treeb620cd3f223850b28716e474e80c58059dca5dd4 /tests/codegen/issue-96497-slice-size-nowrap.rs
parentAdding upstream version 1.67.1+dfsg1. (diff)
downloadrustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.tar.xz
rustc-a4b7ed7a42c716ab9f05e351f003d589124fd55d.zip
Adding upstream version 1.68.2+dfsg1.upstream/1.68.2+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/codegen/issue-96497-slice-size-nowrap.rs')
-rw-r--r--tests/codegen/issue-96497-slice-size-nowrap.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/codegen/issue-96497-slice-size-nowrap.rs b/tests/codegen/issue-96497-slice-size-nowrap.rs
new file mode 100644
index 000000000..0413ed6b2
--- /dev/null
+++ b/tests/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: 15.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;
+}