forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix transmute::<T, U> where T requires a bigger alignment than U
For types that are not bitcast-compatible, transmute tries to avoid generating a temporary by translating its source expression directly into its destination, but when the source type has a bigger alignment requirement than the destination, this can lead to code that breaks due to misaligned stores. So in that case we need to generate a temporary for the source expression and then copy that into the destination, setting the proper alignment information on the memcpy/store. Fixes rust-lang#32947
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(repr_simd, test)] | ||
|
||
extern crate test; | ||
|
||
#[repr(simd)] | ||
pub struct Mu64(pub u64, pub u64, pub u64, pub u64); | ||
|
||
#[inline(never)] | ||
fn new(x: u64) -> Mu64 { | ||
Mu64(x, x, x, x) | ||
} | ||
|
||
#[inline(never)] | ||
fn invoke_doom(x: &u8) -> [u8; 32] { | ||
// This transmute used to directly store the SIMD vector into a location | ||
// that isn't necessarily properly aligned | ||
unsafe { std::mem::transmute(new(*x as u64)) } | ||
} | ||
|
||
fn main() { | ||
// Try to get the dest for the invoke_doom calls to be misaligned even in optimized builds | ||
let x = 0; | ||
test::black_box(invoke_doom(&x)); | ||
let y = 1; | ||
test::black_box(invoke_doom(&y)); | ||
} |