Skip to content

Commit dcf69bc

Browse files
committed
Add implementation of the alignment parameter in Miri
1 parent bbe6cb3 commit dcf69bc

9 files changed

+193
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics, portable_simd)]
2+
3+
use std::intrinsics::simd::*;
4+
use std::simd::*;
5+
6+
fn main() {
7+
unsafe {
8+
let buf = [0u32; 5];
9+
//~v ERROR: accessing memory with alignment
10+
simd_masked_load::<_, _, _, { SimdAlign::Element }>(
11+
i32x4::splat(-1),
12+
// This is not i32-aligned
13+
buf.as_ptr().byte_offset(1),
14+
i32x4::splat(0),
15+
);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_load_element_misaligned.rs:LL:CC
3+
|
4+
LL | / simd_masked_load::<_, _, _, { SimdAlign::Element }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | // This is not i32-aligned
7+
LL | | buf.as_ptr().byte_offset(1),
8+
LL | | i32x4::splat(0),
9+
LL | | );
10+
| |_________^ Undefined Behavior occurred here
11+
|
12+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
13+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
14+
15+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
16+
17+
error: aborting due to 1 previous error
18+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics, portable_simd)]
2+
3+
use std::intrinsics::simd::*;
4+
use std::simd::*;
5+
6+
fn main() {
7+
unsafe {
8+
let buf = Simd::<i32, 8>::splat(0);
9+
//~v ERROR: accessing memory with alignment
10+
simd_masked_load::<_, _, _, { SimdAlign::Vector }>(
11+
i32x4::splat(-1),
12+
// This is i32-aligned but not i32x4-aligned.
13+
buf.as_array()[1..].as_ptr(),
14+
i32x4::splat(0),
15+
);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_load_vector_misaligned.rs:LL:CC
3+
|
4+
LL | / simd_masked_load::<_, _, _, { SimdAlign::Vector }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | // This is i32-aligned but not i32x4-aligned.
7+
LL | | buf.as_array()[1..].as_ptr(),
8+
LL | | i32x4::splat(0),
9+
LL | | );
10+
| |_________^ Undefined Behavior occurred here
11+
|
12+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
13+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
14+
15+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
16+
17+
error: aborting due to 1 previous error
18+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics, portable_simd)]
2+
3+
use std::intrinsics::simd::*;
4+
use std::simd::*;
5+
6+
fn main() {
7+
unsafe {
8+
let mut buf = [0u32; 5];
9+
//~v ERROR: accessing memory with alignment
10+
simd_masked_store::<_, _, _, { SimdAlign::Element }>(
11+
i32x4::splat(-1),
12+
// This is not i32-aligned
13+
buf.as_mut_ptr().byte_offset(1),
14+
i32x4::splat(0),
15+
);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_store_element_misaligned.rs:LL:CC
3+
|
4+
LL | / simd_masked_store::<_, _, _, { SimdAlign::Element }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | // This is not i32-aligned
7+
LL | | buf.as_mut_ptr().byte_offset(1),
8+
LL | | i32x4::splat(0),
9+
LL | | );
10+
| |_________^ Undefined Behavior occurred here
11+
|
12+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
13+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
14+
15+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
16+
17+
error: aborting due to 1 previous error
18+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics, portable_simd)]
2+
3+
use std::intrinsics::simd::*;
4+
use std::simd::*;
5+
6+
fn main() {
7+
unsafe {
8+
let mut buf = Simd::<i32, 8>::splat(0);
9+
//~v ERROR: accessing memory with alignment
10+
simd_masked_store::<_, _, _, { SimdAlign::Vector }>(
11+
i32x4::splat(-1),
12+
// This is i32-aligned but not i32x4-aligned.
13+
buf.as_mut_array()[1..].as_mut_ptr(),
14+
i32x4::splat(0),
15+
);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_store_vector_misaligned.rs:LL:CC
3+
|
4+
LL | / simd_masked_store::<_, _, _, { SimdAlign::Vector }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | // This is i32-aligned but not i32x4-aligned.
7+
LL | | buf.as_mut_array()[1..].as_mut_ptr(),
8+
LL | | i32x4::splat(0),
9+
LL | | );
10+
| |_________^ Undefined Behavior occurred here
11+
|
12+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
13+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
14+
15+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
16+
17+
error: aborting due to 1 previous error
18+

tests/pass/intrinsics/portable-simd.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,59 @@ fn simd_masked_loadstore() {
970970
)
971971
};
972972
assert_eq!(buf, [2, 3, 4]);
973+
974+
// we use a purposely misaliged buffer to make sure Miri doesn't error in this case
975+
let buf = [0x03030303_i32; 5];
976+
let default = i32x4::splat(0);
977+
let mask = i32x4::splat(!0);
978+
let vals = unsafe {
979+
simd_masked_load::<_, _, _, { SimdAlign::Unaligned }>(
980+
mask,
981+
buf.as_ptr().byte_offset(1), // this is guaranteed to be unaligned
982+
default,
983+
)
984+
};
985+
assert_eq!(vals, i32x4::splat(0x03030303));
986+
987+
let mut buf = [0i32; 5];
988+
let mask = i32x4::splat(!0);
989+
unsafe {
990+
simd_masked_store::<_, _, _, { SimdAlign::Unaligned }>(
991+
mask,
992+
buf.as_mut_ptr().byte_offset(1), // this is guaranteed to be unaligned
993+
vals,
994+
)
995+
};
996+
assert_eq!(
997+
buf,
998+
[
999+
i32::from_ne_bytes([0, 3, 3, 3]),
1000+
0x03030303,
1001+
0x03030303,
1002+
0x03030303,
1003+
i32::from_ne_bytes([3, 0, 0, 0])
1004+
]
1005+
);
1006+
1007+
// `repr(simd)` types like `Simd<T, N>` have the correct alignment for vectors
1008+
let buf = i32x4::splat(3);
1009+
let default = i32x4::splat(0);
1010+
let mask = i32x4::splat(!0);
1011+
let vals = unsafe {
1012+
simd_masked_load::<_, _, _, { SimdAlign::Vector }>(
1013+
mask,
1014+
&raw const buf as *const i32,
1015+
default,
1016+
)
1017+
};
1018+
assert_eq!(vals, buf);
1019+
1020+
let mut buf = i32x4::splat(0);
1021+
let mask = i32x4::splat(!0);
1022+
unsafe {
1023+
simd_masked_store::<_, _, _, { SimdAlign::Vector }>(mask, &raw mut buf as *mut i32, vals)
1024+
};
1025+
assert_eq!(buf, vals);
9731026
}
9741027

9751028
fn simd_ops_non_pow2() {

0 commit comments

Comments
 (0)