Skip to content

Commit 4936869

Browse files
committed
Auto merge of rust-lang#125347 - tesuji:needtests, r=nikic
Add codegen tests for E-needs-test close rust-lang#36010 close rust-lang#68667 close rust-lang#74938 close rust-lang#83585 close rust-lang#93036 close rust-lang#109328 close rust-lang#110797 close rust-lang#111508 close rust-lang#112509 close rust-lang#113757 close rust-lang#120440 close rust-lang#118392 close rust-lang#71096 r? nikic
2 parents f158600 + ab0f87e commit 4936869

12 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ assembly-output: emit-asm
2+
//@ compile-flags: -Copt-level=3
3+
//@ only-x86_64
4+
5+
#![crate_type = "lib"]
6+
7+
type T = u8;
8+
type T1 = (T, T, T, T, T, T, T, T);
9+
10+
// CHECK-LABEL: foo1a
11+
// CHECK: cmpq
12+
// CHECK-NEXT: sete
13+
// CHECK-NEXT: {{retq|popq}}
14+
#[no_mangle]
15+
pub fn foo1a(a: T1, b: T1) -> bool {
16+
a == b
17+
}
18+
19+
// CHECK-LABEL: foo1b
20+
// CHECK: movq
21+
// CHECK: cmpq
22+
// CHECK-NEXT: sete
23+
// CHECK-NEXT: {{retq|popq}}
24+
#[no_mangle]
25+
pub fn foo1b(a: &T1, b: &T1) -> bool {
26+
a == b
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @foo
6+
// CHECK-NEXT: {{.*}}:
7+
// CHECK-NEXT: getelementptr inbounds
8+
// CHECK-NEXT: load [[TYPE:i(32|64)]]
9+
// CHECK-NEXT: icmp eq [[TYPE]]
10+
// CHECK-NEXT: br i1
11+
#[no_mangle]
12+
pub fn foo(input: &mut &[u64]) -> Option<u64> {
13+
let (first, rest) = input.split_first()?;
14+
*input = rest;
15+
Some(*first)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ compile-flags: -O
2+
//@ min-llvm-version: 18
3+
4+
#![crate_type = "lib"]
5+
6+
pub enum K {
7+
A(Box<[i32]>),
8+
B(Box<[u8]>),
9+
C(Box<[String]>),
10+
D(Box<[u16]>),
11+
}
12+
13+
// CHECK-LABEL: @get_len
14+
// CHECK-NEXT: {{.*}}:
15+
// CHECK-NEXT: getelementptr inbounds
16+
// CHECK-NEXT: load [[TYPE:i(32|64)]]
17+
// CHECK-NEXT: ret [[TYPE]]
18+
#[no_mangle]
19+
pub fn get_len(arg: &K) -> usize {
20+
match arg {
21+
K::A(ref lst) => lst.len(),
22+
K::B(ref lst) => lst.len(),
23+
K::C(ref lst) => lst.len(),
24+
K::D(ref lst) => lst.len(),
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ compile-flags: -O
2+
// This regress since Rust version 1.72.
3+
//@ min-llvm-version: 18.1.4
4+
5+
#![crate_type = "lib"]
6+
7+
use std::convert::TryInto;
8+
9+
const N: usize = 24;
10+
11+
// CHECK-LABEL: @example
12+
// CHECK-NOT: unwrap_failed
13+
#[no_mangle]
14+
pub fn example(a: Vec<u8>) -> u8 {
15+
if a.len() != 32 {
16+
return 0;
17+
}
18+
19+
let a: [u8; 32] = a.try_into().unwrap();
20+
21+
a[15] + a[N]
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: -O
2+
#![crate_type = "lib"]
3+
4+
// CHECK-LABEL: @write_u8_variant_a
5+
// CHECK-NEXT: {{.*}}:
6+
// CHECK-NEXT: getelementptr
7+
// CHECK-NEXT: icmp ugt
8+
#[no_mangle]
9+
pub fn write_u8_variant_a(bytes: &mut [u8], buf: u8, offset: usize) -> Option<&mut [u8]> {
10+
let buf = buf.to_le_bytes();
11+
bytes.get_mut(offset..).and_then(|bytes| bytes.get_mut(..buf.len()))
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// in Rust 1.73, -O and opt-level=3 optimizes differently
2+
//@ compile-flags: -C opt-level=3
3+
#![crate_type = "lib"]
4+
5+
use std::cmp::max;
6+
7+
// CHECK-LABEL: @foo
8+
// CHECK-NOT: slice_start_index_len_fail
9+
// CHECK-NOT: unreachable
10+
#[no_mangle]
11+
pub fn foo(v: &mut Vec<u8>, size: usize) -> Option<&mut [u8]> {
12+
if v.len() > max(1, size) {
13+
let start = v.len() - size;
14+
Some(&mut v[start..])
15+
} else {
16+
None
17+
}
18+
}

tests/codegen/issues/issue-118392.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: -O
2+
//@ min-llvm-version: 18
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @div2
6+
// CHECK: ashr i32 %a, 1
7+
// CHECK-NEXT: ret i32
8+
#[no_mangle]
9+
pub fn div2(a: i32) -> i32 {
10+
a.div_euclid(2)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![crate_type = "lib"]
2+
3+
//@ compile-flags: -O
4+
5+
use std::mem;
6+
7+
fn foo<T>(a: &mut T, b: T) -> bool {
8+
let b = Some(mem::replace(a, b));
9+
let ret = b.is_some();
10+
mem::forget(b);
11+
return ret;
12+
}
13+
14+
// CHECK-LABEL: @foo_u32
15+
// CHECK: store i32
16+
// CHECK-NEXT: ret i1 true
17+
#[no_mangle]
18+
pub fn foo_u32(a: &mut u32, b: u32) -> bool {
19+
foo(a, b)
20+
}
21+
22+
// CHECK-LABEL: @foo_box
23+
// CHECK: store ptr
24+
// CHECK-NEXT: ret i1 true
25+
#[no_mangle]
26+
pub fn foo_box(a: &mut Box<u32>, b: Box<u32>) -> bool {
27+
foo(a, b)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![crate_type = "lib"]
2+
3+
//@ compile-flags: -O
4+
5+
// MIR inlining now optimizes this code.
6+
7+
// CHECK-LABEL: @unwrap_combinators
8+
// CHECK: icmp
9+
// CHECK-NEXT: icmp
10+
// CHECK-NEXT: select i1
11+
// CHECK-NEXT: ret i1
12+
#[no_mangle]
13+
pub fn unwrap_combinators(a: Option<i32>, b: i32) -> bool {
14+
a.map(|t| t >= b).unwrap_or(false)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
const N: usize = 3;
6+
pub type T = u8;
7+
8+
// CHECK-LABEL: @split_multiple
9+
// CHECK-NOT: unreachable
10+
#[no_mangle]
11+
pub fn split_multiple(slice: &[T]) -> (&[T], &[T]) {
12+
let len = slice.len() / N;
13+
slice.split_at(len * N)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
#[no_mangle]
6+
// CHECK-LABEL: @foo
7+
// CHECK-NOT: unreachable
8+
pub fn foo(arr: &mut [u32]) {
9+
for i in 0..arr.len() {
10+
for j in 0..i {
11+
assert!(j < arr.len());
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ compile-flags: -O
2+
//@ min-llvm-version: 18
3+
#![crate_type = "lib"]
4+
5+
use std::ptr::NonNull;
6+
7+
// CHECK-LABEL: @slice_ptr_len_1
8+
// CHECK-NEXT: {{.*}}:
9+
// CHECK-NEXT: ret {{i(32|64)}} %ptr.1
10+
#[no_mangle]
11+
pub fn slice_ptr_len_1(ptr: *const [u8]) -> usize {
12+
let ptr = ptr.cast_mut();
13+
if let Some(ptr) = NonNull::new(ptr) {
14+
ptr.len()
15+
} else {
16+
// We know ptr is null, so we know ptr.wrapping_byte_add(1) is not null.
17+
NonNull::new(ptr.wrapping_byte_add(1)).unwrap().len()
18+
}
19+
}

0 commit comments

Comments
 (0)