Skip to content

Commit ac43d58

Browse files
committed
trans: Optimize initialization using repeat expressions
This elides initialization for zero-sized arrays: * for zero-sized elements we previously emitted an empty loop * for arrays with a length of zero we previously emitted a loop with zero iterations This emits llvm.memset() instead of a loop over each element when: * all elements are zero integers * elements are byte sized
1 parent d721c1f commit ac43d58

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

src/librustc_trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub fn const_to_uint(v: ValueRef) -> u64 {
372372
}
373373
}
374374

375-
fn is_const_integral(v: ValueRef) -> bool {
375+
pub fn is_const_integral(v: ValueRef) -> bool {
376376
unsafe {
377377
!llvm::LLVMIsAConstantInt(v).is_null()
378378
}

src/librustc_trans/mir/rvalue.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use llvm::{self, ValueRef};
1212
use rustc::ty::{self, Ty};
1313
use rustc::ty::cast::{CastTy, IntTy};
14-
use rustc::ty::layout::{Layout, LayoutTyper};
14+
use rustc::ty::layout::{self, Layout, LayoutTyper, Primitive};
1515
use rustc::mir::tcx::LvalueTy;
1616
use rustc::mir;
1717
use rustc::middle::lang_items::ExchangeMallocFnLangItem;
@@ -20,7 +20,7 @@ use base;
2020
use builder::Builder;
2121
use callee;
2222
use common::{self, val_ty, C_bool, C_null, C_uint};
23-
use common::{C_integral};
23+
use common::{C_integral, C_i32};
2424
use adt;
2525
use machine;
2626
use monomorphize;
@@ -93,12 +93,47 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
9393
}
9494

9595
mir::Rvalue::Repeat(ref elem, ref count) => {
96+
let dest_ty = dest.ty.to_ty(bcx.tcx());
97+
98+
// No need to inizialize memory of a zero-sized slice
99+
if common::type_is_zero_size(bcx.ccx, dest_ty) {
100+
return bcx;
101+
}
102+
96103
let tr_elem = self.trans_operand(&bcx, elem);
97104
let size = count.as_u64(bcx.tcx().sess.target.uint_type);
98105
let size = C_uint(bcx.ccx, size);
99106
let base = base::get_dataptr(&bcx, dest.llval);
107+
let align = dest.alignment.to_align();
108+
109+
if let OperandValue::Immediate(v) = tr_elem.val {
110+
if common::is_const_integral(v) && common::const_to_uint(v) == 0 {
111+
let align = align.unwrap_or_else(|| bcx.ccx.align_of(tr_elem.ty));
112+
let align = C_i32(bcx.ccx, align as i32);
113+
let ty = type_of::type_of(bcx.ccx, dest_ty);
114+
let size = machine::llsize_of(bcx.ccx, ty);
115+
let fill = C_integral(Type::i8(bcx.ccx), 0, false);
116+
base::call_memset(&bcx, base, fill, size, align, false);
117+
return bcx;
118+
}
119+
}
120+
121+
// Use llvm.memset.p0i8.* to initialize byte arrays
122+
let elem_layout = bcx.ccx.layout_of(tr_elem.ty).layout;
123+
match *elem_layout {
124+
Layout::Scalar { value: Primitive::Int(layout::I8), .. } |
125+
Layout::CEnum { discr: layout::I8, .. } => {
126+
let align = align.unwrap_or_else(|| bcx.ccx.align_of(tr_elem.ty));
127+
let align = C_i32(bcx.ccx, align as i32);
128+
let fill = tr_elem.immediate();
129+
base::call_memset(&bcx, base, fill, size, align, false);
130+
return bcx;
131+
}
132+
_ => ()
133+
}
134+
100135
tvec::slice_for_each(&bcx, base, tr_elem.ty, size, |bcx, llslot, loop_bb| {
101-
self.store_operand(bcx, llslot, dest.alignment.to_align(), tr_elem);
136+
self.store_operand(bcx, llslot, align, tr_elem);
102137
bcx.br(loop_bb);
103138
})
104139
}

src/test/codegen/slice-init.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2017 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+
// compile-flags: -C no-prepopulate-passes
12+
13+
#![crate_type = "lib"]
14+
15+
// CHECK-LABEL: @zero_sized_elem
16+
#[no_mangle]
17+
pub fn zero_sized_elem() {
18+
// CHECK-NOT: br label %slice_loop_header{{.*}}
19+
// CHECK-NOT: call void @llvm.memset.p0i8
20+
let x = [(); 4];
21+
drop(&x);
22+
}
23+
24+
// CHECK-LABEL: @zero_len_array
25+
#[no_mangle]
26+
pub fn zero_len_array() {
27+
// CHECK-NOT: br label %slice_loop_header{{.*}}
28+
// CHECK-NOT: call void @llvm.memset.p0i8
29+
let x = [4; 0];
30+
drop(&x);
31+
}
32+
33+
// CHECK-LABEL: @byte_array
34+
#[no_mangle]
35+
pub fn byte_array() {
36+
// CHECK: call void @llvm.memset.p0i8.i{{[0-9]+}}(i8* {{.*}}, i8 7, i64 4
37+
// CHECK-NOT: br label %slice_loop_header{{.*}}
38+
let x = [7u8; 4];
39+
drop(&x);
40+
}
41+
42+
#[allow(dead_code)]
43+
#[derive(Copy, Clone)]
44+
enum Init {
45+
Loop,
46+
Memset,
47+
}
48+
49+
// CHECK-LABEL: @byte_enum_array
50+
#[no_mangle]
51+
pub fn byte_enum_array() {
52+
// CHECK: call void @llvm.memset.p0i8.i{{[0-9]+}}(i8* {{.*}}, i8 {{.*}}, i64 4
53+
// CHECK-NOT: br label %slice_loop_header{{.*}}
54+
let x = [Init::Memset; 4];
55+
drop(&x);
56+
}
57+
58+
// CHECK-LABEL: @zeroed_integer_array
59+
#[no_mangle]
60+
pub fn zeroed_integer_array() {
61+
// CHECK: call void @llvm.memset.p0i8.i{{[0-9]+}}(i8* {{.*}}, i8 0, i64 16
62+
// CHECK-NOT: br label %slice_loop_header{{.*}}
63+
let x = [0u32; 4];
64+
drop(&x);
65+
}
66+
67+
// CHECK-LABEL: @nonzero_integer_array
68+
#[no_mangle]
69+
pub fn nonzero_integer_array() {
70+
// CHECK: br label %slice_loop_header{{.*}}
71+
// CHECK-NOT: call void @llvm.memset.p0i8
72+
let x = [0x1a_2b_3c_4d_u32; 4];
73+
drop(&x);
74+
}

0 commit comments

Comments
 (0)