• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ignore-debug: the debug assertions get in the way
2 // compile-flags: -O -Z merge-functions=disabled
3 // min-llvm-version: 16
4 #![crate_type = "lib"]
5 
6 // Ensure that trivial casts of vec elements are O(1)
7 
8 pub struct Wrapper<T>(T);
9 
10 // previously repr(C) caused the optimization to fail
11 #[repr(C)]
12 pub struct Foo {
13     a: u64,
14     b: u64,
15     c: u64,
16     d: u64,
17 }
18 
19 // implementing Copy exercises the TrustedRandomAccess specialization inside the in-place
20 // specialization
21 #[derive(Copy, Clone)]
22 pub struct Bar {
23     a: u64,
24     b: u64,
25     c: u64,
26     d: u64,
27 }
28 
29 // this exercises the try-fold codepath
30 pub struct Baz {
31     a: u64,
32     b: u64,
33     c: u64,
34     d: u64,
35 }
36 
37 // CHECK-LABEL: @vec_iterator_cast_primitive
38 #[no_mangle]
vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8>39 pub fn vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8> {
40     // CHECK-NOT: loop
41     // CHECK-NOT: call
42     vec.into_iter().map(|e| e as u8).collect()
43 }
44 
45 // CHECK-LABEL: @vec_iterator_cast_wrapper
46 #[no_mangle]
vec_iterator_cast_wrapper(vec: Vec<u8>) -> Vec<Wrapper<u8>>47 pub fn vec_iterator_cast_wrapper(vec: Vec<u8>) -> Vec<Wrapper<u8>> {
48     // CHECK-NOT: loop
49     // CHECK-NOT: call
50     vec.into_iter().map(|e| Wrapper(e)).collect()
51 }
52 
53 // CHECK-LABEL: @vec_iterator_cast_unwrap
54 #[no_mangle]
vec_iterator_cast_unwrap(vec: Vec<Wrapper<u8>>) -> Vec<u8>55 pub fn vec_iterator_cast_unwrap(vec: Vec<Wrapper<u8>>) -> Vec<u8> {
56     // CHECK-NOT: loop
57     // CHECK-NOT: call
58     vec.into_iter().map(|e| e.0).collect()
59 }
60 
61 // CHECK-LABEL: @vec_iterator_cast_aggregate
62 #[no_mangle]
vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo>63 pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> {
64     // CHECK-NOT: loop
65     // CHECK-NOT: call
66     vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
67 }
68 
69 // CHECK-LABEL: @vec_iterator_cast_deaggregate_tra
70 #[no_mangle]
vec_iterator_cast_deaggregate_tra(vec: Vec<Bar>) -> Vec<[u64; 4]>71 pub fn vec_iterator_cast_deaggregate_tra(vec: Vec<Bar>) -> Vec<[u64; 4]> {
72     // CHECK-NOT: loop
73     // CHECK-NOT: call
74 
75     // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4].
76     // This currently is not guaranteed for repr(Rust) types, but it happens to work here and
77     // the UCG may add additional guarantees for homogenous types in the future that would make this
78     // correct.
79     vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
80 }
81 
82 // CHECK-LABEL: @vec_iterator_cast_deaggregate_fold
83 #[no_mangle]
vec_iterator_cast_deaggregate_fold(vec: Vec<Baz>) -> Vec<[u64; 4]>84 pub fn vec_iterator_cast_deaggregate_fold(vec: Vec<Baz>) -> Vec<[u64; 4]> {
85     // CHECK-NOT: loop
86     // CHECK-NOT: call
87 
88     // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4].
89     // This currently is not guaranteed for repr(Rust) types, but it happens to work here and
90     // the UCG may add additional guarantees for homogenous types in the future that would make this
91     // correct.
92     vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
93 }
94