Skip to content

Commit 736de74

Browse files
authored
Backend changes for local allocations with Flambda 2 (#493)
1 parent 20ed11a commit 736de74

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

backend/amd64/proc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,5 @@ let operation_supported = function
465465
| Cfloatofint | Cintoffloat | Ccmpf _
466466
| Craise _
467467
| Ccheckbound
468-
| Cprobe _ | Cprobe_is_enabled _ | Copaque
468+
| Cprobe _ | Cprobe_is_enabled _ | Copaque | Cbeginregion | Cendregion
469469
-> true

backend/arm64/proc.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,5 @@ let operation_supported = function
325325
| Craise _
326326
| Ccheckbound
327327
| Cprobe _ | Cprobe_is_enabled _ | Copaque
328+
| Cbeginregion | Cendregion
328329
-> true

backend/cmm.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ and operation =
202202
| Cprobe of { name: string; handler_code_sym: string; }
203203
| Cprobe_is_enabled of { name: string }
204204
| Copaque
205+
| Cbeginregion | Cendregion
205206

206207
type expression =
207208
Cconst_int of int * Debuginfo.t

backend/cmm.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ and operation =
207207
| Cprobe of { name: string; handler_code_sym: string; }
208208
| Cprobe_is_enabled of { name: string }
209209
| Copaque (* Sys.opaque_identity *)
210+
| Cbeginregion | Cendregion
210211

211212
(** Every basic block should have a corresponding [Debuginfo.t] for its
212213
beginning. *)

backend/cmm_helpers.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,6 @@ let unboxed_float_array_ref arr ofs dbg =
783783
let float_array_ref arr ofs dbg =
784784
box_float dbg Alloc_heap (unboxed_float_array_ref arr ofs dbg)
785785

786-
(* TODO support mutation of local arrays *)
787786
let addr_array_set arr ofs newval dbg =
788787
Cop(Cextcall { func = "caml_modify"; ty = typ_void; alloc = false;
789788
builtin = false;
@@ -793,6 +792,15 @@ let addr_array_set arr ofs newval dbg =
793792
ty_args = []},
794793
[array_indexing log2_size_addr arr ofs dbg; newval], dbg)
795794

795+
let addr_array_set_local arr ofs newval dbg =
796+
Cop(Cextcall { func = "caml_modify_local"; ty = typ_void; alloc = false;
797+
builtin = false;
798+
returns = true;
799+
effects = Arbitrary_effects;
800+
coeffects = Has_coeffects;
801+
ty_args = []},
802+
[arr; untag_int ofs dbg; newval], dbg)
803+
796804
let addr_array_initialize arr ofs newval dbg =
797805
Cop(Cextcall { func = "caml_initialize";
798806
builtin = false;
@@ -903,7 +911,7 @@ let make_alloc_generic ~mode set_fn dbg tag wordsize args =
903911
fill_fields 1 args)
904912
end
905913

906-
let make_alloc ?(mode=Lambda.Alloc_heap) dbg tag args =
914+
let make_alloc ~mode dbg tag args =
907915
let addr_array_init arr ofs newval dbg =
908916
Cop(Cextcall { func = "caml_initialize"; ty = typ_void; alloc = false;
909917
builtin = false;
@@ -915,7 +923,7 @@ let make_alloc ?(mode=Lambda.Alloc_heap) dbg tag args =
915923
in
916924
make_alloc_generic ~mode addr_array_init dbg tag (List.length args) args
917925

918-
let make_float_alloc ?(mode=Lambda.Alloc_heap) dbg tag args =
926+
let make_float_alloc ~mode dbg tag args =
919927
make_alloc_generic ~mode float_array_set dbg tag
920928
(List.length args * size_float / size_addr) args
921929

@@ -2665,9 +2673,7 @@ let setfield_computed ptr init arg1 arg2 arg3 dbg =
26652673
| Caml_modify ->
26662674
return_unit dbg (addr_array_set arg1 arg2 arg3 dbg)
26672675
| Caml_modify_local ->
2668-
(* TODO: support this, if there are any uses.
2669-
(Currently, setfield_computed is only used by classes) *)
2670-
Misc.fatal_error "setfield_computed: local"
2676+
return_unit dbg (addr_array_set_local arg1 arg2 arg3 dbg)
26712677
| Simple ->
26722678
return_unit dbg (int_array_set arg1 arg2 arg3 dbg)
26732679

backend/cmm_helpers.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ val unboxed_float_array_ref :
267267
val float_array_ref : expression -> expression -> Debuginfo.t -> expression
268268
val addr_array_set :
269269
expression -> expression -> expression -> Debuginfo.t -> expression
270+
val addr_array_set_local :
271+
expression -> expression -> expression -> Debuginfo.t -> expression
270272
val addr_array_initialize :
271273
expression -> expression -> expression -> Debuginfo.t -> expression
272274
val int_array_set :
@@ -309,11 +311,11 @@ val call_cached_method :
309311

310312
(** Allocate a block of regular values with the given tag *)
311313
val make_alloc :
312-
?mode:Lambda.alloc_mode -> Debuginfo.t -> int -> expression list -> expression
314+
mode:Lambda.alloc_mode -> Debuginfo.t -> int -> expression list -> expression
313315

314316
(** Allocate a block of unboxed floats with the given tag *)
315317
val make_float_alloc :
316-
?mode:Lambda.alloc_mode -> Debuginfo.t -> int -> expression list -> expression
318+
mode:Lambda.alloc_mode -> Debuginfo.t -> int -> expression list -> expression
317319

318320
(** Bounds checking *)
319321

backend/cmmgen.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ let rec transl env e =
437437
| [] -> Debuginfo.none
438438
| fundecl::_ -> fundecl.dbg
439439
in
440-
make_alloc dbg Obj.closure_tag (transl_fundecls 0 fundecls)
440+
make_alloc ~mode:Alloc_heap dbg Obj.closure_tag (transl_fundecls 0 fundecls)
441441
| Uoffset(arg, offset) ->
442442
(* produces a valid Caml value, pointing just after an infix header *)
443443
let ptr = transl env arg in

backend/printcmm.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ let operation d = function
207207
Printf.sprintf "prefetch is_write=%b prefetch_temporal_locality_hint=%s"
208208
is_write (temporal_locality locality)
209209
| Copaque -> "opaque"
210+
| Cbeginregion -> "beginregion"
211+
| Cendregion -> "endregion"
210212

211213
let rec expr ppf = function
212214
| Cconst_int (n, _dbg) -> fprintf ppf "%i" n

backend/selectgen.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ let oper_result_type = function
185185
| Cprobe _ -> typ_void
186186
| Cprobe_is_enabled _ -> typ_int
187187
| Copaque -> typ_val
188+
| Cbeginregion ->
189+
(* This must not be typ_val; the begin-region operation returns a
190+
naked pointer into the local allocation stack. *)
191+
typ_int
192+
| Cendregion -> typ_void
188193

189194
(* Infer the size in bytes of the result of an expression whose evaluation
190195
may be deferred (cf. [emit_parts]). *)
@@ -441,7 +446,7 @@ method is_simple_expr = function
441446
| Capply _ | Cextcall _ | Calloc _ | Cstore _
442447
| Craise _ | Ccheckbound
443448
| Cprobe _ | Cprobe_is_enabled _ | Copaque -> false
444-
| Cprefetch _ -> false (* avoid reordering *)
449+
| Cprefetch _ | Cbeginregion | Cendregion -> false (* avoid reordering *)
445450
(* The remaining operations are simple if their args are *)
446451
| Cload _ | Caddi | Csubi | Cmuli | Cmulhi _ | Cdivi | Cmodi | Cand | Cor
447452
| Cxor | Clsl | Clsr | Casr | Ccmpi _ | Caddv | Cadda | Ccmpa _ | Cnegf
@@ -488,6 +493,7 @@ method effects_of exp =
488493
| Calloc Alloc_heap -> EC.none
489494
| Calloc Alloc_local -> EC.coeffect_only Coeffect.Arbitrary
490495
| Cstore _ -> EC.effect_only Effect.Arbitrary
496+
| Cbeginregion | Cendregion -> EC.arbitrary
491497
| Cprefetch _ -> EC.arbitrary
492498
| Craise _ | Ccheckbound -> EC.effect_only Effect.Raise
493499
| Cload (_, Asttypes.Immutable) -> EC.none
@@ -623,6 +629,8 @@ method select_operation op args _dbg =
623629
| (Cprobe { name; handler_code_sym; }, _) ->
624630
Iprobe { name; handler_code_sym; }, args
625631
| (Cprobe_is_enabled {name}, _) -> Iprobe_is_enabled {name}, []
632+
| (Cbeginregion, _) -> Ibeginregion, []
633+
| (Cendregion, _) -> Iendregion, args
626634
| _ -> Misc.fatal_error "Selection.select_oper"
627635

628636
method private select_arith_comm op = function

middle_end/flambda2/to_cmm/to_cmm_helper.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,22 @@ let check_alloc_fields = function
319319
let make_array ?(dbg = Debuginfo.none) kind args =
320320
check_alloc_fields args;
321321
match (kind : Flambda_primitive.Array_kind.t) with
322-
| Immediates | Values -> make_alloc dbg 0 args
323-
| Naked_floats -> make_float_alloc dbg (Tag.to_int Tag.double_array_tag) args
322+
| Immediates | Values -> make_alloc ~mode:Alloc_heap dbg 0 args
323+
| Naked_floats ->
324+
make_float_alloc ~mode:Alloc_heap dbg (Tag.to_int Tag.double_array_tag) args
324325

325326
let make_block ?(dbg = Debuginfo.none) kind args =
326327
check_alloc_fields args;
327328
match (kind : Flambda_primitive.Block_kind.t) with
328-
| Values (tag, _) -> make_alloc dbg (Tag.Scannable.to_int tag) args
329-
| Naked_floats -> make_float_alloc dbg (Tag.to_int Tag.double_array_tag) args
329+
| Values (tag, _) ->
330+
make_alloc ~mode:Alloc_heap dbg (Tag.Scannable.to_int tag) args
331+
| Naked_floats ->
332+
make_float_alloc ~mode:Alloc_heap dbg (Tag.to_int Tag.double_array_tag) args
330333

331334
let make_closure_block ?(dbg = Debuginfo.none) l =
332335
assert (List.compare_length_with l 0 > 0);
333336
let tag = Tag.(to_int closure_tag) in
334-
make_alloc dbg tag l
337+
make_alloc ~mode:Alloc_heap dbg tag l
335338

336339
(* Block access *)
337340

0 commit comments

Comments
 (0)