Skip to content

Commit 65c1722

Browse files
add CSE optimization tests for iterating over slice
1 parent 0160933 commit 65c1722

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Various iterating method over slice correctly optimized using common subexpression elimination.
2+
//! Checks function has memory(argmem: read) attribute.
3+
//! Regression test for <https://github.com/rust-lang/rust/issues/119573>.
4+
//@ compile-flags: -O
5+
6+
#![crate_type = "lib"]
7+
// CHECK-LABEL: @has_zero_iter
8+
// CHECK-SAME: #[[ATTR:[0-9]+]]
9+
#[inline(never)]
10+
#[unsafe(no_mangle)]
11+
pub fn has_zero_iter(xs: &[u8]) -> bool {
12+
xs.iter().any(|&x| x == 0)
13+
}
14+
15+
// CHECK-LABEL: @has_zero_ptr
16+
// CHECK-SAME: #[[ATTR]]
17+
#[inline(never)]
18+
#[unsafe(no_mangle)]
19+
fn has_zero_ptr(xs: &[u8]) -> bool {
20+
let range = xs.as_ptr_range();
21+
let mut start = range.start;
22+
let end = range.end;
23+
while start < end {
24+
unsafe {
25+
if *start == 0 {
26+
return true;
27+
}
28+
start = start.add(1);
29+
}
30+
}
31+
false
32+
}
33+
// CHECK-LABEL: @has_zero_for
34+
// CHECK-SAME: #[[ATTR]]
35+
#[inline(never)]
36+
#[unsafe(no_mangle)]
37+
fn has_zero_for(xs: &[u8]) -> bool {
38+
for x in xs {
39+
if *x == 0 {
40+
return true;
41+
}
42+
}
43+
false
44+
}
45+
46+
// CHECK: attributes #[[ATTR]] = { {{.*}}memory(argmem: read){{.*}} }

0 commit comments

Comments
 (0)