Skip to content

Commit 6085d33

Browse files
fix transmutes between pointers in different address spaces
1 parent c6fcdb6 commit 6085d33

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1810,15 +1810,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
18101810
match (src.layout.abi, dst.layout.abi) {
18111811
(abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
18121812
// HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
1813-
if (src_scalar.primitive() == abi::Pointer)
1814-
== (dst_scalar.primitive() == abi::Pointer)
1815-
{
1813+
let src_is_ptr = src_scalar.primitive() == abi::Pointer;
1814+
let dst_is_ptr = dst_scalar.primitive() == abi::Pointer;
1815+
if src_is_ptr == dst_is_ptr {
18161816
assert_eq!(src.layout.size, dst.layout.size);
18171817

18181818
// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
18191819
// conversions allow handling `bool`s the same as `u8`s.
18201820
let src = bx.from_immediate(src.immediate());
1821-
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
1821+
// LLVM also doesn't like `bitcast`s between pointers in different address spaces.
1822+
let src_as_dst = if src_is_ptr {
1823+
bx.pointercast(src, bx.backend_type(dst.layout))
1824+
} else {
1825+
bx.bitcast(src, bx.backend_type(dst.layout))
1826+
};
18221827
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
18231828
return;
18241829
}

src/test/codegen/avr/avr-func-addrspace.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// It also validates that functions can be called through function pointers
1010
// through traits.
1111

12-
#![feature(no_core, lang_items, unboxed_closures, arbitrary_self_types)]
12+
#![feature(no_core, lang_items, intrinsics, unboxed_closures, arbitrary_self_types)]
1313
#![crate_type = "lib"]
1414
#![no_core]
1515

@@ -49,6 +49,10 @@ pub trait Fn<Args: Tuple>: FnOnce<Args> {
4949
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
5050
}
5151

52+
extern "rust-intrinsic" {
53+
pub fn transmute<Src, Dst>(src: Src) -> Dst;
54+
}
55+
5256
pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
5357
pub static mut STORAGE_BAR: u32 = 12;
5458

@@ -87,3 +91,21 @@ pub extern "C" fn test() {
8791
STORAGE_FOO(&1, &mut buf);
8892
}
8993
}
94+
95+
// Validate that we can codegen transmutes between data ptrs and fn ptrs.
96+
97+
// CHECK: define{{.+}}{{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}} @transmute_data_ptr_to_fn({{\{\}\*|ptr}}{{.*}} %x)
98+
#[no_mangle]
99+
pub unsafe fn transmute_data_ptr_to_fn(x: *const ()) -> fn() {
100+
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
101+
// as long as it doesn't cause a verifier error by using `bitcast`.
102+
transmute(x)
103+
}
104+
105+
// CHECK: define{{.+}}{{\{\}\*|ptr}} @transmute_fn_ptr_to_data({{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}}{{.*}} %x)
106+
#[no_mangle]
107+
pub unsafe fn transmute_fn_ptr_to_data(x: fn()) -> *const () {
108+
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
109+
// as long as it doesn't cause a verifier error by using `bitcast`.
110+
transmute(x)
111+
}

0 commit comments

Comments
 (0)