Skip to content

Commit d7b2c20

Browse files
authored
Add result layout in Lapply and Lsend (#1102)
1 parent 23c793d commit d7b2c20

File tree

16 files changed

+139
-98
lines changed

16 files changed

+139
-98
lines changed

middle_end/closure/closure.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
10991099
ap_loc=loc;
11001100
ap_func=(Lvar funct_var);
11011101
ap_args=internal_args;
1102+
ap_result_layout=Lambda.layout_top;
11021103
ap_region_close=Rc_normal;
11031104
ap_mode=ret_mode;
11041105
ap_tailcall=Default_tailcall;
@@ -1165,7 +1166,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
11651166
fail_if_probe ~probe "Unknown function";
11661167
(Ugeneric_apply(ufunct, uargs, (pos, mode), dbg), Value_unknown)
11671168
end
1168-
| Lsend(kind, met, obj, args, pos, mode, loc) ->
1169+
| Lsend(kind, met, obj, args, pos, mode, loc, _result_layout) ->
11691170
let (umet, _) = close env met in
11701171
let (uobj, _) = close env obj in
11711172
let dbg = Debuginfo.from_location loc in

middle_end/flambda/closure_conversion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
329329
in
330330
Let_rec (defs, close t env body)
331331
end
332-
| Lsend (kind, meth, obj, args, reg_close, mode, loc) ->
332+
| Lsend (kind, meth, obj, args, reg_close, mode, loc, _layout) ->
333333
let meth_var = Variable.create Names.meth in
334334
let obj_var = Variable.create Names.obj in
335335
let dbg = Debuginfo.from_location loc in

middle_end/flambda2/from_lambda/lambda_to_flambda.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
10531053
| Lapply
10541054
{ ap_func;
10551055
ap_args;
1056+
ap_result_layout;
10561057
ap_region_close;
10571058
ap_mode;
10581059
ap_loc;
@@ -1063,7 +1064,7 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
10631064
} ->
10641065
(* Note that we don't need kind information about [ap_args] since we already
10651066
have it on the corresponding [Simple]s in the environment. *)
1066-
maybe_insert_let_cont "apply_result" Lambda.layout_top k acc env ccenv
1067+
maybe_insert_let_cont "apply_result" ap_result_layout k acc env ccenv
10671068
(fun acc env ccenv k ->
10681069
cps_tail_apply acc env ccenv ap_func ap_args ap_region_close ap_mode
10691070
ap_loc ap_inlined ap_probe k k_exn)
@@ -1254,16 +1255,16 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
12541255
let body acc ccenv = cps_tail acc body_env ccenv body k k_exn in
12551256
CC.close_let_cont acc ccenv ~name:continuation ~is_exn_handler:false
12561257
~params ~recursive ~body ~handler)
1257-
| Lsend (meth_kind, meth, obj, args, pos, mode, loc) ->
1258+
| Lsend (meth_kind, meth, obj, args, pos, mode, loc, layout) ->
12581259
cps_non_tail_simple acc env ccenv obj
12591260
(fun acc env ccenv obj ->
12601261
cps_non_tail_var "meth" acc env ccenv meth
12611262
Flambda_kind.With_subkind.any_value
12621263
(fun acc env ccenv meth ->
12631264
cps_non_tail_list acc env ccenv args
12641265
(fun acc env ccenv args ->
1265-
maybe_insert_let_cont "send_result" Lambda.layout_top k acc env
1266-
ccenv (fun acc env ccenv k ->
1266+
maybe_insert_let_cont "send_result" layout k acc env ccenv
1267+
(fun acc env ccenv k ->
12671268
let exn_continuation : IR.exn_continuation =
12681269
{ exn_handler = k_exn;
12691270
extra_args = extra_args_for_exn_continuation env k_exn

ocaml/bytecomp/bytegen.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ let rec comp_expr env exp sz cont =
591591
(Kapply nargs :: cont1))
592592
end
593593
end
594-
| Lsend(kind, met, obj, args, rc, _, _) ->
594+
| Lsend(kind, met, obj, args, rc, _, _, _) ->
595595
assert (kind <> Cached);
596596
let nargs = List.length args + 1 in
597597
let getmethod, args' =
@@ -994,7 +994,7 @@ let rec comp_expr env exp sz cont =
994994
match lam with
995995
| Lprim(prim, _, _) -> preserve_tailcall_for_prim prim
996996
| Lapply {ap_region_close=rc; _}
997-
| Lsend(_, _, _, _, rc, _, _) ->
997+
| Lsend(_, _, _, _, rc, _, _, _) ->
998998
not (is_nontail rc)
999999
| _ -> true
10001000
in
@@ -1005,7 +1005,7 @@ let rec comp_expr env exp sz cont =
10051005
let info =
10061006
match lam with
10071007
Lapply{ap_args = args} -> Event_return (List.length args)
1008-
| Lsend(_, _, _, args, _, _, _) ->
1008+
| Lsend(_, _, _, args, _, _, _, _) ->
10091009
Event_return (List.length args + 1)
10101010
| Lprim(_,args,_) -> Event_return (List.length args)
10111011
| _ -> Event_other

ocaml/lambda/lambda.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ type lambda =
442442
| Lassign of Ident.t * lambda
443443
| Lsend of
444444
meth_kind * lambda * lambda * lambda list
445-
* region_close * alloc_mode * scoped_location
445+
* region_close * alloc_mode * scoped_location * layout
446446
| Levent of lambda * lambda_event
447447
| Lifused of Ident.t * lambda
448448
| Lregion of lambda
@@ -476,6 +476,7 @@ and lambda_for =
476476
and lambda_apply =
477477
{ ap_func : lambda;
478478
ap_args : lambda list;
479+
ap_result_layout : layout;
479480
ap_region_close : region_close;
480481
ap_mode : alloc_mode;
481482
ap_loc : scoped_location;
@@ -565,6 +566,7 @@ let layout_string = Pvalue Pgenval
565566
let layout_boxedint bi = Pvalue (Pboxedintval bi)
566567
let layout_lazy = Pvalue Pgenval
567568
let layout_lazy_contents = Pvalue Pgenval
569+
let layout_any_value = Pvalue Pgenval
568570

569571
let layout_top = Pvalue Pgenval
570572

@@ -650,8 +652,8 @@ let make_key e =
650652
Lsequence (tr_rec env e1,tr_rec env e2)
651653
| Lassign (x,e) ->
652654
Lassign (x,tr_rec env e)
653-
| Lsend (m,e1,e2,es,pos,mo,_loc) ->
654-
Lsend (m,tr_rec env e1,tr_rec env e2,tr_recs env es,pos,mo,Loc_unknown)
655+
| Lsend (m,e1,e2,es,pos,mo,_loc,layout) ->
656+
Lsend (m,tr_rec env e1,tr_rec env e2,tr_recs env es,pos,mo,Loc_unknown,layout)
655657
| Lifused (id,e) -> Lifused (id,tr_rec env e)
656658
| Lregion e -> Lregion (tr_rec env e)
657659
| Lletrec _|Lfunction _
@@ -746,7 +748,7 @@ let shallow_iter ~tail ~non_tail:f = function
746748
f for_from; f for_to; f for_body
747749
| Lassign(_, e) ->
748750
f e
749-
| Lsend (_k, met, obj, args, _, _, _) ->
751+
| Lsend (_k, met, obj, args, _, _, _, _) ->
750752
List.iter f (met::obj::args)
751753
| Levent (e, _evt) ->
752754
tail e
@@ -825,7 +827,7 @@ let rec free_variables = function
825827
(Ident.Set.remove for_id (free_variables for_body)))
826828
| Lassign(id, e) ->
827829
Ident.Set.add id (free_variables e)
828-
| Lsend (_k, met, obj, args, _, _, _) ->
830+
| Lsend (_k, met, obj, args, _, _, _, _) ->
829831
free_variables_list
830832
(Ident.Set.union (free_variables met) (free_variables obj))
831833
args
@@ -1007,9 +1009,9 @@ let subst update_env ?(freshen_bound_variables = false) s input_lam =
10071009
assert (not (Ident.Map.mem id s));
10081010
let id = try Ident.Map.find id l with Not_found -> id in
10091011
Lassign(id, subst s l e)
1010-
| Lsend (k, met, obj, args, pos, mode, loc) ->
1012+
| Lsend (k, met, obj, args, pos, mode, loc, layout) ->
10111013
Lsend (k, subst s l met, subst s l obj, subst_list s l args,
1012-
pos, mode, loc)
1014+
pos, mode, loc, layout)
10131015
| Levent (lam, evt) ->
10141016
let old_env = evt.lev_env in
10151017
let env_updates =
@@ -1070,11 +1072,12 @@ let shallow_map ~tail ~non_tail:f = function
10701072
| Lvar _
10711073
| Lmutvar _
10721074
| Lconst _ as lam -> lam
1073-
| Lapply { ap_func; ap_args; ap_region_close; ap_mode; ap_loc; ap_tailcall;
1075+
| Lapply { ap_func; ap_args; ap_result_layout; ap_region_close; ap_mode; ap_loc; ap_tailcall;
10741076
ap_inlined; ap_specialised; ap_probe } ->
10751077
Lapply {
10761078
ap_func = f ap_func;
10771079
ap_args = List.map f ap_args;
1080+
ap_result_layout;
10781081
ap_region_close;
10791082
ap_mode;
10801083
ap_loc;
@@ -1131,8 +1134,8 @@ let shallow_map ~tail ~non_tail:f = function
11311134
for_body = f lf.for_body }
11321135
| Lassign (v, e) ->
11331136
Lassign (v, f e)
1134-
| Lsend (k, m, o, el, pos, mode, loc) ->
1135-
Lsend (k, f m, f o, List.map f el, pos, mode, loc)
1137+
| Lsend (k, m, o, el, pos, mode, loc, layout) ->
1138+
Lsend (k, f m, f o, List.map f el, pos, mode, loc, layout)
11361139
| Levent (l, ev) ->
11371140
Levent (tail l, ev)
11381141
| Lifused (v, e) ->
@@ -1329,3 +1332,5 @@ let structured_constant_layout = function
13291332
| Const_base const -> constant_layout const
13301333
| Const_block _ | Const_immstring _ -> Pvalue Pgenval
13311334
| Const_float_array _ | Const_float_block _ -> Pvalue (Parrayval Pfloatarray)
1335+
1336+
let primitive_result_layout (_p : primitive) = layout_top

ocaml/lambda/lambda.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ type lambda =
370370
| Lfor of lambda_for
371371
| Lassign of Ident.t * lambda
372372
| Lsend of meth_kind * lambda * lambda * lambda list
373-
* region_close * alloc_mode * scoped_location
373+
* region_close * alloc_mode * scoped_location * layout
374374
| Levent of lambda * lambda_event
375375
| Lifused of Ident.t * lambda
376376
| Lregion of lambda
@@ -409,6 +409,7 @@ and lambda_for =
409409
and lambda_apply =
410410
{ ap_func : lambda;
411411
ap_args : lambda list;
412+
ap_result_layout : layout;
412413
ap_region_close : region_close;
413414
ap_mode : alloc_mode;
414415
ap_loc : scoped_location;
@@ -482,6 +483,8 @@ val layout_boxedint : boxed_integer -> layout
482483
val layout_field : layout
483484
val layout_lazy : layout
484485
val layout_lazy_contents : layout
486+
(* A layout that is Pgenval because we are missing layout polymorphism *)
487+
val layout_any_value : layout
485488

486489
val layout_top : layout
487490

@@ -626,3 +629,5 @@ val mod_field: ?read_semantics: field_read_semantics -> int -> primitive
626629
val mod_setfield: int -> primitive
627630

628631
val structured_constant_layout : structured_constant -> layout
632+
633+
val primitive_result_layout : primitive -> layout

ocaml/lambda/matching.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ let inline_lazy_force_cond arg pos loc =
19291929
ap_loc = loc;
19301930
ap_func = force_fun;
19311931
ap_args = [ varg ];
1932+
ap_result_layout = Lambda.layout_lazy_contents;
19321933
ap_region_close = pos;
19331934
ap_mode = alloc_heap;
19341935
ap_inlined = Default_inlined;
@@ -1965,6 +1966,7 @@ let inline_lazy_force_switch arg pos loc =
19651966
ap_loc = loc;
19661967
ap_func = force_fun;
19671968
ap_args = [ varg ];
1969+
ap_result_layout = Lambda.layout_lazy_contents;
19681970
ap_region_close = pos;
19691971
ap_mode = alloc_heap;
19701972
ap_inlined = Default_inlined;
@@ -1987,6 +1989,7 @@ let inline_lazy_force arg pos loc =
19871989
ap_loc = loc;
19881990
ap_func = Lazy.force code_force_lazy;
19891991
ap_args = [ arg ];
1992+
ap_result_layout = Lambda.layout_lazy_contents;
19901993
ap_region_close = pos;
19911994
ap_mode = alloc_heap;
19921995
ap_inlined = Default_inlined;

ocaml/lambda/printlambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ let rec lam ppf = function
771771
lam for_to (alloc_mode mode) lam for_body
772772
| Lassign(id, expr) ->
773773
fprintf ppf "@[<2>(assign@ %a@ %a)@]" Ident.print id lam expr
774-
| Lsend (k, met, obj, largs, pos, reg, _) ->
774+
| Lsend (k, met, obj, largs, pos, reg, _, _) ->
775775
let args ppf largs =
776776
List.iter (fun l -> fprintf ppf "@ %a" lam l) largs in
777777
let kind =

ocaml/lambda/simplif.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ let rec eliminate_ref id = function
8888
for_body = eliminate_ref id lf.for_body }
8989
| Lassign(v, e) ->
9090
Lassign(v, eliminate_ref id e)
91-
| Lsend(k, m, o, el, pos, mode, loc) ->
91+
| Lsend(k, m, o, el, pos, mode, loc, layout) ->
9292
Lsend(k, eliminate_ref id m, eliminate_ref id o,
93-
List.map (eliminate_ref id) el, pos, mode, loc)
93+
List.map (eliminate_ref id) el, pos, mode, loc, layout)
9494
| Levent(l, ev) ->
9595
Levent(eliminate_ref id l, ev)
9696
| Lifused(v, e) ->
@@ -181,7 +181,7 @@ let simplify_exits lam =
181181
count ~try_depth lf.for_to;
182182
count ~try_depth lf.for_body
183183
| Lassign(_v, l) -> count ~try_depth l
184-
| Lsend(_k, m, o, ll, _, _, _) -> List.iter (count ~try_depth) (m::o::ll)
184+
| 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
187187
| Lregion l -> count ~try_depth l
@@ -324,9 +324,9 @@ let simplify_exits lam =
324324
for_to = simplif ~try_depth lf.for_to;
325325
for_body = simplif ~try_depth lf.for_body}
326326
| Lassign(v, l) -> Lassign(v, simplif ~try_depth l)
327-
| Lsend(k, m, o, ll, pos, mode, loc) ->
327+
| Lsend(k, m, o, ll, pos, mode, loc, layout) ->
328328
Lsend(k, simplif ~try_depth m, simplif ~try_depth o,
329-
List.map (simplif ~try_depth) ll, pos, mode, loc)
329+
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)
332332
| Lregion l -> Lregion (simplif ~try_depth l)
@@ -458,7 +458,7 @@ let simplify_lets lam =
458458
(* Lalias-bound variables are never assigned, so don't increase
459459
v's refcount *)
460460
count bv l
461-
| Lsend(_, m, o, ll, _, _, _) -> List.iter (count bv) (m::o::ll)
461+
| Lsend(_, m, o, ll, _, _, _, _) -> List.iter (count bv) (m::o::ll)
462462
| Levent(l, _) -> count bv l
463463
| Lifused(v, l) ->
464464
if count_var v > 0 then count bv l
@@ -608,8 +608,8 @@ let simplify_lets lam =
608608
for_to = simplif lf.for_to;
609609
for_body = simplif lf.for_body}
610610
| Lassign(v, l) -> Lassign(v, simplif l)
611-
| Lsend(k, m, o, ll, pos, mode, loc) ->
612-
Lsend(k, simplif m, simplif o, List.map simplif ll, pos, mode, loc)
611+
| Lsend(k, m, o, ll, pos, mode, loc, layout) ->
612+
Lsend(k, simplif m, simplif o, List.map simplif ll, pos, mode, loc, layout)
613613
| Levent(l, ev) -> Levent(simplif l, ev)
614614
| Lifused(v, l) ->
615615
if count_var v > 0 then simplif l else lambda_unit
@@ -696,7 +696,7 @@ let rec emit_tail_infos is_tail lambda =
696696
emit_tail_infos false for_body
697697
| Lassign (_, lam) ->
698698
emit_tail_infos false lam
699-
| Lsend (_, meth, obj, args, _, _, _loc) ->
699+
| Lsend (_, meth, obj, args, _, _, _loc, _) ->
700700
emit_tail_infos false meth;
701701
emit_tail_infos false obj;
702702
list_emit_tail_infos false args
@@ -760,6 +760,7 @@ let split_default_wrapper ~id:fun_id ~kind ~params ~return ~body
760760
Lapply {
761761
ap_func = Lvar inner_id;
762762
ap_args = args;
763+
ap_result_layout = return;
763764
ap_loc = Loc_unknown;
764765
ap_region_close = Rc_normal;
765766
ap_mode = alloc_heap;

0 commit comments

Comments
 (0)