Skip to content

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

Cranelift: ISLE: aarch64: add missing ty_int guards (#14701)

Alexa VanHattumb4f912b69307
diff --git a/cranelift/codegen/src/isa/aarch64/lower.isle b/cranelift/codegen/src/isa/aarch64/lower.isle
index 00c695a110..a5a6d13e93 100644
--- a/cranelift/codegen/src/isa/aarch64/lower.isle
+++ b/cranelift/codegen/src/isa/aarch64/lower.isle
@@ -437,7 +437,7 @@
(rule iabs_64 2 (lower (iabs $I64 x))
(abs (OperandSize.Size64) x))
-(rule iabs_8_16_32 1 (lower (iabs (fits_in_32 ty) x))
+(rule iabs_8_16_32 1 (lower (iabs (fits_in_32 (ty_int ty)) x))
(abs (OperandSize.Size32) (put_in_reg_sext32 x)))
; `rustc` implementation.
@@ -908,7 +908,7 @@
;;;; Rules for `ineg` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; `i64` and smaller.
-(rule ineg_base_case 1 (lower (ineg (fits_in_64 ty) x))
+(rule ineg_base_case 1 (lower (ineg (fits_in_64 (ty_int ty)) x))
(sub ty (zero_reg) x))
;; `i128`
@@ -918,6 +918,8 @@
;; vectors.
(rule (lower (ineg (ty_vec128 ty) x))
(neg x (vector_size ty)))
+(rule -1 (lower (ineg (ty_vec64 ty) x))
+ (neg x (vector_size ty)))
;;;; Rules for `imul` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1110,7 +1112,7 @@
(rule smulhi_64 1 (lower (smulhi $I64 x y))
(smulh $I64 x y))
-(rule smulhi_fits_in_32 (lower (smulhi (fits_in_32 ty) x y))
+(rule smulhi_fits_in_32 (lower (smulhi (fits_in_32 (ty_int ty)) x y))
(let ((x64 Reg (put_in_reg_sext64 x))
(y64 Reg (put_in_reg_sext64 y))
(mul Reg (madd $I64 x64 y64 (zero_reg)))
@@ -1122,7 +1124,7 @@
(rule umulhi_64 1 (lower (umulhi $I64 x y))
(umulh $I64 x y))
-(rule umulhi_fits_in_32 (lower (umulhi (fits_in_32 ty) x y))
+(rule umulhi_fits_in_32 (lower (umulhi (fits_in_32 (ty_int ty)) x y))
(let (
(x64 Reg (put_in_reg_zext64 x))
(y64 Reg (put_in_reg_zext64 y))
@@ -1562,13 +1564,13 @@
;; anything CLIF-wise and this should compile down to a `bor` operation. Leave
;; that edge case to the mid-end and only lower to `extr` here.
(rule extr_32_or_64 5 (lower
- (bor (ty_32_or_64 ty) (ishl _ x (u8_from_iconst xs)) (ushr _ y (u8_from_iconst ys))))
+ (bor (ty_32_or_64 (ty_int ty)) (ishl _ x (u8_from_iconst xs)) (ushr _ y (u8_from_iconst ys))))
(if-let true (u64_eq (ty_bits ty) (u64_wrapping_add xs ys)))
(if-let true (u64_gt xs 0))
(if-let true (u64_gt ys 0))
(a64_extr ty x y (imm_shift_from_u8 ys)))
(rule extr_32_or_64_2 5 (lower
- (bor (ty_32_or_64 ty) (ushr _ y (u8_from_iconst ys)) (ishl _ x (u8_from_iconst xs))))
+ (bor (ty_32_or_64 (ty_int ty)) (ushr _ y (u8_from_iconst ys)) (ishl _ x (u8_from_iconst xs))))
(if-let true (u64_eq (ty_bits ty) (u64_wrapping_add xs ys)))
(if-let true (u64_gt xs 0))
(if-let true (u64_gt ys 0))
@@ -1605,7 +1607,7 @@
;;;; Rules for `ishl` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shift for i8/i16/i32.
-(rule ishl_fits_in_32 -1 (lower (ishl (fits_in_32 ty) x y))
+(rule ishl_fits_in_32 -1 (lower (ishl (fits_in_32 (ty_int ty)) x y))
(do_shift (ALUOp.Lsl) ty x y))
;; Shift for i64.
@@ -1650,6 +1652,13 @@
(sshl x shift size)))
(rule -2 (lower (ishl (ty_vec128 ty) x (iconst _ (u64_from_imm64 n))))
(ushl_vec_imm x (shift_masked_imm ty n) (vector_size ty)))
+(rule -5 (lower (ishl (ty_vec64 ty) x y))
+ (let ((size VectorSize (vector_size ty))
+ (masked_shift_amt Reg (and_imm $I32 (lo_reg y) (shift_mask ty)))
+ (shift Reg (vec_dup masked_shift_amt size)))
+ (sshl x shift size)))
+(rule -4 (lower (ishl (ty_vec64 ty) x (iconst _ (u64_from_imm64 n))))
+ (ushl_vec_imm x (shift_masked_imm ty n) (vector_size ty)))
(decl pure shift_masked_imm (Type u64) u8)
(extern constructor shift_masked_imm shift_masked_imm)
@@ -1698,7 +1707,7 @@
;;;; Rules for `ushr` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shift for i8/i16/i32.
-(rule ushr_fits_in_32 -1 (lower (ushr (fits_in_32 ty) x y))
+(rule ushr_fits_in_32 -1 (lower (ushr (fits_in_32 (ty_int ty)) x y))
(do_shift (ALUOp.Lsr) ty (put_in_reg_zext32 x) y))
;; Shift for i64.
@@ -1724,6 +1733,16 @@
(rule -2 (lower (ushr (ty_vec128 ty) x (iconst _ (u64_from_imm64 n))))
(if-let 0 (shift_masked_imm ty n))
x)
+(rule -7 (lower (ushr (ty_vec64 ty) x y))
+ (let ((size VectorSize (vector_size ty))
+ (masked_shift_amt Reg (and_imm $I32 (lo_reg y) (shift_mask ty)))
+ (shift Reg (vec_dup (sub $I64 (zero_reg) masked_shift_amt) size)))
+ (ushl x shift size)))
+(rule -6 (lower (ushr (ty_vec64 ty) x (iconst _ (u64_from_imm64 n))))
+ (ushr_vec_imm x (shift_masked_imm ty n) (vector_size ty)))
+(rule -5 (lower (ushr (ty_vec64 ty) x (iconst _ (u64_from_imm64 n))))
+ (if-let 0 (shift_masked_imm ty n))
+ x)
;; lsr lo_rshift, src_lo, amt
;; lsr hi_rshift, src_hi, amt
@@ -1755,7 +1774,7 @@
;;;; Rules for `sshr` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shift for i8/i16/i32.
-(rule sshr_fits_in_32 -4 (lower (sshr (fits_in_32 ty) x y))
+(rule sshr_fits_in_32 -4 (lower (sshr (fits_in_32 (ty_int ty)) x y))
(do_shift (ALUOp.Asr) ty (put_in_reg_sext32 x) y))
;; Shift for i64.
@@ -1799,6 +1818,16 @@
(rule -1 (lower (sshr (ty_vec128 ty) x (iconst _ (u64_from_imm64 n))))
(if-let 0 (shift_masked_imm ty n))
x)
+(rule -7 (lower (sshr (ty_vec64 ty) x y))
+ (let ((size VectorSize (vector_size ty))
+ (masked_shift_amt Reg (and_imm $I32 (lo_reg y) (shift_mask ty)))
+ (shift Reg (vec_dup (sub $I64 (zero_reg) masked_shift_amt) size)))
+ (sshl x shift size)))
+(rule -6 (lower (sshr (ty_vec64 ty) x (iconst _ (u64_from_imm64 n))))
+ (sshr_vec_imm x (shift_masked_imm ty n) (vector_size ty)))
+(rule -5 (lower (sshr (ty_vec64 ty) x (iconst _ (u64_from_imm64 n))))
+ (if-let 0 (shift_masked_imm ty n))
+ x)
;; lsr lo_rshift, src_lo, amt
;; asr hi_rshift, src_hi, amt
diff --git a/cranelift/filetests/filetests/runtests/simd-ineg-shifts-64bit.clif b/cranelift/filetests/filetests/runtests/simd-ineg-shifts-64bit.clif
new file mode 100644
index 0000000000..2dddea173e
--- /dev/null
+++ b/cranelift/filetests/filetests/runtests/simd-ineg-shifts-64bit.clif
@@ -0,0 +1,125 @@
+; `ineg` and shifts on 64-bit vectors. Shift amounts are masked to the lane
+; width. The `bor` cases match aarch64's `extr` rules, which should not apply to
+; vectors.
+test interpret
+test run
+target aarch64
+
+function %ineg_i8x8(i8x8) -> i8x8 {
+block0(v0: i8x8):
+ v1 = ineg v0
+ return v1
+}
+; run: %ineg_i8x8([1 0 -1 127 -128 2 3 4]) == [-1 0 1 -127 -128 -2 -3 -4]
+
+function %ineg_i16x4(i16x4) -> i16x4 {
+block0(v0: i16x4):
+ v1 = ineg v0
+ return v1
+}
+; run: %ineg_i16x4([1 0 -1 -32768]) == [-1 0 1 -32768]
+
+function %ineg_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = ineg v0
+ return v1
+}
+; run: %ineg_i32x2([1 0]) == [-1 0]
+; run: %ineg_i32x2([-2147483648 7]) == [-2147483648 -7]
+
+function %ishl_i8x8(i8x8, i32) -> i8x8 {
+block0(v0: i8x8, v1: i32):
+ v2 = ishl v0, v1
+ return v2
+}
+; run: %ishl_i8x8([1 2 3 4 5 6 7 -128], 1) == [2 4 6 8 10 12 14 0]
+; run: %ishl_i8x8([1 2 3 4 5 6 7 8], 9) == [2 4 6 8 10 12 14 16]
+; run: %ishl_i8x8([1 2 3 4 5 6 7 8], 0) == [1 2 3 4 5 6 7 8]
+
+function %ishl_imm_i16x4(i16x4) -> i16x4 {
+block0(v0: i16x4):
+ v1 = iconst.i32 3
+ v2 = ishl v0, v1
+ return v2
+}
+; run: %ishl_imm_i16x4([1 2 -1 0x1000]) == [8 16 -8 -32768]
+
+function %ishl_imm_masked_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = iconst.i32 33
+ v2 = ishl v0, v1
+ return v2
+}
+; run: %ishl_imm_masked_i32x2([1 0x40000000]) == [2 -2147483648]
+
+function %ushr_i16x4(i16x4, i64) -> i16x4 {
+block0(v0: i16x4, v1: i64):
+ v2 = ushr v0, v1
+ return v2
+}
+; run: %ushr_i16x4([-1 0x100 2 1], 4) == [0x0fff 0x10 0 0]
+; run: %ushr_i16x4([-1 0x100 2 1], 20) == [0x0fff 0x10 0 0]
+; run: %ushr_i16x4([-1 0x100 2 1], 0) == [-1 0x100 2 1]
+
+function %ushr_imm_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = iconst.i32 31
+ v2 = ushr v0, v1
+ return v2
+}
+; run: %ushr_imm_i32x2([-1 0x7fffffff]) == [1 0]
+
+function %ushr_imm_zero_i8x8(i8x8) -> i8x8 {
+block0(v0: i8x8):
+ v1 = iconst.i32 8
+ v2 = ushr v0, v1
+ return v2
+}
+; run: %ushr_imm_zero_i8x8([-1 1 2 3 4 5 6 7]) == [-1 1 2 3 4 5 6 7]
+
+function %sshr_i32x2(i32x2, i8) -> i32x2 {
+block0(v0: i32x2, v1: i8):
+ v2 = sshr v0, v1
+ return v2
+}
+; run: %sshr_i32x2([-8 8], 2) == [-2 2]
+; run: %sshr_i32x2([-8 8], 34) == [-2 2]
+; run: %sshr_i32x2([-8 8], 0) == [-8 8]
+
+function %sshr_imm_i8x8(i8x8) -> i8x8 {
+block0(v0: i8x8):
+ v1 = iconst.i32 7
+ v2 = sshr v0, v1
+ return v2
+}
+; run: %sshr_imm_i8x8([-128 127 -1 1 -64 64 0 -2]) == [-1 0 -1 0 -1 0 0 -1]
+
+function %sshr_imm_zero_i16x4(i16x4) -> i16x4 {
+block0(v0: i16x4):
+ v1 = iconst.i32 16
+ v2 = sshr v0, v1
+ return v2
+}
+; run: %sshr_imm_zero_i16x4([-1 1 -32768 32767]) == [-1 1 -32768 32767]
+
+function %ishl_ushr_bor_i32x2(i32x2, i32x2) -> i32x2 {
+block0(v0: i32x2, v1: i32x2):
+ v2 = iconst.i64 2
+ v3 = ishl v0, v2
+ v4 = iconst.i64 62
+ v5 = ushr v1, v4
+ v6 = bor v3, v5
+ return v6
+}
+; run: %ishl_ushr_bor_i32x2([1 2], [0x80000000 0xc0000000]) == [6 11]
+
+function %ushr_ishl_bor_i32x2(i32x2, i32x2) -> i32x2 {
+block0(v0: i32x2, v1: i32x2):
+ v2 = iconst.i64 62
+ v3 = ushr v1, v2
+ v4 = iconst.i64 2
+ v5 = ishl v0, v4
+ v6 = bor v3, v5
+ return v6
+}
+; run: %ushr_ishl_bor_i32x2([1 2], [0x80000000 0xc0000000]) == [6 11]