Skip to content

Commit 3b5660d

Browse files
vouillonhhugo
authored andcommitted
Inlining: make sure inner closures are processed before their enclosing closure
1 parent cb5982c commit 3b5660d

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* Compiler: speed-up compilation by improving the scheduling of optimization passes (#1962, #2001, #2012)
3939
* Compiler: deadcode elimination of cyclic values (#1978)
4040
* Compiler: directly write Wasm binary modules (#2000, #2003)
41-
* Compiler: rewrote inlining pass (#1935)
41+
* Compiler: rewrote inlining pass (#1935, #2018)
4242

4343
## Bug fixes
4444
* Compiler: fix stack overflow issues with double translation (#1869)

compiler/lib/inline.ml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,41 +94,44 @@ module Var_SCC = Strongly_connected_components.Make (Var)
9494
let visit_closures p ~live_vars f acc =
9595
let closures = collect_closures p in
9696
let deps = collect_deps p closures in
97-
let scc = Var_SCC.connected_components_sorted_from_roots_to_leaf deps in
98-
let f' recursive acc g =
97+
let f' ~recursive acc g =
9998
let params, cont, enclosing_function = Var.Hashtbl.find closures g in
10099
f ~recursive ~enclosing_function ~current_function:(Some g) ~params ~cont acc
101100
in
102-
let acc =
101+
let rec visit ~recursive deps acc =
102+
let scc = Var_SCC.connected_components_sorted_from_roots_to_leaf deps in
103103
Array.fold_left
104104
scc
105105
~f:(fun acc group ->
106106
match group with
107-
| Var_SCC.No_loop g -> f' false acc g
107+
| Var_SCC.No_loop g -> f' ~recursive acc g
108108
| Has_loop l ->
109109
let set = Var.Set.of_list l in
110110
let deps' =
111111
List.fold_left
112112
~f:(fun deps' g ->
113113
Var.Map.add
114114
g
115-
(if live_vars.(Var.idx g) > 1
116-
then Var.Set.empty
117-
else Var.Set.inter (Var.Map.find g deps) set)
115+
(Var.Set.inter
116+
(if recursive || live_vars.(Var.idx g) > 1
117+
then
118+
(* Make sure that inner closures are
119+
processed before their enclosing
120+
closure *)
121+
let _, _, enclosing = Var.Hashtbl.find closures g in
122+
match enclosing with
123+
| None -> Var.Set.empty
124+
| Some enclosing -> Var.Set.singleton enclosing
125+
else Var.Map.find g deps)
126+
set)
118127
deps')
119128
~init:Var.Map.empty
120129
l
121130
in
122-
let scc = Var_SCC.connected_components_sorted_from_roots_to_leaf deps' in
123-
Array.fold_left
124-
scc
125-
~f:(fun acc group ->
126-
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)
131+
visit ~recursive:true deps' acc)
130132
~init:acc
131133
in
134+
let acc = visit ~recursive:false deps acc in
132135
f
133136
~recursive:false
134137
~enclosing_function:None

0 commit comments

Comments
 (0)