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:
@@ -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` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
Reference in New Issue
Block a user