A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
x64: Lower fcvt_to_{u,s}int{,_sat} to i8 and i16 (#14778)
Ilyas Mallah2d9c19b0fe7f
diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isleindex 453cbe654d..7940f326ab 100644--- a/cranelift/codegen/src/isa/x64/lower.isle+++ b/cranelift/codegen/src/isa/x64/lower.isle@@ -3889,6 +3889,62 @@(rule (lower (fcvt_to_sint_sat (fits_in_64 out_ty) val @ (value_type (ty_scalar_float _))))(cvt_float_to_sint_seq out_ty val true))+;; `i8` and `i16` results reuse the `i32` conversion and then clamp+;; (saturating) or range check (trapping) its result.+(rule 2 (lower (fcvt_to_uint (ty_8_or_16 out_ty) val @ (value_type (ty_scalar_float _))))+ (trap_if_outside_i32 (cvt_float_to_sint_seq $I32 val false)+ (narrow_int_min out_ty false)+ (narrow_int_max out_ty false)))++(rule 2 (lower (fcvt_to_uint_sat (ty_8_or_16 out_ty) val @ (value_type (ty_scalar_float _))))+ (clamp_i32 (cvt_float_to_sint_seq $I32 val true)+ (narrow_int_min out_ty false)+ (narrow_int_max out_ty false)))++(rule 2 (lower (fcvt_to_sint (ty_8_or_16 out_ty) val @ (value_type (ty_scalar_float _))))+ (trap_if_outside_i32 (cvt_float_to_sint_seq $I32 val false)+ (narrow_int_min out_ty true)+ (narrow_int_max out_ty true)))++(rule 2 (lower (fcvt_to_sint_sat (ty_8_or_16 out_ty) val @ (value_type (ty_scalar_float _))))+ (clamp_i32 (cvt_float_to_sint_seq $I32 val true)+ (narrow_int_min out_ty true)+ (narrow_int_max out_ty true)))++;; Range of an `i8` or `i16` result as `i32` immediates. The `bool` is whether+;; the result is signed.+(decl narrow_int_min (Type bool) u32)+(rule (narrow_int_min $I8 true) 0xffffff80)+(rule (narrow_int_min $I16 true) 0xffff8000)+(rule (narrow_int_min _ false) 0)++(decl narrow_int_max (Type bool) u32)+(rule (narrow_int_max $I8 true) 0x7f)+(rule (narrow_int_max $I16 true) 0x7fff)+(rule (narrow_int_max $I8 false) 0xff)+(rule (narrow_int_max $I16 false) 0xffff)++;; Clamps the signed `i32` in `x` to `[min, max]`.+(decl clamp_i32 (Gpr u32 u32) Gpr)+(rule (clamp_i32 x min max)+ (let ((max_reg Gpr (imm $I32 (u32_into_u64 max)))+ (x Gpr (with_flags_reg (x64_cmp $I32 x (RegMemImm.Imm max))+ (cmove $I32 (CC.NLE) max_reg x)))+ (min_reg Gpr (imm $I32 (u32_into_u64 min))))+ (with_flags_reg (x64_cmp $I32 x (RegMemImm.Imm min))+ (cmove $I32 (CC.L) min_reg x))))++;; Traps with `int_ovf` unless the signed `i32` in `x` is in `[min, max]`.+(decl trap_if_outside_i32 (Gpr u32 u32) Gpr)+(rule (trap_if_outside_i32 x min max)+ (let ((_ InstOutput (side_effect (with_flags_side_effect+ (x64_cmp $I32 x (RegMemImm.Imm max))+ (trap_if (CC.NLE) (TrapCode.INTEGER_OVERFLOW)))))+ (_ InstOutput (side_effect (with_flags_side_effect+ (x64_cmp $I32 x (RegMemImm.Imm min))+ (trap_if (CC.L) (TrapCode.INTEGER_OVERFLOW))))))+ x))+;; The x64 backend currently only supports these two type combinations.(rule 1 (lower (fcvt_to_sint_sat $I32X4 val @ (value_type $F32X4)))(let ((src Xmm val)diff --git a/cranelift/filetests/filetests/isa/x64/fcvt-small.clif b/cranelift/filetests/filetests/isa/x64/fcvt-small.clifnew file mode 100644index 0000000000..d49cea1d4d--- /dev/null+++ b/cranelift/filetests/filetests/isa/x64/fcvt-small.clif@@ -0,0 +1,390 @@+test compile precise-output+set unwind_info=false+target x86_64++function %fcvt_to_uint_i8_f32(f32) -> i8 {+block0(v0: f32):+ v1 = fcvt_to_uint.i8 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float32_to_sint32_seq %xmm0, %eax, %rsi, %xmm3+; cmpl $0xff, %eax+; jnle #trap=int_ovf+; cmpl $0x0, %eax+; jl #trap=int_ovf+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttss2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x39+; ucomiss %xmm0, %xmm0+; jp 0x52+; movl $0xcf000000, %esi+; movd %esi, %xmm3+; ucomiss %xmm3, %xmm0+; jb 0x54+; xorpd %xmm3, %xmm3+; ucomiss %xmm0, %xmm3+; jb 0x56+; cmpl $0xff, %eax+; jg 0x58+; cmpl $0, %eax+; jl 0x5a+; movq %rbp, %rsp+; popq %rbp+; retq+; ud2 ; trap: bad_toint+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf++function %fcvt_to_sint_i8_f32(f32) -> i8 {+block0(v0: f32):+ v1 = fcvt_to_sint.i8 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float32_to_sint32_seq %xmm0, %eax, %rsi, %xmm3+; cmpl $0x7f, %eax+; jnle #trap=int_ovf+; cmpl $0xffffff80, %eax+; jl #trap=int_ovf+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttss2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x39+; ucomiss %xmm0, %xmm0+; jp 0x50+; movl $0xcf000000, %esi+; movd %esi, %xmm3+; ucomiss %xmm3, %xmm0+; jb 0x52+; xorpd %xmm3, %xmm3+; ucomiss %xmm0, %xmm3+; jb 0x54+; cmpl $0x7f, %eax+; jg 0x56+; cmpl $-0x80, %eax+; jl 0x58+; movq %rbp, %rsp+; popq %rbp+; retq+; ud2 ; trap: bad_toint+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf++function %fcvt_to_uint_sat_i8_f32(f32) -> i8 {+block0(v0: f32):+ v1 = fcvt_to_uint_sat.i8 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float32_to_sint32_sat_seq %xmm0, %eax, %rsi, %xmm3+; movl $0xff, %esi+; cmpl $0xff, %eax+; cmovgl %esi, %eax+; uninit %rcx+; xorl %ecx, %ecx+; cmpl $0x0, %eax+; cmovll %ecx, %eax+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttss2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x33+; ucomiss %xmm0, %xmm0+; jnp 0x21+; xorl %eax, %eax+; jmp 0x33+; xorpd %xmm3, %xmm3+; ucomiss %xmm0, %xmm3+; jae 0x33+; movl $0x7fffffff, %eax+; movl $0xff, %esi+; cmpl $0xff, %eax+; cmovgl %esi, %eax+; xorl %ecx, %ecx+; cmpl $0, %eax+; cmovll %ecx, %eax+; movq %rbp, %rsp+; popq %rbp+; retq++function %fcvt_to_sint_sat_i8_f32(f32) -> i8 {+block0(v0: f32):+ v1 = fcvt_to_sint_sat.i8 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float32_to_sint32_sat_seq %xmm0, %eax, %rsi, %xmm3+; movl $0x7f, %esi+; cmpl $0x7f, %eax+; cmovgl %esi, %eax+; movl $0xffffff80, %r11d+; cmpl $0xffffff80, %eax+; cmovll %r11d, %eax+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttss2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x33+; ucomiss %xmm0, %xmm0+; jnp 0x21+; xorl %eax, %eax+; jmp 0x33+; xorpd %xmm3, %xmm3+; ucomiss %xmm0, %xmm3+; jae 0x33+; movl $0x7fffffff, %eax+; movl $0x7f, %esi+; cmpl $0x7f, %eax+; cmovgl %esi, %eax+; movl $0xffffff80, %r11d+; cmpl $-0x80, %eax+; cmovll %r11d, %eax+; movq %rbp, %rsp+; popq %rbp+; retq++function %fcvt_to_uint_i16_f64(f64) -> i16 {+block0(v0: f64):+ v1 = fcvt_to_uint.i16 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float64_to_sint32_seq %xmm0, %eax, %rsi, %xmm3+; cmpl $0xffff, %eax+; jnle #trap=int_ovf+; cmpl $0x0, %eax+; jl #trap=int_ovf+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttsd2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x42+; ucomisd %xmm0, %xmm0+; jp 0x5b+; movabsq $13970166044105375744, %rsi+; movq %rsi, %xmm3+; ucomisd %xmm3, %xmm0+; jbe 0x5d+; xorpd %xmm3, %xmm3+; ucomisd %xmm0, %xmm3+; jb 0x5f+; cmpl $0xffff, %eax+; jg 0x61+; cmpl $0, %eax+; jl 0x63+; movq %rbp, %rsp+; popq %rbp+; retq+; ud2 ; trap: bad_toint+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf++function %fcvt_to_sint_i16_f64(f64) -> i16 {+block0(v0: f64):+ v1 = fcvt_to_sint.i16 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float64_to_sint32_seq %xmm0, %eax, %rsi, %xmm3+; cmpl $0x7fff, %eax+; jnle #trap=int_ovf+; cmpl $0xffff8000, %eax+; jl #trap=int_ovf+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttsd2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x42+; ucomisd %xmm0, %xmm0+; jp 0x5d+; movabsq $13970166044105375744, %rsi+; movq %rsi, %xmm3+; ucomisd %xmm3, %xmm0+; jbe 0x5f+; xorpd %xmm3, %xmm3+; ucomisd %xmm0, %xmm3+; jb 0x61+; cmpl $0x7fff, %eax+; jg 0x63+; cmpl $0xffff8000, %eax+; jl 0x65+; movq %rbp, %rsp+; popq %rbp+; retq+; ud2 ; trap: bad_toint+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf+; ud2 ; trap: int_ovf++function %fcvt_to_uint_sat_i16_f64(f64) -> i16 {+block0(v0: f64):+ v1 = fcvt_to_uint_sat.i16 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float64_to_sint32_sat_seq %xmm0, %eax, %rsi, %xmm3+; movl $0xffff, %esi+; cmpl $0xffff, %eax+; cmovgl %esi, %eax+; uninit %rcx+; xorl %ecx, %ecx+; cmpl $0x0, %eax+; cmovll %ecx, %eax+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttsd2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x35+; ucomisd %xmm0, %xmm0+; jnp 0x22+; xorl %eax, %eax+; jmp 0x35+; xorpd %xmm3, %xmm3+; ucomisd %xmm0, %xmm3+; jae 0x35+; movl $0x7fffffff, %eax+; movl $0xffff, %esi+; cmpl $0xffff, %eax+; cmovgl %esi, %eax+; xorl %ecx, %ecx+; cmpl $0, %eax+; cmovll %ecx, %eax+; movq %rbp, %rsp+; popq %rbp+; retq++function %fcvt_to_sint_sat_i16_f64(f64) -> i16 {+block0(v0: f64):+ v1 = fcvt_to_sint_sat.i16 v0+ return v1+}++; VCode:+; pushq %rbp+; movq %rsp, %rbp+; block0:+; cvt_float64_to_sint32_sat_seq %xmm0, %eax, %rsi, %xmm3+; movl $0x7fff, %esi+; cmpl $0x7fff, %eax+; cmovgl %esi, %eax+; movl $0xffff8000, %r11d+; cmpl $0xffff8000, %eax+; cmovll %r11d, %eax+; movq %rbp, %rsp+; popq %rbp+; retq+;+; Disassembled:+; block0: ; offset 0x0+; pushq %rbp+; movq %rsp, %rbp+; block1: ; offset 0x4+; cvttsd2si %xmm0, %eax+; cmpl $1, %eax+; jno 0x35+; ucomisd %xmm0, %xmm0+; jnp 0x22+; xorl %eax, %eax+; jmp 0x35+; xorpd %xmm3, %xmm3+; ucomisd %xmm0, %xmm3+; jae 0x35+; movl $0x7fffffff, %eax+; movl $0x7fff, %esi+; cmpl $0x7fff, %eax+; cmovgl %esi, %eax+; movl $0xffff8000, %r11d+; cmpl $0xffff8000, %eax+; cmovll %r11d, %eax+; movq %rbp, %rsp+; popq %rbp+; retq+diff --git a/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif b/cranelift/filetests/filetests/runtests/conversion-i8-i16.clifindex dde4635be5..a511ea6f8f 100644--- a/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif+++ b/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif@@ -2,6 +2,8 @@ test interprettest runtarget aarch64target s390x+target x86_64+target x86_64 has_avxtarget riscv64 has_c has_zcbtarget riscv64@@ -17,6 +19,7 @@ block0(v0: f32):; run: %f32_to_i8(-0x80.1) == 0x80; run: %f32_to_i8(0x7f.1) == 0x7f; run: %f32_to_i8(-0x0.1) == 0+; run: %f32_to_i8(-0x80.f) == 0x80function %f64_to_i8(f64) -> i8 {block0(v0: f64):@@ -56,6 +59,7 @@ block0(v0: f64):; run: %f64_to_i16(-0x8000.1) == 0x8000; run: %f64_to_i16(0x7fff.1) == 0x7fff; run: %f64_to_i16(-0x0.1) == 0+; run: %f64_to_i16(0x7fff.f) == 0x7ffffunction %f32_to_u8(f32) -> i8 {block0(v0:f32):@@ -67,6 +71,7 @@ block0(v0:f32):; run: %f32_to_u8(0x4.2) == 4; run: %f32_to_u8(0x4.6) == 4; run: %f32_to_u8(-0x0.1) == 0+; run: %f32_to_u8(0xff.f) == 0xfffunction %f64_to_u8(f64) -> i8 {block0(v0:f64):@@ -78,6 +83,7 @@ block0(v0:f64):; run: %f64_to_u8(0x4.2) == 4; run: %f64_to_u8(0x4.6) == 4; run: %f64_to_u8(-0x0.1) == 0+; run: %f64_to_u8(0xff.f) == 0xfffunction %f32_to_u16(f32) -> i16 {block0(v0:f32):@@ -89,6 +95,7 @@ block0(v0:f32):; run: %f32_to_u16(0x4.2) == 4; run: %f32_to_u16(0x4.6) == 4; run: %f32_to_u16(-0x0.1) == 0+; run: %f32_to_u16(0xffff.f) == 0xfffffunction %f64_to_u16(f64) -> i16 {block0(v0:f64):@@ -100,6 +107,7 @@ block0(v0:f64):; run: %f64_to_u16(0x4.2) == 4; run: %f64_to_u16(0x4.6) == 4; run: %f64_to_u16(-0x0.1) == 0+; run: %f64_to_u16(0xffff.f) == 0xfffffunction %f32_to_i8_sat(f32) -> i8 {block0(v0: f32):diff --git a/cranelift/filetests/filetests/runtests/fcvt-sat-small.clif b/cranelift/filetests/filetests/runtests/fcvt-sat-small.clifindex 977336e903..9dffd9dcf7 100644--- a/cranelift/filetests/filetests/runtests/fcvt-sat-small.clif+++ b/cranelift/filetests/filetests/runtests/fcvt-sat-small.clif@@ -1,7 +1,9 @@+test interprettest runtarget aarch64target s390x-; x86_64 does not support `fcvt_to_{u,s}int_sat` to integers < 32 bits.+target x86_64+target x86_64 has_avxfunction %fcvt_to_sint_sat_i8(f32) -> i8 {block0(v0: f32):@@ -96,7 +98,7 @@ block0(v0: f64):; run: %fcvt_to_uint_sat_i8_f64(0xB2D05E00.0) == 255; run: %fcvt_to_uint_sat_i8_f64(-0xB2D05E00.0) == 0; run: %fcvt_to_uint_sat_i8_f64(0x1.fffffffffffffp1023) == 255-; run: %fcvt_to_sint_sat_i8_f64(-0x1.fffffffffffffp1023) == 0+; run: %fcvt_to_uint_sat_i8_f64(-0x1.fffffffffffffp1023) == 0; run: %fcvt_to_uint_sat_i8_f64(NaN) == 0function %fcvt_to_sint_sat_i16_f64(f64) -> i16 {diff --git a/cranelift/filetests/filetests/runtests/issue-5992.clif b/cranelift/filetests/filetests/runtests/issue-5992.clifindex a1d9e36926..ac6b7e6e51 100644--- a/cranelift/filetests/filetests/runtests/issue-5992.clif+++ b/cranelift/filetests/filetests/runtests/issue-5992.clif@@ -3,6 +3,7 @@ test runtarget riscv64target aarch64target s390x+target x86_64function %a_f32(f32) -> i16 {block0(v0: f32):diff --git a/cranelift/filetests/filetests/runtests/issue-5993.clif b/cranelift/filetests/filetests/runtests/issue-5993.clifindex b19a865b24..ddd66dd87e 100644--- a/cranelift/filetests/filetests/runtests/issue-5993.clif+++ b/cranelift/filetests/filetests/runtests/issue-5993.clif@@ -3,6 +3,7 @@ test runtarget riscv64target aarch64target s390x+target x86_64function %a(f64) -> i8 {block0(v0: f64):diff --git a/cranelift/fuzzgen/src/function_generator.rs b/cranelift/fuzzgen/src/function_generator.rsindex 543ad4cc84..fd2a498206 100644--- a/cranelift/fuzzgen/src/function_generator.rs+++ b/cranelift/fuzzgen/src/function_generator.rs@@ -497,7 +497,6 @@ fn valid_for_target(triple: &Triple, op: Opcode, args: &[Type], rets: &[Type]) -(Opcode::Cls, &[I128], &[I128]),// TODO(Opcode::Bitselect, &[_, _, _], &[F32 | F64]),- // https://github.com/bytecodealliance/wasmtime/issues/4897// https://github.com/bytecodealliance/wasmtime/issues/4899(Opcode::FcvtToUint@@ -505,7 +504,7 @@ fn valid_for_target(triple: &Triple, op: Opcode, args: &[Type], rets: &[Type]) -| Opcode::FcvtToSint| Opcode::FcvtToSintSat,&[F32 | F64],- &[I8 | I16 | I128]+ &[I128]),(Opcode::FcvtToUint | Opcode::FcvtToSint, &[F32X4], &[I32X4]),(