Skip to content

Add layout on Lregion #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion middle_end/closure/closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
close env lam
| Lifused _ ->
assert false
| Lregion lam ->
| Lregion (lam, _) ->
let ulam, approx = close env lam in
region ulam, approx

Expand Down
2 changes: 1 addition & 1 deletion middle_end/flambda/closure_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
or by completely removing it (replacing by unit). *)
Misc.fatal_error "[Lifused] should have been removed by \
[Simplif.simplify_lets]"
| Lregion body ->
| Lregion (body, _) ->
Region (close t env body)

(** Perform closure conversion on a set of function declarations, returning a
Expand Down
4 changes: 2 additions & 2 deletions middle_end/flambda2/from_lambda/dissect_letrec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ let rec prepare_letrec (recursive_set : Ident.Set.t)
| None -> fun ~tail : Lambda.lambda -> Lsequence (lam, letrec.pre ~tail)
in
{ letrec with pre }
| Lregion body ->
| Lregion (body, _) ->
let letrec = prepare_letrec recursive_set current_let body letrec in
{ letrec with needs_region = true }
[@@ocaml.warning "-fragile-match"]
Expand Down Expand Up @@ -600,7 +600,7 @@ let dissect_letrec ~bindings ~body =
then substituted
else
Lstaticcatch
( Lregion (Lambda.rename bound_ids_freshening substituted),
( Lregion (Lambda.rename bound_ids_freshening substituted, Lambda.layout_top),
( cont,
List.map (fun (bound_id, _) -> bound_id, Lambda.layout_top) bindings
),
Expand Down
8 changes: 4 additions & 4 deletions middle_end/flambda2/from_lambda/lambda_to_flambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1391,9 +1391,9 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
by completely removing it (replacing by unit). *)
Misc.fatal_error
"[Lifused] should have been removed by [Simplif.simplify_lets]"
| Lregion body when not (Flambda_features.stack_allocation_enabled ()) ->
| Lregion (body, _) when not (Flambda_features.stack_allocation_enabled ()) ->
cps acc env ccenv body k k_exn
| Lregion body ->
| Lregion (body, layout) ->
(* Here we need to build the region closure continuation (see long comment
above). Since we're not in tail position, we also need to have a new
continuation for the code after the body. *)
Expand All @@ -1403,12 +1403,12 @@ let rec cps acc env ccenv (lam : L.lambda) (k : cps_continuation)
Flambda_kind.With_subkind.region
(Begin_region { try_region_parent = None })
~body:(fun acc ccenv ->
maybe_insert_let_cont "body_return" Lambda.layout_top k acc env ccenv
maybe_insert_let_cont "body_return" layout k acc env ccenv
(fun acc env ccenv k ->
let wrap_return = Ident.create_local "region_return" in
let_cont_nonrecursive_with_extra_params acc env ccenv
~is_exn_handler:false
~params:[wrap_return, Not_user_visible, Lambda.layout_top]
~params:[wrap_return, Not_user_visible, layout]
~body:(fun acc env ccenv continuation_closing_region ->
(* We register this region to be closed by the newly-created
region closure continuation. When we reach a point in [body]
Expand Down
4 changes: 2 additions & 2 deletions ocaml/bytecomp/bytegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ let rec size_of_lambda env = function
| Lprim (Pduprecord (Record_float, size), _, _) -> RHS_floatblock size
| Levent (lam, _) -> size_of_lambda env lam
| Lsequence (_lam, lam') -> size_of_lambda env lam'
| Lregion lam -> size_of_lambda env lam
| Lregion (lam, _) -> size_of_lambda env lam
| _ -> RHS_nonrec

(**** Merging consecutive events ****)
Expand Down Expand Up @@ -1019,7 +1019,7 @@ let rec comp_expr env exp sz cont =
end
| Lifused (_, exp) ->
comp_expr env exp sz cont
| Lregion exp ->
| Lregion (exp, _) ->
comp_expr env exp sz cont

(* Compile a list of arguments [e1; ...; eN] to a primitive operation.
Expand Down
16 changes: 8 additions & 8 deletions ocaml/lambda/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ type lambda =
* region_close * alloc_mode * scoped_location * layout
| Levent of lambda * lambda_event
| Lifused of Ident.t * lambda
| Lregion of lambda
| Lregion of lambda * layout

and lfunction =
{ kind: function_kind;
Expand Down Expand Up @@ -655,7 +655,7 @@ let make_key e =
| Lsend (m,e1,e2,es,pos,mo,_loc,layout) ->
Lsend (m,tr_rec env e1,tr_rec env e2,tr_recs env es,pos,mo,Loc_unknown,layout)
| Lifused (id,e) -> Lifused (id,tr_rec env e)
| Lregion e -> Lregion (tr_rec env e)
| Lregion (e,layout) -> Lregion (tr_rec env e,layout)
| Lletrec _|Lfunction _
| Lfor _ | Lwhile _
(* Beware: (PR#6412) the event argument to Levent
Expand Down Expand Up @@ -754,7 +754,7 @@ let shallow_iter ~tail ~non_tail:f = function
tail e
| Lifused (_v, e) ->
tail e
| Lregion e ->
| Lregion (e, _) ->
f e

let iter_head_constructor f l =
Expand Down Expand Up @@ -836,7 +836,7 @@ let rec free_variables = function
| Lifused (_v, e) ->
(* Shouldn't v be considered a free variable ? *)
free_variables e
| Lregion e ->
| Lregion (e, _) ->
free_variables e

and free_variables_list set exprs =
Expand Down Expand Up @@ -1041,8 +1041,8 @@ let subst update_env ?(freshen_bound_variables = false) s input_lam =
| Lifused (id, e) ->
let id = try Ident.Map.find id l with Not_found -> id in
Lifused (id, subst s l e)
| Lregion e ->
Lregion (subst s l e)
| Lregion (e, layout) ->
Lregion (subst s l e, layout)
and subst_list s l li = List.map (subst s l) li
and subst_decl s l (id, exp) = (id, subst s l exp)
and subst_case s l (key, case) = (key, subst s l case)
Expand Down Expand Up @@ -1140,8 +1140,8 @@ let shallow_map ~tail ~non_tail:f = function
Levent (tail l, ev)
| Lifused (v, e) ->
Lifused (v, tail e)
| Lregion e ->
Lregion (f e)
| Lregion (e, layout) ->
Lregion (f e, layout)

let map f =
let rec g lam = f (shallow_map ~tail:g ~non_tail:g lam) in
Expand Down
2 changes: 1 addition & 1 deletion ocaml/lambda/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ type lambda =
* region_close * alloc_mode * scoped_location * layout
| Levent of lambda * lambda_event
| Lifused of Ident.t * lambda
| Lregion of lambda
| Lregion of lambda * layout

and lfunction = private
{ kind: function_kind;
Expand Down
2 changes: 1 addition & 1 deletion ocaml/lambda/matching.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3662,7 +3662,7 @@ let rec map_return f = function
| ( Lvar _ | Lmutvar _ | Lconst _ | Lapply _ | Lfunction _ | Lsend _ | Lprim _
| Lwhile _ | Lfor _ | Lassign _ | Lifused _ ) as l ->
f l
| Lregion l -> Lregion (map_return f l)
| Lregion (l, layout) -> Lregion (map_return f l, layout)

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

Expand Down
2 changes: 1 addition & 1 deletion ocaml/lambda/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ let rec lam ppf = function
end
| Lifused(id, expr) ->
fprintf ppf "@[<2>(ifused@ %a@ %a)@]" Ident.print id lam expr
| Lregion expr ->
| Lregion (expr, _) ->
fprintf ppf "@[<2>(region@ %a)@]" lam expr

and sequence ppf = function
Expand Down
20 changes: 10 additions & 10 deletions ocaml/lambda/simplif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ let rec eliminate_ref id = function
Levent(eliminate_ref id l, ev)
| Lifused(v, e) ->
Lifused(v, eliminate_ref id e)
| Lregion e ->
Lregion(eliminate_ref id e)
| Lregion (e, layout) ->
Lregion(eliminate_ref id e, layout)

(* Simplification of exits *)

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

and count_default ~try_depth sw = match sw.sw_failaction with
| None -> ()
Expand Down Expand Up @@ -329,7 +329,7 @@ let simplify_exits lam =
List.map (simplif ~try_depth) ll, pos, mode, loc, layout)
| Levent(l, ev) -> Levent(simplif ~try_depth l, ev)
| Lifused(v, l) -> Lifused (v,simplif ~try_depth l)
| Lregion l -> Lregion (simplif ~try_depth l)
| Lregion (l, layout) -> Lregion (simplif ~try_depth l, layout)
in
simplif ~try_depth:0 lam

Expand Down Expand Up @@ -462,7 +462,7 @@ let simplify_lets lam =
| Levent(l, _) -> count bv l
| Lifused(v, l) ->
if count_var v > 0 then count bv l
| Lregion l ->
| Lregion (l, _) ->
count bv l

and count_default bv sw = match sw.sw_failaction with
Expand Down Expand Up @@ -613,7 +613,7 @@ let simplify_lets lam =
| Levent(l, ev) -> Levent(simplif l, ev)
| Lifused(v, l) ->
if count_var v > 0 then simplif l else lambda_unit
| Lregion l -> Lregion (simplif l)
| Lregion (l, layout) -> Lregion (simplif l, layout)
in
simplif lam

Expand Down Expand Up @@ -704,7 +704,7 @@ let rec emit_tail_infos is_tail lambda =
emit_tail_infos is_tail lam
| Lifused (_, lam) ->
emit_tail_infos is_tail lam
| Lregion lam ->
| Lregion (lam, _) ->
emit_tail_infos is_tail lam
and list_emit_tail_infos_fun f is_tail =
List.iter (fun x -> emit_tail_infos is_tail (f x))
Expand Down Expand Up @@ -745,7 +745,7 @@ let split_default_wrapper ~id:fun_id ~kind ~params ~return ~body
->
let wrapper_body, inner = aux ((optparam, id) :: map) add_region rest in
Llet(Strict, k, id, def, wrapper_body), inner
| Lregion rest -> aux map true rest
| Lregion (rest, _) -> aux map true rest
| _ when map = [] -> raise Exit
| body ->
(* Check that those *opt* identifiers don't appear in the remaining
Expand Down Expand Up @@ -778,7 +778,7 @@ let split_default_wrapper ~id:fun_id ~kind ~params ~return ~body
) Ident.Map.empty inner_params new_ids
in
let body = Lambda.rename subst body in
let body = if add_region then Lregion body else body in
let body = if add_region then Lregion (body, return) else body in
let inner_fun =
lfunction ~kind:(Curried {nlocal=0})
~params:(List.map (fun id -> id, Lambda.layout_top) new_ids)
Expand Down Expand Up @@ -902,7 +902,7 @@ let simplify_local_functions lam =
| Lfunction lf ->
check_static lf;
function_definition lf
| Lregion lam -> region lam
| Lregion (lam, _) -> region lam
| lam ->
Lambda.shallow_iter ~tail ~non_tail lam
and non_tail lam =
Expand Down
4 changes: 2 additions & 2 deletions ocaml/lambda/tmc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,9 @@ let rec choice ctx t =
| Lifused (x, lam) ->
let+ lam = choice ctx ~tail lam in
Lifused (x, lam)
| Lregion lam ->
| Lregion (lam, layout) ->
let+ lam = choice ctx ~tail lam in
Lregion lam
Lregion (lam, layout)

and choice_apply ctx ~tail apply =
let exception No_tmc in
Expand Down
28 changes: 14 additions & 14 deletions ocaml/lambda/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let may_allocate_in_region lam =
| None | Some Alloc_heap ->
List.iter loop args
end
| Lregion _body ->
| Lregion (_body, _layout) ->
(* [_body] might do local allocations, but not in the current region *)
()
| Lwhile {wh_cond_region=false} -> raise Exit
Expand All @@ -147,7 +147,7 @@ let may_allocate_in_region lam =
| exception Exit -> true
end

let maybe_region lam =
let maybe_region layout lam =
let rec remove_tail_markers = function
| Lapply ({ap_region_close = Rc_close_at_apply} as ap) ->
Lapply ({ap with ap_region_close = Rc_normal})
Expand All @@ -158,7 +158,7 @@ let maybe_region lam =
Lambda.shallow_map ~tail:remove_tail_markers ~non_tail:Fun.id lam
in
if not Config.stack_allocation then lam
else if may_allocate_in_region lam then Lregion lam
else if may_allocate_in_region lam then Lregion (lam, layout)
else remove_tail_markers lam

(* Push the default values under the functional abstractions *)
Expand Down Expand Up @@ -589,10 +589,10 @@ and transl_exp0 ~in_new_scope ~scopes e =
let cond = transl_exp ~scopes wh_cond in
let body = transl_exp ~scopes wh_body in
Lwhile {
wh_cond = if wh_cond_region then maybe_region cond else cond;
wh_cond = if wh_cond_region then maybe_region layout_int cond else cond;
wh_cond_region;
wh_body = event_before ~scopes wh_body
(if wh_body_region then maybe_region body else body);
(if wh_body_region then maybe_region layout_unit body else body);
wh_body_region;
}
| Texp_arr_comprehension (body, blocks) ->
Expand All @@ -613,7 +613,7 @@ and transl_exp0 ~in_new_scope ~scopes e =
for_to = transl_exp ~scopes for_to;
for_dir;
for_body = event_before ~scopes for_body
(if for_region then maybe_region body else body);
(if for_region then maybe_region layout_unit body else body);
for_region;
}
| Texp_send(expr, met, pos) ->
Expand Down Expand Up @@ -773,7 +773,7 @@ and transl_exp0 ~in_new_scope ~scopes e =
~loc:(of_location ~scopes e.exp_loc)
~mode:alloc_heap
~region:true
~body:(maybe_region (transl_exp ~scopes e))
~body:(maybe_region (layout e.exp_env e.exp_type) (transl_exp ~scopes e))
in
Lprim(Pmakeblock(Config.lazy_tag, Mutable, None, alloc_heap), [fn],
of_location ~scopes e.exp_loc)
Expand Down Expand Up @@ -1201,7 +1201,7 @@ and transl_function ~scopes e param cases partial warnings region curry =
in
let attr = default_function_attribute in
let loc = of_location ~scopes e.exp_loc in
let body = if region then maybe_region body else body in
let body = if region then maybe_region return body else body in
let lam = lfunction ~kind ~params ~return ~body ~attr ~loc ~mode ~region in
Translattribute.add_function_attributes lam e.exp_loc e.exp_attributes

Expand Down Expand Up @@ -1237,7 +1237,7 @@ and transl_let ~scopes ?(add_regions=false) ?(in_structure=false)
| {vb_pat=pat; vb_expr=expr; vb_attributes=attr; vb_loc} :: rem ->
let lam = transl_bound_exp ~scopes ~in_structure pat expr in
let lam = Translattribute.add_function_attributes lam vb_loc attr in
let lam = if add_regions then maybe_region lam else lam in
let lam = if add_regions then maybe_region (layout expr.exp_env expr.exp_type) lam else lam in
let mk_body = transl rem in
fun body ->
Matching.for_let ~scopes pat.pat_loc lam pat body_kind (mk_body body)
Expand All @@ -1255,7 +1255,7 @@ and transl_let ~scopes ?(add_regions=false) ?(in_structure=false)
let lam =
Translattribute.add_function_attributes lam vb_loc vb_attributes
in
let lam = if add_regions then maybe_region lam else lam in
let lam = if add_regions then maybe_region (layout expr.exp_env expr.exp_type) lam else lam in
begin match transl_exp_mode expr, lam with
| Alloc_heap, _ -> ()
| Alloc_local, Lfunction _ -> ()
Expand Down Expand Up @@ -1539,7 +1539,7 @@ and transl_letop ~scopes loc env let_ ands param case partial warnings =
in
let attr = default_function_attribute in
let loc = of_location ~scopes case.c_rhs.exp_loc in
let body = maybe_region body in
let body = maybe_region return body in
lfunction ~kind ~params ~return ~body ~attr ~loc
~mode:alloc_heap ~region:true
in
Expand All @@ -1560,17 +1560,17 @@ and transl_letop ~scopes loc env let_ ands param case partial warnings =
that can only return global values *)

let transl_exp ~scopes exp =
maybe_region (transl_exp ~scopes exp)
maybe_region (layout exp.exp_env exp.exp_type) (transl_exp ~scopes exp)

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

let transl_scoped_exp ~scopes exp =
maybe_region (transl_scoped_exp ~scopes exp)
maybe_region (layout exp.exp_env exp.exp_type) (transl_scoped_exp ~scopes exp)

let transl_apply
~scopes ?tailcall ?inlined ?specialised ?position ?mode fn args loc =
maybe_region (transl_apply
maybe_region Lambda.layout_top (transl_apply
~scopes ?tailcall ?inlined ?specialised ?position ?mode fn args loc)

(* Error report *)
Expand Down
2 changes: 1 addition & 1 deletion ocaml/middle_end/closure/closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
close env lam
| Lifused _ ->
assert false
| Lregion lam ->
| Lregion (lam, _) ->
let ulam, approx = close env lam in
region ulam, approx

Expand Down
2 changes: 1 addition & 1 deletion ocaml/middle_end/flambda/closure_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
or by completely removing it (replacing by unit). *)
Misc.fatal_error "[Lifused] should have been removed by \
[Simplif.simplify_lets]"
| Lregion body ->
| Lregion (body, _) ->
Region (close t env body)

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