]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/packed.rs
Auto merge of #100626 - Dylan-DPC:rollup-mwbm7kj, r=Dylan-DPC
[rust.git] / src / test / codegen / packed.rs
1 //
2 // compile-flags: -O -C no-prepopulate-passes
3
4 #![crate_type = "lib"]
5
6 #[repr(packed)]
7 pub struct Packed1 {
8     dealign: u8,
9     data: u32
10 }
11
12 #[repr(packed(2))]
13 pub struct Packed2 {
14     dealign: u8,
15     data: u32
16 }
17
18 // CHECK-LABEL: @write_pkd1
19 #[no_mangle]
20 pub fn write_pkd1(pkd: &mut Packed1) -> u32 {
21 // CHECK: %{{.*}} = load i32, {{i32\*|ptr}} %{{.*}}, align 1
22 // CHECK: store i32 42, {{i32\*|ptr}} %{{.*}}, align 1
23     let result = pkd.data;
24     pkd.data = 42;
25     result
26 }
27
28 // CHECK-LABEL: @write_pkd2
29 #[no_mangle]
30 pub fn write_pkd2(pkd: &mut Packed2) -> u32 {
31 // CHECK: %{{.*}} = load i32, {{i32\*|ptr}} %{{.*}}, align 2
32 // CHECK: store i32 42, {{i32\*|ptr}} %{{.*}}, align 2
33     let result = pkd.data;
34     pkd.data = 42;
35     result
36 }
37
38 pub struct Array([i32; 8]);
39 #[repr(packed)]
40 pub struct BigPacked1 {
41     dealign: u8,
42     data: Array
43 }
44
45 #[repr(packed(2))]
46 pub struct BigPacked2 {
47     dealign: u8,
48     data: Array
49 }
50
51 // CHECK-LABEL: @call_pkd1
52 #[no_mangle]
53 pub fn call_pkd1(f: fn() -> Array) -> BigPacked1 {
54 // CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
55 // CHECK: call void %{{.*}}({{%Array\*|ptr}} noalias nocapture noundef sret{{.*}} dereferenceable(32) [[ALLOCA]])
56 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 4 %{{.*}}, i{{[0-9]+}} 32, i1 false)
57     // check that calls whose destination is a field of a packed struct
58     // go through an alloca rather than calling the function with an
59     // unaligned destination.
60     BigPacked1 { dealign: 0, data: f() }
61 }
62
63 // CHECK-LABEL: @call_pkd2
64 #[no_mangle]
65 pub fn call_pkd2(f: fn() -> Array) -> BigPacked2 {
66 // CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
67 // CHECK: call void %{{.*}}({{%Array\*|ptr}} noalias nocapture noundef sret{{.*}} dereferenceable(32) [[ALLOCA]])
68 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 2 %{{.*}}, {{i8\*|ptr}} align 4 %{{.*}}, i{{[0-9]+}} 32, i1 false)
69     // check that calls whose destination is a field of a packed struct
70     // go through an alloca rather than calling the function with an
71     // unaligned destination.
72     BigPacked2 { dealign: 0, data: f() }
73 }
74
75 // CHECK-LABEL: @write_packed_array1
76 // CHECK: store i32 0, {{i32\*|ptr}} %{{.+}}, align 1
77 // CHECK: store i32 1, {{i32\*|ptr}} %{{.+}}, align 1
78 // CHECK: store i32 2, {{i32\*|ptr}} %{{.+}}, align 1
79 #[no_mangle]
80 pub fn write_packed_array1(p: &mut BigPacked1) {
81     p.data.0[0] = 0;
82     p.data.0[1] = 1;
83     p.data.0[2] = 2;
84 }
85
86 // CHECK-LABEL: @write_packed_array2
87 // CHECK: store i32 0, {{i32\*|ptr}} %{{.+}}, align 2
88 // CHECK: store i32 1, {{i32\*|ptr}} %{{.+}}, align 2
89 // CHECK: store i32 2, {{i32\*|ptr}} %{{.+}}, align 2
90 #[no_mangle]
91 pub fn write_packed_array2(p: &mut BigPacked2) {
92     p.data.0[0] = 0;
93     p.data.0[1] = 1;
94     p.data.0[2] = 2;
95 }
96
97 // CHECK-LABEL: @repeat_packed_array1
98 // CHECK: store i32 42, {{i32\*|ptr}} %{{.+}}, align 1
99 #[no_mangle]
100 pub fn repeat_packed_array1(p: &mut BigPacked1) {
101     p.data.0 = [42; 8];
102 }
103
104 // CHECK-LABEL: @repeat_packed_array2
105 // CHECK: store i32 42, {{i32\*|ptr}} %{{.+}}, align 2
106 #[no_mangle]
107 pub fn repeat_packed_array2(p: &mut BigPacked2) {
108     p.data.0 = [42; 8];
109 }
110
111 #[repr(packed)]
112 #[derive(Copy, Clone)]
113 pub struct Packed1Pair(u8, u32);
114
115 #[repr(packed(2))]
116 #[derive(Copy, Clone)]
117 pub struct Packed2Pair(u8, u32);
118
119 // CHECK-LABEL: @pkd1_pair
120 #[no_mangle]
121 pub fn pkd1_pair(pair1: &mut Packed1Pair, pair2: &mut Packed1Pair) {
122 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 1 %{{.*}}, i{{[0-9]+}} 5, i1 false)
123     *pair2 = *pair1;
124 }
125
126 // CHECK-LABEL: @pkd2_pair
127 #[no_mangle]
128 pub fn pkd2_pair(pair1: &mut Packed2Pair, pair2: &mut Packed2Pair) {
129 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 2 %{{.*}}, {{i8\*|ptr}} align 2 %{{.*}}, i{{[0-9]+}} 6, i1 false)
130     *pair2 = *pair1;
131 }
132
133 #[repr(packed)]
134 #[derive(Copy, Clone)]
135 pub struct Packed1NestedPair((u32, u32));
136
137 #[repr(packed(2))]
138 #[derive(Copy, Clone)]
139 pub struct Packed2NestedPair((u32, u32));
140
141 // CHECK-LABEL: @pkd1_nested_pair
142 #[no_mangle]
143 pub fn pkd1_nested_pair(pair1: &mut Packed1NestedPair, pair2: &mut Packed1NestedPair) {
144 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 1 %{{.*}}, {{i8\*|ptr}} align 1 %{{.*}}, i{{[0-9]+}} 8, i1 false)
145     *pair2 = *pair1;
146 }
147
148 // CHECK-LABEL: @pkd2_nested_pair
149 #[no_mangle]
150 pub fn pkd2_nested_pair(pair1: &mut Packed2NestedPair, pair2: &mut Packed2NestedPair) {
151 // CHECK: call void @llvm.memcpy.{{.*}}({{i8\*|ptr}} align 2 %{{.*}}, {{i8\*|ptr}} align 2 %{{.*}}, i{{[0-9]+}} 8, i1 false)
152     *pair2 = *pair1;
153 }