Skip to content

Commit 6da0959

Browse files
committed
Auto merge of rust-lang#3205 - RalfJung:simd-bitmask, r=RalfJung
also test simd_select_bitmask on arrays for less than 8 elements
2 parents 28f9fe3 + 6e74d2a commit 6da0959

File tree

2 files changed

+94
-48
lines changed

2 files changed

+94
-48
lines changed

Diff for: src/tools/miri/src/shims/intrinsics/simd.rs

+45-43
Original file line numberDiff line numberDiff line change
@@ -405,37 +405,35 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
405405
this.write_immediate(*val, &dest)?;
406406
}
407407
}
408+
// Variant of `select` that takes a bitmask rather than a "vector of bool".
408409
"select_bitmask" => {
409410
let [mask, yes, no] = check_arg_count(args)?;
410411
let (yes, yes_len) = this.operand_to_simd(yes)?;
411412
let (no, no_len) = this.operand_to_simd(no)?;
412413
let (dest, dest_len) = this.place_to_simd(dest)?;
413414
let bitmask_len = dest_len.max(8);
414415

416+
// The mask must be an integer or an array.
417+
assert!(
418+
mask.layout.ty.is_integral()
419+
|| matches!(mask.layout.ty.kind(), ty::Array(elemty, _) if elemty == &this.tcx.types.u8)
420+
);
415421
assert!(bitmask_len <= 64);
416422
assert_eq!(bitmask_len, mask.layout.size.bits());
417423
assert_eq!(dest_len, yes_len);
418424
assert_eq!(dest_len, no_len);
419425
let dest_len = u32::try_from(dest_len).unwrap();
420426
let bitmask_len = u32::try_from(bitmask_len).unwrap();
421427

422-
// The mask can be a single integer or an array.
423-
let mask: u64 = match mask.layout.ty.kind() {
424-
ty::Int(..) | ty::Uint(..) =>
425-
this.read_scalar(mask)?.to_bits(mask.layout.size)?.try_into().unwrap(),
426-
ty::Array(elem, _) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) => {
427-
let mask_ty = this.machine.layouts.uint(mask.layout.size).unwrap();
428-
let mask = mask.transmute(mask_ty, this)?;
429-
this.read_scalar(&mask)?.to_bits(mask_ty.size)?.try_into().unwrap()
430-
}
431-
_ => bug!("simd_select_bitmask: invalid mask type {}", mask.layout.ty),
432-
};
428+
// To read the mask, we transmute it to an integer.
429+
// That does the right thing wrt endianess.
430+
let mask_ty = this.machine.layouts.uint(mask.layout.size).unwrap();
431+
let mask = mask.transmute(mask_ty, this)?;
432+
let mask: u64 = this.read_scalar(&mask)?.to_bits(mask_ty.size)?.try_into().unwrap();
433433

434434
for i in 0..dest_len {
435-
let mask = mask
436-
& 1u64
437-
.checked_shl(simd_bitmask_index(i, dest_len, this.data_layout().endian))
438-
.unwrap();
435+
let bit_i = simd_bitmask_index(i, dest_len, this.data_layout().endian);
436+
let mask = mask & 1u64.checked_shl(bit_i).unwrap();
439437
let yes = this.read_immediate(&this.project_index(&yes, i.into())?)?;
440438
let no = this.read_immediate(&this.project_index(&no, i.into())?)?;
441439
let dest = this.project_index(&dest, i.into())?;
@@ -445,6 +443,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
445443
}
446444
for i in dest_len..bitmask_len {
447445
// If the mask is "padded", ensure that padding is all-zero.
446+
// This deliberately does not use `simd_bitmask_index`; these bits are outside
447+
// the bitmask. It does not matter in which order we check them.
448448
let mask = mask & 1u64.checked_shl(i).unwrap();
449449
if mask != 0 {
450450
throw_ub_format!(
@@ -453,6 +453,36 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
453453
}
454454
}
455455
}
456+
// Converts a "vector of bool" into a bitmask.
457+
"bitmask" => {
458+
let [op] = check_arg_count(args)?;
459+
let (op, op_len) = this.operand_to_simd(op)?;
460+
let bitmask_len = op_len.max(8);
461+
462+
// Returns either an unsigned integer or array of `u8`.
463+
assert!(
464+
dest.layout.ty.is_integral()
465+
|| matches!(dest.layout.ty.kind(), ty::Array(elemty, _) if elemty == &this.tcx.types.u8)
466+
);
467+
assert!(bitmask_len <= 64);
468+
assert_eq!(bitmask_len, dest.layout.size.bits());
469+
let op_len = u32::try_from(op_len).unwrap();
470+
471+
let mut res = 0u64;
472+
for i in 0..op_len {
473+
let op = this.read_immediate(&this.project_index(&op, i.into())?)?;
474+
if simd_element_to_bool(op)? {
475+
res |= 1u64
476+
.checked_shl(simd_bitmask_index(i, op_len, this.data_layout().endian))
477+
.unwrap();
478+
}
479+
}
480+
// We have to change the type of the place to be able to write `res` into it. This
481+
// transmutes the integer to an array, which does the right thing wrt endianess.
482+
let dest =
483+
dest.transmute(this.machine.layouts.uint(dest.layout.size).unwrap(), this)?;
484+
this.write_int(res, &dest)?;
485+
}
456486
"cast" | "as" | "cast_ptr" | "expose_addr" | "from_exposed_addr" => {
457487
let [op] = check_arg_count(args)?;
458488
let (op, op_len) = this.operand_to_simd(op)?;
@@ -635,34 +665,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
635665
}
636666
}
637667
}
638-
"bitmask" => {
639-
let [op] = check_arg_count(args)?;
640-
let (op, op_len) = this.operand_to_simd(op)?;
641-
let bitmask_len = op_len.max(8);
642-
643-
// Returns either an unsigned integer or array of `u8`.
644-
assert!(
645-
dest.layout.ty.is_integral()
646-
|| matches!(dest.layout.ty.kind(), ty::Array(elemty, _) if elemty == &this.tcx.types.u8)
647-
);
648-
assert!(bitmask_len <= 64);
649-
assert_eq!(bitmask_len, dest.layout.size.bits());
650-
let op_len = u32::try_from(op_len).unwrap();
651-
652-
let mut res = 0u64;
653-
for i in 0..op_len {
654-
let op = this.read_immediate(&this.project_index(&op, i.into())?)?;
655-
if simd_element_to_bool(op)? {
656-
res |= 1u64
657-
.checked_shl(simd_bitmask_index(i, op_len, this.data_layout().endian))
658-
.unwrap();
659-
}
660-
}
661-
// We have to force the place type to be an int so that we can write `res` into it.
662-
let mut dest = this.force_allocation(dest)?;
663-
dest.layout = this.machine.layouts.uint(dest.layout.size).unwrap();
664-
this.write_int(res, &dest)?;
665-
}
666668

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

Diff for: src/tools/miri/tests/pass/portable-simd.rs

+49-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
#![allow(incomplete_features, internal_features)]
44
use std::simd::{prelude::*, StdFloat};
55

6-
extern "platform-intrinsic" {
7-
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;
8-
}
9-
106
fn simd_ops_f32() {
117
let a = f32x4::splat(10.0);
128
let b = f32x4::from_array([1.0, 2.0, 3.0, -4.0]);
@@ -218,6 +214,11 @@ fn simd_ops_i32() {
218214
}
219215

220216
fn simd_mask() {
217+
extern "platform-intrinsic" {
218+
pub(crate) fn simd_bitmask<T, U>(x: T) -> U;
219+
pub(crate) fn simd_select_bitmask<M, T>(m: M, yes: T, no: T) -> T;
220+
}
221+
221222
let intmask = Mask::from_int(i32x4::from_array([0, -1, 0, 0]));
222223
assert_eq!(intmask, Mask::from_array([false, true, false, false]));
223224
assert_eq!(intmask.to_array(), [false, true, false, false]);
@@ -266,7 +267,16 @@ fn simd_mask() {
266267
}
267268
}
268269

269-
// This used to cause an ICE.
270+
// This used to cause an ICE. It exercises simd_select_bitmask with an array as input.
271+
if cfg!(target_endian = "little") {
272+
// FIXME this test currently fails on big-endian:
273+
// <https://github.com/rust-lang/portable-simd/issues/379>
274+
let bitmask = u8x4::from_array([0b00001101, 0, 0, 0]);
275+
assert_eq!(
276+
mask32x4::from_bitmask_vector(bitmask),
277+
mask32x4::from_array([true, false, true, true]),
278+
);
279+
}
270280
let bitmask = u8x8::from_array([0b01000101, 0, 0, 0, 0, 0, 0, 0]);
271281
assert_eq!(
272282
mask32x8::from_bitmask_vector(bitmask),
@@ -281,6 +291,40 @@ fn simd_mask() {
281291
true, true, true,
282292
]),
283293
);
294+
295+
// Also directly call simd_select_bitmask, to test both kinds of argument types.
296+
unsafe {
297+
// These masks are exactly the results we got out above in the `simd_bitmask` tests.
298+
let selected1 = simd_select_bitmask::<u16, _>(
299+
if cfg!(target_endian = "little") { 0b1010001101001001 } else { 0b1001001011000101 },
300+
i32x16::splat(1), // yes
301+
i32x16::splat(0), // no
302+
);
303+
let selected2 = simd_select_bitmask::<[u8; 2], _>(
304+
if cfg!(target_endian = "little") {
305+
[0b01001001, 0b10100011]
306+
} else {
307+
[0b10010010, 0b11000101]
308+
},
309+
i32x16::splat(1), // yes
310+
i32x16::splat(0), // no
311+
);
312+
assert_eq!(selected1, i32x16::from_array([1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1]));
313+
assert_eq!(selected2, selected1);
314+
// Also try masks less than a byte long.
315+
let selected1 = simd_select_bitmask::<u8, _>(
316+
if cfg!(target_endian = "little") { 0b1000 } else { 0b0001 },
317+
i32x4::splat(1), // yes
318+
i32x4::splat(0), // no
319+
);
320+
let selected2 = simd_select_bitmask::<[u8; 1], _>(
321+
if cfg!(target_endian = "little") { [0b1000] } else { [0b0001] },
322+
i32x4::splat(1), // yes
323+
i32x4::splat(0), // no
324+
);
325+
assert_eq!(selected1, i32x4::from_array([0, 0, 0, 1]));
326+
assert_eq!(selected2, selected1);
327+
}
284328
}
285329

286330
fn simd_cast() {

0 commit comments

Comments
 (0)