diff options
| author | Ben Kimock <kimockb@gmail.com> | 2025-03-17 21:29:07 -0400 |
|---|---|---|
| committer | Ben Kimock <kimockb@gmail.com> | 2025-03-19 23:57:49 -0400 |
| commit | 8e7d8ddffe802180b504e0ecaaa40b10b28b291e (patch) | |
| tree | 0eac6843f8d6a6f9f1c1aac526f6ab961abe5180 /compiler/rustc_codegen_ssa/src | |
| parent | 43a2e9d2c72db101f5fedac8b3acb78981b06bf2 (diff) | |
| download | rust-8e7d8ddffe802180b504e0ecaaa40b10b28b291e.tar.gz rust-8e7d8ddffe802180b504e0ecaaa40b10b28b291e.zip | |
Lower to a memset(undef) when Rvalue::Repeat repeats uninit
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 72cfd2bffb5..1df00465f74 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -86,13 +86,30 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } mir::Rvalue::Repeat(ref elem, count) => { - let cg_elem = self.codegen_operand(bx, elem); - // Do not generate the loop for zero-sized elements or empty arrays. if dest.layout.is_zst() { return; } + // When the element is a const with all bytes uninit, emit a single memset that + // writes undef to the entire destination. + if let mir::Operand::Constant(const_op) = elem { + let val = self.eval_mir_constant(const_op); + if val.all_bytes_uninit(self.cx.tcx()) { + let size = bx.const_usize(dest.layout.size.bytes()); + bx.memset( + dest.val.llval, + bx.const_undef(bx.type_i8()), + size, + dest.val.align, + MemFlags::empty(), + ); + return; + } + } + + let cg_elem = self.codegen_operand(bx, elem); + let try_init_all_same = |bx: &mut Bx, v| { let start = dest.val.llval; let size = bx.const_usize(dest.layout.size.bytes()); |
