Skip to content

Commit 3a526e4

Browse files
authored
Delete unused local allocation regions in Flambda 2 (#809)
1 parent c3903ce commit 3a526e4

File tree

78 files changed

+1984
-1196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1984
-1196
lines changed

middle_end/flambda2/bound_identifiers/bound_for_function.ml

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,46 @@ type t =
1919
exn_continuation : Continuation.t;
2020
params : Bound_parameters.t;
2121
my_closure : Variable.t;
22+
my_region : Variable.t;
2223
my_depth : Variable.t
2324
}
2425

2526
let[@ocamlformat "disable"] print ppf
26-
{ return_continuation; exn_continuation; params; my_closure; my_depth } =
27+
{ return_continuation; exn_continuation; params; my_closure; my_region; my_depth } =
2728
Format.fprintf ppf "@[<hov 1>(\
2829
@[<hov 1>(return_continuation@ %a)@]@ \
2930
@[<hov 1>(exn_continuation@ %a)@]@ \
3031
@[<hov 1>(params@ %a)@]@ \
3132
@[<hov 1>(my_closure@ %a)@]@ \
33+
@[<hov 1>(my_region@ %a)@]@ \
3234
@[<hov 1>(my_depth@ %a)@])@]"
3335
Continuation.print return_continuation
3436
Continuation.print exn_continuation
3537
Bound_parameters.print params
3638
Variable.print my_closure
39+
Variable.print my_region
3740
Variable.print my_depth
3841

39-
let create ~return_continuation ~exn_continuation ~params ~my_closure ~my_depth
40-
=
42+
let create ~return_continuation ~exn_continuation ~params ~my_closure ~my_region
43+
~my_depth =
4144
Bound_parameters.check_no_duplicates params;
4245
(if Flambda_features.check_invariants ()
4346
then
4447
let params_set = Bound_parameters.var_set params in
45-
if Variable.equal my_closure my_depth
46-
|| Variable.Set.mem my_closure params_set
47-
|| Variable.Set.mem my_depth params_set
48+
let my_set = Variable.Set.of_list [my_closure; my_region; my_depth] in
49+
if Variable.Set.cardinal my_set <> 3
50+
|| not (Variable.Set.is_empty (Variable.Set.inter my_set params_set))
4851
then
4952
Misc.fatal_errorf
50-
"[my_closure] and [my_depth] must be disjoint from themselves and the \
51-
other parameters");
52-
{ return_continuation; exn_continuation; params; my_closure; my_depth }
53+
"[my_closure], [my_region] and [my_depth] must be disjoint from \
54+
themselves and the other parameters");
55+
{ return_continuation;
56+
exn_continuation;
57+
params;
58+
my_closure;
59+
my_region;
60+
my_depth
61+
}
5362

5463
let return_continuation t = t.return_continuation
5564

@@ -59,10 +68,18 @@ let params t = t.params
5968

6069
let my_closure t = t.my_closure
6170

71+
let my_region t = t.my_region
72+
6273
let my_depth t = t.my_depth
6374

6475
let free_names
65-
{ return_continuation; exn_continuation; params; my_closure; my_depth } =
76+
{ return_continuation;
77+
exn_continuation;
78+
params;
79+
my_closure;
80+
my_region;
81+
my_depth
82+
} =
6683
(* See [bound_continuations.ml] for why [add_traps] is [true]. *)
6784
let free_names =
6885
Name_occurrences.add_continuation Name_occurrences.empty return_continuation
@@ -78,11 +95,19 @@ let free_names
7895
let free_names =
7996
Name_occurrences.add_variable free_names my_closure Name_mode.normal
8097
in
98+
let free_names =
99+
Name_occurrences.add_variable free_names my_region Name_mode.normal
100+
in
81101
Name_occurrences.add_variable free_names my_depth Name_mode.normal
82102

83103
let apply_renaming
84-
{ return_continuation; exn_continuation; params; my_closure; my_depth }
85-
renaming =
104+
{ return_continuation;
105+
exn_continuation;
106+
params;
107+
my_closure;
108+
my_region;
109+
my_depth
110+
} renaming =
86111
let return_continuation =
87112
Renaming.apply_continuation renaming return_continuation
88113
in
@@ -91,25 +116,47 @@ let apply_renaming
91116
in
92117
let params = Bound_parameters.apply_renaming params renaming in
93118
let my_closure = Renaming.apply_variable renaming my_closure in
119+
let my_region = Renaming.apply_variable renaming my_region in
94120
let my_depth = Renaming.apply_variable renaming my_depth in
95-
{ return_continuation; exn_continuation; params; my_closure; my_depth }
121+
(* CR mshinwell: this should have a phys-equal check *)
122+
{ return_continuation;
123+
exn_continuation;
124+
params;
125+
my_closure;
126+
my_region;
127+
my_depth
128+
}
96129

97130
let ids_for_export
98-
{ return_continuation; exn_continuation; params; my_closure; my_depth } =
131+
{ return_continuation;
132+
exn_continuation;
133+
params;
134+
my_closure;
135+
my_region;
136+
my_depth
137+
} =
99138
let ids =
100139
Ids_for_export.add_continuation Ids_for_export.empty return_continuation
101140
in
102141
let ids = Ids_for_export.add_continuation ids exn_continuation in
103142
let ids = Ids_for_export.union ids (Bound_parameters.ids_for_export params) in
104143
let ids = Ids_for_export.add_variable ids my_closure in
144+
let ids = Ids_for_export.add_variable ids my_region in
105145
Ids_for_export.add_variable ids my_depth
106146

107147
let rename
108-
{ return_continuation; exn_continuation; params; my_closure; my_depth } =
148+
{ return_continuation;
149+
exn_continuation;
150+
params;
151+
my_closure;
152+
my_region;
153+
my_depth
154+
} =
109155
{ return_continuation = Continuation.rename return_continuation;
110156
exn_continuation = Continuation.rename exn_continuation;
111157
params = Bound_parameters.rename params;
112158
my_closure = Variable.rename my_closure;
159+
my_region = Variable.rename my_region;
113160
my_depth = Variable.rename my_depth
114161
}
115162

@@ -118,13 +165,15 @@ let renaming
118165
exn_continuation = exn_continuation1;
119166
params = params1;
120167
my_closure = my_closure1;
168+
my_region = my_region1;
121169
my_depth = my_depth1
122170
}
123171
~guaranteed_fresh:
124172
{ return_continuation = return_continuation2;
125173
exn_continuation = exn_continuation2;
126174
params = params2;
127175
my_closure = my_closure2;
176+
my_region = my_region2;
128177
my_depth = my_depth2
129178
} =
130179
let renaming =
@@ -144,4 +193,7 @@ let renaming
144193
Renaming.add_fresh_variable renaming my_closure1
145194
~guaranteed_fresh:my_closure2
146195
in
196+
let renaming =
197+
Renaming.add_fresh_variable renaming my_region1 ~guaranteed_fresh:my_region2
198+
in
147199
Renaming.add_fresh_variable renaming my_depth1 ~guaranteed_fresh:my_depth2

middle_end/flambda2/bound_identifiers/bound_for_function.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ val create :
2424
exn_continuation:Continuation.t ->
2525
params:Bound_parameters.t ->
2626
my_closure:Variable.t ->
27+
my_region:Variable.t ->
2728
my_depth:Variable.t ->
2829
t
2930

@@ -35,6 +36,8 @@ val params : t -> Bound_parameters.t
3536

3637
val my_closure : t -> Variable.t
3738

39+
val my_region : t -> Variable.t
40+
3841
val my_depth : t -> Variable.t
3942

4043
include Bindable.S with type t := t

middle_end/flambda2/compare/compare.ml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ let subst_set_of_closures env set =
274274
subst_value_slot env var, subst_simple env simple)
275275
|> Value_slot.Map.of_list
276276
in
277-
Set_of_closures.create Heap ~value_slots decls
277+
Set_of_closures.create Alloc_mode.With_region.heap ~value_slots decls
278278

279279
let subst_rec_info_expr _env ri =
280280
(* Only depth variables can occur in [Rec_info_expr], and we only mess with
@@ -290,7 +290,7 @@ let subst_call_kind env (call_kind : Call_kind.t) : Call_kind.t =
290290
match call_kind with
291291
| Function { function_call = Direct { code_id; return_arity }; _ } ->
292292
let code_id = subst_code_id env code_id in
293-
Call_kind.direct_function_call code_id ~return_arity Heap
293+
Call_kind.direct_function_call code_id ~return_arity Alloc_mode.heap
294294
| _ -> call_kind
295295

296296
let rec subst_expr env e =
@@ -387,12 +387,13 @@ and subst_params_and_body env params_and_body =
387387
~body
388388
~my_closure
389389
~is_my_closure_used:_
390+
~my_region
390391
~my_depth
391392
~free_names_of_body
392393
->
393394
let body = subst_expr env body in
394395
Function_params_and_body.create ~return_continuation ~exn_continuation
395-
params ~body ~my_closure ~free_names_of_body ~my_depth)
396+
params ~body ~my_closure ~my_region ~free_names_of_body ~my_depth)
396397

397398
and subst_let_cont env (let_cont_expr : Let_cont_expr.t) =
398399
match let_cont_expr with
@@ -432,8 +433,10 @@ and subst_apply env apply =
432433
let inlining_state = Apply_expr.inlining_state apply in
433434
let relative_history = Apply_expr.relative_history apply in
434435
let position = Apply_expr.position apply in
436+
let region = Apply_expr.region apply in
435437
Apply_expr.create ~callee ~continuation exn_continuation ~args ~call_kind dbg
436438
~inlined ~inlining_state ~probe_name:None ~position ~relative_history
439+
~region
437440
|> Expr.create_apply
438441

439442
and subst_apply_cont env apply_cont =
@@ -898,7 +901,7 @@ let call_kinds env (call_kind1 : Call_kind.t) (call_kind2 : Call_kind.t) :
898901
~subst2:(fun _ arity -> arity)
899902
env (code_id1, return_arity1) (code_id2, return_arity2)
900903
|> Comparison.map ~f:(fun (code_id, return_arity) ->
901-
Call_kind.direct_function_call code_id ~return_arity Heap)
904+
Call_kind.direct_function_call code_id ~return_arity Alloc_mode.heap)
902905
| ( Function
903906
{ function_call =
904907
Indirect_known_arity
@@ -923,7 +926,7 @@ let call_kinds env (call_kind1 : Call_kind.t) (call_kind2 : Call_kind.t) :
923926
pairs ~f1:method_kinds ~f2:simple_exprs ~subst2:subst_simple env
924927
(kind1, obj1) (kind2, obj2)
925928
|> Comparison.map ~f:(fun (kind, obj) ->
926-
Call_kind.method_call kind ~obj Heap)
929+
Call_kind.method_call kind ~obj Alloc_mode.heap)
927930
| ( C_call
928931
{ alloc = alloc1;
929932
param_arity = param_arity1;
@@ -988,6 +991,7 @@ let apply_exprs env apply1 apply2 : Expr.t Comparison.t =
988991
~inlining_state:(Apply.inlining_state apply1)
989992
~probe_name:None ~position:(Apply.position apply1)
990993
~relative_history:(Apply_expr.relative_history apply1)
994+
~region:(Apply_expr.region apply1)
991995
|> Expr.create_apply
992996
}
993997

@@ -1149,13 +1153,14 @@ and codes env (code1 : Code.t) (code2 : Code.t) =
11491153
~body1
11501154
~body2
11511155
~my_closure
1156+
~my_region
11521157
~my_depth
11531158
->
11541159
exprs env body1 body2
11551160
|> Comparison.map ~f:(fun body1' ->
11561161
Function_params_and_body.create ~return_continuation
1157-
~exn_continuation params ~body:body1' ~my_closure ~my_depth
1158-
~free_names_of_body:Unknown))
1162+
~exn_continuation params ~body:body1' ~my_closure ~my_region
1163+
~my_depth ~free_names_of_body:Unknown))
11591164
in
11601165
pairs ~f1:bodies
11611166
~f2:(options ~f:code_ids ~subst:subst_code_id)
@@ -1265,6 +1270,7 @@ and cont_handlers env handler1 handler2 =
12651270
let flambda_units u1 u2 =
12661271
let ret_cont = Continuation.create ~sort:Toplevel_return () in
12671272
let exn_cont = Continuation.create () in
1273+
let toplevel_my_region = Variable.create "toplevel_my_region" in
12681274
let mk_renaming u =
12691275
let renaming = Renaming.empty in
12701276
let renaming =
@@ -1277,6 +1283,11 @@ let flambda_units u1 u2 =
12771283
(Flambda_unit.exn_continuation u)
12781284
~guaranteed_fresh:exn_cont
12791285
in
1286+
let renaming =
1287+
Renaming.add_fresh_variable renaming
1288+
(Flambda_unit.toplevel_my_region u)
1289+
~guaranteed_fresh:toplevel_my_region
1290+
in
12801291
renaming
12811292
in
12821293
let env = Env.create () in
@@ -1287,4 +1298,4 @@ let flambda_units u1 u2 =
12871298
let module_symbol = Flambda_unit.module_symbol u1 in
12881299
Flambda_unit.create ~return_continuation:ret_cont
12891300
~exn_continuation:exn_cont ~body ~module_symbol
1290-
~used_value_slots:Unknown)
1301+
~used_value_slots:Unknown ~toplevel_my_region)

0 commit comments

Comments
 (0)