Skip to content

Commit 4f9812a

Browse files
authored
Auto merge of #36496 - pnkfelix:workaround-issue-34427, r=eddyb
Workaround #34427 by using memset of 0 on ARM to set the discriminant. Workaround #34427 by using memset of 0 on ARM to set the discriminant.
2 parents 53f9730 + c41a806 commit 4f9812a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/librustc_trans/adt.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use syntax::ast;
5454
use syntax::attr;
5555
use syntax::attr::IntType;
5656
use abi::FAT_PTR_ADDR;
57+
use base;
5758
use build::*;
5859
use common::*;
5960
use debuginfo::DebugLoc;
@@ -963,16 +964,32 @@ pub fn trans_set_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
963964
Store(bcx, C_null(llptrty), val);
964965
}
965966
}
966-
StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
967+
StructWrappedNullablePointer { nndiscr, ref discrfield, ref nonnull, .. } => {
967968
if discr != nndiscr {
968-
let llptrptr = GEPi(bcx, val, &discrfield[..]);
969-
let llptrty = val_ty(llptrptr).element_type();
970-
Store(bcx, C_null(llptrty), llptrptr);
969+
if target_sets_discr_via_memset(bcx) {
970+
// Issue #34427: As workaround for LLVM bug on
971+
// ARM, use memset of 0 on whole struct rather
972+
// than storing null to single target field.
973+
let b = B(bcx);
974+
let llptr = b.pointercast(val, Type::i8(b.ccx).ptr_to());
975+
let fill_byte = C_u8(b.ccx, 0);
976+
let size = C_uint(b.ccx, nonnull.size);
977+
let align = C_i32(b.ccx, nonnull.align as i32);
978+
base::call_memset(&b, llptr, fill_byte, size, align, false);
979+
} else {
980+
let llptrptr = GEPi(bcx, val, &discrfield[..]);
981+
let llptrty = val_ty(llptrptr).element_type();
982+
Store(bcx, C_null(llptrty), llptrptr);
983+
}
971984
}
972985
}
973986
}
974987
}
975988

989+
fn target_sets_discr_via_memset<'blk, 'tcx>(bcx: Block<'blk, 'tcx>) -> bool {
990+
bcx.sess().target.target.arch == "arm" || bcx.sess().target.target.arch == "aarch64"
991+
}
992+
976993
fn assert_discr_in_range(ity: IntType, min: Disr, max: Disr, discr: Disr) {
977994
match ity {
978995
attr::UnsignedInt(_) => {

src/test/run-pass/issue-34427.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// Issue #34427: On ARM, the code in `foo` at one time was generating
12+
// a machine code instruction of the form: `str r0, [r0, rN]!` (for
13+
// some N), which is not legal because the source register and base
14+
// register cannot be identical in the preindexed form signalled by
15+
// the `!`.
16+
//
17+
// See LLVM bug: https://llvm.org/bugs/show_bug.cgi?id=28809
18+
19+
#[inline(never)]
20+
fn foo(n: usize) -> Vec<Option<(*mut (), &'static ())>> {
21+
(0..n).map(|_| None).collect()
22+
}
23+
24+
fn main() {
25+
let _ = (foo(10), foo(32));
26+
}

0 commit comments

Comments
 (0)