Skip to content

Commit 8d87bba

Browse files
authored
Compiler: enable global deadcode with effects (#1526)
1 parent 80bf1ab commit 8d87bba

File tree

9 files changed

+2216
-2202
lines changed

9 files changed

+2216
-2202
lines changed

Diff for: compiler/lib/driver.ml

+19-8
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@ let ( +> ) f g x = g (f x)
8989

9090
let map_fst f (x, y) = f x, y
9191

92-
let effects p =
92+
let effects ~deadcode_sentinal p =
9393
if Config.Flag.effects ()
9494
then (
9595
if debug () then Format.eprintf "Effects...@.";
96-
p |> Deadcode.f +> Effects.f +> map_fst Lambda_lifting.f)
96+
let p, live_vars = Deadcode.f p in
97+
let p = Effects.remove_empty_blocks ~live_vars p in
98+
let p, live_vars = Deadcode.f p in
99+
let info = Global_flow.f ~fast:false p in
100+
let p, live_vars =
101+
if Config.Flag.globaldeadcode ()
102+
then
103+
let p = Global_deadcode.f p ~deadcode_sentinal info in
104+
Deadcode.f p
105+
else p, live_vars
106+
in
107+
let p, cps = p |> Effects.f ~flow_info:info ~live_vars +> map_fst Lambda_lifting.f in
108+
p, cps)
97109
else p, (Code.Var.Set.empty : Effects.cps_calls)
98110

99111
let exact_calls profile ~deadcode_sentinal p =
@@ -110,8 +122,7 @@ let exact_calls profile ~deadcode_sentinal p =
110122
then Global_deadcode.f p ~deadcode_sentinal info
111123
else p
112124
in
113-
let p = Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p in
114-
p
125+
Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p
115126
else p
116127

117128
let print p =
@@ -579,19 +590,19 @@ let configure formatter =
579590
Code.Var.set_stable (Config.Flag.stable_var ())
580591

581592
let full ~standalone ~wrap_with_fun ~profile ~linkall ~source_map formatter d p =
593+
let exported_runtime = not standalone in
582594
let deadcode_sentinal =
583595
(* If deadcode is disabled, this field is just fresh variable *)
584-
Code.Var.fresh ()
596+
Code.Var.fresh_n "undef"
585597
in
586-
let exported_runtime = not standalone in
587598
let opt =
588599
specialize_js_once
589600
+> (match profile with
590601
| O1 -> o1
591602
| O2 -> o2
592603
| O3 -> o3)
593-
+> exact_calls profile ~deadcode_sentinal
594-
+> effects
604+
+> exact_calls ~deadcode_sentinal profile
605+
+> effects ~deadcode_sentinal
595606
+> map_fst (Generate_closure.f +> deadcode')
596607
in
597608
let emit =

Diff for: compiler/lib/effects.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,12 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
923923

924924
(****)
925925

926-
let f (p, live_vars) =
926+
let f ~flow_info ~live_vars p =
927927
let t = Timer.make () in
928-
let p = remove_empty_blocks ~live_vars p in
929-
(* [remove_empty_blocks] can affect [Deadcode.variable_uses] *)
930-
let p, live_vars = Deadcode.f p in
931-
let flow_info = Global_flow.f ~fast:false p in
932928
let cps_needed = Partial_cps_analysis.f p flow_info in
933929
let p, cps_needed = rewrite_toplevel ~cps_needed p in
934930
let p = split_blocks ~cps_needed p in
935931
let p, cps_calls = cps_transform ~live_vars ~flow_info ~cps_needed p in
936932
if Debug.find "times" () then Format.eprintf " effects: %a@." Timer.print t;
933+
Code.invariant p;
937934
p, cps_calls

Diff for: compiler/lib/effects.mli

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818

1919
type cps_calls = Code.Var.Set.t
2020

21-
val f : Code.program * Deadcode.variable_uses -> Code.program * cps_calls
21+
val remove_empty_blocks : live_vars:Deadcode.variable_uses -> Code.program -> Code.program
22+
23+
val f :
24+
flow_info:Global_flow.info
25+
-> live_vars:Deadcode.variable_uses
26+
-> Code.program
27+
-> Code.program * cps_calls

Diff for: compiler/lib/global_deadcode.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ let zero prog sentinal live_table =
352352
let branch =
353353
(* Zero out return values in last instruction, otherwise do nothing. *)
354354
match block.branch with
355-
| Return x, loc -> Return (zero_var x), loc
355+
| Return x, loc ->
356+
if Config.Flag.effects () then block.branch else Return (zero_var x), loc
356357
| Raise (_, _), _
357358
| Stop, _
358359
| Branch _, _
@@ -412,6 +413,7 @@ let add_sentinal p sentinal =
412413

413414
(** Run the liveness analysis and replace dead variables with the given sentinal. *)
414415
let f p ~deadcode_sentinal global_info =
416+
Code.invariant p;
415417
let t = Timer.make () in
416418
(* Add sentinal variable *)
417419
let p = add_sentinal p deadcode_sentinal in

Diff for: compiler/tests-compiler/direct_calls.ml

+13-15
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,25 @@ let%expect_test "direct calls without --enable effects" =
5858
{|
5959
function test1(param){
6060
function f(g, x){caml_call1(g, x); return;}
61-
var _e_ = 7;
62-
f(function(x){return x + 1 | 0;}, _e_);
63-
var _f_ = 4.;
64-
f(function(x){return x * 2.;}, _f_);
61+
var _d_ = 7;
62+
f(function(x){return x + 1 | 0;}, _d_);
63+
var _e_ = 4.;
64+
f(function(x){return x * 2.;}, _e_);
6565
return 0;
6666
}
6767
//end
6868
function test2(param){
6969
function f(g, x){caml_call1(g, x); return;}
70-
var _d_ = 7;
71-
f(function(x){return x + 1 | 0;}, _d_);
70+
var _c_ = 7;
71+
f(function(x){return x + 1 | 0;}, _c_);
7272
f(function(x){return caml_call2(Stdlib[28], x, cst_a$0);}, cst_a);
7373
return 0;
7474
}
7575
//end
7676
function test3(x){
7777
function F(symbol){function f(x){return x + 1 | 0;} return [0, f];}
78-
var M1 = F([0]), M2 = F([0]), _c_ = M2[1].call(null, 2);
79-
return [0, M1[1].call(null, 1), _c_];
78+
var M1 = F([0]), M2 = F([0]), _b_ = M2[1].call(null, 2);
79+
return [0, M1[1].call(null, 1), _b_];
8080
}
8181
//end
8282
function test4(x){
@@ -130,11 +130,9 @@ let%expect_test "direct calls with --enable effects" =
130130
[%expect
131131
{|
132132
function test1(param, cont){
133-
function f(g, x){return g(x);}
134-
var _k_ = 7;
135-
f(function(x){return x + 1 | 0;}, _k_);
136-
var _l_ = 4.;
137-
f(function(x){return x * 2.;}, _l_);
133+
function f(g, x){return g(undef);}
134+
f(function(x){return x + 1 | 0;}, undef);
135+
f(function(x){return x * 2.;}, undef);
138136
return cont(0);
139137
}
140138
//end
@@ -157,7 +155,7 @@ let%expect_test "direct calls with --enable effects" =
157155
//end
158156
function test3(x, cont){
159157
function F(symbol){function f(x){return x + 1 | 0;} return [0, f];}
160-
var M1 = F([0]), M2 = F([0]), _e_ = M2[1].call(null, 2);
158+
var M1 = F(undef), M2 = F(undef), _e_ = M2[1].call(null, 2);
161159
return cont([0, M1[1].call(null, 1), _e_]);
162160
}
163161
//end
@@ -166,7 +164,7 @@ let%expect_test "direct calls with --enable effects" =
166164
function f(x, cont){return caml_cps_call3(Stdlib_Printf[2], _a_, x, cont);}
167165
return [0, f];
168166
}
169-
var M1 = F([0]), M2 = F([0]), _b_ = 1, _c_ = M1[1];
167+
var M1 = F(undef), M2 = F(undef), _b_ = 1, _c_ = M1[1];
170168
return caml_cps_exact_call2
171169
(_c_,
172170
_b_,

Diff for: compiler/tests-compiler/effects_toplevel.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
6565
return caml_callback
6666
(function(cont){
6767
var
68+
undef = undefined,
6869
global_data = runtime.caml_get_global_data(),
6970
Stdlib_Printf = global_data.Stdlib__Printf,
7071
_a_ =
@@ -74,17 +75,16 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
7475
function g(param, cont){
7576
return caml_cps_call2(Stdlib_Printf[2], _a_, cont);
7677
}
77-
caml_callback(g, [0]);
78+
caml_callback(g, [undef]);
7879
var _b_ = 1;
7980
function _c_(i){
80-
var _d_ = 0;
8181
return caml_cps_exact_call2
8282
(g,
83-
_d_,
84-
function(_e_){
85-
var _f_ = i + 1 | 0;
86-
if(5 !== i) return caml_cps_exact_call1(_c_, _f_);
87-
caml_callback(g, [0]);
83+
undef,
84+
function(_d_){
85+
var _e_ = i + 1 | 0;
86+
if(5 !== i) return caml_cps_exact_call1(_c_, _e_);
87+
caml_callback(g, [undef]);
8888
var Test = [0];
8989
runtime.caml_register_global(2, Test, "Test");
9090
return;

Diff for: compiler/tests-compiler/gh1494.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let () =
3939
[%expect
4040
{|
4141
function bug(param){
42-
var g = [0, function(x){return function(_d_){return _d_;};}];
42+
var g = [0, function(x){return function(_c_){return _c_;};}];
4343
return [0, function(param){return caml_call1(g[1], 1);}, g];
4444
}
4545
//end |}]

Diff for: compiler/tests-compiler/global_deadcode.ml

+12-12
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,30 @@ let%expect_test "Eliminates unused functions from functor" =
4040
function create(l, v, r){
4141
if(l) var h = l[4], hl = h; else var hl = 0;
4242
if(r) var h$0 = r[4], hr = h$0; else var hr = 0;
43-
var _n_ = hr <= hl ? hl + 1 | 0 : hr + 1 | 0;
44-
return [0, l, v, r, _n_];
43+
var _m_ = hr <= hl ? hl + 1 | 0 : hr + 1 | 0;
44+
return [0, l, v, r, _m_];
4545
}
4646
function bal(l, v, r){
4747
if(l) var h = l[4], hl = h; else var hl = 0;
4848
if(r) var h$0 = r[4], hr = h$0; else var hr = 0;
4949
if((hr + 2 | 0) < hl){
5050
if(! l) return invalid_arg(_b_);
51-
var lr = l[3], lv = l[2], ll = l[1], _i_ = height(lr);
52-
if(_i_ <= height(ll)) return create(ll, lv, create(lr, v, r));
51+
var lr = l[3], lv = l[2], ll = l[1], _h_ = height(lr);
52+
if(_h_ <= height(ll)) return create(ll, lv, create(lr, v, r));
5353
if(! lr) return invalid_arg(_a_);
54-
var lrr = lr[3], lrv = lr[2], lrl = lr[1], _j_ = create(lrr, v, r);
55-
return create(create(ll, lv, lrl), lrv, _j_);
54+
var lrr = lr[3], lrv = lr[2], lrl = lr[1], _i_ = create(lrr, v, r);
55+
return create(create(ll, lv, lrl), lrv, _i_);
5656
}
5757
if((hl + 2 | 0) >= hr){
58-
var _m_ = hr <= hl ? hl + 1 | 0 : hr + 1 | 0;
59-
return [0, l, v, r, _m_];
58+
var _l_ = hr <= hl ? hl + 1 | 0 : hr + 1 | 0;
59+
return [0, l, v, r, _l_];
6060
}
6161
if(! r) return invalid_arg(_d_);
62-
var rr = r[3], rv = r[2], rl = r[1], _k_ = height(rl);
63-
if(_k_ <= height(rr)) return create(create(l, v, rl), rv, rr);
62+
var rr = r[3], rv = r[2], rl = r[1], _j_ = height(rl);
63+
if(_j_ <= height(rr)) return create(create(l, v, rl), rv, rr);
6464
if(! rl) return invalid_arg(_c_);
65-
var rlr = rl[3], rlv = rl[2], rll = rl[1], _l_ = create(rlr, rv, rr);
66-
return create(create(l, v, rll), rlv, _l_);
65+
var rlr = rl[3], rlv = rl[2], rll = rl[1], _k_ = create(rlr, rv, rr);
66+
return create(create(l, v, rll), rlv, _k_);
6767
}
6868
function add(x, t){
6969
if(! t) return [0, 0, x, 0, 1];

0 commit comments

Comments
 (0)