Skip to content

Commit f5b2403

Browse files
mmcloughlinvaishuc17avanhatt
authored
aarch64: verify popcnt rules (bytecodealliance#217)
Verify `popcnt` rules. Requires a slight rewrite of the `mov_to_fpu` rules to avoid recursion, and therefore allow chaining. Extracted from original implementation in bytecodealliance#189. Updates avanhatt#34 Co-authored-by: Vaishu Chintam <[email protected]> Co-authored-by: Alexa VanHattum <[email protected]>
1 parent 214a0fa commit f5b2403

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

cranelift/codegen/src/inst_specs.isle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@
235235
(provide (= result (clz (rev x)))))
236236
(instantiate ctz bv_unary_8_to_64)
237237

238+
(spec (popcnt x)
239+
(provide (= result (popcnt x))))
240+
(instantiate popcnt bv_unary_8_to_64)
241+
238242
(spec (load flags p offset)
239243
(modifies clif_load loaded_value)
240244
(provide

cranelift/codegen/src/isa/aarch64/inst.isle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2869,13 +2869,16 @@
28692869

28702870
;; Helper for emitting `MInst.MovToFpu` instructions.
28712871
(decl mov_to_fpu (Reg ScalarSize) Reg)
2872+
(attr mov_to_fpu (veri chain))
28722873
(rule (mov_to_fpu x size)
28732874
(let ((dst WritableReg (temp_writable_reg $I8X16))
28742875
(_ Unit (emit (MInst.MovToFpu dst x size))))
28752876
dst))
28762877
(rule 1 (mov_to_fpu x (ScalarSize.Size16))
28772878
(if-let $false (use_fp16))
2878-
(mov_to_fpu x (ScalarSize.Size32)))
2879+
(let ((dst WritableReg (temp_writable_reg $I8X16))
2880+
(_ Unit (emit (MInst.MovToFpu dst x (ScalarSize.Size32)))))
2881+
dst))
28792882

28802883
;; Helper for emitting `MInst.FpuMoveFPImm` instructions.
28812884
(decl fpu_move_fp_imm (ASIMDFPModImm ScalarSize) Reg)

cranelift/codegen/src/isa/aarch64/lower.isle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,25 +1899,25 @@
18991899
;; if ty == i128:
19001900
;; mov out_hi, 0
19011901

1902-
(rule (lower (has_type $I8 (popcnt x)))
1902+
(rule popcnt_8 (lower (has_type $I8 (popcnt x)))
19031903
(let ((tmp Reg (mov_to_fpu x (ScalarSize.Size32)))
19041904
(nbits Reg (vec_cnt tmp (VectorSize.Size8x8))))
19051905
(mov_from_vec nbits 0 (ScalarSize.Size8))))
19061906

19071907
;; Note that this uses `addp` instead of `addv` as it's usually cheaper.
1908-
(rule (lower (has_type $I16 (popcnt x)))
1908+
(rule popcnt_16 (lower (has_type $I16 (popcnt x)))
19091909
(let ((tmp Reg (mov_to_fpu x (ScalarSize.Size32)))
19101910
(nbits Reg (vec_cnt tmp (VectorSize.Size8x8)))
19111911
(added Reg (addp nbits nbits (VectorSize.Size8x8))))
19121912
(mov_from_vec added 0 (ScalarSize.Size8))))
19131913

1914-
(rule (lower (has_type $I32 (popcnt x)))
1914+
(rule popcnt_32 (lower (has_type $I32 (popcnt x)))
19151915
(let ((tmp Reg (mov_to_fpu x (ScalarSize.Size32)))
19161916
(nbits Reg (vec_cnt tmp (VectorSize.Size8x8)))
19171917
(added Reg (addv nbits (VectorSize.Size8x8))))
19181918
(mov_from_vec added 0 (ScalarSize.Size8))))
19191919

1920-
(rule (lower (has_type $I64 (popcnt x)))
1920+
(rule popcnt_64 (lower (has_type $I64 (popcnt x)))
19211921
(let ((tmp Reg (mov_to_fpu x (ScalarSize.Size64)))
19221922
(nbits Reg (vec_cnt tmp (VectorSize.Size8x8)))
19231923
(added Reg (addv nbits (VectorSize.Size8x8))))

cranelift/codegen/src/prelude.isle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@
409409

410410
(extern const $I8X8 Type)
411411
(extern const $I8X16 Type)
412+
(model I8X16 (const 128))
412413
(extern const $I16X4 Type)
413414
(extern const $I16X8 Type)
414415
(extern const $I32X2 Type)

0 commit comments

Comments
 (0)