A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift: fail cleanly on some unsupported f16/f128 cases, and support some bitwise ops. (#14653)
Chris Fallin14e8bd6baa9d
diff --git a/cranelift/codegen/src/isa/s390x/lower.isle b/cranelift/codegen/src/isa/s390x/lower.isleindex 138f106ffd..a3d378cf80 100644--- a/cranelift/codegen/src/isa/s390x/lower.isle+++ b/cranelift/codegen/src/isa/s390x/lower.isle@@ -1586,6 +1586,12 @@(rule (lower (bitselect (vr128_ty ty) x y z))(vec_select ty y z x))+;; Bitselect float registers, using vector overlay (as for `band`/`bor`/`bxor`+;; on scalar floats). This must take priority over the `fits_in_64` rules above,+;; which use GPR instructions.+(rule 8 (lower (bitselect (ty_scalar_float _) x y z))+ (vec_select $F64X2 y z x))+;; Bitselect-not vector registers.(rule 5 (lower (bitselect (vr128_ty ty) x (bnot _ y) z))(if-let true (has_vxrs_ext3))diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isleindex 765863ba4d..dc5c3169cc 100644--- a/cranelift/codegen/src/isa/x64/lower.isle+++ b/cranelift/codegen/src/isa/x64/lower.isle@@ -381,8 +381,10 @@(decl sse_and (Type Xmm XmmMem128) Xmm)(rule (sse_and $F32X4 x y) (x64_andps x y))(rule (sse_and $F64X2 x y) (x64_andpd x y))+(rule (sse_and $F16 x y) (x64_andps x y))(rule (sse_and $F32 x y) (x64_andps x y))(rule (sse_and $F64 x y) (x64_andpd x y))+(rule (sse_and $F128 x y) (x64_andps x y))(rule -1 (sse_and (multi_lane _bits _lanes) x y) (x64_pand x y))(rule 6 (lower (band ty @ (multi_lane _bits _lanes) x y))@@ -509,8 +511,10 @@(decl sse_or (Type Xmm XmmMem128) Xmm)(rule (sse_or $F32X4 x y) (x64_orps x y))(rule (sse_or $F64X2 x y) (x64_orpd x y))+(rule (sse_or $F16 x y) (x64_orps x y))(rule (sse_or $F32 x y) (x64_orps x y))(rule (sse_or $F64 x y) (x64_orpd x y))+(rule (sse_or $F128 x y) (x64_orps x y))(rule -1 (sse_or (multi_lane _bits _lanes) x y) (x64_por x y))(rule 6 (lower (bor ty @ (multi_lane _bits _lanes) x y))@@ -4359,8 +4363,10 @@;; Helper for the `ceil`/`floor`/`nearest`/`trunc` instructions ;;;;;;;;;;;;;;;;;; Emits either a `round{ss,sd,ps,pd}` instruction, as appropriate, or generates-;; the appropriate libcall and sequence to call that.-(decl x64_round (Type Value RoundImm) Xmm)+;; the appropriate libcall and sequence to call that. Partial: there's no+;; lowering for other types (e.g. `f16` and `f128`), which are then rejected as+;; unsupported.+(decl partial x64_round (Type Value RoundImm) Xmm)(rule (x64_round $F32 a imm) (x64_round_f32 a imm))(rule (x64_round $F64 a imm) (x64_round_f64 a imm))(rule (x64_round $F32X4 a imm) (x64_round_f32x4 a imm))diff --git a/cranelift/filetests/filetests/isa/x64/f16-f128-bitops.clif b/cranelift/filetests/filetests/isa/x64/f16-f128-bitops.clifnew file mode 100644index 0000000000..4e622ab4b9--- /dev/null+++ b/cranelift/filetests/filetests/isa/x64/f16-f128-bitops.clif@@ -0,0 +1,185 @@+; Bitwise operations on `f16` and `f128` on baseline x86-64 (SSE2): they use+; the full XMM register, like the `f32`/`f64` lowerings.+test compile precise-output+target x86_64++function %band_f16(f16, f16) -> f16 {+block0(v0: f16, v1: f16):+ v2 = band v0, v1+ return v2+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; andps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; andps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq++function %bor_f16(f16, f16) -> f16 {+block0(v0: f16, v1: f16):+ v2 = bor v0, v1+ return v2+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq++function %bitselect_f16(f16, f16, f16) -> f16 {+block0(v0: f16, v1: f16, v2: f16):+ v3 = bitselect v0, v1, v2+ return v3+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; movdqa %xmm0, %xmm3+; andps %xmm1, %xmm3+; uninit %xmm7+; pcmpeqd %xmm7, %xmm7+; xorps %xmm7, %xmm0+; andps %xmm2, %xmm0+; movdqa %xmm0, %xmm1+; movdqa %xmm3, %xmm0+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; movdqa %xmm0, %xmm3+; andps %xmm1, %xmm3+; pcmpeqd %xmm7, %xmm7+; xorps %xmm7, %xmm0+; andps %xmm2, %xmm0+; movdqa %xmm0, %xmm1+; movdqa %xmm3, %xmm0+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq++function %band_f128(f128, f128) -> f128 {+block0(v0: f128, v1: f128):+ v2 = band v0, v1+ return v2+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; andps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; andps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq++function %bor_f128(f128, f128) -> f128 {+block0(v0: f128, v1: f128):+ v2 = bor v0, v1+ return v2+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq++function %bitselect_f128(f128, f128, f128) -> f128 {+block0(v0: f128, v1: f128, v2: f128):+ v3 = bitselect v0, v1, v2+ return v3+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; movdqa %xmm0, %xmm3+; andps %xmm1, %xmm3+; uninit %xmm7+; pcmpeqd %xmm7, %xmm7+; xorps %xmm7, %xmm0+; andps %xmm2, %xmm0+; movdqa %xmm0, %xmm1+; movdqa %xmm3, %xmm0+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; movdqa %xmm0, %xmm3+; andps %xmm1, %xmm3+; pcmpeqd %xmm7, %xmm7+; xorps %xmm7, %xmm0+; andps %xmm2, %xmm0+; movdqa %xmm0, %xmm1+; movdqa %xmm3, %xmm0+; orps %xmm1, %xmm0+; movq %rbp, %rsp+; popq %rbp+; retq+diff --git a/cranelift/filetests/filetests/isa/x64/f16-f128-round-unsupported.clif b/cranelift/filetests/filetests/isa/x64/f16-f128-round-unsupported.clifnew file mode 100644index 0000000000..a731eae60f--- /dev/null+++ b/cranelift/filetests/filetests/isa/x64/f16-f128-round-unsupported.clif@@ -0,0 +1,54 @@+; There's no x86-64 lowering for rounding `f16` or `f128` values (it would need+; floating-point arithmetic in those formats); compilation must fail cleanly+; with an unsupported-feature error rather than panicking.+test compile expect-fail+target x86_64+target x86_64 has_sse41 has_avx++function %ceil_f16(f16) -> f16 {+block0(v0: f16):+ v1 = ceil v0+ return v1+}++function %floor_f16(f16) -> f16 {+block0(v0: f16):+ v1 = floor v0+ return v1+}++function %trunc_f16(f16) -> f16 {+block0(v0: f16):+ v1 = trunc v0+ return v1+}++function %nearest_f16(f16) -> f16 {+block0(v0: f16):+ v1 = nearest v0+ return v1+}++function %ceil_f128(f128) -> f128 {+block0(v0: f128):+ v1 = ceil v0+ return v1+}++function %floor_f128(f128) -> f128 {+block0(v0: f128):+ v1 = floor v0+ return v1+}++function %trunc_f128(f128) -> f128 {+block0(v0: f128):+ v1 = trunc v0+ return v1+}++function %nearest_f128(f128) -> f128 {+block0(v0: f128):+ v1 = nearest v0+ return v1+}diff --git a/cranelift/filetests/filetests/runtests/f16-f128-bitops.clif b/cranelift/filetests/filetests/runtests/f16-f128-bitops.clifnew file mode 100644index 0000000000..e32c134588--- /dev/null+++ b/cranelift/filetests/filetests/runtests/f16-f128-bitops.clif@@ -0,0 +1,74 @@+; Bitwise operations on `f16` and `f128` values.+;+; Only x86-64 implements all of these for both types so far; other backends+; reject some of them as unsupported.+test interpret+test run+set enable_llvm_abi_extensions+target x86_64+target x86_64 has_avx++function %band_f16(i16, i16) -> i16 {+block0(v0: i16, v1: i16):+ v2 = bitcast.f16 v0+ v3 = bitcast.f16 v1+ v4 = band v2, v3+ v5 = bitcast.i16 v4+ return v5+}+; run: %band_f16(0xff00, 0x0ff0) == 0x0f00+; run: %band_f16(0x7fff, 0x8001) == 0x0001++function %bor_f16(i16, i16) -> i16 {+block0(v0: i16, v1: i16):+ v2 = bitcast.f16 v0+ v3 = bitcast.f16 v1+ v4 = bor v2, v3+ v5 = bitcast.i16 v4+ return v5+}+; run: %bor_f16(0xff00, 0x0ff0) == 0xfff0+; run: %bor_f16(0x7c00, 0x8001) == 0xfc01++function %bitselect_f16(i16, i16, i16) -> i16 {+block0(v0: i16, v1: i16, v2: i16):+ v3 = bitcast.f16 v0+ v4 = bitcast.f16 v1+ v5 = bitcast.f16 v2+ v6 = bitselect v3, v4, v5+ v7 = bitcast.i16 v6+ return v7+}+; run: %bitselect_f16(0xff00, 0x1234, 0xabcd) == 0x12cd+; run: %bitselect_f16(0x8000, 0x8000, 0x7fff) == 0xffff++function %band_f128(i128, i128) -> i128 {+block0(v0: i128, v1: i128):+ v2 = bitcast.f128 v0+ v3 = bitcast.f128 v1+ v4 = band v2, v3+ v5 = bitcast.i128 v4+ return v5+}+; run: %band_f128(0xffffffff_00000000_ffff0000_ff00ff00, 0x0ff00ff0_0ff00ff0_0ff00ff0_0ff00ff0) == 0x0ff00ff0_00000000_0ff00000_0f000f00++function %bor_f128(i128, i128) -> i128 {+block0(v0: i128, v1: i128):+ v2 = bitcast.f128 v0+ v3 = bitcast.f128 v1+ v4 = bor v2, v3+ v5 = bitcast.i128 v4+ return v5+}+; run: %bor_f128(0xffffffff_00000000_ffff0000_ff00ff00, 0x0ff00ff0_0ff00ff0_0ff00ff0_0ff00ff0) == 0xffffffff_0ff00ff0_ffff0ff0_fff0fff0++function %bitselect_f128(i128, i128, i128) -> i128 {+block0(v0: i128, v1: i128, v2: i128):+ v3 = bitcast.f128 v0+ v4 = bitcast.f128 v1+ v5 = bitcast.f128 v2+ v6 = bitselect v3, v4, v5+ v7 = bitcast.i128 v6+ return v7+}+; run: %bitselect_f128(0xffffffff_ffffffff_00000000_00000000, 0x11111111_22222222_33333333_44444444, 0xaaaaaaaa_bbbbbbbb_cccccccc_dddddddd) == 0x11111111_22222222_cccccccc_dddddddddiff --git a/cranelift/filetests/filetests/runtests/float-bitselect.clif b/cranelift/filetests/filetests/runtests/float-bitselect.clifnew file mode 100644index 0000000000..eebed3df0d--- /dev/null+++ b/cranelift/filetests/filetests/runtests/float-bitselect.clif@@ -0,0 +1,43 @@+; `bitselect` on scalar floats. (aarch64 and riscv64 don't support it for+; `f16` yet, so they aren't targeted here.)+test interpret+test run+target x86_64+target x86_64 has_avx+target s390x++function %bitselect_f16(i16, i16, i16) -> i16 {+block0(v0: i16, v1: i16, v2: i16):+ v3 = bitcast.f16 v0+ v4 = bitcast.f16 v1+ v5 = bitcast.f16 v2+ v6 = bitselect v3, v4, v5+ v7 = bitcast.i16 v6+ return v7+}+; run: %bitselect_f16(0xff00, 0x1234, 0xabcd) == 0x12cd+; run: %bitselect_f16(0x8000, 0x8000, 0x7fff) == 0xffff++function %bitselect_f32(i32, i32, i32) -> i32 {+block0(v0: i32, v1: i32, v2: i32):+ v3 = bitcast.f32 v0+ v4 = bitcast.f32 v1+ v5 = bitcast.f32 v2+ v6 = bitselect v3, v4, v5+ v7 = bitcast.i32 v6+ return v7+}+; run: %bitselect_f32(0xffff0000, 0x12345678, 0x9abcdef0) == 0x1234def0+; run: %bitselect_f32(0x80000000, 0x80000000, 0x7fffffff) == 0xffffffff++function %bitselect_f64(i64, i64, i64) -> i64 {+block0(v0: i64, v1: i64, v2: i64):+ v3 = bitcast.f64 v0+ v4 = bitcast.f64 v1+ v5 = bitcast.f64 v2+ v6 = bitselect v3, v4, v5+ v7 = bitcast.i64 v6+ return v7+}+; run: %bitselect_f64(0xffffffff_00000000, 0x11111111_22222222, 0x33333333_44444444) == 0x11111111_44444444+; run: %bitselect_f64(0x80000000_00000000, 0x80000000_00000000, 0x7fffffff_ffffffff) == 0xffffffff_ffffffffdiff --git a/cranelift/interpreter/src/value.rs b/cranelift/interpreter/src/value.rsindex dee0da96f3..c3c182f84d 100644--- a/cranelift/interpreter/src/value.rs+++ b/cranelift/interpreter/src/value.rs@@ -233,8 +233,10 @@ macro_rules! bitop {(DataValue::I32(a), DataValue::I32(b)) => DataValue::I32(a $op b),(DataValue::I64(a), DataValue::I64(b)) => DataValue::I64(a $op b),(DataValue::I128(a), DataValue::I128(b)) => DataValue::I128(a $op b),+ (DataValue::F16(a), DataValue::F16(b)) => DataValue::F16(a $op b),(DataValue::F32(a), DataValue::F32(b)) => DataValue::F32(a $op b),(DataValue::F64(a), DataValue::F64(b)) => DataValue::F64(a $op b),+ (DataValue::F128(a), DataValue::F128(b)) => DataValue::F128(a $op b),(DataValue::V64(a), DataValue::V64(b)) => {let mut a2 = a.clone();for (a, b) in a2.iter_mut().zip(b.iter()) {@@ -843,8 +845,10 @@ impl DataValueExt for DataValue {DataValue::I32(a) => DataValue::I32(!a),DataValue::I64(a) => DataValue::I64(!a),DataValue::I128(a) => DataValue::I128(!a),+ DataValue::F16(a) => DataValue::F16(!a),DataValue::F32(a) => DataValue::F32(!a),DataValue::F64(a) => DataValue::F64(!a),+ DataValue::F128(a) => DataValue::F128(!a),DataValue::V64(mut a) => {for byte in a.iter_mut() {*byte = !*byte;