Skip to content

Commit d5769e9

Browse files
switch to limiting the number of init/uninit chunks
1 parent b7e5597 commit d5769e9

File tree

5 files changed

+16
-26
lines changed

5 files changed

+16
-26
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) ->
3737
alloc: &'a Allocation,
3838
range: Range<usize>,
3939
) {
40-
let mut chunks = alloc
40+
let chunks = alloc
4141
.init_mask()
4242
.range_as_init_chunks(Size::from_bytes(range.start), Size::from_bytes(range.end));
4343

@@ -53,32 +53,20 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) ->
5353
}
5454
};
5555

56-
// Generating partially-uninit consts is limited to small allocations,
56+
// Generating partially-uninit consts is limited to small numbers of chunks,
5757
// to avoid the cost of generating large complex const expressions.
5858
// For example, `[(u32, u8); 1024 * 1024]` contains uninit padding in each element,
5959
// and would result in `{ [5 x i8] zeroinitializer, [3 x i8] undef, ...repeat 1M times... }`.
60-
let allow_partially_uninit =
61-
match cx.sess().opts.debugging_opts.partially_uninit_const_threshold {
62-
Some(max) => range.len() <= max,
63-
None => false,
64-
};
60+
let max = cx.sess().opts.debugging_opts.uninit_const_chunk_threshold;
61+
let allow_uninit_chunks = chunks.clone().take(max.saturating_add(1)).count() <= max;
6562

66-
if allow_partially_uninit {
63+
if allow_uninit_chunks {
6764
llvals.extend(chunks.map(chunk_to_llval));
6865
} else {
69-
let llval = match (chunks.next(), chunks.next()) {
70-
(Some(chunk), None) => {
71-
// exactly one chunk, either fully init or fully uninit
72-
chunk_to_llval(chunk)
73-
}
74-
_ => {
75-
// partially uninit, codegen as if it was initialized
76-
// (using some arbitrary value for uninit bytes)
77-
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
78-
cx.const_bytes(bytes)
79-
}
80-
};
81-
llvals.push(llval);
66+
// If this allocation contains any uninit bytes, codegen as if it was initialized
67+
// (using some arbitrary value for uninit bytes).
68+
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
69+
llvals.push(cx.const_bytes(bytes));
8270
}
8371
}
8472

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,6 @@ fn test_debugging_options_tracking_hash() {
758758
tracked!(osx_rpath_install_name, true);
759759
tracked!(panic_abort_tests, true);
760760
tracked!(panic_in_drop, PanicStrategy::Abort);
761-
tracked!(partially_uninit_const_threshold, Some(123));
762761
tracked!(pick_stable_methods_before_any_unstable, false);
763762
tracked!(plt, Some(true));
764763
tracked!(polonius, true);
@@ -789,6 +788,7 @@ fn test_debugging_options_tracking_hash() {
789788
tracked!(trap_unreachable, Some(false));
790789
tracked!(treat_err_as_bug, NonZeroUsize::new(1));
791790
tracked!(tune_cpu, Some(String::from("abc")));
791+
tracked!(uninit_const_chunk_threshold, 123);
792792
tracked!(unleash_the_miri_inside_of_you, true);
793793
tracked!(use_ctors_section, Some(true));
794794
tracked!(verify_llvm_ir, true);

compiler/rustc_middle/src/mir/interpret/allocation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ impl InitMask {
957957
}
958958

959959
/// Yields [`InitChunk`]s. See [`InitMask::range_as_init_chunks`].
960+
#[derive(Clone)]
960961
pub struct InitChunkIter<'a> {
961962
init_mask: &'a InitMask,
962963
/// Whether the next chunk we will return is initialized.

compiler/rustc_session/src/options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1341,9 +1341,6 @@ 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> = (Some(1024), parse_opt_number, [TRACKED],
1345-
"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: 1024 bytes)"),
13471344
perf_stats: bool = (false, parse_bool, [UNTRACKED],
13481345
"print some performance-related statistics (default: no)"),
13491346
pick_stable_methods_before_any_unstable: bool = (true, parse_bool, [TRACKED],
@@ -1488,6 +1485,9 @@ options! {
14881485
"in diagnostics, use heuristics to shorten paths referring to items"),
14891486
ui_testing: bool = (false, parse_bool, [UNTRACKED],
14901487
"emit compiler diagnostics in a form suitable for UI testing (default: no)"),
1488+
uninit_const_chunk_threshold: usize = (256, parse_number, [TRACKED],
1489+
"allow generating const initializers with mixed init/uninit chunks, \
1490+
and set the maximum number of chunks for which this is allowed (default: 256)"),
14911491
unleash_the_miri_inside_of_you: bool = (false, parse_bool, [TRACKED],
14921492
"take the brakes off const evaluation. NOTE: this is unsound (default: no)"),
14931493
unpretty: Option<String> = (None, parse_unpretty, [UNTRACKED],

src/test/codegen/uninit-consts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub struct PartiallyUninit {
1515

1616
// 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
1717

18-
// This shouldn't contain undef, since it's larger than the 1024 byte limit.
18+
// This shouldn't contain undef, since it contains more than 256 chunks
19+
// (the default value of uninit_const_chunk_threshold).
1920
// CHECK: [[UNINIT_PADDING_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [32768 x i8] }> <{ [32768 x i8] c"{{.+}}" }>, align 4
2021

2122
// CHECK: [[FULLY_UNINIT_HUGE:@[0-9]+]] = private unnamed_addr constant <{ [16384 x i8] }> undef

0 commit comments

Comments
 (0)