about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-01 20:25:23 +0200
committerGitHub <noreply@github.com>2025-04-01 20:25:23 +0200
commita04b0e3cd361859efec92fd00fbd0d579b670b6c (patch)
tree72c8ac9393af742a1035484c68bd4267757a91ac /tests/codegen
parent000d01811096a477bd16a7d9698de7fc3d77cc41 (diff)
parentdea947212795fb4fcdef2b2351008d08be3fb854 (diff)
downloadrust-a04b0e3cd361859efec92fd00fbd0d579b670b6c.tar.gz
rust-a04b0e3cd361859efec92fd00fbd0d579b670b6c.zip
Rollup merge of #139129 - reez12g:add-tests-for-slice-bounds-check-optimization, r=fee1-dead
Add tests for slice bounds check optimization

Closes https://github.com/rust-lang/rust/issues/134466
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/slice-last-elements-optimization.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/codegen/slice-last-elements-optimization.rs b/tests/codegen/slice-last-elements-optimization.rs
new file mode 100644
index 00000000000..b90f91d7b17
--- /dev/null
+++ b/tests/codegen/slice-last-elements-optimization.rs
@@ -0,0 +1,37 @@
+//@ compile-flags: -Copt-level=3
+//@ only-x86_64
+//@ min-llvm-version: 20
+#![crate_type = "lib"]
+
+// This test verifies that LLVM 20 properly optimizes the bounds check
+// when accessing the last few elements of a slice with proper conditions.
+// Previously, this would generate an unreachable branch to
+// slice_start_index_len_fail even when the bounds check was provably safe.
+
+// CHECK-LABEL: @last_four_initial(
+#[no_mangle]
+pub fn last_four_initial(s: &[u8]) -> &[u8] {
+    // Previously this would generate a branch to slice_start_index_len_fail
+    // that is unreachable. The LLVM 20 fix should eliminate this branch.
+    // CHECK-NOT: slice_start_index_len_fail
+    // CHECK-NOT: unreachable
+    let start = if s.len() <= 4 { 0 } else { s.len() - 4 };
+    &s[start..]
+}
+
+// CHECK-LABEL: @last_four_optimized(
+#[no_mangle]
+pub fn last_four_optimized(s: &[u8]) -> &[u8] {
+    // This version was already correctly optimized before the fix in LLVM 20.
+    // CHECK-NOT: slice_start_index_len_fail
+    // CHECK-NOT: unreachable
+    if s.len() <= 4 { &s[0..] } else { &s[s.len() - 4..] }
+}
+
+// Just to verify we're correctly checking for the right thing
+// CHECK-LABEL: @test_bounds_check_happens(
+#[no_mangle]
+pub fn test_bounds_check_happens(s: &[u8], i: usize) -> &[u8] {
+    // CHECK: slice_start_index_len_fail
+    &s[i..]
+}