Skip to content

Commit 20618d0

Browse files
committed
[MIR] Avoid some code generation for stores of ZST
Fixes #30831
1 parent 3246eae commit 20618d0

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/librustc_trans/trans/mir/operand.rs

+5
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
169169
operand: OperandRef<'tcx>)
170170
{
171171
debug!("store_operand: operand={}", operand.repr(bcx));
172+
// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
173+
// value is through `undef`, and store itself is useless.
174+
if common::type_is_zero_size(bcx.ccx(), operand.ty) {
175+
return;
176+
}
172177
match operand.val {
173178
OperandValue::Ref(r) => base::memcpy_ty(bcx, lldest, r, operand.ty),
174179
OperandValue::Immediate(s) => base::store_ty(bcx, s, lldest, operand.ty),

src/librustc_trans/trans/mir/rvalue.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
108108
},
109109
_ => {
110110
for (i, operand) in operands.iter().enumerate() {
111-
// Note: perhaps this should be StructGep, but
112-
// note that in some cases the values here will
113-
// not be structs but arrays.
114-
let lldest_i = build::GEPi(bcx, dest.llval, &[0, i]);
115-
self.trans_operand_into(bcx, lldest_i, operand);
111+
let op = self.trans_operand(bcx, operand);
112+
// Do not generate stores and GEPis for zero-sized fields.
113+
if !common::type_is_zero_size(bcx.ccx(), op.ty) {
114+
// Note: perhaps this should be StructGep, but
115+
// note that in some cases the values here will
116+
// not be structs but arrays.
117+
let dest = build::GEPi(bcx, dest.llval, &[0, i]);
118+
self.store_operand(bcx, dest, op);
119+
}
116120
}
117121
}
118122
}

src/test/codegen/mir_zst_stores.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 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+
#![feature(rustc_attrs)]
14+
#![crate_type = "lib"]
15+
use std::marker::PhantomData;
16+
17+
18+
struct Zst { phantom: PhantomData<Zst> }
19+
20+
// CHECK-LABEL: @mir
21+
#[no_mangle]
22+
#[rustc_mir]
23+
fn mir(){
24+
// CHECK-NOT: getelementptr
25+
// CHECK-NOT: store{{.*}}undef
26+
let x = Zst { phantom: PhantomData };
27+
}

0 commit comments

Comments
 (0)