Skip to content

Commit 33389ed

Browse files
authored
flambda-backend: Try to share code to pop regions in local functions optimisation (#2361)
1 parent 41c8e59 commit 33389ed

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

lambda/simplif.ml

+37-12
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,14 @@ type slot =
917917
func: lfunction;
918918
function_scope: lambda;
919919
mutable scope: lambda option;
920+
mutable closed_region: lambda option;
920921
}
921922

923+
type exclave_status =
924+
| No_exclave
925+
| Exclave
926+
| Within_exclave
927+
922928
module LamTbl = Hashtbl.Make(struct
923929
type t = lambda
924930
let equal = (==)
@@ -963,21 +969,27 @@ let simplify_local_functions lam =
963969
let r =
964970
{ func = lf;
965971
function_scope = !current_function_scope;
966-
scope = None }
972+
scope = None;
973+
closed_region = None }
967974
in
968975
Hashtbl.add slots id r;
969976
tail cont;
970977
begin match Hashtbl.find_opt slots id with
971-
| Some {scope = Some scope; _} ->
978+
| Some {scope = Some scope; closed_region; _} ->
972979
let st = next_raise_count () in
973-
let sc, pop_region =
980+
let sc, exclave =
974981
(* Do not move higher than current lambda *)
975-
if scope == !current_scope then cont, Same_region
976-
else if is_current_region_scope scope then cont, Popped_region
977-
else scope, Same_region
982+
if scope == !current_scope then cont, No_exclave
983+
else if is_current_region_scope scope then begin
984+
match closed_region with
985+
| Some region when region == !current_scope ->
986+
cont, Exclave
987+
| _ ->
988+
cont, Within_exclave
989+
end else scope, No_exclave
978990
in
979991
Hashtbl.add static_id id st;
980-
LamTbl.add static sc (st, lf, pop_region);
992+
LamTbl.add static sc (st, lf, exclave);
981993
(* The body of the function will become an handler
982994
in that "scope". *)
983995
with_scope ~scope lf.body
@@ -987,11 +999,11 @@ let simplify_local_functions lam =
987999
function_definition lf
9881000
end
9891001
| Lapply {ap_func = Lvar id; ap_args; ap_region_close; _} ->
990-
let curr_scope =
1002+
let curr_scope, closed_region =
9911003
match ap_region_close with
992-
| Rc_normal | Rc_nontail -> !current_scope
1004+
| Rc_normal | Rc_nontail -> !current_scope, None
9931005
| Rc_close_at_apply ->
994-
Option.get !current_region_scope
1006+
Option.get !current_region_scope, Some !current_scope
9951007
in
9961008
begin match Hashtbl.find_opt slots id with
9971009
| Some {func; _}
@@ -1007,7 +1019,14 @@ let simplify_local_functions lam =
10071019
Hashtbl.remove slots id
10081020
| Some ({scope = None; _} as slot) ->
10091021
(* First use of the function: remember the current tail scope *)
1010-
slot.scope <- Some curr_scope
1022+
slot.scope <- Some curr_scope;
1023+
slot.closed_region <- closed_region
1024+
| Some ({closed_region = Some old_closed_region} as slot) -> begin
1025+
match closed_region with
1026+
| Some closed_region when closed_region == old_closed_region ->
1027+
()
1028+
| _ -> slot.closed_region <- None
1029+
end
10111030
| _ -> ()
10121031
end;
10131032
List.iter non_tail ap_args
@@ -1074,8 +1093,14 @@ let simplify_local_functions lam =
10741093
(fun p -> (p.name, p.layout)) lf.params
10751094
in
10761095
List.fold_right
1077-
(fun (st, lf, r) lam ->
1096+
(fun (st, lf, exclave) lam ->
10781097
let body = rewrite lf.body in
1098+
let body, r =
1099+
match exclave with
1100+
| No_exclave -> body, Same_region
1101+
| Exclave -> Lexclave body, Same_region
1102+
| Within_exclave -> body, Popped_region
1103+
in
10791104
Lstaticcatch (lam, (st, new_params lf), body, r, lf.return)
10801105
)
10811106
(LamTbl.find_all static lam0)

0 commit comments

Comments
 (0)