Skip to content

Commit

Permalink
Auto merge of #2895 - RalfJung:simd_bitmask, r=RalfJung
Browse files Browse the repository at this point in the history
support array return types in simd_bitmask

Fixes #2734

As usual I am stomped by the simd_bitmask behavior wrt endianess. I confirmed that for little endian, Miri matches what rustc currently does, but I can't test rustc on big endian. `@workingjubilee` `@calebzulawski` could you have a look whether those new tests make sense?
  • Loading branch information
bors committed May 13, 2023
2 parents 9f1392a + 54e1bd0 commit 1a80d1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/shims/intrinsics/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let (op, op_len) = this.operand_to_simd(op)?;
let bitmask_len = op_len.max(8);

assert!(dest.layout.ty.is_integral());
// Returns either an unsigned integer or array of `u8`.
assert!(
dest.layout.ty.is_integral()
|| matches!(dest.layout.ty.kind(), ty::Array(elemty, _) if elemty == &this.tcx.types.u8)
);
assert!(bitmask_len <= 64);
assert_eq!(bitmask_len, dest.layout.size.bits());
let op_len = u32::try_from(op_len).unwrap();
Expand All @@ -577,7 +581,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
.unwrap();
}
}
this.write_int(res, dest)?;
// We have to force the place type to be an int so that we can write `res` into it.
let mut dest = this.force_allocation(dest)?;
dest.layout = this.machine.layouts.uint(dest.layout.size).unwrap();
this.write_int(res, &dest.into())?;
}

name => throw_unsup_format!("unimplemented intrinsic: `simd_{name}`"),
Expand Down
33 changes: 33 additions & 0 deletions tests/pass/portable-simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#![feature(portable_simd, platform_intrinsics)]
use std::simd::*;

extern "platform-intrinsic" {
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;
}

fn simd_ops_f32() {
let a = f32x4::splat(10.0);
let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
Expand Down Expand Up @@ -208,11 +212,40 @@ fn simd_mask() {
assert_eq!(bitmask, 0b1010001101001001);
assert_eq!(Mask::<i64, 16>::from_bitmask(bitmask), mask);

// Also directly call intrinsic, to test both kinds of return types.
unsafe {
let bitmask1: u16 = simd_bitmask(mask.to_int());
let bitmask2: [u8; 2] = simd_bitmask(mask.to_int());
if cfg!(target_endian = "little") {
assert_eq!(bitmask1, 0b1010001101001001);
assert_eq!(bitmask2, [0b01001001, 0b10100011]);
} else {
// All the bitstrings are reversed compared to above, but the array elements are in the
// same order.
assert_eq!(bitmask1, 0b1001001011000101);
assert_eq!(bitmask2, [0b10010010, 0b11000101]);
}
}

// Mask less than 8 bits long, which is a special case (padding with 0s).
let values = [false, false, false, true];
let mask = Mask::<i64, 4>::from_array(values);
let bitmask = mask.to_bitmask();
assert_eq!(bitmask, 0b1000);
assert_eq!(Mask::<i64, 4>::from_bitmask(bitmask), mask);

// Also directly call intrinsic, to test both kinds of return types.
unsafe {
let bitmask1: u8 = simd_bitmask(mask.to_int());
let bitmask2: [u8; 1] = simd_bitmask(mask.to_int());
if cfg!(target_endian = "little") {
assert_eq!(bitmask1, 0b1000);
assert_eq!(bitmask2, [0b1000]);
} else {
assert_eq!(bitmask1, 0b0001);
assert_eq!(bitmask2, [0b0001]);
}
}
}

fn simd_cast() {
Expand Down

0 comments on commit 1a80d1e

Please sign in to comment.