Skip to content

Commit 3d9c8af

Browse files
authored
Merge pull request #365 from rust-lang/fix/volatile-load
Fix volatile_load
2 parents 46887f2 + a93d1b7 commit 3d9c8af

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

Diff for: src/builder.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
751751
loaded_value.to_rvalue()
752752
}
753753

754-
fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
755-
// TODO(antoyo): use ty.
756-
let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
754+
fn volatile_load(&mut self, ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
755+
let ptr = self.context.new_cast(None, ptr, ty.make_volatile().make_pointer());
757756
ptr.dereference(None).to_rvalue()
758757
}
759758

Diff for: src/intrinsic/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ mod simd;
44
#[cfg(feature="master")]
55
use std::iter;
66

7-
use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp, FunctionType};
7+
#[cfg(feature="master")]
8+
use gccjit::FunctionType;
9+
use gccjit::{ComparisonOp, Function, RValue, ToRValue, Type, UnaryOp};
810
use rustc_codegen_ssa::MemFlags;
911
use rustc_codegen_ssa::base::wants_msvc_seh;
1012
use rustc_codegen_ssa::common::IntPredicate;
@@ -143,11 +145,15 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
143145

144146
sym::volatile_load | sym::unaligned_volatile_load => {
145147
let tp_ty = fn_args.type_at(0);
146-
let mut ptr = args[0].immediate();
147-
if let PassMode::Cast { cast: ty, .. } = &fn_abi.ret.mode {
148-
ptr = self.pointercast(ptr, self.type_ptr_to(ty.gcc_type(self)));
149-
}
150-
let load = self.volatile_load(ptr.get_type(), ptr);
148+
let ptr = args[0].immediate();
149+
let load =
150+
if let PassMode::Cast { cast: ty, pad_i32: _ } = &fn_abi.ret.mode {
151+
let gcc_ty = ty.gcc_type(self);
152+
self.volatile_load(gcc_ty, ptr)
153+
}
154+
else {
155+
self.volatile_load(self.layout_of(tp_ty).gcc_type(self), ptr)
156+
};
151157
// TODO(antoyo): set alignment.
152158
self.to_immediate(load, self.layout_of(tp_ty))
153159
}

Diff for: tests/run/volatile.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Compiler:
2+
//
3+
// Run-time:
4+
// status: 0
5+
6+
use std::mem::MaybeUninit;
7+
8+
#[derive(Debug)]
9+
struct Struct {
10+
pointer: *const (),
11+
func: unsafe fn(*const ()),
12+
}
13+
14+
fn func(ptr: *const ()) {
15+
}
16+
17+
fn main() {
18+
let mut x = MaybeUninit::<&Struct>::uninit();
19+
x.write(&Struct {
20+
pointer: std::ptr::null(),
21+
func,
22+
});
23+
let x = unsafe { x.assume_init() };
24+
let value = unsafe { (x as *const Struct).read_volatile() };
25+
println!("{:?}", value);
26+
}

0 commit comments

Comments
 (0)