Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transmutes between pointers in different address spaces (e.g. fn ptrs on AVR) #105578

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,15 +1810,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
match (src.layout.abi, dst.layout.abi) {
(abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
// HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
if (src_scalar.primitive() == abi::Pointer)
== (dst_scalar.primitive() == abi::Pointer)
{
let src_is_ptr = src_scalar.primitive() == abi::Pointer;
let dst_is_ptr = dst_scalar.primitive() == abi::Pointer;
if src_is_ptr == dst_is_ptr {
assert_eq!(src.layout.size, dst.layout.size);

// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
// conversions allow handling `bool`s the same as `u8`s.
let src = bx.from_immediate(src.immediate());
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
// LLVM also doesn't like `bitcast`s between pointers in different address spaces.
let src_as_dst = if src_is_ptr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the from type is a pointer and the to type is an integer? Does pointercast work for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, but that's not possible here due to src_is_ptr == dst_is_ptr above. I can change this line to src_is_ptr && dst_is_ptr if that would be clearer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

bx.pointercast(src, bx.backend_type(dst.layout))
} else {
bx.bitcast(src, bx.backend_type(dst.layout))
};
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
return;
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/codegen/avr/avr-func-addrspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// It also validates that functions can be called through function pointers
// through traits.

#![feature(no_core, lang_items, unboxed_closures, arbitrary_self_types)]
#![feature(no_core, lang_items, intrinsics, unboxed_closures, arbitrary_self_types)]
#![crate_type = "lib"]
#![no_core]

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

extern "rust-intrinsic" {
pub fn transmute<Src, Dst>(src: Src) -> Dst;
}

pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
pub static mut STORAGE_BAR: u32 = 12;

Expand Down Expand Up @@ -87,3 +91,21 @@ pub extern "C" fn test() {
STORAGE_FOO(&1, &mut buf);
}
}

// Validate that we can codegen transmutes between data ptrs and fn ptrs.

// CHECK: define{{.+}}{{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}} @transmute_data_ptr_to_fn({{\{\}\*|ptr}}{{.*}} %x)
#[no_mangle]
pub unsafe fn transmute_data_ptr_to_fn(x: *const ()) -> fn() {
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
// as long as it doesn't cause a verifier error by using `bitcast`.
transmute(x)
}

// CHECK: define{{.+}}{{\{\}\*|ptr}} @transmute_fn_ptr_to_data({{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}}{{.*}} %x)
#[no_mangle]
pub unsafe fn transmute_fn_ptr_to_data(x: fn()) -> *const () {
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
// as long as it doesn't cause a verifier error by using `bitcast`.
transmute(x)
}