Skip to content

Commit 4bd9fd5

Browse files
committed
Compiler: fix inlining 2106
1 parent e54b688 commit 4bd9fd5

File tree

4 files changed

+43
-47
lines changed

4 files changed

+43
-47
lines changed

compiler/lib/duplicate.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ let rec blocks_to_rename p pc lst =
6060
p.blocks
6161
lst
6262

63-
let closure p ~f ~params ~cont =
64-
let s = Subst.from_map (bound_variables p ~f ~params ~cont) in
63+
let closure p ~f ~params ~cont live_vars =
64+
let s =
65+
let map = bound_variables p ~f ~params ~cont in
66+
fun x ->
67+
try Var.Map.find x map
68+
with Not_found ->
69+
live_vars.(Var.idx x) <- live_vars.(Var.idx x) + 1;
70+
x
71+
in
6572
let pc, args = cont in
6673
let blocks = blocks_to_rename p pc [] in
6774
let free_pc, m =

compiler/lib/duplicate.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ val closure :
2121
-> f:Code.Var.t
2222
-> params:Code.Var.t list
2323
-> cont:int * Code.Var.t list
24+
-> int Array.t
2425
-> Code.program * Code.Var.t * Code.Var.t list * (int * Code.Var.t list)
2526
(** Given a program and a closure [f] -- defined by its name,
2627
parameters, and its continuation --, return a program with a copy

compiler/lib/inline.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,9 @@ and inline_function ~context i x f args rem state =
643643
let p, params, cont =
644644
if context.live_vars.(Var.idx f) > 0
645645
then (
646-
let p, _f, params, cont = Duplicate.closure p ~f ~params ~cont in
646+
let p, _f, params, cont =
647+
Duplicate.closure p ~f ~params ~cont context.live_vars
648+
in
647649
(* It's ok to ignore the [_f] because the function is not recursive *)
648650
assert (not info.recursive);
649651
p, params, cont)

compiler/tests-compiler/gh2106.ml

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,33 @@ end
4141
in
4242
let p = compile_and_parse ~flags:[ "--debug"; "js_assign" ] p in
4343
print_program p;
44-
[%expect.unreachable]
45-
[@@expect.uncaught_exn {|
46-
(* CR expect_test_collector: This test expectation appears to contain a backtrace.
47-
This is strongly discouraged as backtraces are fragile.
48-
Please change this test to not include a backtrace. *)
49-
(Failure "non-zero exit code")
50-
Raised at Stdlib__Buffer.add_channel in file "buffer.ml", line 213, characters 18-35
51-
Called from Jsoo_compiler_expect_tests_helper__Util.channel_to_string.loop in file "compiler/tests-compiler/util/util.ml", line 169, characters 4-52
52-
Called from Jsoo_compiler_expect_tests_helper__Util.channel_to_string in file "compiler/tests-compiler/util/util.ml", line 172, characters 7-14
53-
54-
Trailing output
55-
---------------
56-
(function(globalThis){
57-
"use strict";
58-
var
59-
runtime = globalThis.jsoo_runtime,
60-
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace;
61-
function caml_call1(f, a0){
62-
return (f.l >= 0 ? f.l : f.l = f.length) === 1
63-
? f(a0)
64-
: runtime.caml_call_gen(f, [a0]);
65-
}
66-
var
67-
global_data = runtime.caml_get_global_data(),
68-
Assert_failure = global_data.Assert_failure,
69-
_a_ = [0, runtime.caml_string_of_jsbytes("test.ml"), 12, 38],
70-
dummy = 0;
71-
runtime.foo
72-
(function(param){
73-
caml_call1(<v30{f}>, dummy);
74-
throw caml_maybe_attach_backtrace([0, Assert_failure, _a_], 1);
75-
});
76-
var X = [0], Test = [0, X];
77-
runtime.caml_register_global(2, Test, "Test");
78-
return;
79-
}
80-
(globalThis));
81-
Some variables escaped: <v30{f}>
82-
/home/hugo/js_of_ocaml/_build/default/compiler/bin-js_of_ocaml/js_of_ocaml.exe: You found a bug. Please report it at https://github.com/ocsigen/js_of_ocaml/issues :
83-
Error: File "compiler/lib/js_assign.ml", line 503, characters 5-11: Assertion failed
84-
85-
process exited with error code 125
86-
/home/hugo/js_of_ocaml/_build/default/compiler/bin-js_of_ocaml/js_of_ocaml.exe --pretty --debug var --sourcemap --effects=disabled --disable=use-js-string --disable header --debug js_assign --Werror test.cmo -o test.js
87-
|}]
44+
[%expect
45+
{|
46+
(function(globalThis){
47+
"use strict";
48+
var
49+
runtime = globalThis.jsoo_runtime,
50+
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
51+
global_data = runtime.caml_get_global_data(),
52+
Assert_failure = global_data.Assert_failure,
53+
_a_ = [0, runtime.caml_string_of_jsbytes("test.ml"), 12, 38];
54+
runtime.foo
55+
(function(param){
56+
function f(param){
57+
throw caml_maybe_attach_backtrace([0, Assert_failure, _a_], 1);
58+
}
59+
var block = [0, f(0), 24029], dst = block, offset = 1;
60+
for(;;){
61+
var dst$0 = [0, f(0), 24029];
62+
dst[offset + 1] = dst$0;
63+
dst = dst$0;
64+
offset = 1;
65+
}
66+
});
67+
var X = [0], Test = [0, X];
68+
runtime.caml_register_global(2, Test, "Test");
69+
return;
70+
}
71+
(globalThis));
72+
//end
73+
|}]

0 commit comments

Comments
 (0)