Skip to content

Commit bd807d5

Browse files
committed
Code review
1 parent f07bd82 commit bd807d5

File tree

11 files changed

+63
-78
lines changed

11 files changed

+63
-78
lines changed

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ module Inlining = struct
306306

307307
let inline acc ~apply ~apply_depth ~func_desc:code =
308308
let callee = Apply.callee apply in
309-
let region_inlined_into = Simple.var (Apply.region apply) in
309+
let region_inlined_into = Apply.region apply in
310310
let args = Apply.args apply in
311311
let apply_return_continuation = Apply.continuation apply in
312312
let apply_exn_continuation = Apply.exn_continuation apply in
@@ -1708,7 +1708,7 @@ let wrap_over_application acc env full_call (apply : IR.apply) over_args
17081708
Some (over_app_region, Continuation.create ())
17091709
| Heap, true | Local, _ -> None
17101710
in
1711-
let current_region =
1711+
let apply_region =
17121712
match needs_region with
17131713
| None -> Env.find_var env apply.region
17141714
| Some (region, _) -> region
@@ -1745,7 +1745,7 @@ let wrap_over_application acc env full_call (apply : IR.apply) over_args
17451745
~inlining_state:(Inlining_state.default ~round:0)
17461746
~probe_name ~position
17471747
~relative_history:(Env.relative_history_from_scoped ~loc:apply.loc env)
1748-
~region:current_region
1748+
~region:apply_region
17491749
in
17501750
match needs_region with
17511751
| None -> Expr_with_acc.create_apply acc over_application
@@ -1777,7 +1777,7 @@ let wrap_over_application acc env full_call (apply : IR.apply) over_args
17771777
~body:(fun acc -> Expr_with_acc.create_apply acc over_application)
17781778
~is_exn_handler:false
17791779
in
1780-
let body = full_call wrapper_cont ~region:current_region in
1780+
let body = full_call wrapper_cont ~region:apply_region in
17811781
let acc, both_applications =
17821782
Let_cont_with_acc.build_non_recursive acc wrapper_cont
17831783
~handler_params:

middle_end/flambda2/from_lambda/lambda_to_flambda.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,11 @@ end = struct
366366
let leaving_try_region t =
367367
match t.region_stack with
368368
| [] -> Misc.fatal_error "Cannot pop try region, region stack is empty"
369-
| _ :: region_stack -> { t with region_stack }
369+
| Try_with _ :: region_stack -> { t with region_stack }
370+
| Regular region :: _ ->
371+
Misc.fatal_errorf
372+
"Attempted to pop try region but found regular region %a" Ident.print
373+
region
370374

371375
let current_region t =
372376
if not (Flambda_features.stack_allocation_enabled ())

middle_end/flambda2/simplify/expr_builder.ml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ let create_let uacc (bound_vars : Bound_pattern.t) (defining_expr : Named.t)
125125
| Prim (prim, _) -> P.is_end_region prim
126126
| Simple _ | Set_of_closures _ | Static_consts _ | Rec_info _ -> None
127127
in
128-
(* XXX should clarify effects judgement somehow to generalise here *)
128+
let is_end_region_for_unused_region =
129+
match is_end_region with
130+
| None -> false
131+
| Some region ->
132+
not (Name.Set.mem (Name.var region) (UA.required_names uacc))
133+
in
129134
if Option.is_none is_end_region
130135
&& not (Named.at_most_generative_effects defining_expr)
131136
then (
@@ -149,19 +154,8 @@ let create_let uacc (bound_vars : Bound_pattern.t) (defining_expr : Named.t)
149154
Variable.user_visible (VB.var bound_var))
150155
in
151156
let will_delete_binding =
152-
let is_end_region_for_unused_region =
153-
match is_end_region with
154-
| None -> false
155-
| Some region ->
156-
not (Name.Set.mem (Name.var region) (UA.required_names uacc))
157-
in
158157
if is_end_region_for_unused_region
159-
then
160-
(* Format.eprintf "deleting End_region %a\n%!"
161-
(Misc.Stdlib.Option.print Variable.print) (match defining_expr with
162-
| Prim (prim, _) -> P.is_end_region prim | Simple _ |
163-
Set_of_closures _ | Static_consts _ | Rec_info _ -> None); *)
164-
true
158+
then true
165159
else if Option.is_some is_end_region
166160
then false
167161
else

middle_end/flambda2/simplify/inlining/inlining_transforms.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ let make_inlined_body ~callee ~unroll_to ~params ~args ~my_closure ~my_region
3939
let my_closure =
4040
Bound_parameter.create my_closure Flambda_kind.With_subkind.any_value
4141
in
42-
let my_region =
43-
Bound_parameter.create my_region Flambda_kind.With_subkind.region
44-
in
4542
let bind_params ~params ~args ~body =
4643
if List.compare_lengths params args <> 0
4744
then
@@ -88,7 +85,7 @@ let wrap_inlined_body_for_exn_extra_args ~extra_args ~apply_exn_continuation
8885

8986
let inline dacc ~apply ~unroll_to ~was_inline_always function_decl =
9087
let callee = Apply.callee apply in
91-
let region_inlined_into = Simple.var (Apply.region apply) in
88+
let region_inlined_into = Apply.region apply in
9289
let args = Apply.args apply in
9390
let apply_return_continuation = Apply.continuation apply in
9491
let apply_exn_continuation = Apply.exn_continuation apply in

middle_end/flambda2/simplify/simplify_common.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ let split_direct_over_application apply ~param_arity ~result_arity
150150
match needs_region with
151151
| None -> Expr.create_apply perform_over_application
152152
| Some (region, after_over_application) ->
153-
(* This wraps the second application (the over application itself) with
154-
[Begin_region] ... [End_region]. This application might raise an
155-
exception, but that doesn't need any special handling, since we're not
156-
actually introducing any more local allocations here. (Missing the
157-
[End_region] on the exceptional return path is fine, c.f. the usual
158-
compilation of [try ... with] -- see [Closure_conversion].) *)
153+
(* This wraps both applications (the full application and the second
154+
application) with [Begin_region] ... [End_region]. The applications
155+
might raise an exception, but that doesn't need any special handling,
156+
since we're not actually introducing any more local allocations here.
157+
(Missing the [End_region] on the exceptional return path is fine, c.f.
158+
the usual compilation of [try ... with] -- see
159+
[Closure_conversion].) *)
159160
let over_application_results =
160161
List.mapi
161162
(fun i kind ->

middle_end/flambda2/simplify_shared/inlining_helpers.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ let make_inlined_body ~callee ~region_inlined_into ~params ~args ~my_closure
3030
let renaming =
3131
Renaming.add_continuation renaming exn_continuation apply_exn_continuation
3232
in
33+
let renaming =
34+
(* Unlike for parameters, we know that the argument for the [my_region]
35+
parameter is fresh for [body], so we can use a permutation without fear
36+
of swapping out existing occurrences of such argument within [body]. *)
37+
Renaming.add_variable renaming my_region region_inlined_into
38+
in
3339
let body =
34-
bind_params
35-
~params:(my_closure :: my_region :: params)
36-
~args:(callee :: region_inlined_into :: args)
37-
~body
40+
bind_params ~params:(my_closure :: params) ~args:(callee :: args) ~body
3841
in
3942
let body = bind_depth ~my_depth ~rec_info ~body in
4043
apply_renaming body renaming

middle_end/flambda2/simplify_shared/inlining_helpers.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
val make_inlined_body :
1818
callee:Simple.t ->
19-
region_inlined_into:Simple.t ->
19+
region_inlined_into:Variable.t ->
2020
params:'param list ->
2121
args:Simple.List.t ->
2222
my_closure:'param ->
23-
my_region:'param ->
23+
my_region:Variable.t ->
2424
my_depth:Variable.t ->
2525
rec_info:Rec_info_expr.t ->
2626
body:'expr_with_acc ->

middle_end/flambda2/to_cmm/to_cmm.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ let unit0 ~offsets ~make_symbol flambda_unit ~all_code =
7878
let env =
7979
Env.create offsets all_code ~return_continuation:dummy_k
8080
~exn_continuation:(Flambda_unit.exn_continuation flambda_unit)
81-
~my_region:(Flambda_unit.toplevel_my_region flambda_unit)
8281
in
8382
let _env, return_cont_params =
8483
(* The environment is dropped because the handler for the dummy continuation

middle_end/flambda2/to_cmm/to_cmm_env.ml

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,36 @@ type t =
8383
stages : stage list (* Stages of let-bindings, most recent at the head. *)
8484
}
8585

86+
let create offsets functions_info ~return_continuation ~exn_continuation =
87+
{ return_continuation;
88+
exn_continuation;
89+
offsets;
90+
functions_info;
91+
stages = [];
92+
pures = Variable.Map.empty;
93+
vars = Variable.Map.empty;
94+
vars_extra = Variable.Map.empty;
95+
conts = Continuation.Map.empty;
96+
exn_handlers = Continuation.Set.singleton exn_continuation;
97+
exn_conts_extra_args = Continuation.Map.empty
98+
}
99+
100+
let enter_function_body env ~return_continuation ~exn_continuation =
101+
create env.offsets env.functions_info ~return_continuation ~exn_continuation
102+
86103
let return_continuation env = env.return_continuation
87104

88105
let exn_continuation env = env.exn_continuation
89106

107+
(* Code and closures *)
108+
109+
let get_code_metadata env code_id =
110+
match Exported_code.find_exn env.functions_info code_id with
111+
| code_or_metadata -> Code_or_metadata.code_metadata code_or_metadata
112+
| exception Not_found ->
113+
Misc.fatal_errorf "To_cmm_env.get_code_metadata: code ID %a not bound"
114+
Code_id.print code_id
115+
90116
let exported_offsets t = t.offsets
91117

92118
(* Variables *)
@@ -369,39 +395,3 @@ let flush_delayed_lets ?(entering_loop = false) env =
369395
in
370396
let flush e = flush pures_to_flush env.stages e in
371397
flush, { env with stages = []; pures = pures_to_keep }
372-
373-
(* Creation *)
374-
375-
let create offsets functions_info ~return_continuation ~exn_continuation
376-
~my_region =
377-
let t =
378-
{ return_continuation;
379-
exn_continuation;
380-
offsets;
381-
functions_info;
382-
stages = [];
383-
pures = Variable.Map.empty;
384-
vars = Variable.Map.empty;
385-
vars_extra = Variable.Map.empty;
386-
conts = Continuation.Map.empty;
387-
exn_handlers = Continuation.Set.singleton exn_continuation;
388-
exn_conts_extra_args = Continuation.Map.empty
389-
}
390-
in
391-
(* Dummy binding for [my_region] *)
392-
bind_variable t my_region ~num_normal_occurrences_of_bound_vars:Unknown
393-
~effects_and_coeffects_of_defining_expr:Ece.pure
394-
~defining_expr:(C.int_const Debuginfo.none 0)
395-
396-
(* Code and closures *)
397-
398-
let get_code_metadata env code_id =
399-
match Exported_code.find_exn env.functions_info code_id with
400-
| code_or_metadata -> Code_or_metadata.code_metadata code_or_metadata
401-
| exception Not_found ->
402-
Misc.fatal_errorf "To_cmm_env.get_code_metadata: code ID %a not bound"
403-
Code_id.print code_id
404-
405-
let enter_function_body env ~return_continuation ~exn_continuation ~my_region =
406-
create env.offsets env.functions_info ~return_continuation ~exn_continuation
407-
~my_region

middle_end/flambda2/to_cmm/to_cmm_env.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ val create :
2424
Exported_code.t ->
2525
return_continuation:Continuation.t ->
2626
exn_continuation:Continuation.t ->
27-
my_region:Variable.t ->
2827
t
2928

3029
(** Given an existing environment providing the "global" information (such as
@@ -34,7 +33,6 @@ val enter_function_body :
3433
t ->
3534
return_continuation:Continuation.t ->
3635
exn_continuation:Continuation.t ->
37-
my_region:Variable.t ->
3836
t
3937

4038
(** {2 Continuations} *)

middle_end/flambda2/to_cmm/to_cmm_set_of_closures.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ end)
250250
(* Translation of the bodies of functions. *)
251251

252252
let params_and_body0 env res code_id ~fun_dbg ~return_continuation
253-
~exn_continuation ~my_region params ~body ~my_closure
253+
~exn_continuation params ~body ~my_closure
254254
~(is_my_closure_used : _ Or_unknown.t) ~translate_expr =
255255
let params =
256256
let is_my_closure_used =
@@ -271,7 +271,6 @@ let params_and_body0 env res code_id ~fun_dbg ~return_continuation
271271
trap action is attached to one of its calls *)
272272
let env =
273273
Env.enter_function_body env ~return_continuation ~exn_continuation
274-
~my_region
275274
in
276275
(* Translate the arg list and body *)
277276
let env, fun_args = C.bound_parameters env params in
@@ -291,14 +290,14 @@ let params_and_body env res code_id p ~fun_dbg ~translate_expr =
291290
~body
292291
~my_closure
293292
~is_my_closure_used
294-
~my_region
293+
~my_region:_
295294
~my_depth:_
296295
~free_names_of_body:_
297296
->
298297
try
299298
params_and_body0 env res code_id ~fun_dbg ~return_continuation
300-
~exn_continuation ~my_region params ~body ~my_closure
301-
~is_my_closure_used ~translate_expr
299+
~exn_continuation params ~body ~my_closure ~is_my_closure_used
300+
~translate_expr
302301
with Misc.Fatal_error as e ->
303302
Format.eprintf
304303
"\n\

0 commit comments

Comments
 (0)