Skip to content

Commit

Permalink
cranelift-interpreter: Fix panic when bitcasting SIMD values
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-justin committed May 31, 2023
1 parent c044790 commit 34b609f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
33 changes: 33 additions & 0 deletions cranelift/filetests/filetests/runtests/simd-bitcast.clif
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,36 @@ block0(v0: f64x2):
}
; run: %bitcast_fi64x2([0x0.0 -NaN:0x7ffffffffffff]) == [0 18446744073709551615]
; run: %bitcast_fi64x2([-NaN:0x7ffffffffffff 0x0.000000000007fp-1022]) == [-1 127]

function %bitcast_more_lanes_little(i64x2) -> i32x4 {
block0(v0: i64x2):
v1 = bitcast.i32x4 little v0
return v1
}
; run: %bitcast_more_lanes_little(0x00000000000000000000000000000000) == 0x00000000000000000000000000000000
; run: %bitcast_more_lanes_little(0x00000000000000ff00000000000000ff) == 0x00000000000000ff00000000000000ff

function %bitcast_fewer_lanes_little(i16x8) -> i64x2 {
block0(v0: i16x8):
v1 = bitcast.i64x2 little v0
return v1
}
; run: %bitcast_fewer_lanes_little(0x00000000000000000000000000000000) == 0x00000000000000000000000000000000
; run: %bitcast_fewer_lanes_little(0x00ff0000000000000000000000000000) == 0x00ff0000000000000000000000000000

function %bitcast_more_lanes_big(i64x2) -> i32x4 {
block0(v0: i64x2):
v1 = bitcast.i32x4 big v0
return v1
}
; run: %bitcast_more_lanes_big(0x00000000000000ff00000000000000ff) == 0x00000000000000ff00000000000000ff
; run: %bitcast_more_lanes_big(0x00000000000000000000000000000000) == 0x00000000000000000000000000000000

function %bitcast_fewer_lanes_big(i16x8) -> i64x2 {
block0(v0: i16x8):
v1 = bitcast.i64x2 big v0
return v1
}
; run: %bitcast_fewer_lanes_big(0x00000000000000000000000000000000) == 0x00000000000000000000000000000000
; run: %bitcast_fewer_lanes_big(0x00ff00ff000000ff00000000000000ff) == 0x00ff00ff000000ff00000000000000ff
; run: %bitcast_fewer_lanes_big(0x00ff00ff000000000000000000000000) == 0x00ff00ff000000000000000000000000
13 changes: 8 additions & 5 deletions cranelift/interpreter/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,11 +904,14 @@ where
Opcode::IsInvalid => unimplemented!("IsInvalid"),
Opcode::Bitcast | Opcode::ScalarToVector => {
let input_ty = inst_context.type_of(inst_context.args()[0]).unwrap();
let arg0 = extractlanes(&arg(0), input_ty)?;
let lanes = &arg0
.into_iter()
.map(|x| DataValue::convert(x, ValueConversionKind::Exact(ctrl_ty.lane_type())))
.collect::<ValueResult<SimdVec<DataValue>>>()?;
let lanes = &if input_ty.is_vector() {
extractlanes(&arg(0), ctrl_ty)?
} else {
extractlanes(&arg(0), input_ty)?
.into_iter()
.map(|x| DataValue::convert(x, ValueConversionKind::Exact(ctrl_ty.lane_type())))
.collect::<ValueResult<SimdVec<DataValue>>>()?
};
assign(match inst.opcode() {
Opcode::Bitcast => vectorizelanes(lanes, ctrl_ty)?,
Opcode::ScalarToVector => vectorizelanes_all(lanes, ctrl_ty)?,
Expand Down

0 comments on commit 34b609f

Please sign in to comment.