Skip to content

Commit dea9472

Browse files
committed
Add tests for LLVM 20 slice bounds check optimization
1 parent 85f518e commit dea9472

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Diff for: tests/codegen/slice-last-elements-optimization.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ compile-flags: -Copt-level=3
2+
//@ only-x86_64
3+
//@ min-llvm-version: 20
4+
#![crate_type = "lib"]
5+
6+
// This test verifies that LLVM 20 properly optimizes the bounds check
7+
// when accessing the last few elements of a slice with proper conditions.
8+
// Previously, this would generate an unreachable branch to
9+
// slice_start_index_len_fail even when the bounds check was provably safe.
10+
11+
// CHECK-LABEL: @last_four_initial(
12+
#[no_mangle]
13+
pub fn last_four_initial(s: &[u8]) -> &[u8] {
14+
// Previously this would generate a branch to slice_start_index_len_fail
15+
// that is unreachable. The LLVM 20 fix should eliminate this branch.
16+
// CHECK-NOT: slice_start_index_len_fail
17+
// CHECK-NOT: unreachable
18+
let start = if s.len() <= 4 { 0 } else { s.len() - 4 };
19+
&s[start..]
20+
}
21+
22+
// CHECK-LABEL: @last_four_optimized(
23+
#[no_mangle]
24+
pub fn last_four_optimized(s: &[u8]) -> &[u8] {
25+
// This version was already correctly optimized before the fix in LLVM 20.
26+
// CHECK-NOT: slice_start_index_len_fail
27+
// CHECK-NOT: unreachable
28+
if s.len() <= 4 { &s[0..] } else { &s[s.len() - 4..] }
29+
}
30+
31+
// Just to verify we're correctly checking for the right thing
32+
// CHECK-LABEL: @test_bounds_check_happens(
33+
#[no_mangle]
34+
pub fn test_bounds_check_happens(s: &[u8], i: usize) -> &[u8] {
35+
// CHECK: slice_start_index_len_fail
36+
&s[i..]
37+
}

0 commit comments

Comments
 (0)