Skip to content

Commit ffe6cf6

Browse files
committed
Add implementation of the alignment parameter in Miri
1 parent 75de619 commit ffe6cf6

10 files changed

+243
-6
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use either::Either;
2-
use rustc_abi::Endian;
2+
use rustc_abi::{BackendRepr, Endian};
33
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
44
use rustc_apfloat::{Float, Round};
5-
use rustc_middle::mir::interpret::{InterpErrorKind, UndefinedBehaviorInfo};
6-
use rustc_middle::ty::FloatTy;
5+
use rustc_middle::mir::interpret::{InterpErrorKind, Pointer, UndefinedBehaviorInfo};
6+
use rustc_middle::ty::{FloatTy, SimdAlign};
77
use rustc_middle::{bug, err_ub_format, mir, span_bug, throw_unsup_format, ty};
88
use rustc_span::{Symbol, sym};
99
use tracing::trace;
1010

1111
use super::{
1212
ImmTy, InterpCx, InterpResult, Machine, MinMax, MulAddType, OpTy, PlaceTy, Provenance, Scalar,
13-
Size, interp_ok, throw_ub_format,
13+
Size, TyAndLayout, assert_matches, interp_ok, throw_ub_format,
1414
};
1515
use crate::interpret::Writeable;
1616

@@ -644,6 +644,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
644644
}
645645
}
646646
sym::simd_masked_load => {
647+
let dest_layout = dest.layout;
648+
647649
let (mask, mask_len) = self.project_to_simd(&args[0])?;
648650
let ptr = self.read_pointer(&args[1])?;
649651
let (default, default_len) = self.project_to_simd(&args[2])?;
@@ -652,6 +654,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
652654
assert_eq!(dest_len, mask_len);
653655
assert_eq!(dest_len, default_len);
654656

657+
self.check_simd_ptr_alignment(
658+
ptr,
659+
dest_layout,
660+
generic_args[3].expect_const().to_value().valtree.unwrap_branch()[0]
661+
.unwrap_leaf()
662+
.to_simd_alignment(),
663+
)?;
664+
655665
for i in 0..dest_len {
656666
let mask = self.read_immediate(&self.project_index(&mask, i)?)?;
657667
let default = self.read_immediate(&self.project_index(&default, i)?)?;
@@ -660,7 +670,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
660670
let val = if simd_element_to_bool(mask)? {
661671
// Size * u64 is implemented as always checked
662672
let ptr = ptr.wrapping_offset(dest.layout.size * i, self);
663-
let place = self.ptr_to_mplace(ptr, dest.layout);
673+
// we have already checked the alignment requirements
674+
let place = self.ptr_to_mplace_unaligned(ptr, dest.layout);
664675
self.read_immediate(&place)?
665676
} else {
666677
default
@@ -675,14 +686,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
675686

676687
assert_eq!(mask_len, vals_len);
677688

689+
self.check_simd_ptr_alignment(
690+
ptr,
691+
args[2].layout,
692+
generic_args[3].expect_const().to_value().valtree.unwrap_branch()[0]
693+
.unwrap_leaf()
694+
.to_simd_alignment(),
695+
)?;
696+
678697
for i in 0..vals_len {
679698
let mask = self.read_immediate(&self.project_index(&mask, i)?)?;
680699
let val = self.read_immediate(&self.project_index(&vals, i)?)?;
681700

682701
if simd_element_to_bool(mask)? {
683702
// Size * u64 is implemented as always checked
684703
let ptr = ptr.wrapping_offset(val.layout.size * i, self);
685-
let place = self.ptr_to_mplace(ptr, val.layout);
704+
// we have already checked the alignment requirements
705+
let place = self.ptr_to_mplace_unaligned(ptr, val.layout);
686706
self.write_immediate(*val, &place)?
687707
};
688708
}
@@ -753,6 +773,30 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
753773
FloatTy::F128 => self.float_minmax::<Quad>(left, right, op)?,
754774
})
755775
}
776+
777+
fn check_simd_ptr_alignment(
778+
&self,
779+
ptr: Pointer<Option<M::Provenance>>,
780+
vector_layout: TyAndLayout<'tcx>,
781+
alignment: SimdAlign,
782+
) -> InterpResult<'tcx> {
783+
assert_matches!(vector_layout.backend_repr, BackendRepr::SimdVector { .. });
784+
785+
let align = match alignment {
786+
ty::SimdAlign::Unaligned => {
787+
// The pointer is supposed to be unaligned, so no check is required.
788+
return interp_ok(());
789+
}
790+
ty::SimdAlign::Element => {
791+
// Take the alignment of the only field, which is an array and therefore has the same
792+
// alignment as the element type.
793+
vector_layout.field(self, 0).align.abi
794+
}
795+
ty::SimdAlign::Vector => vector_layout.align.abi,
796+
};
797+
798+
self.check_ptr_align(ptr, align)
799+
}
756800
}
757801

758802
fn simd_bitmask_index(idx: u32, vec_len: u32, endianness: Endian) -> u32 {
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+

src/tools/miri/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)