@@ -91,20 +91,25 @@ let collect_deps p closures =
9191
9292module Var_SCC = Strongly_connected_components. Make (Var )
9393
94- let visit_closures p ~live_vars f acc =
94+ let visit_closures p ~live_vars f ~ cleanup acc =
9595 let closures = collect_closures p in
9696 let deps = collect_deps p closures in
9797 let scc = Var_SCC. connected_components_sorted_from_roots_to_leaf deps in
9898 let f' recursive acc g =
9999 let params, cont, enclosing_function = Var.Hashtbl. find closures g in
100100 f ~recursive ~enclosing_function ~current_function: (Some g) ~params ~cont acc
101101 in
102+ let cleanup' l acc =
103+ List. fold_left l ~init: acc ~f: (fun acc x ->
104+ let _, (pc, _), _ = Var.Hashtbl. find closures x in
105+ cleanup pc acc)
106+ in
102107 let acc =
103108 Array. fold_left
104109 scc
105110 ~f: (fun acc group ->
106111 match group with
107- | Var_SCC. No_loop g -> f' false acc g
112+ | Var_SCC. No_loop g -> f' false acc g |> cleanup' [ g ]
108113 | Has_loop l ->
109114 let set = Var.Set. of_list l in
110115 let deps' =
@@ -124,9 +129,14 @@ let visit_closures p ~live_vars f acc =
124129 scc
125130 ~f: (fun acc group ->
126131 match group with
127- | Var_SCC. No_loop g -> f' true acc g
128- | Has_loop l -> List. fold_left ~f: (fun acc g -> f' true acc g) ~init: acc l)
129- ~init: acc)
132+ | Var_SCC. No_loop g -> f' true acc g |> cleanup' [ g ]
133+ | Has_loop l ->
134+ List. fold_left
135+ ~f: (fun acc g -> f' true acc g |> cleanup' [ g ])
136+ ~init: acc
137+ l)
138+ ~init: acc
139+ |> cleanup' l)
130140 ~init: acc
131141 in
132142 f
@@ -136,6 +146,7 @@ let visit_closures p ~live_vars f acc =
136146 ~params: []
137147 ~cont: (p.start, [] )
138148 acc
149+ |> cleanup p.start
139150
140151(* ***)
141152
@@ -409,7 +420,8 @@ and should_inline ~context info args =
409420 || Option. equal Var. equal info.enclosing_function context.current_function
410421 || (not ! (context.has_closures))
411422 && Option. equal Var. equal info.enclosing_function context.enclosing_function
412- | `Wasm , _ | `JavaScript , `Double_translation -> true
423+ | `JavaScript , `Double_translation -> true
424+ | `Wasm , _ -> true
413425 | `JavaScript , `Jspi -> assert false )
414426 && (functor_like ~context info
415427 || (context.live_vars.(Var. idx info.f) = 1
@@ -461,7 +473,7 @@ let trace_inlining ~context info x args =
461473 with an initial continuation pointing to a block belonging to
462474 another function. This removes these closures. *)
463475
464- let remove_dead_closures_from_block ~live_vars p pc block =
476+ let remove_dead_closures_from_block ~live_vars block =
465477 let is_dead_closure i =
466478 match i with
467479 | Let (f , Closure _ ) ->
@@ -471,30 +483,27 @@ let remove_dead_closures_from_block ~live_vars p pc block =
471483 in
472484 if List. exists ~f: is_dead_closure block.body
473485 then
474- { p with
475- blocks =
476- Addr.Map. add
477- pc
478- { block with
479- body =
480- List. fold_left block.body ~init: [] ~f: (fun acc i ->
481- match i, acc with
482- | Event _ , Event _ :: prev ->
483- (* Avoid consecutive events (keep just the last one) *)
484- i :: prev
485- | _ -> if is_dead_closure i then acc else i :: acc)
486- |> List. rev
487- }
488- p.blocks
486+ { block with
487+ body =
488+ List. fold_left block.body ~init: [] ~f: (fun acc i ->
489+ match i, acc with
490+ | Event _ , Event _ :: prev ->
491+ (* Avoid consecutive events (keep just the last one) *)
492+ i :: prev
493+ | _ -> if is_dead_closure i then acc else i :: acc)
494+ |> List. rev
489495 }
490- else p
496+ else block
491497
492498let remove_dead_closures ~live_vars p pc =
493499 Code. traverse
494500 { fold = fold_children }
495501 (fun pc p ->
496502 let block = Addr.Map. find pc p.blocks in
497- remove_dead_closures_from_block ~live_vars p pc block)
503+ let block' = remove_dead_closures_from_block ~live_vars block in
504+ if phys_equal block block'
505+ then p
506+ else { p with blocks = Addr.Map. add pc block' p.blocks })
498507 pc
499508 p.blocks
500509 p
@@ -612,7 +621,6 @@ let inline ~profile ~inline_count p ~live_vars =
612621 p.blocks
613622 p
614623 in
615- let p = remove_dead_closures ~live_vars p pc in
616624 let env =
617625 match current_function with
618626 | Some f ->
@@ -635,6 +643,9 @@ let inline ~profile ~inline_count p ~live_vars =
635643 | None -> context.env
636644 in
637645 { context with p; env })
646+ ~cleanup: (fun pc context ->
647+ let p = remove_dead_closures ~live_vars context.p pc in
648+ { context with p })
638649 { profile
639650 ; p
640651 ; live_vars
0 commit comments