Source file effects.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
open! Stdlib
open Code
let debug = Debug.find "effects"
let get_edges g src = try Hashtbl.find g src with Not_found -> Addr.Set.empty
let add_edge g src dst = Hashtbl.replace g src (Addr.Set.add dst (get_edges g src))
let reverse_graph g =
let g' = Hashtbl.create 16 in
Hashtbl.iter
(fun child parents -> Addr.Set.iter (fun parent -> add_edge g' parent child) parents)
g;
g'
type control_flow_graph =
{ succs : (Addr.t, Addr.Set.t) Hashtbl.t
; preds : (Addr.t, Addr.Set.t) Hashtbl.t
; reverse_post_order : Addr.t list
; block_order : (Addr.t, int) Hashtbl.t
}
let build_graph blocks pc =
let succs = Hashtbl.create 16 in
let l = ref [] in
let visited = Hashtbl.create 16 in
let rec traverse pc =
if not (Hashtbl.mem visited pc)
then (
Hashtbl.add visited pc ();
let successors = Code.fold_children blocks pc Addr.Set.add Addr.Set.empty in
Hashtbl.add succs pc successors;
Addr.Set.iter traverse successors;
l := pc :: !l)
in
traverse pc;
let block_order = Hashtbl.create 16 in
List.iteri !l ~f:(fun i pc -> Hashtbl.add block_order pc i);
let preds = reverse_graph succs in
{ succs; preds; reverse_post_order = !l; block_order }
let dominator_tree g =
let dom = Hashtbl.create 16 in
let rec inter pc pc' =
if pc = pc'
then pc
else if Hashtbl.find g.block_order pc < Hashtbl.find g.block_order pc'
then inter pc (Hashtbl.find dom pc')
else inter (Hashtbl.find dom pc) pc'
in
List.iter g.reverse_post_order ~f:(fun pc ->
let l = Hashtbl.find g.succs pc in
Addr.Set.iter
(fun pc' ->
let d = try inter pc (Hashtbl.find dom pc') with Not_found -> pc in
Hashtbl.replace dom pc' d)
l);
List.iter g.reverse_post_order ~f:(fun pc ->
let l = Hashtbl.find g.succs pc in
Addr.Set.iter
(fun pc' ->
let d = Hashtbl.find dom pc' in
assert (inter pc d = d))
l);
dom
let rec dominates g idom pc pc' =
pc = pc'
|| Hashtbl.find g.block_order pc < Hashtbl.find g.block_order pc'
&& dominates g idom pc (Hashtbl.find idom pc')
let is_merge_node g pc =
let s = try Hashtbl.find g.preds pc with Not_found -> assert false in
let o = Hashtbl.find g.block_order pc in
let n =
Addr.Set.fold
(fun pc' n -> if Hashtbl.find g.block_order pc' < o then n + 1 else n)
s
0
in
n > 1
let dominance_frontier g idom =
let frontiers = Hashtbl.create 16 in
Hashtbl.iter
(fun pc preds ->
if Addr.Set.cardinal preds > 1
then
let dom = Hashtbl.find idom pc in
let rec loop runner =
if runner <> dom
then (
add_edge frontiers runner pc;
loop (Hashtbl.find idom runner))
in
Addr.Set.iter loop preds)
g.preds;
frontiers
let compute_needed_transformations ~cfg ~idom ~cps_needed ~blocks ~start =
let frontiers = dominance_frontier cfg idom in
let transformation_needed = ref Addr.Set.empty in
let matching_exn_handler = Hashtbl.create 16 in
let is_continuation = Hashtbl.create 16 in
let rec mark_needed pc =
if not (Addr.Set.mem pc !transformation_needed)
then (
transformation_needed := Addr.Set.add pc !transformation_needed;
Addr.Set.iter mark_needed (get_edges frontiers pc))
in
let mark_continuation pc x =
if not (Hashtbl.mem is_continuation pc)
then
Hashtbl.add
is_continuation
pc
(if Addr.Set.mem pc (get_edges frontiers pc) then `Loop else `Param x)
in
let rec traverse visited ~englobing_exn_handlers pc =
if Addr.Set.mem pc visited
then visited
else
let visited = Addr.Set.add pc visited in
let block = Addr.Map.find pc blocks in
(match fst block.branch with
| Branch (dst, _) -> (
match List.last block.body with
| Some
( Let
(x, (Apply _ | Prim (Extern ("%resume" | "%perform" | "%reperform"), _)))
, _ )
when Var.Set.mem x cps_needed ->
mark_needed dst;
List.iter ~f:mark_needed englobing_exn_handlers;
mark_continuation dst x
| _ -> ())
| Pushtrap (_, x, (handler_pc, _)) -> mark_continuation handler_pc x
| Poptrap _ | Raise _ -> (
match englobing_exn_handlers with
| handler_pc :: _ -> Hashtbl.add matching_exn_handler pc handler_pc
| _ -> ())
| _ -> ());
Code.fold_children
blocks
pc
(fun pc visited ->
let englobing_exn_handlers =
match block.branch with
| Pushtrap (_, _, (handler_pc, _)), _ when pc <> handler_pc ->
handler_pc :: englobing_exn_handlers
| Poptrap _, _ -> List.tl englobing_exn_handlers
| _ -> englobing_exn_handlers
in
traverse visited ~englobing_exn_handlers pc)
visited
in
ignore @@ traverse Addr.Set.empty ~englobing_exn_handlers:[] start;
!transformation_needed, matching_exn_handler, is_continuation
type jump_closures =
{ closure_of_jump : Var.t Addr.Map.t
; closures_of_alloc_site : (Var.t * Addr.t) list Addr.Map.t
}
let jump_closures blocks_to_transform idom : jump_closures =
Hashtbl.fold
(fun node idom_node jc ->
match Addr.Set.mem node blocks_to_transform with
| false -> jc
| true ->
let cname = Var.fresh () in
{ closure_of_jump = Addr.Map.add node cname jc.closure_of_jump
; closures_of_alloc_site =
Addr.Map.add
idom_node
((cname, node)
::
(try Addr.Map.find idom_node jc.closures_of_alloc_site
with Not_found -> []))
jc.closures_of_alloc_site
})
idom
{ closure_of_jump = Addr.Map.empty; closures_of_alloc_site = Addr.Map.empty }
type cps_calls = Var.Set.t
type st =
{ mutable new_blocks : Code.block Addr.Map.t * Code.Addr.t
; blocks : Code.block Addr.Map.t
; cfg : control_flow_graph
; idom : (int, int) Hashtbl.t
; jc : jump_closures
; closure_info : (Addr.t, Var.t * Code.cont) Hashtbl.t
; cps_needed : Var.Set.t
; blocks_to_transform : Addr.Set.t
; is_continuation : (Addr.t, [ `Param of Var.t | `Loop ]) Hashtbl.t
; matching_exn_handler : (Addr.t, Addr.t) Hashtbl.t
; block_order : (Addr.t, int) Hashtbl.t
; live_vars : Deadcode.variable_uses
; flow_info : Global_flow.info
; cps_calls : cps_calls ref
}
let add_block st block =
let blocks, free_pc = st.new_blocks in
st.new_blocks <- Addr.Map.add free_pc block blocks, free_pc + 1;
free_pc
let closure_of_pc ~st pc =
try Addr.Map.find pc st.jc.closure_of_jump with Not_found -> assert false
let allocate_closure ~st ~params ~body ~branch loc =
let block = { params = []; body; branch } in
let pc = add_block st block in
let name = Var.fresh () in
[ Let (name, Closure (params, (pc, []))), loc ], name
let tail_call ~st ?(instrs = []) ~exact ~check ~f args loc =
assert (exact || check);
let ret = Var.fresh () in
if check then st.cps_calls := Var.Set.add ret !(st.cps_calls);
instrs @ [ Let (ret, Apply { f; args; exact }), loc ], (Return ret, loc)
let cps_branch ~st ~src (pc, args) loc =
match Addr.Set.mem pc st.blocks_to_transform with
| false -> [], (Branch (pc, args), loc)
| true ->
let args, instrs =
if List.is_empty args && Hashtbl.mem st.is_continuation pc
then
let x = Var.fresh () in
[ x ], [ Let (x, Constant (Int 0l)), noloc ]
else args, []
in
let check = Hashtbl.find st.block_order src >= Hashtbl.find st.block_order pc in
tail_call ~st ~instrs ~exact:true ~check ~f:(closure_of_pc ~st pc) args loc
let cps_jump_cont ~st ~src ((pc, _) as cont) loc =
match Addr.Set.mem pc st.blocks_to_transform with
| false -> cont
| true ->
let call_block =
let body, branch = cps_branch ~st ~src cont loc in
add_block st { params = []; body; branch }
in
call_block, []
let allocate_continuation ~st ~alloc_jump_closures ~split_closures pc x cont loc =
let pc', args = cont in
if (match args with
| [] -> true
| [ x' ] -> Var.equal x x'
| _ -> false)
&&
match Hashtbl.find st.is_continuation pc' with
| `Param _ -> true
| `Loop -> st.live_vars.(Var.idx x) = List.length args
then alloc_jump_closures, closure_of_pc ~st pc'
else
let body, branch = cps_branch ~st ~src:pc cont loc in
let inner_closures, outer_closures =
if not split_closures
then alloc_jump_closures, []
else if is_merge_node st.cfg pc'
then [], alloc_jump_closures
else
List.partition
~f:(fun (i, _) ->
match i with
| Let (_, Closure (_, (pc'', []))) -> dominates st.cfg st.idom pc' pc''
| _ -> assert false)
alloc_jump_closures
in
let body, branch =
allocate_closure ~st ~params:[ x ] ~body:(inner_closures @ body) ~branch loc
in
outer_closures @ body, branch
let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
(instr * loc) list * (last * loc) =
match last with
| Return x ->
assert (List.is_empty alloc_jump_closures);
tail_call ~st ~exact:true ~check:false ~f:k [ x ] last_loc
| Raise (x, rmode) -> (
assert (List.is_empty alloc_jump_closures);
match Hashtbl.find_opt st.matching_exn_handler pc with
| Some pc when not (Addr.Set.mem pc st.blocks_to_transform) ->
[], (last, last_loc)
| _ ->
let exn_handler = Var.fresh_n "raise" in
let x, instrs =
match rmode with
| `Notrace -> x, []
| (`Normal | `Reraise) as m ->
let x' = Var.fork x in
let force =
match m with
| `Normal -> true
| `Reraise -> false
in
let i =
[ ( Let
( x'
, Prim
( Extern "caml_maybe_attach_backtrace"
, [ Pv x; Pc (Int (if force then 1l else 0l)) ] ) )
, noloc )
]
in
x', i
in
tail_call
~st
~instrs:
((Let (exn_handler, Prim (Extern "caml_pop_trap", [])), noloc) :: instrs)
~exact:true
~check:false
~f:exn_handler
[ x ]
last_loc)
| Stop ->
assert (List.is_empty alloc_jump_closures);
[], (Stop, last_loc)
| Branch cont ->
let body, branch = cps_branch ~st ~src:pc cont last_loc in
alloc_jump_closures @ body, branch
| Cond (x, cont1, cont2) ->
( alloc_jump_closures
, ( Cond
( x
, cps_jump_cont ~st ~src:pc cont1 last_loc
, cps_jump_cont ~st ~src:pc cont2 last_loc )
, last_loc ) )
| Switch (x, c1) ->
let cps_jump_cont = Fun.memoize (fun x -> cps_jump_cont ~st ~src:pc x last_loc) in
alloc_jump_closures, (Switch (x, Array.map c1 ~f:cps_jump_cont), last_loc)
| Pushtrap (body_cont, exn, ((handler_pc, _) as handler_cont)) -> (
assert (Hashtbl.mem st.is_continuation handler_pc);
match Addr.Set.mem handler_pc st.blocks_to_transform with
| false -> alloc_jump_closures, (last, last_loc)
| true ->
let constr_cont, exn_handler =
allocate_continuation
~st
~alloc_jump_closures
~split_closures:true
pc
exn
handler_cont
last_loc
in
let push_trap =
Let (Var.fresh (), Prim (Extern "caml_push_trap", [ Pv exn_handler ])), noloc
in
let body, branch = cps_branch ~st ~src:pc body_cont last_loc in
constr_cont @ (push_trap :: body), branch)
| Poptrap cont -> (
match
Addr.Set.mem (Hashtbl.find st.matching_exn_handler pc) st.blocks_to_transform
with
| false ->
( alloc_jump_closures
, (Poptrap (cps_jump_cont ~st ~src:pc cont last_loc), last_loc) )
| true ->
let exn_handler = Var.fresh () in
let body, branch = cps_branch ~st ~src:pc cont last_loc in
( alloc_jump_closures
@ ((Let (exn_handler, Prim (Extern "caml_pop_trap", [])), noloc) :: body)
, branch ))
let cps_instr ~st (instr : instr) : instr =
match instr with
| Let (x, Closure (params, (pc, _))) when Var.Set.mem x st.cps_needed ->
let k, cont = Hashtbl.find st.closure_info pc in
Let (x, Closure (params @ [ k ], cont))
| Let (x, Prim (Extern "caml_alloc_dummy_function", [ size; arity ])) -> (
match arity with
| Pc (Int a) ->
Let
( x
, Prim (Extern "caml_alloc_dummy_function", [ size; Pc (Int (Int32.succ a)) ])
)
| _ -> assert false)
| Let (x, Apply { f; args; _ }) when not (Var.Set.mem x st.cps_needed) ->
assert (Global_flow.exact_call st.flow_info f (List.length args));
Let (x, Apply { f; args; exact = true })
| Let (_, (Apply _ | Prim (Extern ("%resume" | "%perform" | "%reperform"), _))) ->
assert false
| _ -> instr
let cps_block ~st ~k pc block =
let alloc_jump_closures =
match Addr.Map.find pc st.jc.closures_of_alloc_site with
| to_allocate ->
List.map to_allocate ~f:(fun (cname, jump_pc) ->
let params =
let jump_block = Addr.Map.find jump_pc st.blocks in
if List.is_empty jump_block.params && Hashtbl.mem st.is_continuation jump_pc
then
let x =
match Hashtbl.find st.is_continuation jump_pc with
| `Param x -> x
| `Loop -> Var.fresh ()
in
[ x ]
else jump_block.params
in
Let (cname, Closure (params, (jump_pc, []))), noloc)
| exception Not_found -> []
in
let rewrite_instr x e loc =
let perform_effect ~effect ~continuation loc =
Some
(fun ~k ->
let e =
Prim (Extern "caml_perform_effect", [ Pv effect; continuation; Pv k ])
in
let x = Var.fresh () in
[ Let (x, e), loc ], (Return x, loc))
in
match e with
| Apply { f; args; exact } when Var.Set.mem x st.cps_needed ->
Some
(fun ~k ->
let exact =
exact || Global_flow.exact_call st.flow_info f (List.length args)
in
tail_call ~st ~exact ~check:true ~f (args @ [ k ]) loc)
| Prim (Extern "%resume", [ Pv stack; Pv f; Pv arg ]) ->
Some
(fun ~k ->
let k' = Var.fresh_n "cont" in
tail_call
~st
~instrs:
[ Let (k', Prim (Extern "caml_resume_stack", [ Pv stack; Pv k ])), noloc ]
~exact:(Global_flow.exact_call st.flow_info f 1)
~check:true
~f
[ arg; k' ]
loc)
| Prim (Extern "%perform", [ Pv effect ]) ->
perform_effect ~effect ~continuation:(Pc (Int 0l)) loc
| Prim (Extern "%reperform", [ Pv effect; continuation ]) ->
perform_effect ~effect ~continuation loc
| _ -> None
in
let rewritten_block =
match List.split_last block.body, block.branch with
| Some (body_prefix, (Let (x, e), loc)), (Return ret, _loc_ret) ->
Option.map (rewrite_instr x e loc) ~f:(fun f ->
assert (List.is_empty alloc_jump_closures);
assert (Var.equal x ret);
let instrs, branch = f ~k in
body_prefix, instrs, branch)
| Some (body_prefix, (Let (x, e), loc)), (Branch cont, loc_ret) ->
Option.map (rewrite_instr x e loc) ~f:(fun f ->
let constr_cont, k' =
allocate_continuation
~st
~alloc_jump_closures
~split_closures:false
pc
x
cont
loc_ret
in
let instrs, branch = f ~k:k' in
body_prefix, constr_cont @ instrs, branch)
| Some (_, ((Set_field _ | Offset_ref _ | Array_set _ | Assign _), _)), _
| Some _, ((Raise _ | Stop | Cond _ | Switch _ | Pushtrap _ | Poptrap _), _)
| None, _ -> None
in
let body, last =
match rewritten_block with
| Some (body_prefix, last_instrs, last) ->
List.map body_prefix ~f:(fun (i, loc) -> cps_instr ~st i, loc) @ last_instrs, last
| None ->
let last_instrs, last = cps_last ~st ~alloc_jump_closures pc block.branch ~k in
let body =
List.map block.body ~f:(fun (i, loc) -> cps_instr ~st i, loc) @ last_instrs
in
body, last
in
{ params = (if Addr.Set.mem pc st.blocks_to_transform then [] else block.params)
; body
; branch = last
}
let cps_transform ~live_vars ~flow_info ~cps_needed p =
let closure_info = Hashtbl.create 16 in
let cps_calls = ref Var.Set.empty in
let p =
Code.fold_closures_innermost_first
p
(fun name_opt _ (start, args) ({ blocks; free_pc; _ } as p) ->
let initial_start = start in
let start', blocks' =
( free_pc
, Addr.Map.add
free_pc
{ params = []; body = []; branch = Branch (start, args), noloc }
blocks )
in
let cfg = build_graph blocks' start' in
let idom = dominator_tree cfg in
let should_compute_needed_transformations =
match name_opt with
| Some name -> Var.Set.mem name cps_needed
| None ->
true
in
let blocks_to_transform, matching_exn_handler, is_continuation =
if should_compute_needed_transformations
then
compute_needed_transformations
~cfg
~idom
~cps_needed
~blocks:blocks'
~start:start'
else Addr.Set.empty, Hashtbl.create 1, Hashtbl.create 1
in
let closure_jc = jump_closures blocks_to_transform idom in
let start, args, blocks, free_pc =
if Addr.Map.mem start' closure_jc.closures_of_alloc_site
then start', [], blocks', free_pc + 1
else start, args, blocks, free_pc
in
let st =
{ new_blocks = Addr.Map.empty, free_pc
; blocks
; cfg
; idom
; jc = closure_jc
; closure_info
; cps_needed
; blocks_to_transform
; is_continuation
; matching_exn_handler
; block_order = cfg.block_order
; flow_info
; live_vars
; cps_calls
}
in
let function_needs_cps =
match name_opt with
| Some _ -> should_compute_needed_transformations
| None ->
not (Addr.Set.is_empty blocks_to_transform)
in
if debug ()
then (
Format.eprintf "======== %b@." function_needs_cps;
Code.preorder_traverse
{ fold = Code.fold_children }
(fun pc _ ->
if Addr.Set.mem pc blocks_to_transform then Format.eprintf "CPS@.";
let block = Addr.Map.find pc blocks in
Code.Print.block
(fun _ xi -> Partial_cps_analysis.annot cps_needed xi)
pc
block)
start
blocks
());
let blocks =
let transform_block =
if function_needs_cps
then (
let k = Var.fresh_n "cont" in
Hashtbl.add closure_info initial_start (k, (start, args));
fun pc block -> cps_block ~st ~k pc block)
else
fun _ block ->
{ block with
body = List.map block.body ~f:(fun (i, loc) -> cps_instr ~st i, loc)
}
in
Code.traverse
{ fold = Code.fold_children }
(fun pc blocks ->
Addr.Map.add pc (transform_block pc (Addr.Map.find pc blocks)) blocks)
start
st.blocks
st.blocks
in
let new_blocks, free_pc = st.new_blocks in
let blocks = Addr.Map.fold Addr.Map.add new_blocks blocks in
{ p with blocks; free_pc })
p
in
let p =
match Hashtbl.find_opt closure_info p.start with
| None -> p
| Some (k, _) ->
let new_start = p.free_pc in
let blocks =
let main = Var.fresh () in
let args = Var.fresh () in
let res = Var.fresh () in
Addr.Map.add
new_start
{ params = []
; body =
[ Let (main, Closure ([ k ], (p.start, []))), noloc
; Let (args, Prim (Extern "%js_array", [])), noloc
; Let (res, Prim (Extern "caml_callback", [ Pv main; Pv args ])), noloc
]
; branch = Return res, noloc
}
p.blocks
in
{ start = new_start; blocks; free_pc = new_start + 1 }
in
p, !cps_calls
let frontiers in_loop pc =
let frontier = get_edges frontiers pc in
match in_loop with
| Some when Addr.Set.mem header frontier -> in_loop
| _ -> if Addr.Set.mem pc frontier then Some pc else None
let wrap_call ~cps_needed p x f args accu loc =
let arg_array = Var.fresh () in
( p
, Var.Set.remove x cps_needed
, [ Let (arg_array, Prim (Extern "%js_array", List.map ~f:(fun y -> Pv y) args)), noloc
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv arg_array ])), loc
]
:: accu )
let wrap_primitive ~cps_needed p x e accu loc =
let f = Var.fresh () in
let closure_pc = p.free_pc in
( { p with
free_pc = p.free_pc + 1
; blocks =
Addr.Map.add
closure_pc
(let y = Var.fresh () in
{ params = []; body = [ Let (y, e), loc ]; branch = Return y, loc })
p.blocks
}
, Var.Set.remove x (Var.Set.add f cps_needed)
, let args = Var.fresh () in
[ Let (f, Closure ([], (closure_pc, []))), noloc
; Let (args, Prim (Extern "%js_array", [])), noloc
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv args ])), loc
]
:: accu )
let rewrite_toplevel_instr (p, cps_needed, accu) instr =
match instr with
| Let (x, Apply { f; args; _ }), loc when Var.Set.mem x cps_needed ->
wrap_call ~cps_needed p x f args accu loc
| Let (x, (Prim (Extern ("%resume" | "%perform" | "%reperform"), _) as e)), loc ->
wrap_primitive ~cps_needed p x e accu loc
| _ -> p, cps_needed, [ instr ] :: accu
let rewrite_toplevel ~cps_needed p =
let { start; blocks; _ } = p in
let cfg = build_graph blocks start in
let idom = dominator_tree cfg in
let frontiers = dominance_frontier cfg idom in
let rec traverse visited (p : Code.program) cps_needed in_loop pc =
if Addr.Set.mem pc visited
then visited, p, cps_needed
else
let visited = Addr.Set.add pc visited in
let in_loop = current_loop_header frontiers in_loop pc in
let p, cps_needed =
if Option.is_none in_loop
then
let block = Addr.Map.find pc p.blocks in
let p, cps_needed, body_rev =
List.fold_left ~f:rewrite_toplevel_instr ~init:(p, cps_needed, []) block.body
in
let body = List.concat @@ List.rev body_rev in
{ p with blocks = Addr.Map.add pc { block with body } p.blocks }, cps_needed
else p, cps_needed
in
Code.fold_children
blocks
pc
(fun pc (visited, p, cps_needed) -> traverse visited p cps_needed in_loop pc)
(visited, p, cps_needed)
in
let _, p, cps_needed = traverse Addr.Set.empty p cps_needed None start in
p, cps_needed
let split_blocks ~cps_needed (p : Code.program) =
let split_block pc block p =
let is_split_point i r branch =
match i with
| Let (x, (Apply _ | Prim (Extern ("%resume" | "%perform" | "%reperform"), _))) ->
((not (List.is_empty r))
||
match fst branch with
| Branch _ -> false
| Return x' -> not (Var.equal x x')
| _ -> true)
&& Var.Set.mem x cps_needed
| _ -> false
in
let rec split (p : Code.program) pc block accu l branch =
match l with
| [] ->
let block = { block with body = List.rev accu } in
{ p with blocks = Addr.Map.add pc block p.blocks }
| ((Let (x, e) as i), loc) :: r when is_split_point i r branch ->
let pc' = p.free_pc in
let block' = { params = []; body = []; branch = block.branch } in
let block =
{ block with
body = List.rev ((Let (x, e), loc) :: accu)
; branch = Branch (pc', []), noloc
}
in
let p = { p with blocks = Addr.Map.add pc block p.blocks; free_pc = pc' + 1 } in
split p pc' block' [] r branch
| i :: r -> split p pc block (i :: accu) r branch
in
let rec should_split l branch =
match l with
| [] -> false
| (i, _) :: r -> is_split_point i r branch || should_split r branch
in
if should_split block.body block.branch
then split p pc block [] block.body block.branch
else p
in
Addr.Map.fold split_block p.blocks p
let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
let shortcuts = Hashtbl.create 16 in
let rec resolve_rec visited ((pc, args) as cont) =
if Addr.Set.mem pc visited
then cont
else
match Hashtbl.find_opt shortcuts pc with
| Some (params, cont) ->
let pc', args' = resolve_rec (Addr.Set.add pc visited) cont in
let s = Subst.from_map (Subst.build_mapping params args) in
pc', List.map ~f:s args'
| None -> cont
in
let resolve cont = resolve_rec Addr.Set.empty cont in
Addr.Map.iter
(fun pc block ->
match block with
| { params; body = []; branch = Branch cont, _; _ } ->
let args =
List.fold_left
~f:(fun args x -> Var.Set.add x args)
~init:Var.Set.empty
(snd cont)
in
if List.for_all
~f:(fun x -> live_vars.(Var.idx x) = 1 && Var.Set.mem x args)
params
then Hashtbl.add shortcuts pc (params, cont)
| _ -> ())
p.blocks;
let blocks =
Addr.Map.map
(fun block ->
{ block with
branch =
(let branch, loc = block.branch in
let branch =
match branch with
| Branch cont -> Branch (resolve cont)
| Cond (x, cont1, cont2) -> Cond (x, resolve cont1, resolve cont2)
| Switch (x, a1) -> Switch (x, Array.map ~f:resolve a1)
| Pushtrap (cont1, x, cont2) -> Pushtrap (resolve cont1, x, resolve cont2)
| Poptrap cont -> Poptrap (resolve cont)
| Return _ | Raise _ | Stop -> branch
in
branch, loc)
})
p.blocks
in
{ p with blocks }
let f ~flow_info ~live_vars p =
let t = Timer.make () in
let cps_needed = Partial_cps_analysis.f p flow_info in
let p, cps_needed = rewrite_toplevel ~cps_needed p in
let p = split_blocks ~cps_needed p in
let p, cps_calls = cps_transform ~live_vars ~flow_info ~cps_needed p in
if Debug.find "times" () then Format.eprintf " effects: %a@." Timer.print t;
Code.invariant p;
p, cps_calls