Skip to content

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

Type-guard bitop optimization rules (#13229) (#13237)

Bongjun Jang348a8dcd55f6
diff --git a/cranelift/codegen/src/isle_prelude.rs b/cranelift/codegen/src/isle_prelude.rs
index 84d2438088..f79e850631 100644
--- a/cranelift/codegen/src/isle_prelude.rs
+++ b/cranelift/codegen/src/isle_prelude.rs
@@ -544,6 +544,15 @@ macro_rules! isle_common_prelude_methods {
ty.is_int().then(|| ty)
}
+ #[inline]
+ fn ty_int_vec128(&mut self, ty: Type) -> Option<Type> {
+ if ty.is_int() || (ty.is_vector() && ty.bits() == 128 && ty.lane_type().is_int()) {
+ Some(ty)
+ } else {
+ None
+ }
+ }
+
#[inline]
fn ty_scalar(&mut self, ty: Type) -> Option<Type> {
if ty.lane_count() == 1 { Some(ty) } else { None }
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index 257f6d4e77..d0a130312d 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -20,10 +20,10 @@
;; x ^ not(x) == not(x) ^ x == x | not(x) == not(x) | x == -1.
;; This identity also holds for non-integer types, vectors, and wider types.
-(rule (simplify (bxor ty x (bnot ty x))) (subsume (iconst_s ty -1)))
-(rule (simplify (bxor ty (bnot ty x) x)) (subsume (iconst_s ty -1)))
-(rule (simplify (bor ty x (bnot ty x))) (subsume (iconst_s ty -1)))
-(rule (simplify (bor ty (bnot ty x) x)) (subsume (iconst_s ty -1)))
+(rule (simplify (bxor (ty_int_vec128 ty) x (bnot ty x))) (subsume (iconst_s ty -1)))
+(rule (simplify (bxor (ty_int_vec128 ty) (bnot ty x) x)) (subsume (iconst_s ty -1)))
+(rule (simplify (bor (ty_int_vec128 ty) x (bnot ty x))) (subsume (iconst_s ty -1)))
+(rule (simplify (bor (ty_int_vec128 ty) (bnot ty x) x)) (subsume (iconst_s ty -1)))
;; x & x == x & -1 == x.
(rule (simplify (band ty x x)) (subsume x))
@@ -31,9 +31,9 @@
(subsume x))
;; x & 0 == x & not(x) == not(x) & x == 0.
-(rule (simplify (band ty _ zero @ (iconst_u ty 0))) (subsume zero))
-(rule (simplify (band ty x (bnot ty x))) (subsume (iconst_u ty 0)))
-(rule (simplify (band ty (bnot ty x) x)) (subsume (iconst_u ty 0)))
+(rule (simplify (band (ty_int_vec128 ty) _ zero @ (iconst_u ty 0))) (subsume zero))
+(rule (simplify (band (ty_int_vec128 ty) x (bnot ty x))) (subsume (iconst_u ty 0)))
+(rule (simplify (band (ty_int_vec128 ty) (bnot ty x) x)) (subsume (iconst_u ty 0)))
;; (x & y) ^ (x ^ y) == x | y
(rule (simplify (bxor ty (band ty X Y) (bxor ty X Y))) (bor ty X Y))
diff --git a/cranelift/codegen/src/opts/icmp.isle b/cranelift/codegen/src/opts/icmp.isle
index c472781721..a33eb52d09 100644
--- a/cranelift/codegen/src/opts/icmp.isle
+++ b/cranelift/codegen/src/opts/icmp.isle
@@ -317,10 +317,10 @@
(rule (simplify (bxor ty (ult ty x y) (ult ty y x))) (ne ty x y))
;; a < b && a > b = false
-(rule (simplify (band ty (sgt ty x y) (slt ty x y))) (iconst_u ty 0))
-(rule (simplify (band ty (slt ty x y) (sgt ty x y))) (iconst_u ty 0))
-(rule (simplify (band ty (ugt ty x y) (ult ty x y))) (iconst_u ty 0))
-(rule (simplify (band ty (ult ty x y) (ugt ty x y))) (iconst_u ty 0))
+(rule (simplify (band (ty_int_vec128 ty) (sgt ty x y) (slt ty x y))) (iconst_u ty 0))
+(rule (simplify (band (ty_int_vec128 ty) (slt ty x y) (sgt ty x y))) (iconst_u ty 0))
+(rule (simplify (band (ty_int_vec128 ty) (ugt ty x y) (ult ty x y))) (iconst_u ty 0))
+(rule (simplify (band (ty_int_vec128 ty) (ult ty x y) (ugt ty x y))) (iconst_u ty 0))
(rule
(simplify (band ty (sgt ty x (iconst_s _ y)) (ult ty x (iconst_s _ y))))
(if-let true (i64_gt_eq y 0))
diff --git a/cranelift/codegen/src/prelude.isle b/cranelift/codegen/src/prelude.isle
index cef9ac2c52..e9832f815c 100644
--- a/cranelift/codegen/src/prelude.isle
+++ b/cranelift/codegen/src/prelude.isle
@@ -516,6 +516,10 @@
(decl ty_vec128 (Type) Type)
(extern extractor ty_vec128 ty_vec128)
+;; An extractor that matches types that support iconst_s/iconst_u construction.
+(decl ty_int_vec128 (Type) Type)
+(extern extractor ty_int_vec128 ty_int_vec128)
+
;; An extractor that only matches dynamic vector types with a 64-bit
;; base type.
(decl ty_dyn_vec64 (Type) Type)
diff --git a/cranelift/filetests/filetests/egraph/bitops.clif b/cranelift/filetests/filetests/egraph/bitops.clif
index 8f33aea6d3..7db5432685 100644
--- a/cranelift/filetests/filetests/egraph/bitops.clif
+++ b/cranelift/filetests/filetests/egraph/bitops.clif
@@ -204,6 +204,14 @@ block0(v0: i32x4):
; check: v5 = vconst.i32x4 const0
; check: return v5
+function %issue_13229_band_not_f64(f64) {
+block0(v0: f64):
+ v1 = band_not v0, v0
+ return
+}
+
+; check: return
+
function %bitops_bmask(i64) -> i64 {
block0(v0: i64):
v1 = bnot v0