Skip to content

Commit 54dc89b

Browse files
committed
check and fix invariant
1 parent 97aeb36 commit 54dc89b

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

compiler/lib/code.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,11 @@ let invariant ({ blocks; start; _ } as p) =
988988
let define x =
989989
if check_defs
990990
then (
991-
assert (not (Var.ISet.mem defs x));
991+
if Var.ISet.mem defs x
992+
then (
993+
Format.eprintf "%a already defined@." Var.print x;
994+
Print.program Format.err_formatter (fun _ _ -> "") p;
995+
assert false);
992996
Var.ISet.add defs x)
993997
in
994998
let check_expr = function
@@ -1038,7 +1042,10 @@ let invariant ({ blocks; start; _ } as p) =
10381042
let visited = used_blocks p in
10391043
Addr.Map.iter
10401044
(fun pc block ->
1041-
assert (BitSet.mem visited pc);
1045+
if not (BitSet.mem visited pc)
1046+
then (
1047+
Format.eprintf "%d is dead@." pc;
1048+
assert false);
10421049
List.iter block.params ~f:define;
10431050
List.iter block.body ~f:check_instr;
10441051
check_events block.body;

compiler/lib/duplicate.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ let rec blocks_to_rename p pc lst =
6161
lst
6262

6363
let closure p ~f ~params ~cont =
64-
let s = Subst.from_map (bound_variables p ~f ~params ~cont) in
64+
let bounded = bound_variables p ~f ~params ~cont in
65+
let s = Subst.from_map bounded in
6566
let pc, args = cont in
6667
let blocks = blocks_to_rename p pc [] in
6768
let free_pc, m =

compiler/lib/effects.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ let f ~flow_info ~live_vars p =
11441144
let p = split_blocks ~cps_needed p in
11451145
let p, trampolined_calls, in_cps = cps_transform ~live_vars ~flow_info ~cps_needed p in
11461146
if Debug.find "times" () then Format.eprintf " effects: %a@." Timer.print t;
1147+
let p = Deadcode.remove_unused_blocks p in
11471148
Code.invariant p;
11481149
if debug ()
11491150
then (

compiler/lib/inline.ml

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,25 @@ let collect_deps p closures =
9191

9292
module 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

492498
let 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

dune-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
(runtest_alias runtest-wasm))
88
(js_of_ocaml
99
;; enable for debugging
10-
;; (flags (:standard --debug stats-debug --debug invariant))
10+
(flags (:standard --debug stats-debug --debug invariant))
1111
(runtest_alias runtest-js))))

0 commit comments

Comments
 (0)