don't generate partially-undef consts
This commit is contained in:
parent
4d635fdf63
commit
5e81d643d9
6 changed files with 56 additions and 24 deletions
|
@ -36,11 +36,6 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll
|
|||
alloc: &'a Allocation,
|
||||
range: Range<usize>,
|
||||
) {
|
||||
/// Allocations larger than this will only be codegen'd as entirely initialized or entirely undef.
|
||||
/// This avoids compile time regressions when an alloc would have many chunks,
|
||||
/// e.g. for `[(u64, u8); N]`, which has undef padding in each element.
|
||||
const MAX_PARTIALLY_UNDEF_SIZE: usize = 1024;
|
||||
|
||||
let mut chunks = alloc
|
||||
.init_mask()
|
||||
.range_as_init_chunks(Size::from_bytes(range.start), Size::from_bytes(range.end));
|
||||
|
@ -57,21 +52,30 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll
|
|||
}
|
||||
};
|
||||
|
||||
if range.len() > MAX_PARTIALLY_UNDEF_SIZE {
|
||||
// Generating partially-uninit consts inhibits optimizations, so it is disabled by default.
|
||||
// See https://github.com/rust-lang/rust/issues/84565.
|
||||
let allow_partially_uninit =
|
||||
match cx.sess().opts.debugging_opts.partially_uninit_const_threshold {
|
||||
Some(max) => range.len() <= max,
|
||||
None => false,
|
||||
};
|
||||
|
||||
if allow_partially_uninit {
|
||||
llvals.extend(chunks.map(chunk_to_llval));
|
||||
} else {
|
||||
let llval = match (chunks.next(), chunks.next()) {
|
||||
(Some(chunk), None) => {
|
||||
// exactly one chunk, either fully init or fully uninit
|
||||
chunk_to_llval(chunk)
|
||||
}
|
||||
_ => {
|
||||
// partially uninit
|
||||
// partially uninit, codegen as if it was initialized
|
||||
// (using some arbitrary value for uninit bytes)
|
||||
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
|
||||
cx.const_bytes(bytes)
|
||||
}
|
||||
};
|
||||
llvals.push(llval);
|
||||
} else {
|
||||
llvals.extend(chunks.map(chunk_to_llval));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -743,6 +743,7 @@ fn test_debugging_options_tracking_hash() {
|
|||
tracked!(no_profiler_runtime, true);
|
||||
tracked!(osx_rpath_install_name, true);
|
||||
tracked!(panic_abort_tests, true);
|
||||
tracked!(partially_uninit_const_threshold, Some(123));
|
||||
tracked!(plt, Some(true));
|
||||
tracked!(polonius, true);
|
||||
tracked!(precise_enum_drop_elaboration, false);
|
||||
|
|
|
@ -1186,6 +1186,9 @@ options! {
|
|||
"support compiling tests with panic=abort (default: no)"),
|
||||
parse_only: bool = (false, parse_bool, [UNTRACKED],
|
||||
"parse only; do not compile, assemble, or link (default: no)"),
|
||||
partially_uninit_const_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
|
||||
"allow generating const initializers with mixed init/uninit bytes, \
|
||||
and set the maximum total size of a const allocation for which this is allowed (default: never)"),
|
||||
perf_stats: bool = (false, parse_bool, [UNTRACKED],
|
||||
"print some performance-related statistics (default: no)"),
|
||||
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue