Skip to content

Commit d03b4a0

Browse files
committed
Auto merge of #1978 - RalfJung:simd_eq, r=RalfJung
implement simd_eq and simd_reduce_any This lets us re-enable the division and modulo tests, since those operations now internally use simd_eq and simd_reduce_any. However, I am not sure what exactly the rules are for simd_reduce_any. `@workingjubilee` for now I made it UB to call those with inputs that are not all-0 or all-1, but that might be taking it too far?
2 parents 2b0078e + 1ac1e55 commit d03b4a0

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
78450d2d602b06d9b94349aaf8cece1a4acaf3a8
1+
b8967b0d52a2ba5f0c9da0da03e78ccba5534e4a

src/shims/intrinsics.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
324324
| "simd_shl"
325325
| "simd_shr"
326326
| "simd_and"
327-
| "simd_or" => {
327+
| "simd_or"
328+
| "simd_eq" => {
328329
let &[ref left, ref right] = check_arg_count(args)?;
329330
let (left, left_len) = this.operand_to_simd(left)?;
330331
let (right, right_len) = this.operand_to_simd(right)?;
@@ -343,6 +344,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
343344
"simd_shr" => mir::BinOp::Shr,
344345
"simd_and" => mir::BinOp::BitAnd,
345346
"simd_or" => mir::BinOp::BitOr,
347+
"simd_eq" => mir::BinOp::Eq,
346348
_ => unreachable!(),
347349
};
348350

@@ -351,7 +353,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
351353
let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
352354
let dest = this.mplace_index(&dest, i)?;
353355
let (val, overflowed, ty) = this.overflowing_binary_op(op, &left, &right)?;
354-
assert_eq!(ty, dest.layout.ty);
355356
if matches!(op, mir::BinOp::Shl | mir::BinOp::Shr) {
356357
// Shifts have extra UB as SIMD operations that the MIR binop does not have.
357358
// See <https://github.com/rust-lang/rust/issues/91237>.
@@ -360,8 +361,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
360361
throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
361362
}
362363
}
363-
this.write_scalar(val, &dest.into())?;
364+
if matches!(op, mir::BinOp::Eq) {
365+
// Special handling for boolean-returning operations
366+
assert_eq!(ty, this.tcx.types.bool);
367+
let val = val.to_bool().unwrap();
368+
let val = if val { -1 } else { 0 }; // SIMD uses all-1 as pattern for "true"
369+
let val = Scalar::from_int(val, dest.layout.size);
370+
this.write_scalar(val, &dest.into())?;
371+
} else {
372+
assert_eq!(ty, dest.layout.ty);
373+
this.write_scalar(val, &dest.into())?;
374+
}
375+
}
376+
}
377+
"simd_reduce_any" => {
378+
let &[ref arg] = check_arg_count(args)?;
379+
let (arg, arg_len) = this.operand_to_simd(arg)?;
380+
381+
let mut res = false; // the neutral element
382+
for i in 0..arg_len {
383+
let op = this.read_immediate(&this.mplace_index(&arg, i)?.into())?;
384+
// We convert it to a *signed* integer and expect either 0 or -1 (the latter means all bits were set).
385+
let val = op.to_scalar()?.to_int(op.layout.size)?;
386+
let val = match val {
387+
0 => false,
388+
-1 => true,
389+
_ =>
390+
throw_ub_format!(
391+
"each element of a simd_reduce_any operand must be all-0-bits or all-1-bits"
392+
),
393+
};
394+
res = res | val;
364395
}
396+
397+
this.write_scalar(Scalar::from_bool(res), dest)?;
365398
}
366399

367400
// Atomic operations
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
pub(crate) fn simd_reduce_any<T>(x: T) -> bool;
5+
}
6+
7+
#[repr(simd)]
8+
#[allow(non_camel_case_types)]
9+
struct i32x2(i32, i32);
10+
11+
fn main() { unsafe {
12+
let x = i32x2(0, 1);
13+
simd_reduce_any(x); //~ERROR must be all-0-bits or all-1-bits
14+
} }

tests/run-pass/portable-simd.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(portable_simd)]
1+
#![feature(portable_simd, platform_intrinsics)]
22
use std::simd::*;
33

44
fn simd_ops_f32() {
@@ -18,16 +18,35 @@ fn simd_ops_i32() {
1818
assert_eq!(a + b, i32x4::from_array([11, 12, 13, 14]));
1919
assert_eq!(a - b, i32x4::from_array([9, 8, 7, 6]));
2020
assert_eq!(a * b, i32x4::from_array([10, 20, 30, 40]));
21-
//assert_eq!(a / b, i32x4::from_array([10, 5, 3, 2]));
22-
//assert_eq!(a / i32x4::splat(2), i32x4::splat(5));
23-
//assert_eq!(a % b, i32x4::from_array([0, 0, 1, 2]));
21+
assert_eq!(a / b, i32x4::from_array([10, 5, 3, 2]));
22+
assert_eq!(a / i32x4::splat(2), i32x4::splat(5));
23+
assert_eq!(a % b, i32x4::from_array([0, 0, 1, 2]));
2424
assert_eq!(b << i32x4::splat(2), i32x4::from_array([4, 8, 12, 16]));
2525
assert_eq!(b >> i32x4::splat(1), i32x4::from_array([0, 1, 1, 2]));
2626
assert_eq!(b & i32x4::splat(2), i32x4::from_array([0, 2, 2, 0]));
2727
assert_eq!(b | i32x4::splat(2), i32x4::from_array([3, 2, 3, 6]));
2828
}
2929

30+
fn simd_intrinsics() {
31+
extern "platform-intrinsic" {
32+
pub(crate) fn simd_eq<T, U>(x: T, y: T) -> U;
33+
pub(crate) fn simd_reduce_any<T>(x: T) -> bool;
34+
}
35+
36+
// Make sure simd_eq returns all-1 for `true`
37+
let a = i32x4::splat(10);
38+
let b = i32x4::from_array([1, 2, 10, 4]);
39+
let c: i32x4 = unsafe { simd_eq(a, b) };
40+
assert_eq!(c, i32x4::from_array([0, 0, -1, 0]));
41+
42+
unsafe {
43+
assert!(!simd_reduce_any(i32x4::splat(0)));
44+
assert!(simd_reduce_any(i32x4::splat(-1)));
45+
}
46+
}
47+
3048
fn main() {
3149
simd_ops_f32();
3250
simd_ops_i32();
51+
simd_intrinsics();
3352
}

0 commit comments

Comments
 (0)