Skip to content

Commit 6284de0

Browse files
TheNumbatgretay-js
andauthored
Refactor amd64 simd (ocaml-flambda#3907)
* Disable warning 4 on SIMD instructions only * Move warning "-66" back to one "open" * Stronger condition for [Simd.Seq.equal] * Minor refactor of [Simd.Seq] * Format arm64/regalloc_stack_operands.ml * Refactor Simd.operation * Add a CR * Refactor Simd_selection.pseudoregs_for_operation * Minor refactor in Emit * Refactor SSE string operations: instroduce Pcompare_string layer * minor edit * match on seq id --------- Co-authored-by: Greta Yorsh <45005955+gretay-js@users.noreply.github.com>
1 parent cad7b76 commit 6284de0

File tree

9 files changed

+410
-376
lines changed

9 files changed

+410
-376
lines changed

backend/.ocamlformat-enable

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ amd64/simd*.ml
99
amd64/stack_check.ml
1010
amd64/stack_class.ml
1111
amd64/vectorize_specific.ml
12+
amd64/regalloc_stack_operands.ml
1213
arm64/cfg_selection.ml
1314
arm64/emit.ml
1415
arm64/reg_class.ml

backend/amd64/emit.ml

+42-44
Original file line numberDiff line numberDiff line change
@@ -1452,21 +1452,30 @@ let check_simd_instr (simd : Simd.instr) imm instr =
14521452
| First_arg -> assert (Reg.same_loc instr.arg.(0) instr.res.(0))
14531453
| Res { loc; _ } -> assert_loc loc instr.res.(0)
14541454

1455+
let to_arg_with_width loc instr i =
1456+
match Simd.loc_requires_width loc with
1457+
| Some Eight -> arg8 instr i
1458+
| Some Sixteen -> arg16 instr i
1459+
| Some Thirtytwo -> arg32 instr i
1460+
| Some Sixtyfour | None -> arg instr i
1461+
1462+
let to_res_with_width loc instr i =
1463+
match Simd.loc_requires_width loc with
1464+
| Some Eight -> res8 instr i
1465+
| Some Sixteen -> res16 instr i
1466+
| Some Thirtytwo -> res32 instr i
1467+
| Some Sixtyfour | None -> res instr i
1468+
14551469
let emit_simd_instr (simd : Simd.instr) imm instr =
14561470
check_simd_instr simd imm instr;
14571471
let total_args = Array.length instr.arg in
14581472
if total_args <> Array.length simd.args
14591473
then Misc.fatal_errorf "wrong number of arguments for %s" simd.mnemonic;
14601474
let args =
1461-
List.init total_args (fun j ->
1462-
if Simd.arg_is_implicit simd.args.(j)
1475+
List.init total_args (fun i ->
1476+
if Simd.arg_is_implicit simd.args.(i)
14631477
then None
1464-
else
1465-
match Simd.loc_requires_width simd.args.(j).loc with
1466-
| Some Eight -> Some (arg8 instr j)
1467-
| Some Sixteen -> Some (arg16 instr j)
1468-
| Some Thirtytwo -> Some (arg32 instr j)
1469-
| Some Sixtyfour | None -> Some (arg instr j))
1478+
else Some (to_arg_with_width simd.args.(i).loc instr i))
14701479
|> List.filter_map (fun arg -> arg)
14711480
in
14721481
let args =
@@ -1475,13 +1484,7 @@ let emit_simd_instr (simd : Simd.instr) imm instr =
14751484
| Res { loc; enc = RM_r | RM_rm | Vex_v } -> (
14761485
match Simd.loc_is_pinned loc with
14771486
| Some _ -> args
1478-
| None ->
1479-
(match Simd.loc_requires_width loc with
1480-
| Some Eight -> res8 instr 0
1481-
| Some Sixteen -> res16 instr 0
1482-
| Some Thirtytwo -> res32 instr 0
1483-
| Some Sixtyfour | None -> res instr 0)
1484-
:: args)
1487+
| None -> to_res_with_width loc instr 0 :: args)
14851488
in
14861489
let args =
14871490
match imm with
@@ -1490,38 +1493,33 @@ let emit_simd_instr (simd : Simd.instr) imm instr =
14901493
in
14911494
I.simd simd (Array.of_list args)
14921495

1493-
let emit_simd (simd : Simd.operation) instr =
1494-
match simd with
1495-
| Instruction { instr = simd; imm } -> emit_simd_instr simd imm instr
1496-
| Sequence { seq; imm } -> (
1497-
(* Prefix *)
1498-
(match seq.id with
1496+
let emit_simd (op : Simd.operation) instr =
1497+
let imm = op.imm in
1498+
match op.instr with
1499+
| Instruction simd -> emit_simd_instr simd imm instr
1500+
| Sequence seq -> (
1501+
match seq.id with
14991502
| Sqrtss | Sqrtsd | Roundss | Roundsd ->
15001503
(* Avoids partial register stall *)
15011504
if not (equal_arg (arg instr 0) (res instr 0))
1502-
then I.xorpd (res instr 0) (res instr 0)
1503-
| Pcmpestra | Pcmpistra | Pcmpestrc | Pcmpistrc | Pcmpestro | Pcmpistro
1504-
| Pcmpestrs | Pcmpistrs | Pcmpestrz | Pcmpistrz ->
1505-
());
1506-
(* Instruction *)
1507-
emit_simd_instr seq.instr imm instr;
1508-
(* Suffix *)
1509-
match seq.id with
1510-
| Sqrtss | Sqrtsd | Roundss | Roundsd -> ()
1511-
| Pcmpestra | Pcmpistra ->
1512-
I.set A (res8 instr 0);
1513-
I.movzx (res8 instr 0) (res instr 0)
1514-
| Pcmpestrc | Pcmpistrc ->
1515-
I.set B (res8 instr 0);
1516-
I.movzx (res8 instr 0) (res instr 0)
1517-
| Pcmpestro | Pcmpistro ->
1518-
I.set O (res8 instr 0);
1519-
I.movzx (res8 instr 0) (res instr 0)
1520-
| Pcmpestrs | Pcmpistrs ->
1521-
I.set S (res8 instr 0);
1522-
I.movzx (res8 instr 0) (res instr 0)
1523-
| Pcmpestrz | Pcmpistrz ->
1524-
I.set E (res8 instr 0);
1505+
then I.xorpd (res instr 0) (res instr 0);
1506+
emit_simd_instr seq.instr imm instr
1507+
| Pcompare_string p ->
1508+
let cond : X86_ast.condition =
1509+
match p with
1510+
| Pcmpestra -> A
1511+
| Pcmpestrc -> B
1512+
| Pcmpestro -> O
1513+
| Pcmpestrs -> S
1514+
| Pcmpestrz -> E
1515+
| Pcmpistra -> A
1516+
| Pcmpistrc -> B
1517+
| Pcmpistro -> O
1518+
| Pcmpistrs -> S
1519+
| Pcmpistrz -> E
1520+
in
1521+
emit_simd_instr seq.instr imm instr;
1522+
I.set cond (res8 instr 0);
15251523
I.movzx (res8 instr 0) (res instr 0))
15261524

15271525
let emit_simd_instr_with_memory_arg (simd : Simd.Mem.operation) i addr =

backend/amd64/proc.ml

+4-6
Original file line numberDiff line numberDiff line change
@@ -436,15 +436,13 @@ let destroyed_by_simd_instr (instr : Simd.instr) =
436436
| None -> [||]
437437

438438
let destroyed_by_simd_op (op : Simd.operation) =
439-
match op with
440-
| Instruction { instr; _ } -> destroyed_by_simd_instr instr
441-
| Sequence { seq; _ } ->
439+
match op.instr with
440+
| Instruction instr -> destroyed_by_simd_instr instr
441+
| Sequence seq ->
442442
destroyed_by_simd_instr seq.instr
443443
|> Array.append
444444
(match seq.id with
445-
| Sqrtss | Sqrtsd | Roundss | Roundsd
446-
| Pcmpestra | Pcmpestrc | Pcmpestro | Pcmpestrs | Pcmpestrz
447-
| Pcmpistra | Pcmpistrc | Pcmpistro | Pcmpistrs | Pcmpistrz -> [||])
445+
| Sqrtss | Sqrtsd | Roundss | Roundsd | Pcompare_string _ -> [||])
448446

449447
let destroyed_by_simd_mem_op (instr : Simd.Mem.operation) =
450448
match instr with

0 commit comments

Comments
 (0)