Skip to content

Commit 43f2c19

Browse files
committed
Auto merge of #21282 - Aatch:init-memzero, r=alexcrichton
LLVM gets overwhelmed when presented with a zeroinitializer for a large type. In unoptimised builds, it generates a long sequence of stores to memory. In optmised builds, it manages to generate a standard memset of zero values, but takes a long time doing so. Call out to the `llvm.memset` function to zero out the memory instead. Fixes #21264
2 parents 7b87900 + 25a4adc commit 43f2c19

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/librustc_trans/trans/intrinsic.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,11 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
361361
}
362362
(_, "init") => {
363363
let tp_ty = *substs.types.get(FnSpace, 0);
364-
let lltp_ty = type_of::arg_type_of(ccx, tp_ty);
365-
if return_type_is_void(ccx, tp_ty) {
366-
C_nil(ccx)
367-
} else {
368-
C_null(lltp_ty)
364+
if !return_type_is_void(ccx, tp_ty) {
365+
// Just zero out the stack slot. (See comment on base::memzero for explaination)
366+
zero_mem(bcx, llresult, tp_ty);
369367
}
368+
C_nil(ccx)
370369
}
371370
// Effectively no-ops
372371
(_, "uninit") | (_, "forget") => {

src/test/run-pass/init-large-type.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Makes sure that zero-initializing large types is reasonably fast,
12+
// Doing it incorrectly causes massive slowdown in LLVM during
13+
// optimisation.
14+
15+
#![feature(intrinsics)]
16+
17+
extern "rust-intrinsic" {
18+
pub fn init<T>() -> T;
19+
}
20+
21+
const SIZE: usize = 1024 * 1024;
22+
23+
fn main() {
24+
let _memory: [u8; SIZE] = unsafe { init() };
25+
}

0 commit comments

Comments
 (0)