Skip to content

Commit 66d8d6e

Browse files
committed
interpret: do not ICE on padded non-pow2 SIMD vectors
1 parent 565cadb commit 66d8d6e

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

compiler/rustc_const_eval/src/interpret/place.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,13 @@ where
499499
&self,
500500
mplace: &MPlaceTy<'tcx, M::Provenance>,
501501
) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::Provenance>, u64)> {
502-
// Basically we just transmute this place into an array following simd_size_and_type.
503-
// (Transmuting is okay since this is an in-memory place. We also double-check the size
504-
// stays the same.)
502+
// Basically we want to transmute this place into an array following simd_size_and_type.
505503
let (len, e_ty) = mplace.layout.ty.simd_size_and_type(*self.tcx);
506-
let array = Ty::new_array(self.tcx.tcx, e_ty, len);
507-
let layout = self.layout_of(array)?;
508-
let mplace = mplace.transmute(layout, self)?;
504+
// Some SIMD types have padding, so `len` many `e_ty` does not cover the entire place.
505+
// Therefore we cannot transmute, and instead we project at offset 0, which side-steps
506+
// the size check.
507+
let array_layout = self.layout_of(Ty::new_array(self.tcx.tcx, e_ty, len))?;
508+
let mplace = mplace.offset(Size::ZERO, array_layout, self)?;
509509
Ok((mplace, len))
510510
}
511511

compiler/rustc_const_eval/src/interpret/projection.rs

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
8181
ecx: &InterpCx<'tcx, M>,
8282
) -> InterpResult<'tcx, Self> {
8383
assert!(layout.is_sized());
84+
if self.layout().is_sized() {
85+
// Make sure things don't become bigger. Only works if `self` is sized, since
86+
// we can project e.g. from `[u8]` to a `u8` element.
87+
assert!(offset + layout.size <= self.layout().size);
88+
}
8489
self.offset_with_meta(offset, OffsetMode::Inbounds, MemPlaceMeta::None, layout, ecx)
8590
}
8691

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@compile-flags: -Zmiri-strict-provenance
2-
#![feature(portable_simd, adt_const_params, core_intrinsics)]
2+
#![feature(portable_simd, adt_const_params, core_intrinsics, repr_simd)]
33
#![allow(incomplete_features, internal_features)]
44
use std::intrinsics::simd as intrinsics;
55
use std::ptr;
@@ -581,11 +581,32 @@ fn simd_masked_loadstore() {
581581
assert_eq!(buf, [2, 3, 4]);
582582
}
583583

584+
fn simd_ops_non_pow2() {
585+
// Just a little smoke test for operations on non-power-of-two vectors.
586+
#[repr(simd, packed)]
587+
#[derive(Copy, Clone)]
588+
pub struct SimdPacked<T, const N: usize>([T; N]);
589+
#[repr(simd)]
590+
#[derive(Copy, Clone)]
591+
pub struct SimdPadded<T, const N: usize>([T; N]);
592+
593+
let x = SimdPacked([1u32; 3]);
594+
let y = SimdPacked([2u32; 3]);
595+
let z = unsafe { intrinsics::simd_add(x, y) };
596+
assert_eq!({ z.0 }, [3u32; 3]);
597+
598+
let x = SimdPadded([1u32; 3]);
599+
let y = SimdPadded([2u32; 3]);
600+
let z = unsafe { intrinsics::simd_add(x, y) };
601+
assert_eq!(z.0, [3u32; 3]);
602+
}
603+
584604
fn main() {
585605
simd_mask();
586606
simd_ops_f32();
587607
simd_ops_f64();
588608
simd_ops_i32();
609+
simd_ops_non_pow2();
589610
simd_cast();
590611
simd_swizzle();
591612
simd_gather_scatter();

0 commit comments

Comments
 (0)