Skip to content

Commit 6a63906

Browse files
authored
flambda-backend: Add layout on Lregion (#1107)
1 parent c562fb3 commit 6a63906

File tree

10 files changed

+56
-41
lines changed

10 files changed

+56
-41
lines changed

bytecomp/bytegen.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ let rec size_of_lambda env = function
229229
| Lprim (Pduprecord (Record_float, size), _, _) -> RHS_floatblock size
230230
| Levent (lam, _) -> size_of_lambda env lam
231231
| Lsequence (_lam, lam') -> size_of_lambda env lam'
232-
| Lregion lam -> size_of_lambda env lam
232+
| Lregion (lam, _) -> size_of_lambda env lam
233233
| _ -> RHS_nonrec
234234

235235
(**** Merging consecutive events ****)
@@ -1019,7 +1019,7 @@ let rec comp_expr env exp sz cont =
10191019
end
10201020
| Lifused (_, exp) ->
10211021
comp_expr env exp sz cont
1022-
| Lregion exp ->
1022+
| Lregion (exp, _) ->
10231023
comp_expr env exp sz cont
10241024

10251025
(* Compile a list of arguments [e1; ...; eN] to a primitive operation.

lambda/lambda.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ type lambda =
445445
* region_close * alloc_mode * scoped_location * layout
446446
| Levent of lambda * lambda_event
447447
| Lifused of Ident.t * lambda
448-
| Lregion of lambda
448+
| Lregion of lambda * layout
449449

450450
and lfunction =
451451
{ kind: function_kind;
@@ -655,7 +655,7 @@ let make_key e =
655655
| Lsend (m,e1,e2,es,pos,mo,_loc,layout) ->
656656
Lsend (m,tr_rec env e1,tr_rec env e2,tr_recs env es,pos,mo,Loc_unknown,layout)
657657
| Lifused (id,e) -> Lifused (id,tr_rec env e)
658-
| Lregion e -> Lregion (tr_rec env e)
658+
| Lregion (e,layout) -> Lregion (tr_rec env e,layout)
659659
| Lletrec _|Lfunction _
660660
| Lfor _ | Lwhile _
661661
(* Beware: (PR#6412) the event argument to Levent
@@ -754,7 +754,7 @@ let shallow_iter ~tail ~non_tail:f = function
754754
tail e
755755
| Lifused (_v, e) ->
756756
tail e
757-
| Lregion e ->
757+
| Lregion (e, _) ->
758758
f e
759759

760760
let iter_head_constructor f l =
@@ -836,7 +836,7 @@ let rec free_variables = function
836836
| Lifused (_v, e) ->
837837
(* Shouldn't v be considered a free variable ? *)
838838
free_variables e
839-
| Lregion e ->
839+
| Lregion (e, _) ->
840840
free_variables e
841841

842842
and free_variables_list set exprs =
@@ -1041,8 +1041,8 @@ let subst update_env ?(freshen_bound_variables = false) s input_lam =
10411041
| Lifused (id, e) ->
10421042
let id = try Ident.Map.find id l with Not_found -> id in
10431043
Lifused (id, subst s l e)
1044-
| Lregion e ->
1045-
Lregion (subst s l e)
1044+
| Lregion (e, layout) ->
1045+
Lregion (subst s l e, layout)
10461046
and subst_list s l li = List.map (subst s l) li
10471047
and subst_decl s l (id, exp) = (id, subst s l exp)
10481048
and subst_case s l (key, case) = (key, subst s l case)
@@ -1140,8 +1140,8 @@ let shallow_map ~tail ~non_tail:f = function
11401140
Levent (tail l, ev)
11411141
| Lifused (v, e) ->
11421142
Lifused (v, tail e)
1143-
| Lregion e ->
1144-
Lregion (f e)
1143+
| Lregion (e, layout) ->
1144+
Lregion (f e, layout)
11451145

11461146
let map f =
11471147
let rec g lam = f (shallow_map ~tail:g ~non_tail:g lam) in

lambda/lambda.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ type lambda =
373373
* region_close * alloc_mode * scoped_location * layout
374374
| Levent of lambda * lambda_event
375375
| Lifused of Ident.t * lambda
376-
| Lregion of lambda
376+
| Lregion of lambda * layout
377377

378378
and lfunction = private
379379
{ kind: function_kind;

lambda/matching.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3662,7 +3662,7 @@ let rec map_return f = function
36623662
| ( Lvar _ | Lmutvar _ | Lconst _ | Lapply _ | Lfunction _ | Lsend _ | Lprim _
36633663
| Lwhile _ | Lfor _ | Lassign _ | Lifused _ ) as l ->
36643664
f l
3665-
| Lregion l -> Lregion (map_return f l)
3665+
| Lregion (l, layout) -> Lregion (map_return f l, layout)
36663666

36673667
(* The 'opt' reference indicates if the optimization is worthy.
36683668

lambda/printlambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ let rec lam ppf = function
808808
end
809809
| Lifused(id, expr) ->
810810
fprintf ppf "@[<2>(ifused@ %a@ %a)@]" Ident.print id lam expr
811-
| Lregion expr ->
811+
| Lregion (expr, _) ->
812812
fprintf ppf "@[<2>(region@ %a)@]" lam expr
813813

814814
and sequence ppf = function

lambda/simplif.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ let rec eliminate_ref id = function
9595
Levent(eliminate_ref id l, ev)
9696
| Lifused(v, e) ->
9797
Lifused(v, eliminate_ref id e)
98-
| Lregion e ->
99-
Lregion(eliminate_ref id e)
98+
| Lregion (e, layout) ->
99+
Lregion(eliminate_ref id e, layout)
100100

101101
(* Simplification of exits *)
102102

@@ -184,7 +184,7 @@ let simplify_exits lam =
184184
| Lsend(_k, m, o, ll, _, _, _, _) -> List.iter (count ~try_depth) (m::o::ll)
185185
| Levent(l, _) -> count ~try_depth l
186186
| Lifused(_v, l) -> count ~try_depth l
187-
| Lregion l -> count ~try_depth l
187+
| Lregion (l, _) -> count ~try_depth l
188188

189189
and count_default ~try_depth sw = match sw.sw_failaction with
190190
| None -> ()
@@ -329,7 +329,7 @@ let simplify_exits lam =
329329
List.map (simplif ~try_depth) ll, pos, mode, loc, layout)
330330
| Levent(l, ev) -> Levent(simplif ~try_depth l, ev)
331331
| Lifused(v, l) -> Lifused (v,simplif ~try_depth l)
332-
| Lregion l -> Lregion (simplif ~try_depth l)
332+
| Lregion (l, layout) -> Lregion (simplif ~try_depth l, layout)
333333
in
334334
simplif ~try_depth:0 lam
335335

@@ -462,7 +462,7 @@ let simplify_lets lam =
462462
| Levent(l, _) -> count bv l
463463
| Lifused(v, l) ->
464464
if count_var v > 0 then count bv l
465-
| Lregion l ->
465+
| Lregion (l, _) ->
466466
count bv l
467467

468468
and count_default bv sw = match sw.sw_failaction with
@@ -613,7 +613,7 @@ let simplify_lets lam =
613613
| Levent(l, ev) -> Levent(simplif l, ev)
614614
| Lifused(v, l) ->
615615
if count_var v > 0 then simplif l else lambda_unit
616-
| Lregion l -> Lregion (simplif l)
616+
| Lregion (l, layout) -> Lregion (simplif l, layout)
617617
in
618618
simplif lam
619619

@@ -704,7 +704,7 @@ let rec emit_tail_infos is_tail lambda =
704704
emit_tail_infos is_tail lam
705705
| Lifused (_, lam) ->
706706
emit_tail_infos is_tail lam
707-
| Lregion lam ->
707+
| Lregion (lam, _) ->
708708
emit_tail_infos is_tail lam
709709
and list_emit_tail_infos_fun f is_tail =
710710
List.iter (fun x -> emit_tail_infos is_tail (f x))
@@ -745,7 +745,7 @@ let split_default_wrapper ~id:fun_id ~kind ~params ~return ~body
745745
->
746746
let wrapper_body, inner = aux ((optparam, id) :: map) add_region rest in
747747
Llet(Strict, k, id, def, wrapper_body), inner
748-
| Lregion rest -> aux map true rest
748+
| Lregion (rest, _) -> aux map true rest
749749
| _ when map = [] -> raise Exit
750750
| body ->
751751
(* Check that those *opt* identifiers don't appear in the remaining
@@ -778,7 +778,7 @@ let split_default_wrapper ~id:fun_id ~kind ~params ~return ~body
778778
) Ident.Map.empty inner_params new_ids
779779
in
780780
let body = Lambda.rename subst body in
781-
let body = if add_region then Lregion body else body in
781+
let body = if add_region then Lregion (body, return) else body in
782782
let inner_fun =
783783
lfunction ~kind:(Curried {nlocal=0})
784784
~params:(List.map (fun id -> id, Lambda.layout_top) new_ids)
@@ -902,7 +902,7 @@ let simplify_local_functions lam =
902902
| Lfunction lf ->
903903
check_static lf;
904904
function_definition lf
905-
| Lregion lam -> region lam
905+
| Lregion (lam, _) -> region lam
906906
| lam ->
907907
Lambda.shallow_iter ~tail ~non_tail lam
908908
and non_tail lam =

lambda/tmc.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ let rec choice ctx t =
665665
| Lifused (x, lam) ->
666666
let+ lam = choice ctx ~tail lam in
667667
Lifused (x, lam)
668-
| Lregion lam ->
668+
| Lregion (lam, layout) ->
669669
let+ lam = choice ctx ~tail lam in
670-
Lregion lam
670+
Lregion (lam, layout)
671671

672672
and choice_apply ctx ~tail apply =
673673
let exception No_tmc in

lambda/translcore.ml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let may_allocate_in_region lam =
126126
| None | Some Alloc_heap ->
127127
List.iter loop args
128128
end
129-
| Lregion _body ->
129+
| Lregion (_body, _layout) ->
130130
(* [_body] might do local allocations, but not in the current region *)
131131
()
132132
| Lwhile {wh_cond_region=false} -> raise Exit
@@ -147,7 +147,7 @@ let may_allocate_in_region lam =
147147
| exception Exit -> true
148148
end
149149

150-
let maybe_region lam =
150+
let maybe_region get_layout lam =
151151
let rec remove_tail_markers = function
152152
| Lapply ({ap_region_close = Rc_close_at_apply} as ap) ->
153153
Lapply ({ap with ap_region_close = Rc_normal})
@@ -158,9 +158,15 @@ let maybe_region lam =
158158
Lambda.shallow_map ~tail:remove_tail_markers ~non_tail:Fun.id lam
159159
in
160160
if not Config.stack_allocation then lam
161-
else if may_allocate_in_region lam then Lregion lam
161+
else if may_allocate_in_region lam then Lregion (lam, get_layout ())
162162
else remove_tail_markers lam
163163

164+
let maybe_region_layout layout lam =
165+
maybe_region (fun () -> layout) lam
166+
167+
let maybe_region_exp exp lam =
168+
maybe_region (fun () -> Typeopt.layout exp.exp_env exp.exp_type) lam
169+
164170
(* Push the default values under the functional abstractions *)
165171
(* Also push bindings of module patterns, since this sound *)
166172

@@ -589,10 +595,15 @@ and transl_exp0 ~in_new_scope ~scopes e =
589595
let cond = transl_exp ~scopes wh_cond in
590596
let body = transl_exp ~scopes wh_body in
591597
Lwhile {
592-
wh_cond = if wh_cond_region then maybe_region cond else cond;
598+
wh_cond =
599+
if wh_cond_region then
600+
maybe_region_layout layout_int cond
601+
else cond;
593602
wh_cond_region;
594603
wh_body = event_before ~scopes wh_body
595-
(if wh_body_region then maybe_region body else body);
604+
(if wh_body_region then
605+
maybe_region_layout layout_unit body
606+
else body);
596607
wh_body_region;
597608
}
598609
| Texp_arr_comprehension (body, blocks) ->
@@ -613,7 +624,9 @@ and transl_exp0 ~in_new_scope ~scopes e =
613624
for_to = transl_exp ~scopes for_to;
614625
for_dir;
615626
for_body = event_before ~scopes for_body
616-
(if for_region then maybe_region body else body);
627+
(if for_region then
628+
maybe_region_layout layout_unit body
629+
else body);
617630
for_region;
618631
}
619632
| Texp_send(expr, met, pos) ->
@@ -773,7 +786,9 @@ and transl_exp0 ~in_new_scope ~scopes e =
773786
~loc:(of_location ~scopes e.exp_loc)
774787
~mode:alloc_heap
775788
~region:true
776-
~body:(maybe_region (transl_exp ~scopes e))
789+
~body:(maybe_region_layout
790+
Lambda.layout_lazy_contents
791+
(transl_exp ~scopes e))
777792
in
778793
Lprim(Pmakeblock(Config.lazy_tag, Mutable, None, alloc_heap), [fn],
779794
of_location ~scopes e.exp_loc)
@@ -1201,7 +1216,7 @@ and transl_function ~scopes e param cases partial warnings region curry =
12011216
in
12021217
let attr = default_function_attribute in
12031218
let loc = of_location ~scopes e.exp_loc in
1204-
let body = if region then maybe_region body else body in
1219+
let body = if region then maybe_region_layout return body else body in
12051220
let lam = lfunction ~kind ~params ~return ~body ~attr ~loc ~mode ~region in
12061221
Translattribute.add_function_attributes lam e.exp_loc e.exp_attributes
12071222

@@ -1237,7 +1252,7 @@ and transl_let ~scopes ?(add_regions=false) ?(in_structure=false)
12371252
| {vb_pat=pat; vb_expr=expr; vb_attributes=attr; vb_loc} :: rem ->
12381253
let lam = transl_bound_exp ~scopes ~in_structure pat expr in
12391254
let lam = Translattribute.add_function_attributes lam vb_loc attr in
1240-
let lam = if add_regions then maybe_region lam else lam in
1255+
let lam = if add_regions then maybe_region_exp expr lam else lam in
12411256
let mk_body = transl rem in
12421257
fun body ->
12431258
Matching.for_let ~scopes pat.pat_loc lam pat body_kind (mk_body body)
@@ -1255,7 +1270,7 @@ and transl_let ~scopes ?(add_regions=false) ?(in_structure=false)
12551270
let lam =
12561271
Translattribute.add_function_attributes lam vb_loc vb_attributes
12571272
in
1258-
let lam = if add_regions then maybe_region lam else lam in
1273+
let lam = if add_regions then maybe_region_exp expr lam else lam in
12591274
begin match transl_exp_mode expr, lam with
12601275
| Alloc_heap, _ -> ()
12611276
| Alloc_local, Lfunction _ -> ()
@@ -1539,7 +1554,7 @@ and transl_letop ~scopes loc env let_ ands param case partial warnings =
15391554
in
15401555
let attr = default_function_attribute in
15411556
let loc = of_location ~scopes case.c_rhs.exp_loc in
1542-
let body = maybe_region body in
1557+
let body = maybe_region_layout return body in
15431558
lfunction ~kind ~params ~return ~body ~attr ~loc
15441559
~mode:alloc_heap ~region:true
15451560
in
@@ -1560,17 +1575,17 @@ and transl_letop ~scopes loc env let_ ands param case partial warnings =
15601575
that can only return global values *)
15611576

15621577
let transl_exp ~scopes exp =
1563-
maybe_region (transl_exp ~scopes exp)
1578+
maybe_region_exp exp (transl_exp ~scopes exp)
15641579

15651580
let transl_let ~scopes ?in_structure rec_flag pat_expr_list =
15661581
transl_let ~scopes ~add_regions:true ?in_structure rec_flag pat_expr_list
15671582

15681583
let transl_scoped_exp ~scopes exp =
1569-
maybe_region (transl_scoped_exp ~scopes exp)
1584+
maybe_region_exp exp (transl_scoped_exp ~scopes exp)
15701585

15711586
let transl_apply
15721587
~scopes ?tailcall ?inlined ?specialised ?position ?mode fn args loc =
1573-
maybe_region (transl_apply
1588+
maybe_region_layout Lambda.layout_top (transl_apply
15741589
~scopes ?tailcall ?inlined ?specialised ?position ?mode fn args loc)
15751590

15761591
(* Error report *)

middle_end/closure/closure.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
14111411
close env lam
14121412
| Lifused _ ->
14131413
assert false
1414-
| Lregion lam ->
1414+
| Lregion (lam, _) ->
14151415
let ulam, approx = close env lam in
14161416
region ulam, approx
14171417

middle_end/flambda/closure_conversion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
571571
or by completely removing it (replacing by unit). *)
572572
Misc.fatal_error "[Lifused] should have been removed by \
573573
[Simplif.simplify_lets]"
574-
| Lregion body ->
574+
| Lregion (body, _) ->
575575
Region (close t env body)
576576

577577
(** Perform closure conversion on a set of function declarations, returning a

0 commit comments

Comments
 (0)