Skip to content

Commit 73cf2ba

Browse files
committed
Auto merge of #1923 - RalfJung:more-simd, r=RalfJung
More portable SIMD: rem, shl, shr Also make sure we catch the potential UB in div, rem, shl, shr.
2 parents 4f0faed + 4414d96 commit 73cf2ba

File tree

8 files changed

+87
-3
lines changed

8 files changed

+87
-3
lines changed

src/shims/intrinsics.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
306306
}
307307

308308
// SIMD operations
309-
"simd_add" | "simd_sub" | "simd_mul" | "simd_div" => {
309+
#[rustfmt::skip]
310+
| "simd_add"
311+
| "simd_sub"
312+
| "simd_mul"
313+
| "simd_div"
314+
| "simd_rem"
315+
| "simd_shl"
316+
| "simd_shr" => {
310317
let &[ref left, ref right] = check_arg_count(args)?;
311318
let (left, left_len) = this.operand_to_simd(left)?;
312319
let (right, right_len) = this.operand_to_simd(right)?;
@@ -320,14 +327,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
320327
"simd_sub" => mir::BinOp::Sub,
321328
"simd_mul" => mir::BinOp::Mul,
322329
"simd_div" => mir::BinOp::Div,
330+
"simd_rem" => mir::BinOp::Rem,
331+
"simd_shl" => mir::BinOp::Shl,
332+
"simd_shr" => mir::BinOp::Shr,
323333
_ => unreachable!(),
324334
};
325335

326336
for i in 0..dest_len {
327337
let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
328338
let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
329-
let dest = this.mplace_index(&dest, i)?.into();
330-
this.binop_ignore_overflow(op, &left, &right, &dest)?;
339+
let dest = this.mplace_index(&dest, i)?;
340+
let (val, overflowed, ty) = this.overflowing_binary_op(op, &left, &right)?;
341+
assert_eq!(ty, dest.layout.ty);
342+
if matches!(op, mir::BinOp::Shl | mir::BinOp::Shr) {
343+
// Shifts have extra UB as SIMD operations that the MIR binop does not have.
344+
// See <https://github.com/rust-lang/rust/issues/91237>.
345+
if overflowed {
346+
let r_val = right.to_scalar()?.to_bits(right.layout.size)?;
347+
throw_ub_format!("overflowing shift by {} in `{}` in SIMD lane {}", r_val, intrinsic_name, i);
348+
}
349+
}
350+
this.write_scalar(val, &dest.into())?;
331351
}
332352
}
333353

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
pub(crate) fn simd_div<T>(x: T, y: T) -> T;
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(1, 1);
13+
let y = i32x2(1, 0);
14+
simd_div(x, y); //~ERROR Undefined Behavior: dividing by zero
15+
} }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
pub(crate) fn simd_rem<T>(x: T, y: T) -> T;
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(1, 1);
13+
let y = i32x2(1, 0);
14+
simd_rem(x, y); //~ERROR Undefined Behavior: calculating the remainder with a divisor of zero
15+
} }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
pub(crate) fn simd_shl<T>(x: T, y: T) -> T;
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(1, 1);
13+
let y = i32x2(100, 0);
14+
simd_shl(x, y); //~ERROR overflowing shift by 100 in `simd_shl` in SIMD lane 0
15+
} }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
pub(crate) fn simd_shr<T>(x: T, y: T) -> T;
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(1, 1);
13+
let y = i32x2(20, 40);
14+
simd_shr(x, y); //~ERROR overflowing shift by 40 in `simd_shr` in SIMD lane 1
15+
} }

tests/run-pass/portable-simd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn simd_ops_f32() {
99
assert_eq!(a * b, f32x4::from_array([10.0, 20.0, 30.0, 40.0]));
1010
assert_eq!(b / a, f32x4::from_array([0.1, 0.2, 0.3, 0.4]));
1111
assert_eq!(a / 2.0, f32x4::splat(5.0));
12+
assert_eq!(a % b, f32x4::from_array([0.0, 0.0, 1.0, 2.0]));
1213
}
1314

1415
fn simd_ops_i32() {
@@ -19,6 +20,9 @@ fn simd_ops_i32() {
1920
assert_eq!(a * b, i32x4::from_array([10, 20, 30, 40]));
2021
assert_eq!(a / b, i32x4::from_array([10, 5, 3, 2]));
2122
assert_eq!(a / 2, i32x4::splat(5));
23+
assert_eq!(a % b, i32x4::from_array([0, 0, 1, 2]));
24+
assert_eq!(b << 2, i32x4::from_array([4, 8, 12, 16]));
25+
assert_eq!(b >> 1, i32x4::from_array([0, 1, 1, 2]));
2226
}
2327

2428
fn main() {

0 commit comments

Comments
 (0)