Skip to content

Commit 3fcaea0

Browse files
committed
Auto merge of #142837 - saethlin:maybeuninit-codegen, r=<try>
MaybeImprove MaybeUninit I'm investigating possible solutions to #139355.
2 parents a30f178 + 74a4a77 commit 3fcaea0

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2626
) {
2727
match *rvalue {
2828
mir::Rvalue::Use(ref operand) => {
29+
if let mir::Operand::Constant(const_op) = operand {
30+
let val = self.eval_mir_constant(const_op);
31+
if val.all_bytes_uninit(self.cx.tcx()) {
32+
return;
33+
}
34+
}
35+
2936
let cg_operand = self.codegen_operand(bx, operand);
3037
// FIXME: consider not copying constants through stack. (Fixable by codegen'ing
3138
// constants into `OperandValue::Ref`; why don’t we do that yet if we don’t?)
@@ -95,14 +102,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
95102
if let mir::Operand::Constant(const_op) = elem {
96103
let val = self.eval_mir_constant(const_op);
97104
if val.all_bytes_uninit(self.cx.tcx()) {
98-
let size = bx.const_usize(dest.layout.size.bytes());
99-
bx.memset(
100-
dest.val.llval,
101-
bx.const_undef(bx.type_i8()),
102-
size,
103-
dest.val.align,
104-
MemFlags::empty(),
105-
);
106105
return;
107106
}
108107
}

library/core/src/mem/maybe_uninit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl<T> MaybeUninit<T> {
328328
#[inline(always)]
329329
#[rustc_diagnostic_item = "maybe_uninit_uninit"]
330330
pub const fn uninit() -> MaybeUninit<T> {
331-
MaybeUninit { uninit: () }
331+
const { MaybeUninit { uninit: () } }
332332
}
333333

334334
/// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being

tests/codegen/uninit-consts.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@ pub struct PartiallyUninit {
1111
y: MaybeUninit<[u8; 10]>,
1212
}
1313

14-
// CHECK: [[FULLY_UNINIT:@.*]] = private unnamed_addr constant [10 x i8] undef
15-
1614
// CHECK: [[PARTIALLY_UNINIT:@.*]] = private unnamed_addr constant <{ [4 x i8], [12 x i8] }> <{ [4 x i8] c"{{\\EF\\BE\\AD\\DE|\\DE\\AD\\BE\\EF}}", [12 x i8] undef }>, align 4
1715

1816
// This shouldn't contain undef, since it contains more chunks
1917
// than the default value of uninit_const_chunk_threshold.
2018
// CHECK: [[UNINIT_PADDING_HUGE:@.*]] = private unnamed_addr constant [32768 x i8] c"{{.+}}", align 4
2119

22-
// CHECK: [[FULLY_UNINIT_HUGE:@.*]] = private unnamed_addr constant [16384 x i8] undef
23-
2420
// CHECK-LABEL: @fully_uninit
2521
#[no_mangle]
2622
pub const fn fully_uninit() -> MaybeUninit<[u8; 10]> {
2723
const M: MaybeUninit<[u8; 10]> = MaybeUninit::uninit();
28-
// CHECK: call void @llvm.memcpy.{{.+}}(ptr align 1 %_0, ptr align 1 {{.*}}[[FULLY_UNINIT]]{{.*}}, i{{(32|64)}} 10, i1 false)
24+
// CHECK-NEXT: start:
25+
// CHECK-NEXT: ret void
2926
M
3027
}
3128

@@ -49,6 +46,7 @@ pub const fn uninit_padding_huge() -> [(u32, u8); 4096] {
4946
#[no_mangle]
5047
pub const fn fully_uninit_huge() -> MaybeUninit<[u32; 4096]> {
5148
const F: MaybeUninit<[u32; 4096]> = MaybeUninit::uninit();
52-
// CHECK: call void @llvm.memcpy.{{.+}}(ptr align 4 %_0, ptr align 4 {{.*}}[[FULLY_UNINIT_HUGE]]{{.*}}, i{{(32|64)}} 16384, i1 false)
49+
// CHECK-NEXT: start:
50+
// CHECK-NEXT: ret void
5351
F
5452
}

0 commit comments

Comments
 (0)