Skip to content

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

The ISLE verifier found several more cases related to #14566 where mid-end rules are not correctly guarded to exclude vector 64-bit vector types such as `i32x2` (#14631)

Alexa VanHattum28f50c9aab4c
diff --git a/cranelift/codegen/src/opts/arithmetic.isle b/cranelift/codegen/src/opts/arithmetic.isle
index b7f70b29b9..f5a34f29a2 100644
--- a/cranelift/codegen/src/opts/arithmetic.isle
+++ b/cranelift/codegen/src/opts/arithmetic.isle
@@ -330,8 +330,8 @@
(rule (simplify (iadd ty (ishl ty (band ty x y) (iconst_u ty 1)) (bxor ty y x))) (iadd ty x y))
;; (~x) + x == -1
-(rule (simplify (iadd ty (bnot ty x) x)) (iconst_s ty -1))
-(rule (simplify (iadd ty x (bnot ty x))) (iconst_s ty -1))
+(rule (simplify (iadd ty (bnot (ty_int ty) x) x)) (iconst_s ty -1))
+(rule (simplify (iadd ty x (bnot (ty_int ty) x))) (iconst_s ty -1))
;; ((x + y) - (x + z)) --> (y - z)
(rule (simplify (isub ty (iadd ty x y) (iadd ty x z))) (isub ty y z))
@@ -587,10 +587,10 @@
(rule imul_ineg_const (simplify (imul (fits_in_64 ty) (ineg ty x) (iconst ty y))) (imul ty x (iconst ty (imm64_neg ty y))))
;; -(~x) --> x + 1
-(rule (simplify (ineg ty (bnot ty x))) (iadd ty x (iconst_u ty 1)))
+(rule (simplify (ineg (ty_int ty) (bnot ty x))) (iadd ty x (iconst_u ty 1)))
;; ~(-x) --> x - 1.
-(rule (simplify (bnot ty (ineg ty x))) (isub ty x (iconst_u ty 1)))
+(rule (simplify (bnot (ty_int ty) (ineg ty x))) (isub ty x (iconst_u ty 1)))
;; -1 - x --> ~x
(rule (simplify (isub ty (iconst_s ty -1) x)) (bnot ty x))
diff --git a/cranelift/codegen/src/opts/shifts.isle b/cranelift/codegen/src/opts/shifts.isle
index 044d9000af..5059bb6260 100644
--- a/cranelift/codegen/src/opts/shifts.isle
+++ b/cranelift/codegen/src/opts/shifts.isle
@@ -24,12 +24,12 @@
;; `(x >> k) << k` is the same as masking off the bottom `k` bits (regardless if
;; this is a signed or unsigned shift right).
-(rule (simplify (ishl (fits_in_64 ty)
+(rule (simplify (ishl (fits_in_64 (ty_int ty))
(ushr ty x (iconst _ k))
(iconst _ k)))
(let ((mask Imm64 (imm64_shl ty (imm64 0xFFFF_FFFF_FFFF_FFFF) k)))
(band ty x (iconst ty mask))))
-(rule (simplify (ishl (fits_in_64 ty)
+(rule (simplify (ishl (fits_in_64 (ty_int ty))
(sshr ty x (iconst _ k))
(iconst _ k)))
(let ((mask Imm64 (imm64_shl ty (imm64 0xFFFF_FFFF_FFFF_FFFF) k)))
diff --git a/cranelift/filetests/filetests/egraph/i32x2-scalar-guards.clif b/cranelift/filetests/filetests/egraph/i32x2-scalar-guards.clif
new file mode 100644
index 0000000000..bd06723a3f
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/i32x2-scalar-guards.clif
@@ -0,0 +1,64 @@
+; Rules that build `iconst_u`/`iconst_s` should not fire on a 64-bit vector type.
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+;; `(~x) + x --> -1` should not build `iconst_s` on 64-bit vector type.
+function %bnot_iadd_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = bnot v0
+ v2 = iadd v1, v0
+ return v2
+}
+
+; function %bnot_iadd_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = bnot v0
+; v2 = iadd v1, v0
+; return v2
+; }
+
+;; `x + (~x) --> -1` should not build `iconst_s` on 64-bit vector type.
+function %iadd_bnot_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = bnot v0
+ v2 = iadd v0, v1
+ return v2
+}
+
+; function %iadd_bnot_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = bnot v0
+; v2 = iadd v0, v1
+; return v2
+; }
+
+;; `-(~x) --> x + 1` should not build `iconst_u` on 64-bit vector type.
+function %ineg_bnot_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = bnot v0
+ v2 = ineg v1
+ return v2
+}
+
+; function %ineg_bnot_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = bnot v0
+; v2 = ineg v1
+; return v2
+; }
+
+;; `~(-x) --> x - 1` should not build `iconst_u` on 64-bit vector type.
+function %bnot_ineg_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = ineg v0
+ v2 = bnot v1
+ return v2
+}
+
+; function %bnot_ineg_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = ineg v0
+; v2 = bnot v1
+; return v2
+; }
diff --git a/cranelift/filetests/filetests/egraph/issue-14566.clif b/cranelift/filetests/filetests/egraph/issue-14566.clif
new file mode 100644
index 0000000000..c2e79c3b2d
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/issue-14566.clif
@@ -0,0 +1,39 @@
+; Issue #14566: `(x >> k) << k --> x & mask` built `iconst` at a 64-bit
+; vector type because its `fits_in_64` guard admits vectors.
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+;; `(ishl (ushr x k) k)` should not fire on 64-bit vector type.
+function %ushr_ishl_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = iconst.i32 4
+ v2 = ushr v0, v1
+ v3 = ishl v2, v1
+ return v3
+}
+
+; function %ushr_ishl_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = iconst.i32 4
+; v2 = ushr v0, v1 ; v1 = 4
+; v3 = ishl v2, v1 ; v1 = 4
+; return v3
+; }
+
+;; `(ishl (sshr x k) k)` should not fire on 64-bit vector type.
+function %sshr_ishl_i32x2(i32x2) -> i32x2 {
+block0(v0: i32x2):
+ v1 = iconst.i32 4
+ v2 = sshr v0, v1
+ v3 = ishl v2, v1
+ return v3
+}
+
+; function %sshr_ishl_i32x2(i32x2) -> i32x2 fast {
+; block0(v0: i32x2):
+; v1 = iconst.i32 4
+; v2 = sshr v0, v1 ; v1 = 4
+; v3 = ishl v2, v1 ; v1 = 4
+; return v3
+; }