llvm 16 finally reconizes some additional vec in-place conversions as noops

This commit is contained in:
The 8472 2023-04-03 15:29:46 +02:00
parent 932c173ca1
commit 7a70647f19

View File

@ -1,11 +1,13 @@
// ignore-debug: the debug assertions get in the way // ignore-debug: the debug assertions get in the way
// compile-flags: -O -Z merge-functions=disabled // compile-flags: -O -Z merge-functions=disabled
// min-llvm-version: 16
#![crate_type = "lib"] #![crate_type = "lib"]
// Ensure that trivial casts of vec elements are O(1) // Ensure that trivial casts of vec elements are O(1)
pub struct Wrapper<T>(T); pub struct Wrapper<T>(T);
// previously repr(C) caused the optimization to fail
#[repr(C)] #[repr(C)]
pub struct Foo { pub struct Foo {
a: u64, a: u64,
@ -14,9 +16,8 @@ pub struct Foo {
d: u64, d: u64,
} }
// Going from an aggregate struct to another type currently requires Copy to // implementing Copy exercises the TrustedRandomAccess specialization inside the in-place
// enable the TrustedRandomAccess specialization. Without it optimizations do not yet // specialization
// reliably recognize the loops as noop for repr(C) or non-Copy structs.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Bar { pub struct Bar {
a: u64, a: u64,
@ -25,6 +26,14 @@ pub struct Bar {
d: u64, d: u64,
} }
// this exercises the try-fold codepath
pub struct Baz {
a: u64,
b: u64,
c: u64,
d: u64,
}
// CHECK-LABEL: @vec_iterator_cast_primitive // CHECK-LABEL: @vec_iterator_cast_primitive
#[no_mangle] #[no_mangle]
pub fn vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8> { pub fn vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8> {
@ -52,18 +61,29 @@ pub fn vec_iterator_cast_unwrap(vec: Vec<Wrapper<u8>>) -> Vec<u8> {
// CHECK-LABEL: @vec_iterator_cast_aggregate // CHECK-LABEL: @vec_iterator_cast_aggregate
#[no_mangle] #[no_mangle]
pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> { pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> {
// FIXME These checks should be the same as other functions. // CHECK-NOT: loop
// CHECK-NOT: @__rust_alloc // CHECK-NOT: call
// CHECK-NOT: @__rust_alloc
vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect() vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
} }
// CHECK-LABEL: @vec_iterator_cast_deaggregate // CHECK-LABEL: @vec_iterator_cast_deaggregate_tra
#[no_mangle] #[no_mangle]
pub fn vec_iterator_cast_deaggregate(vec: Vec<Bar>) -> Vec<[u64; 4]> { pub fn vec_iterator_cast_deaggregate_tra(vec: Vec<Bar>) -> Vec<[u64; 4]> {
// FIXME These checks should be the same as other functions. // CHECK-NOT: loop
// CHECK-NOT: @__rust_alloc // CHECK-NOT: call
// CHECK-NOT: @__rust_alloc
// Safety: For the purpose of this test we assume that Bar layout matches [u64; 4].
// This currently is not guaranteed for repr(Rust) types, but it happens to work here and
// the UCG may add additional guarantees for homogenous types in the future that would make this
// correct.
vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
}
// CHECK-LABEL: @vec_iterator_cast_deaggregate_fold
#[no_mangle]
pub fn vec_iterator_cast_deaggregate_fold(vec: Vec<Baz>) -> Vec<[u64; 4]> {
// CHECK-NOT: loop
// CHECK-NOT: call
// Safety: For the purpose of this test we assume that Bar layout matches [u64; 4]. // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4].
// This currently is not guaranteed for repr(Rust) types, but it happens to work here and // This currently is not guaranteed for repr(Rust) types, but it happens to work here and