diff options
| author | bors <bors@rust-lang.org> | 2022-04-18 02:04:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-04-18 02:04:12 +0000 |
| commit | 7b5408d3fbfc4bcb7c5b2b2f08369156406dc03b (patch) | |
| tree | 9520d15c4dce390280301e377565efaa6cba4a8a /src/test/codegen | |
| parent | e27d9df4319bd822e64f620676543d31e9c7ae2c (diff) | |
| parent | 693589619b2689747ba3bb9b9ba3986d7a970ecf (diff) | |
| download | rust-7b5408d3fbfc4bcb7c5b2b2f08369156406dc03b.tar.gz rust-7b5408d3fbfc4bcb7c5b2b2f08369156406dc03b.zip | |
Auto merge of #95695 - the8472:vec-codegen-tests, r=Mark-Simulacrum
Add codegen tests for additional cases where noop iterators get optimized away Optimizations have improved over time and now LLVM manages to optimize more in-place-collect noop-iterators to O(1) functions. This updates the codegen test to match. Many but not all cases reported in #79308 work now.
Diffstat (limited to 'src/test/codegen')
| -rw-r--r-- | src/test/codegen/vec-in-place.rs | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/src/test/codegen/vec-in-place.rs b/src/test/codegen/vec-in-place.rs index a656c9e6f65..13c41f7d4a9 100644 --- a/src/test/codegen/vec-in-place.rs +++ b/src/test/codegen/vec-in-place.rs @@ -1,13 +1,72 @@ +// min-llvm-version: 14.0 // ignore-debug: the debug assertions get in the way -// compile-flags: -O +// compile-flags: -O -Z merge-functions=disabled #![crate_type = "lib"] // Ensure that trivial casts of vec elements are O(1) -// CHECK-LABEL: @vec_iterator_cast +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 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(vec: Vec<isize>) -> Vec<usize> { +pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> { // CHECK-NOT: loop // CHECK-NOT: call - vec.into_iter().map(|e| e as usize).collect() + 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]> { + // CHECK-NOT: loop + // CHECK-NOT: call + + // 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() } |
