Skip to content

Commit 726481c

Browse files
committed
Add implementation of the alignment parameter in Miri
1 parent 138a409 commit 726481c

File tree

6 files changed

+145
-5
lines changed

6 files changed

+145
-5
lines changed

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use either::Either;
22
use rustc_abi::Endian;
33
use rustc_apfloat::{Float, Round};
4-
use rustc_middle::mir::interpret::{InterpErrorKind, UndefinedBehaviorInfo};
5-
use rustc_middle::ty::FloatTy;
4+
use rustc_middle::mir::interpret::{InterpErrorKind, Pointer, UndefinedBehaviorInfo};
5+
use rustc_middle::ty::{FloatTy, SimdAlign};
66
use rustc_middle::{bug, err_ub_format, mir, span_bug, throw_unsup_format, ty};
77
use rustc_span::{Symbol, sym};
88
use tracing::trace;
@@ -11,7 +11,7 @@ use super::{
1111
ImmTy, InterpCx, InterpResult, Machine, OpTy, PlaceTy, Provenance, Scalar, Size, interp_ok,
1212
throw_ub_format,
1313
};
14-
use crate::interpret::Writeable;
14+
use crate::interpret::{Projectable, Writeable};
1515

1616
#[derive(Copy, Clone)]
1717
pub(crate) enum MinMax {
@@ -666,6 +666,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
666666
assert_eq!(dest_len, mask_len);
667667
assert_eq!(dest_len, default_len);
668668

669+
self.check_simd_ptr_alignment(
670+
ptr,
671+
&dest,
672+
generic_args[3].expect_const().to_value().valtree.unwrap_branch()[0]
673+
.unwrap_leaf()
674+
.to_simd_alignment(),
675+
)?;
676+
669677
for i in 0..dest_len {
670678
let mask = self.read_immediate(&self.project_index(&mask, i)?)?;
671679
let default = self.read_immediate(&self.project_index(&default, i)?)?;
@@ -674,7 +682,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
674682
let val = if simd_element_to_bool(mask)? {
675683
// Size * u64 is implemented as always checked
676684
let ptr = ptr.wrapping_offset(dest.layout.size * i, self);
677-
let place = self.ptr_to_mplace(ptr, dest.layout);
685+
// we have already checked the alignment requirements
686+
let place = self.ptr_to_mplace_unaligned(ptr, dest.layout);
678687
self.read_immediate(&place)?
679688
} else {
680689
default
@@ -689,14 +698,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
689698

690699
assert_eq!(mask_len, vals_len);
691700

701+
self.check_simd_ptr_alignment(
702+
ptr,
703+
&vals,
704+
generic_args[3].expect_const().to_value().valtree.unwrap_branch()[0]
705+
.unwrap_leaf()
706+
.to_simd_alignment(),
707+
)?;
708+
692709
for i in 0..vals_len {
693710
let mask = self.read_immediate(&self.project_index(&mask, i)?)?;
694711
let val = self.read_immediate(&self.project_index(&vals, i)?)?;
695712

696713
if simd_element_to_bool(mask)? {
697714
// Size * u64 is implemented as always checked
698715
let ptr = ptr.wrapping_offset(val.layout.size * i, self);
699-
let place = self.ptr_to_mplace(ptr, val.layout);
716+
// we have already checked the alignment requirements
717+
let place = self.ptr_to_mplace_unaligned(ptr, val.layout);
700718
self.write_immediate(*val, &place)?
701719
};
702720
}
@@ -748,6 +766,25 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
748766
FloatTy::F128 => unimplemented!("f16_f128"),
749767
})
750768
}
769+
770+
fn check_simd_ptr_alignment<Prov: Provenance>(
771+
&self,
772+
ptr: Pointer<Option<M::Provenance>>,
773+
vector: &impl Projectable<'tcx, Prov>,
774+
alignment: SimdAlign,
775+
) -> InterpResult<'tcx> {
776+
match alignment {
777+
ty::SimdAlign::Unaligned => {
778+
// the pointer is supposed to be unaligned, so no check is required
779+
interp_ok(())
780+
}
781+
ty::SimdAlign::Element => {
782+
let elem_align = vector.layout().field(self, 0).align.abi;
783+
self.check_ptr_align(ptr, elem_align)
784+
}
785+
ty::SimdAlign::Vector => self.check_ptr_align(ptr, vector.layout().align.abi),
786+
}
787+
}
751788
}
752789

753790
fn simd_bitmask_index(idx: u32, vec_len: u32, endianness: Endian) -> u32 {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
buf.as_ptr().byte_offset(1),
13+
i32x4::splat(0),
14+
);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_load.rs:LL:CC
3+
|
4+
LL | / simd_masked_load::<_, _, _, { SimdAlign::Element }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | buf.as_ptr().byte_offset(1),
7+
LL | | i32x4::splat(0),
8+
LL | | );
9+
| |_________^ Undefined Behavior occurred here
10+
|
11+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
12+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
13+
= note: BACKTRACE:
14+
= note: inside `main` at tests/fail/intrinsics/simd_masked_load.rs:LL:CC
15+
16+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
17+
18+
error: aborting due to 1 previous error
19+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
buf.as_mut_ptr().byte_offset(1),
13+
i32x4::splat(0),
14+
);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
2+
--> tests/fail/intrinsics/simd_masked_store.rs:LL:CC
3+
|
4+
LL | / simd_masked_store::<_, _, _, { SimdAlign::Element }>(
5+
LL | | i32x4::splat(-1),
6+
LL | | buf.as_mut_ptr().byte_offset(1),
7+
LL | | i32x4::splat(0),
8+
LL | | );
9+
| |_________^ Undefined Behavior occurred here
10+
|
11+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
12+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
13+
= note: BACKTRACE:
14+
= note: inside `main` at tests/fail/intrinsics/simd_masked_store.rs:LL:CC
15+
16+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
17+
18+
error: aborting due to 1 previous error
19+

src/tools/miri/tests/pass/intrinsics/portable-simd.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,39 @@ fn simd_masked_loadstore() {
714714
)
715715
};
716716
assert_eq!(buf, [2, 3, 4]);
717+
718+
// we use a purposely misaliged buffer to make sure Miri doesn't error in this case
719+
let buf = [0x03030303_i32; 5];
720+
let default = i32x4::splat(0);
721+
let mask = i32x4::splat(!0);
722+
let vals = unsafe {
723+
simd_masked_load::<_, _, _, { SimdAlign::Unaligned }>(
724+
mask,
725+
buf.as_ptr().byte_offset(1), // this is guaranteed to be unaligned
726+
default,
727+
)
728+
};
729+
assert_eq!(vals, i32x4::splat(0x03030303));
730+
731+
let mut buf = [0i32; 5];
732+
let mask = i32x4::splat(!0);
733+
unsafe {
734+
simd_masked_store::<_, _, _, { SimdAlign::Unaligned }>(
735+
mask,
736+
buf.as_mut_ptr().byte_offset(1), // this is guaranteed to be unaligned
737+
vals,
738+
)
739+
};
740+
assert_eq!(
741+
buf,
742+
[
743+
i32::from_ne_bytes([0, 3, 3, 3]),
744+
0x03030303,
745+
0x03030303,
746+
0x03030303,
747+
i32::from_ne_bytes([3, 0, 0, 0])
748+
]
749+
);
717750
}
718751

719752
fn simd_ops_non_pow2() {

0 commit comments

Comments
 (0)