Skip to content

Commit 7d3238f

Browse files
authored
Rollup merge of #73362 - erikdesjardins:bounds, r=nikomatsakis
Test that bounds checks are elided when slice len is checked up-front Closes #69101
2 parents 8d79ebd + 6351850 commit 7d3238f

File tree

1 file changed

+44
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)