Skip to content

Commit 1a80d1e

Browse files
committed
Auto merge of #2895 - RalfJung:simd_bitmask, r=RalfJung
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?
2 parents 9f1392a + 54e1bd0 commit 1a80d1e

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/shims/intrinsics/simd.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
563563
let (op, op_len) = this.operand_to_simd(op)?;
564564
let bitmask_len = op_len.max(8);
565565

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

583590
name => throw_unsup_format!("unimplemented intrinsic: `simd_{name}`"),

tests/pass/portable-simd.rs

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#![feature(portable_simd, platform_intrinsics)]
33
use std::simd::*;
44

5+
extern "platform-intrinsic" {
6+
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;
7+
}
8+
59
fn simd_ops_f32() {
610
let a = f32x4::splat(10.0);
711
let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
@@ -208,11 +212,40 @@ fn simd_mask() {
208212
assert_eq!(bitmask, 0b1010001101001001);
209213
assert_eq!(Mask::<i64, 16>::from_bitmask(bitmask), mask);
210214

215+
// Also directly call intrinsic, to test both kinds of return types.
216+
unsafe {
217+
let bitmask1: u16 = simd_bitmask(mask.to_int());
218+
let bitmask2: [u8; 2] = simd_bitmask(mask.to_int());
219+
if cfg!(target_endian = "little") {
220+
assert_eq!(bitmask1, 0b1010001101001001);
221+
assert_eq!(bitmask2, [0b01001001, 0b10100011]);
222+
} else {
223+
// All the bitstrings are reversed compared to above, but the array elements are in the
224+
// same order.
225+
assert_eq!(bitmask1, 0b1001001011000101);
226+
assert_eq!(bitmask2, [0b10010010, 0b11000101]);
227+
}
228+
}
229+
230+
// Mask less than 8 bits long, which is a special case (padding with 0s).
211231
let values = [false, false, false, true];
212232
let mask = Mask::<i64, 4>::from_array(values);
213233
let bitmask = mask.to_bitmask();
214234
assert_eq!(bitmask, 0b1000);
215235
assert_eq!(Mask::<i64, 4>::from_bitmask(bitmask), mask);
236+
237+
// Also directly call intrinsic, to test both kinds of return types.
238+
unsafe {
239+
let bitmask1: u8 = simd_bitmask(mask.to_int());
240+
let bitmask2: [u8; 1] = simd_bitmask(mask.to_int());
241+
if cfg!(target_endian = "little") {
242+
assert_eq!(bitmask1, 0b1000);
243+
assert_eq!(bitmask2, [0b1000]);
244+
} else {
245+
assert_eq!(bitmask1, 0b0001);
246+
assert_eq!(bitmask2, [0b0001]);
247+
}
248+
}
216249
}
217250

218251
fn simd_cast() {

0 commit comments

Comments
 (0)