Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: fail cleanly on fpromote/fdemote with f16 or f128 types (#14883)

Ilyas Mallahf4b99898eb70
diff --git a/cranelift/codegen/src/isa/aarch64/lower.isle b/cranelift/codegen/src/isa/aarch64/lower.isle
index bf3d88bb5f..a44f4e6ceb 100644
--- a/cranelift/codegen/src/isa/aarch64/lower.isle
+++ b/cranelift/codegen/src/isa/aarch64/lower.isle
@@ -596,12 +596,12 @@
;;;; Rules for `fpromote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(rule (lower (fpromote $F64 x))
+(rule (lower (fpromote $F64 x @ (value_type $F32)))
(fpu_rr (FPUOp1.Cvt32To64) x (ScalarSize.Size32)))
;;;; Rules for `fdemote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(rule (lower (fdemote $F32 x))
+(rule (lower (fdemote $F32 x @ (value_type $F64)))
(fpu_rr (FPUOp1.Cvt64To32) x (ScalarSize.Size64)))
;;;; Rules for `ceil` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/cranelift/codegen/src/isa/s390x/lower.isle b/cranelift/codegen/src/isa/s390x/lower.isle
index 41c06aa63d..6ce4894aad 100644
--- a/cranelift/codegen/src/isa/s390x/lower.isle
+++ b/cranelift/codegen/src/isa/s390x/lower.isle
@@ -1996,9 +1996,12 @@
;;;; Rules for `fpromote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Promote a register.
-(rule (lower (fpromote dst_ty x @ (value_type src_ty)))
- (fpromote_reg dst_ty src_ty x))
+;; Promote a register. There is no `f16` conversion, so `f16` operands are left
+;; unmatched and rejected as unsupported.
+(rule (lower (fpromote $F64 x @ (value_type $F32)))
+ (fpromote_reg $F64 $F32 x))
+(rule (lower (fpromote $F128 x @ (value_type $F64)))
+ (fpromote_reg $F128 $F64 x))
(rule 1 (lower (fpromote $F128 x @ (value_type $F32)))
(fpromote_reg $F128 $F64 (fpromote_reg $F64 $F32 x)))
@@ -2013,9 +2016,12 @@
;;;; Rules for `fdemote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Demote a register.
-(rule (lower (fdemote dst_ty x @ (value_type src_ty)))
- (fdemote_reg dst_ty src_ty (FpuRoundMode.Current) x))
+;; Demote a register. There is no `f16` conversion, so `f16` results are left
+;; unmatched and rejected as unsupported.
+(rule (lower (fdemote $F32 x @ (value_type $F64)))
+ (fdemote_reg $F32 $F64 (FpuRoundMode.Current) x))
+(rule (lower (fdemote $F64 x @ (value_type $F128)))
+ (fdemote_reg $F64 $F128 (FpuRoundMode.Current) x))
(rule 1 (lower (fdemote $F32 x @ (value_type $F128)))
(fdemote_reg $F32 $F64 (FpuRoundMode.Current)
diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isle
index 7940f326ab..3e4b325335 100644
--- a/cranelift/codegen/src/isa/x64/lower.isle
+++ b/cranelift/codegen/src/isa/x64/lower.isle
@@ -2795,7 +2795,7 @@
(x64_sqrtpd x))
;; Rules for `fpromote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(rule (lower (fpromote $F64 x))
+(rule (lower (fpromote $F64 x @ (value_type $F32)))
(x64_cvtss2sd (xmm_zero $F64X2) x))
;; Rules for `fvpromote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -2803,7 +2803,7 @@
(x64_cvtps2pd (put_in_xmm x)))
;; Rules for `fdemote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-(rule (lower (fdemote $F32 x))
+(rule (lower (fdemote $F32 x @ (value_type $F64)))
(x64_cvtsd2ss (xmm_zero $F32X4) x))
;; Rules for `fvdemote` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/cranelift/filetests/filetests/isa/aarch64/fpromote-fdemote-unsupported.clif b/cranelift/filetests/filetests/isa/aarch64/fpromote-fdemote-unsupported.clif
new file mode 100644
index 0000000000..8ae736b31e
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/aarch64/fpromote-fdemote-unsupported.clif
@@ -0,0 +1,65 @@
+; There is no AArch64 lowering for `fpromote` or `fdemote` with an `f16` or
+; `f128` operand or result. Compilation must fail cleanly with an
+; unsupported-feature error rather than miscompiling.
+test compile expect-fail
+target aarch64
+
+function %fpromote_f16_f32(f16) -> f32 {
+block0(v0: f16):
+ v1 = fpromote.f32 v0
+ return v1
+}
+
+function %fpromote_f16_f64(f16) -> f64 {
+block0(v0: f16):
+ v1 = fpromote.f64 v0
+ return v1
+}
+
+function %fpromote_f16_f128(f16) -> f128 {
+block0(v0: f16):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fpromote_f32_f128(f32) -> f128 {
+block0(v0: f32):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fpromote_f64_f128(f64) -> f128 {
+block0(v0: f64):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fdemote_f32_f16(f32) -> f16 {
+block0(v0: f32):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f64_f16(f64) -> f16 {
+block0(v0: f64):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f128_f16(f128) -> f16 {
+block0(v0: f128):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f128_f32(f128) -> f32 {
+block0(v0: f128):
+ v1 = fdemote.f32 v0
+ return v1
+}
+
+function %fdemote_f128_f64(f128) -> f64 {
+block0(v0: f128):
+ v1 = fdemote.f64 v0
+ return v1
+}
diff --git a/cranelift/filetests/filetests/isa/s390x/fpromote-fdemote-unsupported.clif b/cranelift/filetests/filetests/isa/s390x/fpromote-fdemote-unsupported.clif
new file mode 100644
index 0000000000..17666031c0
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/s390x/fpromote-fdemote-unsupported.clif
@@ -0,0 +1,42 @@
+; There is no s390x lowering for `fpromote` or `fdemote` with an `f16`
+; operand or result. Compilation must fail cleanly with an unsupported-feature
+; error rather than panicking.
+test compile expect-fail
+target s390x
+
+function %fpromote_f16_f32(f16) -> f32 {
+block0(v0: f16):
+ v1 = fpromote.f32 v0
+ return v1
+}
+
+function %fpromote_f16_f64(f16) -> f64 {
+block0(v0: f16):
+ v1 = fpromote.f64 v0
+ return v1
+}
+
+function %fpromote_f16_f128(f16, i64) {
+block0(v0: f16, v1: i64):
+ v2 = fpromote.f128 v0
+ store v2, v1
+ return
+}
+
+function %fdemote_f32_f16(f32) -> f16 {
+block0(v0: f32):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f64_f16(f64) -> f16 {
+block0(v0: f64):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f128_f16(f128) -> f16 {
+block0(v0: f128):
+ v1 = fdemote.f16 v0
+ return v1
+}
diff --git a/cranelift/filetests/filetests/isa/x64/fpromote-fdemote-unsupported.clif b/cranelift/filetests/filetests/isa/x64/fpromote-fdemote-unsupported.clif
new file mode 100644
index 0000000000..947aad59ef
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/x64/fpromote-fdemote-unsupported.clif
@@ -0,0 +1,66 @@
+; There is no x86-64 lowering for `fpromote` or `fdemote` with an `f16` or
+; `f128` operand or result. Compilation must fail cleanly with an
+; unsupported-feature error rather than panicking.
+test compile expect-fail
+target x86_64
+target x86_64 has_avx
+
+function %fpromote_f16_f32(f16) -> f32 {
+block0(v0: f16):
+ v1 = fpromote.f32 v0
+ return v1
+}
+
+function %fpromote_f16_f64(f16) -> f64 {
+block0(v0: f16):
+ v1 = fpromote.f64 v0
+ return v1
+}
+
+function %fpromote_f16_f128(f16) -> f128 {
+block0(v0: f16):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fpromote_f32_f128(f32) -> f128 {
+block0(v0: f32):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fpromote_f64_f128(f64) -> f128 {
+block0(v0: f64):
+ v1 = fpromote.f128 v0
+ return v1
+}
+
+function %fdemote_f32_f16(f32) -> f16 {
+block0(v0: f32):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f64_f16(f64) -> f16 {
+block0(v0: f64):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f128_f16(f128) -> f16 {
+block0(v0: f128):
+ v1 = fdemote.f16 v0
+ return v1
+}
+
+function %fdemote_f128_f32(f128) -> f32 {
+block0(v0: f128):
+ v1 = fdemote.f32 v0
+ return v1
+}
+
+function %fdemote_f128_f64(f128) -> f64 {
+block0(v0: f128):
+ v1 = fdemote.f64 v0
+ return v1
+}