Skip to content

Commit 5d6cb4c

Browse files
TheNumbatmshinwell
andauthored
flambda-backend: Support DLS API (single-domain only) (#1978)
Co-authored-by: Mark Shinwell <[email protected]>
1 parent 0e380c9 commit 5d6cb4c

File tree

14 files changed

+1959
-1903
lines changed

14 files changed

+1959
-1903
lines changed

stdlib/domain.ml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,57 @@
1616
(* *)
1717
(**************************************************************************)
1818

19+
(* CR ocaml 5 runtime: domain-local-storage assumes single-domain,
20+
i.e. calling split will never be necessary. *)
21+
22+
module DLS = struct
23+
24+
let unique_value = Obj.repr (ref 0)
25+
let state = ref (Array.make 8 unique_value)
26+
27+
type 'a key = int * (unit -> 'a)
28+
29+
let key_counter = ref 0
30+
31+
let new_key ?split_from_parent:_ init_orphan =
32+
let idx = !key_counter in
33+
key_counter := idx + 1;
34+
(idx, init_orphan)
35+
36+
(* If necessary, grow the current domain's local state array such that [idx]
37+
* is a valid index in the array. *)
38+
let maybe_grow idx =
39+
let st = !state in
40+
let sz = Array.length st in
41+
if idx < sz then st
42+
else begin
43+
let rec compute_new_size s =
44+
if idx < s then s else compute_new_size (2 * s)
45+
in
46+
let new_sz = compute_new_size sz in
47+
let new_st = Array.make new_sz unique_value in
48+
Array.blit st 0 new_st 0 sz;
49+
state := new_st;
50+
new_st
51+
end
52+
53+
let set (idx, _init) x =
54+
let st = maybe_grow idx in
55+
(* [Sys.opaque_identity] ensures that flambda does not look at the type of
56+
* [x], which may be a [float] and conclude that the [st] is a float array.
57+
* We do not want OCaml's float array optimisation kicking in here. *)
58+
st.(idx) <- Obj.repr (Sys.opaque_identity x)
59+
60+
let get (idx, init) =
61+
let st = maybe_grow idx in
62+
let v = st.(idx) in
63+
if v == unique_value then
64+
let v' = Obj.repr (init ()) in
65+
st.(idx) <- (Sys.opaque_identity v');
66+
Obj.magic v'
67+
else Obj.magic v
68+
end
69+
1970
(* CR ocaml 5 runtime: domains not supported on 4.x
2071
2172
module Raw = struct

stdlib/domain.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ val recommended_domain_count : unit -> int
9292
simultaneously (including domains already running).
9393
9494
The value returned is at least [1]. *)
95+
*)
9596

9697
module DLS : sig
9798
(** Domain-local Storage *)
@@ -146,5 +147,3 @@ module DLS : sig
146147
to [k], which cannot be restored later. *)
147148

148149
end
149-
150-
*)

stdlib/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
complex.mli
111111
digest.ml
112112
digest.mli
113+
domain.ml
114+
domain.mli
113115
either.ml
114116
either.mli
115117
ephemeron.ml
@@ -245,6 +247,9 @@
245247
.stdlib.objs/byte/stdlib__Digest.cmi
246248
.stdlib.objs/byte/stdlib__Digest.cmt
247249
.stdlib.objs/byte/stdlib__Digest.cmti
250+
.stdlib.objs/byte/stdlib__Domain.cmi
251+
.stdlib.objs/byte/stdlib__Domain.cmt
252+
.stdlib.objs/byte/stdlib__Domain.cmti
248253
.stdlib.objs/byte/stdlib__Either.cmi
249254
.stdlib.objs/byte/stdlib__Either.cmt
250255
.stdlib.objs/byte/stdlib__Either.cmti
@@ -466,6 +471,7 @@
466471
.stdlib.objs/native/stdlib__Marshal.cmx
467472
.stdlib.objs/native/stdlib__BytesLabels.cmx
468473
.stdlib.objs/native/stdlib__Digest.cmx
474+
.stdlib.objs/native/stdlib__Domain.cmx
469475
.stdlib.objs/native/stdlib__Atomic.cmx
470476
.stdlib.objs/native/stdlib__Effect.cmx
471477
.stdlib.objs/native/stdlib__Either.cmx

stdlib/stdlib.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ module Complex = Complex
629629
module Condition = Condition
630630
*)
631631
module Digest = Digest
632+
module Domain = Domain
632633
(* CR ocaml 5 runtime:
633634
BACKPORT
634-
module Domain = Domain
635635
module Effect = Effect
636636
*)
637637
module Either = Either

stdlib/stdlib.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,13 +1408,13 @@ module Complex = Complex
14081408
module Condition = Condition
14091409
*)
14101410
module Digest = Digest
1411-
(* CR ocaml 5 runtime:
1412-
BACKPORT
14131411
module Domain = Domain
14141412
[@@alert "-unstable"]
14151413
[@@alert unstable
14161414
"The Domain interface may change in incompatible ways in the future."
14171415
]
1416+
(* CR ocaml 5 runtime:
1417+
BACKPORT
14181418
module Effect = Effect
14191419
[@@alert "-unstable"]
14201420
[@@alert unstable

0 commit comments

Comments
 (0)