Skip to content

Commit d19f1b6

Browse files
committed
Auto merge of #32233 - Amanieu:volatile_store, r=eddyb
Fix LLVM assert with write_volatile Fixes #29663
2 parents af8f85c + 86fd5a0 commit d19f1b6

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/librustc_trans/trans/intrinsic.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,11 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
609609
(_, "volatile_store") => {
610610
let tp_ty = *substs.types.get(FnSpace, 0);
611611
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
612-
let val = from_arg_ty(bcx, llargs[1], tp_ty);
612+
let val = if type_is_immediate(bcx.ccx(), tp_ty) {
613+
from_arg_ty(bcx, llargs[1], tp_ty)
614+
} else {
615+
Load(bcx, llargs[1])
616+
};
613617
let store = VolatileStore(bcx, val, ptr);
614618
unsafe {
615619
llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty));

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

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// write_volatile causes an LLVM assert with composite types
12+
13+
#![feature(volatile)]
14+
use std::ptr::{read_volatile, write_volatile};
15+
16+
#[derive(Debug, Eq, PartialEq)]
17+
struct A(u32);
18+
#[derive(Debug, Eq, PartialEq)]
19+
struct B(u64);
20+
#[derive(Debug, Eq, PartialEq)]
21+
struct C(u32, u32);
22+
#[derive(Debug, Eq, PartialEq)]
23+
struct D(u64, u64);
24+
#[derive(Debug, Eq, PartialEq)]
25+
struct E([u64; 32]);
26+
27+
fn main() {
28+
unsafe {
29+
let mut x: u32 = 0;
30+
write_volatile(&mut x, 1);
31+
assert_eq!(read_volatile(&x), 1);
32+
assert_eq!(x, 1);
33+
34+
let mut x: u64 = 0;
35+
write_volatile(&mut x, 1);
36+
assert_eq!(read_volatile(&x), 1);
37+
assert_eq!(x, 1);
38+
39+
let mut x = A(0);
40+
write_volatile(&mut x, A(1));
41+
assert_eq!(read_volatile(&x), A(1));
42+
assert_eq!(x, A(1));
43+
44+
let mut x = B(0);
45+
write_volatile(&mut x, B(1));
46+
assert_eq!(read_volatile(&x), B(1));
47+
assert_eq!(x, B(1));
48+
49+
let mut x = C(0, 0);
50+
write_volatile(&mut x, C(1, 1));
51+
assert_eq!(read_volatile(&x), C(1, 1));
52+
assert_eq!(x, C(1, 1));
53+
54+
let mut x = D(0, 0);
55+
write_volatile(&mut x, D(1, 1));
56+
assert_eq!(read_volatile(&x), D(1, 1));
57+
assert_eq!(x, D(1, 1));
58+
59+
let mut x = E([0; 32]);
60+
write_volatile(&mut x, E([1; 32]));
61+
assert_eq!(read_volatile(&x), E([1; 32]));
62+
assert_eq!(x, E([1; 32]));
63+
}
64+
}

0 commit comments

Comments
 (0)