diff options
| author | Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> | 2023-01-05 09:13:28 +0100 |
|---|---|---|
| committer | Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> | 2023-01-11 09:32:08 +0000 |
| commit | cf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch) | |
| tree | 40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/codegen/vec-in-place.rs | |
| parent | ca855e6e42787ecd062d81d53336fe6788ef51a9 (diff) | |
| download | rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip | |
Move /src/test to /tests
Diffstat (limited to 'tests/codegen/vec-in-place.rs')
| -rw-r--r-- | tests/codegen/vec-in-place.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/codegen/vec-in-place.rs b/tests/codegen/vec-in-place.rs new file mode 100644 index 00000000000..5df3669056d --- /dev/null +++ b/tests/codegen/vec-in-place.rs @@ -0,0 +1,74 @@ +// min-llvm-version: 14.0 +// ignore-debug: the debug assertions get in the way +// compile-flags: -O -Z merge-functions=disabled +#![crate_type = "lib"] + +// Ensure that trivial casts of vec elements are O(1) + +pub struct Wrapper<T>(T); + +#[repr(C)] +pub struct Foo { + a: u64, + b: u64, + c: u64, + d: u64, +} + +// Going from an aggregate struct to another type currently requires Copy to +// enable the TrustedRandomAccess specialization. Without it optimizations do not yet +// reliably recognize the loops as noop for repr(C) or non-Copy structs. +#[derive(Copy, Clone)] +pub struct Bar { + a: u64, + b: u64, + c: u64, + d: u64, +} + +// CHECK-LABEL: @vec_iterator_cast_primitive +#[no_mangle] +pub fn vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8> { + // CHECK-NOT: loop + // CHECK-NOT: call + vec.into_iter().map(|e| e as u8).collect() +} + +// CHECK-LABEL: @vec_iterator_cast_wrapper +#[no_mangle] +pub fn vec_iterator_cast_wrapper(vec: Vec<u8>) -> Vec<Wrapper<u8>> { + // CHECK-NOT: loop + // CHECK-NOT: call + vec.into_iter().map(|e| Wrapper(e)).collect() +} + +// CHECK-LABEL: @vec_iterator_cast_unwrap +#[no_mangle] +pub fn vec_iterator_cast_unwrap(vec: Vec<Wrapper<u8>>) -> Vec<u8> { + // CHECK-NOT: loop + // CHECK-NOT: call + vec.into_iter().map(|e| e.0).collect() +} + +// CHECK-LABEL: @vec_iterator_cast_aggregate +#[no_mangle] +pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> { + // FIXME These checks should be the same as other functions. + // CHECK-NOT: @__rust_alloc + // CHECK-NOT: @__rust_alloc + vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect() +} + +// CHECK-LABEL: @vec_iterator_cast_deaggregate +#[no_mangle] +pub fn vec_iterator_cast_deaggregate(vec: Vec<Bar>) -> Vec<[u64; 4]> { + // FIXME These checks should be the same as other functions. + // CHECK-NOT: @__rust_alloc + // 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() +} |
