Skip to content

Commit 591abdd

Browse files
committed
Lower to a no-op when Rvalue::Repeat repeats uninit
1 parent 43a2e9d commit 591abdd

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ 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+
// Do not generate the loop when the element is an uninit const.
95+
if let mir::Operand::Constant(const_op) = elem {
96+
let val = self.eval_mir_constant(const_op);
97+
if val.all_bytes_uninit(self.cx.tcx()) {
98+
return;
99+
}
100+
}
101+
102+
let cg_elem = self.codegen_operand(bx, elem);
103+
96104
let try_init_all_same = |bx: &mut Bx, v| {
97105
let start = dest.val.llval;
98106
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)