Skip to content

Commit 9ba70a9

Browse files
authored
Rollup merge of rust-lang#71446 - Amanieu:transmute_copy, r=sfackler
Only use read_unaligned in transmute_copy if necessary I've noticed that this causes LLVM to generate poor code on targets that don't support unaligned memory accesses.
2 parents 6f18ad5 + 99de372 commit 9ba70a9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libcore/mem/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,12 @@ pub fn drop<T>(_x: T) {}
916916
#[inline]
917917
#[stable(feature = "rust1", since = "1.0.0")]
918918
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
919-
ptr::read_unaligned(src as *const T as *const U)
919+
// If U has a higher alignment requirement, src may not be suitably aligned.
920+
if align_of::<U>() > align_of::<T>() {
921+
ptr::read_unaligned(src as *const T as *const U)
922+
} else {
923+
ptr::read(src as *const T as *const U)
924+
}
920925
}
921926

922927
/// Opaque type representing the discriminant of an enum.

0 commit comments

Comments
 (0)