Apply suggestions from code review

Co-authored-by: Nikita Popov <github@npopov.com>
This commit is contained in:
Lzu Tao 2024-06-09 13:25:12 +00:00
parent 95740fbfed
commit f9edd864df
11 changed files with 37 additions and 55 deletions

View File

@ -4,27 +4,25 @@
#![crate_type = "lib"]
#[no_mangle]
type T = u8;
type T1 = (T, T, T, T, T, T, T, T);
type T2 = [T; 8];
#[no_mangle]
// CHECK-LABEL: foo1a
// CHECK: cmp
// CHECK-NEXT: set
// CHECK-NEXT: sete
// CHECK-NEXT: ret
#[no_mangle]
pub fn foo1a(a: T1, b: T1) -> bool {
a == b
}
#[no_mangle]
// CHECK-LABEL: foo1b
// CHECK: mov
// CHECK-NEXT: cmp
// CHECK-NEXT: set
// CHECK-NEXT: sete
// CHECK-NEXT: ret
#[no_mangle]
pub fn foo1b(a: &T1, b: &T1) -> bool {
a == b
}

View File

@ -1,11 +1,10 @@
//@ compile-flags: -O
//@ min-llvm-version: 17
#![crate_type = "lib"]
#[no_mangle]
// CHECK-LABEL: @foo
// CHECK: getelementptr inbounds
// CHECK-NEXT: {{.*}}:
// CHECK-NEXT: getelementptr inbounds
// CHECK-NEXT: load i64
// CHECK-NEXT: icmp eq i64
// CHECK-NEXT: br i1

View File

@ -1,27 +1,26 @@
//@ compile-flags: -O
// XXX: The x86-64 assembly get optimized correclty. But llvm-ir output is not until llvm 18?
//@ min-llvm-version: 18
#![crate_type = "lib"]
pub enum K{
pub enum K {
A(Box<[i32]>),
B(Box<[u8]>),
C(Box<[String]>),
D(Box<[u16]>),
}
#[no_mangle]
// CHECK-LABEL: @get_len
// CHECK: getelementptr inbounds
// CHECK-NEXT: {{.*}}:
// CHECK-NEXT: getelementptr inbounds
// CHECK-NEXT: load
// CHECK-NEXT: ret i64
// CHECK-NOT: switch
pub fn get_len(arg: &K)->usize{
#[no_mangle]
pub fn get_len(arg: &K) -> usize {
match arg {
K::A(ref lst)=>lst.len(),
K::B(ref lst)=>lst.len(),
K::C(ref lst)=>lst.len(),
K::D(ref lst)=>lst.len(),
K::A(ref lst) => lst.len(),
K::B(ref lst) => lst.len(),
K::C(ref lst) => lst.len(),
K::D(ref lst) => lst.len(),
}
}

View File

@ -8,9 +8,9 @@ use std::convert::TryInto;
const N: usize = 24;
#[no_mangle]
// CHECK-LABEL: @example
// CHECK-NOT: unwrap_failed
#[no_mangle]
pub fn example(a: Vec<u8>) -> u8 {
if a.len() != 32 {
return 0;

View File

@ -1,17 +1,12 @@
//@ compile-flags: -O
//@ min-llvm-version: 17
#![crate_type = "lib"]
// CHECK-LABEL: @write_u8_variant_a
// CHECK: getelementptr
// CHECK-NEXT: {{.*}}:
// CHECK-NEXT: getelementptr
// CHECK-NEXT: icmp ugt
#[no_mangle]
pub fn write_u8_variant_a(
bytes: &mut [u8],
buf: u8,
offset: usize,
) -> Option<&mut [u8]> {
pub fn write_u8_variant_a(bytes: &mut [u8], buf: u8, offset: usize) -> Option<&mut [u8]> {
let buf = buf.to_le_bytes();
bytes
.get_mut(offset..).and_then(|bytes| bytes.get_mut(..buf.len()))
bytes.get_mut(offset..).and_then(|bytes| bytes.get_mut(..buf.len()))
}

View File

@ -1,15 +1,14 @@
// in Rust 1.73, -O and opt-level=3 optimizes differently
//@ compile-flags: -C opt-level=3
//@ min-llvm-version: 17
#![crate_type = "lib"]
use std::cmp::max;
#[no_mangle]
// CHECK-LABEL: @foo
// CHECK-NOT: slice_start_index_len_fail
// CHECK-NOT: unreachable
pub fn foo(v: &mut Vec<u8>, size: usize)-> Option<&mut [u8]> {
#[no_mangle]
pub fn foo(v: &mut Vec<u8>, size: usize) -> Option<&mut [u8]> {
if v.len() > max(1, size) {
let start = v.len() - size;
Some(&mut v[start..])

View File

@ -8,12 +8,12 @@ fn foo<T>(a: &mut T, b: T) -> bool {
let b = Some(mem::replace(a, b));
let ret = b.is_some();
mem::forget(b);
return ret
return ret;
}
// CHECK-LABEL: @foo_u32
// CHECK: store i32
// CHECK-NEXT: ret i1
// CHECK-NEXT: ret i1 true
#[no_mangle]
pub fn foo_u32(a: &mut u32, b: u32) -> bool {
foo(a, b)
@ -21,9 +21,8 @@ pub fn foo_u32(a: &mut u32, b: u32) -> bool {
// CHECK-LABEL: @foo_box
// CHECK: store ptr
// CHECK-NEXT: ret i1
// CHECK-NEXT: ret i1 true
#[no_mangle]
pub fn foo_box(a: &mut Box<u32>, b: Box<u32>) -> bool {
foo(a, b)
}

View File

@ -11,7 +11,5 @@
// CHECK-NEXT: ret i1
#[no_mangle]
pub fn unwrap_combinators(a: Option<i32>, b: i32) -> bool {
a.map(|t| t >= b)
.unwrap_or(false)
a.map(|t| t >= b).unwrap_or(false)
}

View File

@ -5,11 +5,10 @@
const N: usize = 3;
pub type T = u8;
#[no_mangle]
// CHECK-LABEL: @split_mutiple
// CHECK-LABEL: @split_multiple
// CHECK-NOT: unreachable
pub fn split_mutiple(slice: &[T]) -> (&[T], &[T]) {
#[no_mangle]
pub fn split_multiple(slice: &[T]) -> (&[T], &[T]) {
let len = slice.len() / N;
slice.split_at(len * N)
}

View File

@ -1,12 +1,9 @@
//@ compile-flags: -O
//@ min-llvm-version: 17
#![crate_type = "lib"]
#[no_mangle]
// CHECK-LABEL: @foo
// CHECK: {{.*}}:
// CHECK: ret
// CHECK-NOT: unreachable
pub fn foo(arr: &mut [u32]) {
for i in 0..arr.len() {
@ -15,4 +12,3 @@ pub fn foo(arr: &mut [u32]) {
}
}
}

View File

@ -5,15 +5,15 @@
use std::ptr::NonNull;
// CHECK-LABEL: @slice_ptr_len_1
// CHECK: {{.*}}:
// CHECK-NEXT: {{.*}}:
// CHECK-NEXT: ret i64 %ptr.1
#[no_mangle]
pub fn slice_ptr_len_1(ptr: *const [u8]) -> usize {
let ptr = ptr.cast_mut();
if let Some(ptr) = NonNull::new(ptr) {
ptr.len()
} else {
// We know ptr is null, so we know ptr.wrapping_byte_add(1) is not null.
NonNull::new(ptr.wrapping_byte_add(1)).unwrap().len()
}
let ptr = ptr.cast_mut();
if let Some(ptr) = NonNull::new(ptr) {
ptr.len()
} else {
// We know ptr is null, so we know ptr.wrapping_byte_add(1) is not null.
NonNull::new(ptr.wrapping_byte_add(1)).unwrap().len()
}
}