Skip to content

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

cranelift(aarch64): fold an i32x4 horizontal add into a single addv (#13717)

Damilare Darmie Akinlaja5937fbcd7b6b
diff --git a/cranelift/codegen/src/isa/aarch64/lower.isle b/cranelift/codegen/src/isa/aarch64/lower.isle
index ffffd57f95..d733c8e095 100644
--- a/cranelift/codegen/src/isa/aarch64/lower.isle
+++ b/cranelift/codegen/src/isa/aarch64/lower.isle
@@ -2772,6 +2772,15 @@
(u8_from_uimm8 lane)))
(fpu_move_from_vec val lane (vector_size vty)))
+;; A full `i32x4` horizontal add — two (GVN-shared) `iadd_pairwise`es then a
+;; lane-0 extract — is a single `addv`, replacing `addp; addp; umov`.
+(rule 3 (lower (extractlane $I32
+ (iadd_pairwise $I32X4
+ (iadd_pairwise $I32X4 x x)
+ (iadd_pairwise $I32X4 x x))
+ (u8_from_uimm8 0)))
+ (mov_from_vec (addv x (vector_size $I32X4)) 0 (scalar_size $I32)))
+
;;; Rules for `insertlane` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(rule 1 (lower (insertlane _ vec @ (value_type vty)
diff --git a/cranelift/filetests/filetests/isa/aarch64/simd-addv-reduce.clif b/cranelift/filetests/filetests/isa/aarch64/simd-addv-reduce.clif
new file mode 100644
index 0000000000..aacb3c3ac6
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/aarch64/simd-addv-reduce.clif
@@ -0,0 +1,25 @@
+test compile precise-output
+set unwind_info=false
+target aarch64
+
+;; A complete `i32x4` horizontal-add reduction contracts to a single `addv`.
+function %hsum_i32x4(i32x4) -> i32 {
+block0(v0: i32x4):
+ v1 = iadd_pairwise v0, v0
+ v2 = iadd_pairwise v1, v1
+ v3 = extractlane v2, 0
+ return v3
+}
+
+; VCode:
+; block0:
+; addv s2, v0.4s
+; mov w0, v2.s[0]
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; addv s2, v0.4s
+; mov w0, v2.s[0]
+; ret
+
diff --git a/cranelift/filetests/filetests/runtests/simd-addv-reduce.clif b/cranelift/filetests/filetests/runtests/simd-addv-reduce.clif
new file mode 100644
index 0000000000..8ada1e67a5
--- /dev/null
+++ b/cranelift/filetests/filetests/runtests/simd-addv-reduce.clif
@@ -0,0 +1,19 @@
+test interpret
+test run
+target aarch64
+target x86_64 has_sse3 has_ssse3 has_sse41
+target s390x
+
+;; A complete `i32x4` horizontal-add reduction, expressed as two pairwise adds
+;; plus a lane-0 extract. On aarch64 this lowers to a single `addv`.
+function %hsum_i32x4(i32x4) -> i32 {
+block0(v0: i32x4):
+ v1 = iadd_pairwise v0, v0
+ v2 = iadd_pairwise v1, v1
+ v3 = extractlane v2, 0
+ return v3
+}
+; run: %hsum_i32x4([1 2 3 4]) == 10
+; run: %hsum_i32x4([10 -5 100 -1]) == 104
+; run: %hsum_i32x4([0 0 0 0]) == 0
+; run: %hsum_i32x4([-1 -1 -1 -1]) == -4