Skip to content

Commit 0f37b5c

Browse files
committed
Auto merge of rust-lang#138634 - saethlin:repeated-uninit, r=scottmcm,oli-obk
Lower to a memset(undef) when Rvalue::Repeat repeats uninit Fixes rust-lang#138625. It is technically correct to just do nothing. But if we actually do nothing, we may miss that this is de-initializing something, so instead we just lower to a single memset that writes undef. This is still superior to the memcpy loop, in both quality of code we hand to the backend and LLVM's final output.
2 parents 4510e86 + 8e7d8dd commit 0f37b5c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,30 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8686
}
8787

8888
mir::Rvalue::Repeat(ref elem, count) => {
89-
let cg_elem = self.codegen_operand(bx, elem);
90-
9189
// Do not generate the loop for zero-sized elements or empty arrays.
9290
if dest.layout.is_zst() {
9391
return;
9492
}
9593

94+
// When the element is a const with all bytes uninit, emit a single memset that
95+
// writes undef to the entire destination.
96+
if let mir::Operand::Constant(const_op) = elem {
97+
let val = self.eval_mir_constant(const_op);
98+
if val.all_bytes_uninit(self.cx.tcx()) {
99+
let size = bx.const_usize(dest.layout.size.bytes());
100+
bx.memset(
101+
dest.val.llval,
102+
bx.const_undef(bx.type_i8()),
103+
size,
104+
dest.val.align,
105+
MemFlags::empty(),
106+
);
107+
return;
108+
}
109+
}
110+
111+
let cg_elem = self.codegen_operand(bx, elem);
112+
96113
let try_init_all_same = |bx: &mut Bx, v| {
97114
let start = dest.val.llval;
98115
let size = bx.const_usize(dest.layout.size.bytes());

compiler/rustc_middle/src/mir/consts.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use rustc_span::{DUMMY_SP, Span, Symbol};
99
use rustc_type_ir::TypeVisitableExt;
1010

1111
use super::interpret::ReportedErrorInfo;
12-
use crate::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, Scalar, alloc_range};
12+
use crate::mir::interpret::{
13+
AllocId, AllocRange, ConstAllocation, ErrorHandled, GlobalAlloc, Scalar, alloc_range,
14+
};
1315
use crate::mir::{Promoted, pretty_print_const_value};
1416
use crate::ty::print::{pretty_print_const, with_no_trimmed_paths};
1517
use crate::ty::{self, ConstKind, GenericArgsRef, ScalarInt, Ty, TyCtxt};
@@ -192,9 +194,31 @@ impl<'tcx> ConstValue<'tcx> {
192194
.unwrap_memory()
193195
.inner()
194196
.provenance()
195-
.range_empty(super::AllocRange::from(offset..offset + size), &tcx),
197+
.range_empty(AllocRange::from(offset..offset + size), &tcx),
196198
}
197199
}
200+
201+
/// Check if a constant only contains uninitialized bytes.
202+
pub fn all_bytes_uninit(&self, tcx: TyCtxt<'tcx>) -> bool {
203+
let ConstValue::Indirect { alloc_id, .. } = self else {
204+
return false;
205+
};
206+
let alloc = tcx.global_alloc(*alloc_id);
207+
let GlobalAlloc::Memory(alloc) = alloc else {
208+
return false;
209+
};
210+
let init_mask = alloc.0.init_mask();
211+
let init_range = init_mask.is_range_initialized(AllocRange {
212+
start: Size::ZERO,
213+
size: Size::from_bytes(alloc.0.len()),
214+
});
215+
if let Err(range) = init_range {
216+
if range.size == alloc.0.size() {
217+
return true;
218+
}
219+
}
220+
false
221+
}
198222
}
199223

200224
///////////////////////////////////////////////////////////////////////////
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ compile-flags: -Copt-level=3
2+
3+
#![crate_type = "lib"]
4+
5+
use std::mem::MaybeUninit;
6+
7+
// We need to make sure len is at offset 0, otherwise codegen needs an extra instruction
8+
#[repr(C)]
9+
pub struct SmallVec<T> {
10+
pub len: u64,
11+
pub arr: [MaybeUninit<T>; 24],
12+
}
13+
14+
// CHECK-LABEL: @uninit_arr_via_const
15+
#[no_mangle]
16+
pub fn uninit_arr_via_const() -> SmallVec<String> {
17+
// CHECK-NEXT: start:
18+
// CHECK-NEXT: store i64 0,
19+
// CHECK-NEXT: ret
20+
SmallVec { len: 0, arr: [const { MaybeUninit::uninit() }; 24] }
21+
}

0 commit comments

Comments
 (0)