Skip to content

Commit 31e5d22

Browse files
committed
Compiler: Global DCE does not break TC
1 parent 922b472 commit 31e5d22

File tree

4 files changed

+287
-347
lines changed

4 files changed

+287
-347
lines changed

compiler/lib/global_deadcode.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,14 @@ 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+
let tc =
357+
(* We don't want to break tailcalls. *)
358+
match List.last body with
359+
| Some (Let (x', Apply _), _) when Code.Var.equal x' x -> true
360+
| Some _ | None -> false
361+
in
362+
if tc then Return x, loc else Return (zero_var x), loc
356363
| Raise (_, _), _
357364
| Stop, _
358365
| Branch _, _

compiler/tests-compiler/direct_calls.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ let%expect_test "direct calls without --enable effects" =
5757
[%expect
5858
{|
5959
function test1(param){
60-
function f(g, x){caml_call1(g, x); return;}
60+
function f(g, x){return caml_call1(g, x);}
6161
var _d_ = 7;
6262
f(function(x){return x + 1 | 0;}, _d_);
6363
var _e_ = 4.;
@@ -66,7 +66,7 @@ let%expect_test "direct calls without --enable effects" =
6666
}
6767
//end
6868
function test2(param){
69-
function f(g, x){caml_call1(g, x); return;}
69+
function f(g, x){return caml_call1(g, x);}
7070
var _c_ = 7;
7171
f(function(x){return x + 1 | 0;}, _c_);
7272
f(function(x){return caml_call2(Stdlib[28], x, cst_a$0);}, cst_a);
@@ -130,16 +130,14 @@ let%expect_test "direct calls with --enable effects" =
130130
[%expect
131131
{|
132132
function test1(param, cont){
133-
function f(g, x){g(undef); return;}
133+
function f(g, x){return g(undef);}
134134
f(function(x){return;}, undef);
135135
f(function(x){return;}, undef);
136136
return cont(0);
137137
}
138138
//end
139139
function test2(param, cont){
140-
function f(g, x, cont){
141-
return caml_cps_exact_call2(g, x, function(_m_){return cont(undef);});
142-
}
140+
function f(g, x, cont){return caml_cps_exact_call2(g, x, cont);}
143141
var _f_ = 7;
144142
function _g_(x, cont){return cont(undef);}
145143
return caml_cps_exact_call3
@@ -148,9 +146,7 @@ let%expect_test "direct calls with --enable effects" =
148146
_f_,
149147
function(_h_){
150148
function _i_(x, cont){
151-
var _k_ = Stdlib[28];
152-
return caml_cps_call3
153-
(_k_, x, cst_a$0, function(_l_){return cont(undef);});
149+
return caml_cps_call3(Stdlib[28], x, cst_a$0, cont);
154150
}
155151
return caml_cps_exact_call3
156152
(f, _i_, cst_a, function(_j_){return cont(0);});

compiler/tests-compiler/effects_toplevel.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ let%expect_test "test-compiler/lib-effects/test1.ml" =
7373
[11, caml_string_of_jsbytes("abc"), 0],
7474
caml_string_of_jsbytes("abc")];
7575
function g(param, cont){
76-
var _f_ = Stdlib_Printf[2];
77-
return caml_cps_call2
78-
(_f_, _a_, function(_g_){return cont(undef);});
76+
return caml_cps_call2(Stdlib_Printf[2], _a_, cont);
7977
}
8078
caml_callback(g, [undef]);
8179
var _b_ = 1;

0 commit comments

Comments
 (0)