Skip to content

Commit b73f9fa

Browse files
authored
Rollup merge of rust-lang#73362 - erikdesjardins:bounds, r=nikomatsakis
Test that bounds checks are elided when slice len is checked up-front Closes rust-lang#69101
2 parents 6441486 + e0975b9 commit b73f9fa

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// no-system-llvm
2+
// compile-flags: -O
3+
#![crate_type = "lib"]
4+
5+
// Make sure no bounds checks are emitted in the loop when upfront slicing
6+
// ensures that the slices are big enough.
7+
// In particular, bounds checks were not always optimized out if the upfront
8+
// check was for a greater len than the loop requires.
9+
// (i.e. `already_sliced_no_bounds_check` was not always optimized even when
10+
// `already_sliced_no_bounds_check_exact` was)
11+
// CHECK-LABEL: @already_sliced_no_bounds_check
12+
#[no_mangle]
13+
pub fn already_sliced_no_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) {
14+
// CHECK: slice_index_len_fail
15+
// CHECK-NOT: panic_bounds_check
16+
let _ = (&a[..2048], &b[..2048], &mut c[..2048]);
17+
for i in 0..1024 {
18+
c[i] = a[i] ^ b[i];
19+
}
20+
}
21+
22+
// CHECK-LABEL: @already_sliced_no_bounds_check_exact
23+
#[no_mangle]
24+
pub fn already_sliced_no_bounds_check_exact(a: &[u8], b: &[u8], c: &mut [u8]) {
25+
// CHECK: slice_index_len_fail
26+
// CHECK-NOT: panic_bounds_check
27+
let _ = (&a[..1024], &b[..1024], &mut c[..1024]);
28+
for i in 0..1024 {
29+
c[i] = a[i] ^ b[i];
30+
}
31+
}
32+
33+
// Make sure we're checking for the right thing: there can be a panic if the slice is too small.
34+
// CHECK-LABEL: @already_sliced_bounds_check
35+
#[no_mangle]
36+
pub fn already_sliced_bounds_check(a: &[u8], b: &[u8], c: &mut [u8]) {
37+
// CHECK: slice_index_len_fail
38+
// CHECK: panic_bounds_check
39+
let _ = (&a[..1023], &b[..2048], &mut c[..2048]);
40+
for i in 0..1024 {
41+
c[i] = a[i] ^ b[i];
42+
}
43+
}

0 commit comments

Comments
 (0)