Skip to content

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

cranelift: Simplify i8/i16 division by constants via i32 (#14795)

Pujitha Paladugub8c95e33366c
diff --git a/cranelift/codegen/src/opts/arithmetic.isle b/cranelift/codegen/src/opts/arithmetic.isle
index e500678727..afa325f9c3 100644
--- a/cranelift/codegen/src/opts/arithmetic.isle
+++ b/cranelift/codegen/src/opts/arithmetic.isle
@@ -118,6 +118,13 @@
(if-let false (u64_is_power_of_two d))
(apply_div_const_magic_u64 (Opcode.Udiv) x d))
+;; Narrow `udiv`s with constant divisors are performed in 32 bits so that they
+;; can use the same magic-number sequence and, importantly, so that the
+;; potentially-trapping skeleton instruction is removed.
+(rule (simplify_skeleton (udiv x (iconst_u (ty_int (fits_in_16 ty)) (u64_extract_non_zero (u32_from_u64 d)))))
+ (if-let false (u32_is_power_of_two d))
+ (ireduce ty (apply_div_const_magic_u32 (Opcode.Udiv) (uextend $I32 x) d)))
+
;; General cases for `sdiv` with constant divisors
(rule (simplify_skeleton (sdiv x (iconst_s $I32 (i64_extract_non_zero (i32_from_i64 d)))))
(if-let false (i64_is_any_sign_power_of_two d))
@@ -126,6 +133,11 @@
(if-let false (i64_is_any_sign_power_of_two d))
(apply_div_const_magic_s64 (Opcode.Sdiv) x d))
+;; Same as above for narrow `sdiv`s.
+(rule (simplify_skeleton (sdiv x (iconst_s (ty_int (fits_in_16 ty)) (i64_extract_non_zero (i32_from_i64 d)))))
+ (if-let false (i64_is_any_sign_power_of_two (i32_into_i64 d)))
+ (ireduce ty (apply_div_const_magic_s32 (Opcode.Sdiv) (sextend $I32 x) d)))
+
;; x % 1 == 0
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (all_zero ty))
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (all_zero ty))
diff --git a/cranelift/filetests/filetests/egraph/sdiv.clif b/cranelift/filetests/filetests/egraph/sdiv.clif
index 4fcda5f006..0c78a4e195 100644
--- a/cranelift/filetests/filetests/egraph/sdiv.clif
+++ b/cranelift/filetests/filetests/egraph/sdiv.clif
@@ -124,3 +124,32 @@ block0(v0: i64):
; return v9
; }
+
+function %sdiv8_dead(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 7
+ v2 = sdiv v0, v1
+ v3 = iconst.i8 0
+ return v3
+}
+
+; function %sdiv8_dead(i8) -> i8 fast {
+; block0(v0: i8):
+; v3 = iconst.i8 0
+; return v3 ; v3 = 0
+; }
+
+function %sdiv16_dead(i16) -> i16 {
+block0(v0: i16):
+ v1 = iconst.i16 -7
+ v2 = sdiv v0, v1
+ v3 = iconst.i16 0
+ return v3
+}
+
+; function %sdiv16_dead(i16) -> i16 fast {
+; block0(v0: i16):
+; v3 = iconst.i16 0
+; return v3 ; v3 = 0
+; }
+
diff --git a/cranelift/filetests/filetests/egraph/udiv.clif b/cranelift/filetests/filetests/egraph/udiv.clif
index 881c600c3d..b115ed8bd9 100644
--- a/cranelift/filetests/filetests/egraph/udiv.clif
+++ b/cranelift/filetests/filetests/egraph/udiv.clif
@@ -112,3 +112,32 @@ block0(v0: i64, v1: i8):
; v5 -> v9
; return v9
; }
+
+function %udiv8_dead(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 7
+ v2 = udiv v0, v1
+ v3 = iconst.i8 0
+ return v3
+}
+
+; function %udiv8_dead(i8) -> i8 fast {
+; block0(v0: i8):
+; v3 = iconst.i8 0
+; return v3 ; v3 = 0
+; }
+
+function %udiv16_dead(i16) -> i16 {
+block0(v0: i16):
+ v1 = iconst.i16 7
+ v2 = udiv v0, v1
+ v3 = iconst.i16 0
+ return v3
+}
+
+; function %udiv16_dead(i16) -> i16 fast {
+; block0(v0: i16):
+; v3 = iconst.i16 0
+; return v3 ; v3 = 0
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/div-const-narrow.clif b/cranelift/filetests/filetests/runtests/div-const-narrow.clif
new file mode 100644
index 0000000000..fd0a70b23d
--- /dev/null
+++ b/cranelift/filetests/filetests/runtests/div-const-narrow.clif
@@ -0,0 +1,62 @@
+test interpret
+test run
+set opt_level=speed
+target x86_64
+target aarch64
+target s390x
+target riscv64
+
+function %sdiv8_7(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 7
+ v2 = sdiv v0, v1
+ return v2
+}
+; run: %sdiv8_7(100) == 14
+; run: %sdiv8_7(-100) == -14
+; run: %sdiv8_7(-128) == -18
+; run: %sdiv8_7(127) == 18
+
+function %sdiv16_m7(i16) -> i16 {
+block0(v0: i16):
+ v1 = iconst.i16 -7
+ v2 = sdiv v0, v1
+ return v2
+}
+; run: %sdiv16_m7(30000) == -4285
+; run: %sdiv16_m7(-32768) == 4681
+
+function %udiv8_7(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 7
+ v2 = udiv v0, v1
+ return v2
+}
+; run: %udiv8_7(255) == 36
+; run: %udiv8_7(6) == 0
+
+function %udiv16_7(i16) -> i16 {
+block0(v0: i16):
+ v1 = iconst.i16 7
+ v2 = udiv v0, v1
+ return v2
+}
+; run: %udiv16_7(65535) == 9362
+
+function %udiv8_200(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 200
+ v2 = udiv v0, v1
+ return v2
+}
+; run: %udiv8_200(255) == 1
+; run: %udiv8_200(199) == 0
+
+function %sdiv8_m3(i8) -> i8 {
+block0(v0: i8):
+ v1 = iconst.i8 -3
+ v2 = sdiv v0, v1
+ return v2
+}
+; run: %sdiv8_m3(100) == -33
+; run: %sdiv8_m3(-128) == 42