]> git.lizzy.rs Git - rust.git/blob - tests/codegen/uninit-consts.rs
Rollup merge of #107761 - oli-obk:miri_🪵, r=TaKO8Ki
[rust.git] / tests / codegen / uninit-consts.rs
1 // compile-flags: -C no-prepopulate-passes
2 // min-llvm-version: 14.0
3
4 // Check that we use undef (and not zero) for uninitialized bytes in constants.
5
6 #![crate_type = "lib"]
7
8 use std::mem::MaybeUninit;
9
10 pub struct PartiallyUninit {
11     x: u32,
12     y: MaybeUninit<[u8; 10]>
13 }
14
15 // CHECK: [[FULLY_UNINIT:@[0-9]+]] = private unnamed_addr constant <{ [10 x i8] }> undef
16
17 // CHECK: [[PARTIALLY_UNINIT:@[0-9]+]] = private unnamed_addr constant <{ [4 x i8], [12 x i8] }> <{ [4 x i8] c"{{\\EF\\BE\\AD\\DE|\\DE\\AD\\BE\\EF}}", [12 x i8] undef }>, align 4
18
19 // This shouldn't contain undef, since it contains more chunks
20 // than the default value of uninit_const_chunk_threshold.
21 // CHECK: [[UNINIT_PADDING_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [32768 x i8] }> <{ [32768 x i8] c"{{.+}}" }>, align 4
22
23 // CHECK: [[FULLY_UNINIT_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [16384 x i8] }> undef
24
25 // CHECK-LABEL: @fully_uninit
26 #[no_mangle]
27 pub const fn fully_uninit() -> MaybeUninit<[u8; 10]> {
28     const M: MaybeUninit<[u8; 10]> = MaybeUninit::uninit();
29     // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 1 %{{[0-9]+}}, {{i8\*|ptr}} align 1 {{.*}}[[FULLY_UNINIT]]{{.*}}, i{{(32|64)}} 10, i1 false)
30     M
31 }
32
33 // CHECK-LABEL: @partially_uninit
34 #[no_mangle]
35 pub const fn partially_uninit() -> PartiallyUninit {
36     const X: PartiallyUninit = PartiallyUninit { x: 0xdeadbeef, y: MaybeUninit::uninit() };
37     // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 4 %{{[0-9]+}}, {{i8\*|ptr}} align 4 {{.*}}[[PARTIALLY_UNINIT]]{{.*}}, i{{(32|64)}} 16, i1 false)
38     X
39 }
40
41 // CHECK-LABEL: @uninit_padding_huge
42 #[no_mangle]
43 pub const fn uninit_padding_huge() -> [(u32, u8); 4096] {
44     const X: [(u32, u8); 4096] = [(123, 45); 4096];
45     // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 4 %{{[0-9]+}}, {{i8\*|ptr}} align 4 {{.*}}[[UNINIT_PADDING_HUGE]]{{.*}}, i{{(32|64)}} 32768, i1 false)
46     X
47 }
48
49 // CHECK-LABEL: @fully_uninit_huge
50 #[no_mangle]
51 pub const fn fully_uninit_huge() -> MaybeUninit<[u32; 4096]> {
52     const F: MaybeUninit<[u32; 4096]> = MaybeUninit::uninit();
53     // CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 4 %{{[0-9]+}}, {{i8\*|ptr}} align 4 {{.*}}[[FULLY_UNINIT_HUGE]]{{.*}}, i{{(32|64)}} 16384, i1 false)
54     F
55 }