Skip to content

Commit 5c888e7

Browse files
gascheclef-men
andcommitted
[refactor] simplify the definition of atomic functions
In trunk, all atomic functions exposed in the runtime are also exposed as language primitives in our intermediate representations (lambda, clambda). But except for `Patomic_load`, which benefits from dedicated code generation, they are all transformed into C calls on all backends. The present PR simplifies the code noticeably by removing the intermediate primitives, by producing C calls directly in lambda/translprim.ml. This reduces the amount of boilerplate to modify to implement atomic record fields (ocaml/RFCs#39). Co-authored-by: Clément Allain <clef-men@orange.fr>
1 parent 4eb993c commit 5c888e7

14 files changed

+19
-68
lines changed

asmcomp/cmmgen.ml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,7 @@ let rec transl env e =
508508
| ((Pfield_computed|Psequand
509509
| Prunstack | Pperform | Presume | Preperform
510510
| Pdls_get
511-
| Patomic_load _ | Patomic_exchange
512-
| Patomic_cas | Patomic_fetch_add
511+
| Patomic_load _
513512
| Psequor | Pnot | Pnegint | Paddint | Psubint
514513
| Pmulint | Pandint | Porint | Pxorint | Plslint
515514
| Plsrint | Pasrint | Pintoffloat | Pfloatofint
@@ -841,7 +840,6 @@ and transl_prim_1 env p arg dbg =
841840
return_unit dbg (Cop(Cpoll, [], dbg))))
842841
| (Pfield_computed | Psequand | Psequor
843842
| Prunstack | Presume | Preperform
844-
| Patomic_exchange | Patomic_cas | Patomic_fetch_add
845843
| Paddint | Psubint | Pmulint | Pandint
846844
| Porint | Pxorint | Plslint | Plsrint | Pasrint
847845
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
@@ -1025,14 +1023,8 @@ and transl_prim_2 env p arg1 arg2 dbg =
10251023
tag_int (Cop(Ccmpi cmp,
10261024
[transl_unbox_int dbg env bi arg1;
10271025
transl_unbox_int dbg env bi arg2], dbg)) dbg
1028-
| Patomic_exchange ->
1029-
Cop (Cextcall ("caml_atomic_exchange", typ_val, [], false),
1030-
[transl env arg1; transl env arg2], dbg)
1031-
| Patomic_fetch_add ->
1032-
Cop (Cextcall ("caml_atomic_fetch_add", typ_int, [], false),
1033-
[transl env arg1; transl env arg2], dbg)
10341026
| Prunstack | Pperform | Presume | Preperform | Pdls_get
1035-
| Patomic_cas | Patomic_load _
1027+
| Patomic_load _
10361028
| Pnot | Pnegint | Pintoffloat | Pfloatofint | Pnegfloat
10371029
| Pabsfloat | Pstringlength | Pbyteslength | Pbytessetu | Pbytessets
10381030
| Pisint | Pbswap16 | Pint_as_pointer | Popaque | Pread_symbol _
@@ -1084,10 +1076,6 @@ and transl_prim_3 env p arg1 arg2 arg3 dbg =
10841076
bigstring_set size unsafe (transl env arg1) (transl env arg2)
10851077
(transl_unbox_sized size dbg env arg3) dbg
10861078

1087-
| Patomic_cas ->
1088-
Cop (Cextcall ("caml_atomic_cas", typ_int, [], false),
1089-
[transl env arg1; transl env arg2; transl env arg3], dbg)
1090-
10911079
(* Effects *)
10921080

10931081
| Prunstack ->
@@ -1103,7 +1091,7 @@ and transl_prim_3 env p arg1 arg2 arg3 dbg =
11031091
dbg)
11041092

11051093
| Pperform | Pdls_get | Presume
1106-
| Patomic_exchange | Patomic_fetch_add | Patomic_load _
1094+
| Patomic_load _
11071095
| Pfield_computed | Psequand | Psequor | Pnot | Pnegint | Paddint
11081096
| Psubint | Pmulint | Pandint | Porint | Pxorint | Plslint | Plsrint | Pasrint
11091097
| Pintoffloat | Pfloatofint | Pnegfloat | Pabsfloat | Paddfloat | Psubfloat
@@ -1134,9 +1122,9 @@ and transl_prim_4 env p arg1 arg2 arg3 arg4 dbg =
11341122
dbg)
11351123
| Psetfield_computed _
11361124
| Pbytessetu | Pbytessets | Parraysetu _
1137-
| Parraysets _ | Pbytes_set _ | Pbigstring_set _ | Patomic_cas
1125+
| Parraysets _ | Pbytes_set _ | Pbigstring_set _
11381126
| Prunstack | Preperform | Pperform | Pdls_get
1139-
| Patomic_exchange | Patomic_fetch_add | Patomic_load _
1127+
| Patomic_load _
11401128
| Pfield_computed | Psequand | Psequor | Pnot | Pnegint | Paddint
11411129
| Psubint | Pmulint | Pandint | Porint | Pxorint | Plslint | Plsrint | Pasrint
11421130
| Pintoffloat | Pfloatofint | Pnegfloat | Pabsfloat | Paddfloat | Psubfloat

bytecomp/bytegen.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ let preserve_tailcall_for_prim = function
164164
| Pbytes_set_64 _ | Pbigstring_load_16 _ | Pbigstring_load_32 _
165165
| Pbigstring_load_64 _ | Pbigstring_set_16 _ | Pbigstring_set_32 _
166166
| Pbigstring_set_64 _ | Pctconst _ | Pbswap16 | Pbbswap _ | Pint_as_pointer
167-
| Patomic_exchange | Patomic_cas | Patomic_fetch_add | Patomic_load _
167+
| Patomic_load _
168168
| Pdls_get ->
169169
false
170170

@@ -489,9 +489,6 @@ let comp_primitive stack_info p sz args =
489489
| Pbytes_to_string -> Kccall("caml_string_of_bytes", 1)
490490
| Pbytes_of_string -> Kccall("caml_bytes_of_string", 1)
491491
| Patomic_load _ -> Kccall("caml_atomic_load", 1)
492-
| Patomic_exchange -> Kccall("caml_atomic_exchange", 2)
493-
| Patomic_cas -> Kccall("caml_atomic_cas", 3)
494-
| Patomic_fetch_add -> Kccall("caml_atomic_fetch_add", 2)
495492
| Pdls_get -> Kccall("caml_domain_dls_get", 1)
496493
| Ppoll -> Kccall("caml_process_pending_actions_with_root", 1)
497494
(* The cases below are handled in [comp_expr] before the [comp_primitive] call

lambda/lambda.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ type primitive =
144144
| Pint_as_pointer
145145
(* Atomic operations *)
146146
| Patomic_load of {immediate_or_pointer : immediate_or_pointer}
147-
| Patomic_exchange
148-
| Patomic_cas
149-
| Patomic_fetch_add
150147
(* Inhibition of optimisation *)
151148
| Popaque
152149
(* Fetching domain-local state *)

lambda/lambda.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ type primitive =
154154
| Pint_as_pointer
155155
(* Atomic operations *)
156156
| Patomic_load of {immediate_or_pointer : immediate_or_pointer}
157-
| Patomic_exchange
158-
| Patomic_cas
159-
| Patomic_fetch_add
160157
(* Inhibition of optimisation *)
161158
| Popaque
162159
(* Fetching domain-local state *)

lambda/printlambda.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@ let primitive ppf = function
354354
(match immediate_or_pointer with
355355
| Immediate -> fprintf ppf "atomic_load_imm"
356356
| Pointer -> fprintf ppf "atomic_load_ptr")
357-
| Patomic_exchange -> fprintf ppf "atomic_exchange"
358-
| Patomic_cas -> fprintf ppf "atomic_cas"
359-
| Patomic_fetch_add -> fprintf ppf "atomic_fetch_add"
360357
| Popaque -> fprintf ppf "opaque"
361358
| Pdls_get -> fprintf ppf "dls_get"
362359
| Ppoll -> fprintf ppf "poll"
@@ -466,9 +463,6 @@ let name_of_primitive = function
466463
(match immediate_or_pointer with
467464
| Immediate -> "atomic_load_imm"
468465
| Pointer -> "atomic_load_ptr")
469-
| Patomic_exchange -> "Patomic_exchange"
470-
| Patomic_cas -> "Patomic_cas"
471-
| Patomic_fetch_add -> "Patomic_fetch_add"
472466
| Popaque -> "Popaque"
473467
| Prunstack -> "Prunstack"
474468
| Presume -> "Presume"

lambda/tmc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ let rec choice ctx t =
865865
| Prunstack | Pperform | Presume | Preperform | Pdls_get
866866

867867
(* we don't handle atomic primitives *)
868-
| Patomic_exchange | Patomic_cas | Patomic_fetch_add | Patomic_load _
868+
| Patomic_load _
869869

870870
(* we don't handle array indices as destinations yet *)
871871
| (Pmakearray _ | Pduparray _)

lambda/translprim.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ let gen_array_kind =
113113
let prim_sys_argv =
114114
Primitive.simple ~name:"caml_sys_argv" ~arity:1 ~alloc:true
115115

116+
let prim_atomic_exchange =
117+
Primitive.simple ~name:"caml_atomic_exchange" ~arity:2 ~alloc:false
118+
let prim_atomic_cas =
119+
Primitive.simple ~name:"caml_atomic_cas" ~arity:3 ~alloc:false
120+
let prim_atomic_fetch_add =
121+
Primitive.simple ~name:"caml_atomic_fetch_add" ~arity:2 ~alloc:false
122+
123+
116124
let primitives_table =
117125
create_hashtable 57 [
118126
"%identity", Identity;
@@ -366,9 +374,9 @@ let primitives_table =
366374
"%compare", Comparison(Compare, Compare_generic);
367375
"%atomic_load",
368376
Primitive ((Patomic_load {immediate_or_pointer=Pointer}), 1);
369-
"%atomic_exchange", Primitive (Patomic_exchange, 2);
370-
"%atomic_cas", Primitive (Patomic_cas, 3);
371-
"%atomic_fetch_add", Primitive (Patomic_fetch_add, 2);
377+
"%atomic_exchange", External prim_atomic_exchange;
378+
"%atomic_cas", External prim_atomic_cas;
379+
"%atomic_fetch_add", External prim_atomic_fetch_add;
372380
"%runstack", Primitive (Prunstack, 3);
373381
"%reperform", Primitive (Preperform, 3);
374382
"%perform", Primitive (Pperform, 1);
@@ -826,7 +834,7 @@ let lambda_primitive_needs_event_after = function
826834
| Pfloatcomp _ | Pstringlength | Pstringrefu | Pbyteslength | Pbytesrefu
827835
| Pbytessetu | Pmakearray ((Pintarray | Paddrarray | Pfloatarray), _)
828836
| Parraylength _ | Parrayrefu _ | Parraysetu _ | Pisint | Pisout
829-
| Patomic_exchange | Patomic_cas | Patomic_fetch_add | Patomic_load _
837+
| Patomic_load _
830838
| Pintofbint _ | Pctconst _ | Pbswap16 | Pint_as_pointer | Popaque | Pdls_get
831839
-> false
832840

lambda/value_rec_compiler.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ let compute_static_size lam =
305305
| Pbbswap _
306306
| Pint_as_pointer
307307
| Patomic_load _
308-
| Patomic_exchange
309-
| Patomic_cas
310-
| Patomic_fetch_add
311308
| Popaque
312309
| Pdls_get ->
313310
dynamic_size ()

middle_end/clambda_primitives.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ type primitive =
121121
| Pint_as_pointer
122122
(* Atomic operations *)
123123
| Patomic_load of {immediate_or_pointer : immediate_or_pointer}
124-
| Patomic_exchange
125-
| Patomic_cas
126-
| Patomic_fetch_add
127124
(* Inhibition of optimisation *)
128125
| Popaque
129126
(* Fetch domain-local state *)

middle_end/clambda_primitives.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ type primitive =
124124
| Pint_as_pointer
125125
(* Atomic operations *)
126126
| Patomic_load of {immediate_or_pointer : immediate_or_pointer}
127-
| Patomic_exchange
128-
| Patomic_cas
129-
| Patomic_fetch_add
130127
(* Inhibition of optimisation *)
131128
| Popaque
132129
(* Fetch domain-local state *)

0 commit comments

Comments
 (0)