Skip to content

Commit b7e5597

Browse files
Use undef for partially-uninit constants up to 1024 bytes
There needs to be some limit to avoid perf regressions on large arrays with undef in each element (see comment in the code).
1 parent b8c56fa commit b7e5597

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) ->
5353
}
5454
};
5555

56-
// Generating partially-uninit consts inhibits optimizations, so it is disabled by default.
57-
// See https://github.com/rust-lang/rust/issues/84565.
56+
// Generating partially-uninit consts is limited to small allocations,
57+
// to avoid the cost of generating large complex const expressions.
58+
// For example, `[(u32, u8); 1024 * 1024]` contains uninit padding in each element,
59+
// and would result in `{ [5 x i8] zeroinitializer, [3 x i8] undef, ...repeat 1M times... }`.
5860
let allow_partially_uninit =
5961
match cx.sess().opts.debugging_opts.partially_uninit_const_threshold {
6062
Some(max) => range.len() <= max,

compiler/rustc_session/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,9 @@ options! {
13411341
"panic strategy for panics in drops"),
13421342
parse_only: bool = (false, parse_bool, [UNTRACKED],
13431343
"parse only; do not compile, assemble, or link (default: no)"),
1344-
partially_uninit_const_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
1344+
partially_uninit_const_threshold: Option<usize> = (Some(1024), parse_opt_number, [TRACKED],
13451345
"allow generating const initializers with mixed init/uninit bytes, \
1346-
and set the maximum total size of a const allocation for which this is allowed (default: never)"),
1346+
and set the maximum total size of a const allocation for which this is allowed (default: 1024 bytes)"),
13471347
perf_stats: bool = (false, parse_bool, [UNTRACKED],
13481348
"print some performance-related statistics (default: no)"),
13491349
pick_stable_methods_before_any_unstable: bool = (true, parse_bool, [TRACKED],

src/test/codegen/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ pub fn inline_enum_const() -> E<i8, i16> {
4343
#[no_mangle]
4444
pub fn low_align_const() -> E<i16, [i16; 3]> {
4545
// Check that low_align_const and high_align_const use the same constant
46-
// CHECK: memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 2 %1, i8* align 2 getelementptr inbounds (<{ [8 x i8] }>, <{ [8 x i8] }>* [[LOW_HIGH]], i32 0, i32 0, i32 0), i{{(32|64)}} 8, i1 false)
46+
// CHECK: memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 2 %1, i8* align 2 getelementptr inbounds (<{ [4 x i8], [4 x i8] }>, <{ [4 x i8], [4 x i8] }>* [[LOW_HIGH]], i32 0, i32 0, i32 0), i{{(32|64)}} 8, i1 false)
4747
*&E::A(0)
4848
}
4949

5050
// CHECK-LABEL: @high_align_const
5151
#[no_mangle]
5252
pub fn high_align_const() -> E<i16, i32> {
5353
// Check that low_align_const and high_align_const use the same constant
54-
// CHECK: memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 4 %1, i8* align 4 getelementptr inbounds (<{ [8 x i8] }>, <{ [8 x i8] }>* [[LOW_HIGH]], i32 0, i32 0, i32 0), i{{(32|64)}} 8, i1 false)
54+
// CHECK: memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 4 %1, i8* align 4 getelementptr inbounds (<{ [4 x i8], [4 x i8] }>, <{ [4 x i8], [4 x i8] }>* [[LOW_HIGH]], i32 0, i32 0, i32 0), i{{(32|64)}} 8, i1 false)
5555
*&E::A(0)
5656
}

src/test/codegen/uninit-consts-allow-partially-uninit.rs

-35
This file was deleted.

src/test/codegen/uninit-consts.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ pub struct PartiallyUninit {
1212
}
1313

1414
// CHECK: [[FULLY_UNINIT:@[0-9]+]] = private unnamed_addr constant <{ [10 x i8] }> undef
15-
// CHECK: [[PARTIALLY_UNINIT:@[0-9]+]] = private unnamed_addr constant <{ [16 x i8] }> <{ [16 x i8] c"\EF\BE\AD\DE\00\00\00\00\00\00\00\00\00\00\00\00" }>, align 4
15+
16+
// CHECK: [[PARTIALLY_UNINIT:@[0-9]+]] = private unnamed_addr constant <{ [4 x i8], [12 x i8] }> <{ [4 x i8] c"\EF\BE\AD\DE", [12 x i8] undef }>, align 4
17+
18+
// This shouldn't contain undef, since it's larger than the 1024 byte limit.
19+
// CHECK: [[UNINIT_PADDING_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [32768 x i8] }> <{ [32768 x i8] c"{{.+}}" }>, align 4
20+
1621
// CHECK: [[FULLY_UNINIT_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [16384 x i8] }> undef
1722

1823
// CHECK-LABEL: @fully_uninit
@@ -27,7 +32,15 @@ pub const fn fully_uninit() -> MaybeUninit<[u8; 10]> {
2732
#[no_mangle]
2833
pub const fn partially_uninit() -> PartiallyUninit {
2934
const X: PartiallyUninit = PartiallyUninit { x: 0xdeadbeef, y: MaybeUninit::uninit() };
30-
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 4 %1, i8* align 4 getelementptr inbounds (<{ [16 x i8] }>, <{ [16 x i8] }>* [[PARTIALLY_UNINIT]], i32 0, i32 0, i32 0), i{{(32|64)}} 16, i1 false)
35+
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 4 %1, i8* align 4 getelementptr inbounds (<{ [4 x i8], [12 x i8] }>, <{ [4 x i8], [12 x i8] }>* [[PARTIALLY_UNINIT]], i32 0, i32 0, i32 0), i{{(32|64)}} 16, i1 false)
36+
X
37+
}
38+
39+
// CHECK-LABEL: @uninit_padding_huge
40+
#[no_mangle]
41+
pub const fn uninit_padding_huge() -> [(u32, u8); 4096] {
42+
const X: [(u32, u8); 4096] = [(123, 45); 4096];
43+
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i{{(32|64)}}(i8* align 4 %1, i8* align 4 getelementptr inbounds (<{ [32768 x i8] }>, <{ [32768 x i8] }>* [[UNINIT_PADDING_HUGE]], i32 0, i32 0, i32 0), i{{(32|64)}} 32768, i1 false)
3144
X
3245
}
3346

0 commit comments

Comments
 (0)