Support big- and little-endian lane order with bitcast (#5196)
Add a MemFlags operand to the bitcast instruction, where only the `big` and `little` flags are accepted. These define the lane order to be used when casting between types of different lane counts. Update all users to pass an appropriate MemFlags argument. Implement lane swaps where necessary in the s390x back-end. This is the final part necessary to fix https://github.com/bytecodealliance/wasmtime/issues/4566.
This commit is contained in:
@@ -2197,25 +2197,25 @@
|
||||
;;; Rules for `bitcast` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
; SIMD&FP <=> SIMD&FP
|
||||
(rule 5 (lower (has_type (ty_float_or_vec _) (bitcast x @ (value_type (ty_float_or_vec _)))))
|
||||
(rule 5 (lower (has_type (ty_float_or_vec _) (bitcast _ x @ (value_type (ty_float_or_vec _)))))
|
||||
x)
|
||||
|
||||
; GPR => SIMD&FP
|
||||
(rule 4 (lower (has_type (ty_float_or_vec _) (bitcast x @ (value_type in_ty))))
|
||||
(rule 4 (lower (has_type (ty_float_or_vec _) (bitcast _ x @ (value_type in_ty))))
|
||||
(if (ty_int_ref_scalar_64 in_ty))
|
||||
(mov_to_fpu x (scalar_size in_ty)))
|
||||
|
||||
; SIMD&FP => GPR
|
||||
(rule 3 (lower (has_type out_ty (bitcast x @ (value_type (fits_in_64 (ty_float_or_vec _))))))
|
||||
(rule 3 (lower (has_type out_ty (bitcast _ x @ (value_type (fits_in_64 (ty_float_or_vec _))))))
|
||||
(if (ty_int_ref_scalar_64 out_ty))
|
||||
(mov_from_vec x 0 (scalar_size out_ty)))
|
||||
|
||||
; GPR <=> GPR
|
||||
(rule 2 (lower (has_type out_ty (bitcast x @ (value_type in_ty))))
|
||||
(rule 2 (lower (has_type out_ty (bitcast _ x @ (value_type in_ty))))
|
||||
(if (ty_int_ref_scalar_64 out_ty))
|
||||
(if (ty_int_ref_scalar_64 in_ty))
|
||||
x)
|
||||
(rule 1 (lower (has_type $I128 (bitcast x @ (value_type $I128)))) x)
|
||||
(rule 1 (lower (has_type $I128 (bitcast _ x @ (value_type $I128)))) x)
|
||||
|
||||
;;; Rules for `extractlane` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
@@ -811,7 +811,7 @@
|
||||
)
|
||||
;;;;; Rules for `bitcast`;;;;;;;;;
|
||||
(rule
|
||||
(lower (has_type out (bitcast v @ (value_type in_ty))))
|
||||
(lower (has_type out (bitcast _ v @ (value_type in_ty))))
|
||||
(gen_moves v in_ty out))
|
||||
|
||||
;;;;; Rules for `ceil`;;;;;;;;;
|
||||
|
||||
@@ -1486,6 +1486,11 @@
|
||||
(rule (lane_order_equal (LaneOrder.BigEndian) (LaneOrder.LittleEndian)) $false)
|
||||
(rule (lane_order_equal (LaneOrder.BigEndian) (LaneOrder.BigEndian)) $true)
|
||||
|
||||
;; Return lane order matching memory byte order.
|
||||
(decl pure lane_order_from_memflags (MemFlags) LaneOrder)
|
||||
(rule 0 (lane_order_from_memflags (littleendian)) (LaneOrder.LittleEndian))
|
||||
(rule 1 (lane_order_from_memflags (bigendian)) (LaneOrder.BigEndian))
|
||||
|
||||
;; Convert a CLIF immediate lane index value to big-endian lane order.
|
||||
(decl be_lane_idx (Type u8) u8)
|
||||
(extern constructor be_lane_idx be_lane_idx)
|
||||
|
||||
@@ -1738,40 +1738,46 @@
|
||||
;;;; Rules for `bitcast` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
;; Reinterpret a 64-bit integer value as floating-point.
|
||||
(rule (lower (has_type $F64 (bitcast x @ (value_type $I64))))
|
||||
(rule (lower (has_type $F64 (bitcast _ x @ (value_type $I64))))
|
||||
(vec_insert_lane_undef $F64X2 x 0 (zero_reg)))
|
||||
|
||||
;; Reinterpret a 64-bit floating-point value as integer.
|
||||
(rule (lower (has_type $I64 (bitcast x @ (value_type $F64))))
|
||||
(rule (lower (has_type $I64 (bitcast _ x @ (value_type $F64))))
|
||||
(vec_extract_lane $F64X2 x 0 (zero_reg)))
|
||||
|
||||
;; Reinterpret a 32-bit integer value as floating-point.
|
||||
(rule (lower (has_type $F32 (bitcast x @ (value_type $I32))))
|
||||
(rule (lower (has_type $F32 (bitcast _ x @ (value_type $I32))))
|
||||
(vec_insert_lane_undef $F32X4 x 0 (zero_reg)))
|
||||
|
||||
;; Reinterpret a 32-bit floating-point value as integer.
|
||||
(rule (lower (has_type $I32 (bitcast x @ (value_type $F32))))
|
||||
(rule (lower (has_type $I32 (bitcast _ x @ (value_type $F32))))
|
||||
(vec_extract_lane $F32X4 x 0 (zero_reg)))
|
||||
|
||||
;; Bitcast between types residing in GPRs is a no-op.
|
||||
(rule 1 (lower (has_type (gpr32_ty _)
|
||||
(bitcast x @ (value_type (gpr32_ty _))))) x)
|
||||
(bitcast _ x @ (value_type (gpr32_ty _))))) x)
|
||||
(rule 2 (lower (has_type (gpr64_ty _)
|
||||
(bitcast x @ (value_type (gpr64_ty _))))) x)
|
||||
(bitcast _ x @ (value_type (gpr64_ty _))))) x)
|
||||
|
||||
;; Bitcast between types residing in FPRs is a no-op.
|
||||
(rule 3 (lower (has_type (ty_scalar_float _)
|
||||
(bitcast x @ (value_type (ty_scalar_float _))))) x)
|
||||
(bitcast _ x @ (value_type (ty_scalar_float _))))) x)
|
||||
|
||||
;; Bitcast between types residing in VRs is a no-op.
|
||||
;; FIXME: There are two flavors of vector bitcast, which are currently not
|
||||
;; distinguished in CLIF IR. Those generated by Wasmtime assume little-endian
|
||||
;; lane order, and those generated elsewhere assume big-endian lane order.
|
||||
;; Bitcast is a no-op if current lane order matches that assumed lane order.
|
||||
;; However, due to our choice of lane order depending on the current function
|
||||
;; ABI, every bitcast we currently see here is indeed a no-op.
|
||||
(rule 4 (lower (has_type (vr128_ty _)
|
||||
(bitcast x @ (value_type (vr128_ty _))))) x)
|
||||
;; Bitcast between types residing in VRs is a no-op if lane count is unchanged.
|
||||
(rule 5 (lower (has_type (multi_lane bits count)
|
||||
(bitcast _ x @ (value_type (multi_lane bits count))))) x)
|
||||
|
||||
;; Bitcast between types residing in VRs with different lane counts is a
|
||||
;; no-op if the operation's MemFlags indicate a byte order compatible with
|
||||
;; the current lane order. Otherwise, lane elements need to be swapped,
|
||||
;; first in the input type, and then again in the output type. This could
|
||||
;; be optimized further, but we don't bother at the moment since due to our
|
||||
;; choice of lane order depending on the current function ABI, this case will
|
||||
;; currently never arise in practice.
|
||||
(rule 4 (lower (has_type (vr128_ty out_ty)
|
||||
(bitcast flags x @ (value_type (vr128_ty in_ty)))))
|
||||
(abi_vec_elt_rev (lane_order_from_memflags flags) out_ty
|
||||
(abi_vec_elt_rev (lane_order_from_memflags flags) in_ty x)))
|
||||
|
||||
|
||||
;;;; Rules for `insertlane` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
@@ -3298,25 +3298,25 @@
|
||||
|
||||
;; Rules for `bitcast` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(rule (lower (has_type $I32 (bitcast src @ (value_type $F32))))
|
||||
(rule (lower (has_type $I32 (bitcast _ src @ (value_type $F32))))
|
||||
(bitcast_xmm_to_gpr $F32 src))
|
||||
|
||||
(rule (lower (has_type $F32 (bitcast src @ (value_type $I32))))
|
||||
(rule (lower (has_type $F32 (bitcast _ src @ (value_type $I32))))
|
||||
(bitcast_gpr_to_xmm $I32 src))
|
||||
|
||||
(rule (lower (has_type $I64 (bitcast src @ (value_type $F64))))
|
||||
(rule (lower (has_type $I64 (bitcast _ src @ (value_type $F64))))
|
||||
(bitcast_xmm_to_gpr $F64 src))
|
||||
|
||||
(rule (lower (has_type $F64 (bitcast src @ (value_type $I64))))
|
||||
(rule (lower (has_type $F64 (bitcast _ src @ (value_type $I64))))
|
||||
(bitcast_gpr_to_xmm $I64 src))
|
||||
|
||||
;; Bitcast between types residing in GPR registers is a no-op.
|
||||
(rule 1 (lower (has_type (is_gpr_type _)
|
||||
(bitcast x @ (value_type (is_gpr_type _))))) x)
|
||||
(bitcast _ x @ (value_type (is_gpr_type _))))) x)
|
||||
|
||||
;; Bitcast between types residing in XMM registers is a no-op.
|
||||
(rule 2 (lower (has_type (is_xmm_type _)
|
||||
(bitcast x @ (value_type (is_xmm_type _))))) x)
|
||||
(bitcast _ x @ (value_type (is_xmm_type _))))) x)
|
||||
|
||||
;; Rules for `fcopysign` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user