Skip to content

Commit

Permalink
Fix global flow analysis
Browse files Browse the repository at this point in the history
In fast mode, we don't track function arguments, so we should consider them as escaping.
  • Loading branch information
vouillon committed Jul 27, 2023
1 parent 67034f6 commit 130b8f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dev (2023-??-??) - ??

* Runtime: fix Dom_html.onIE (#1493)
* Compiler: fix global flow analysis (#1494)

# 5.4.0 (2023-07-06) - Lille

Expand Down
17 changes: 14 additions & 3 deletions compiler/lib/global_flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ let expr_deps blocks st x e =
match st.defs.(Var.idx f) with
| Expr (Closure (params, _)) when List.length args = List.length params ->
Hashtbl.add st.applied_functions (x, f) ();
if not st.fast then List.iter2 ~f:(fun p a -> add_assign_def st p a) params args;
if st.fast
then List.iter ~f:(fun a -> do_escape st Escape a) args
else List.iter2 ~f:(fun p a -> add_assign_def st p a) params args;
Var.Set.iter (fun y -> add_dep st x y) (Var.Map.find f st.return_values)
| _ -> ())
| Closure (l, cont) ->
Expand Down Expand Up @@ -470,8 +472,13 @@ let propagate st ~update approx x =
if not (Hashtbl.mem st.applied_functions (x, g))
then (
Hashtbl.add st.applied_functions (x, g) ();
if not st.fast
if st.fast
then
List.iter
~f:(fun y ->
Domain.variable_escape ~update ~st ~approx Escape y)
args
else
List.iter2
~f:(fun p a ->
add_assign_def st p a;
Expand Down Expand Up @@ -593,7 +600,7 @@ let f ~fast p =
| Values { known; others } ->
Format.fprintf
f
"{%a/%b} mut:%b vmut:%b esc:%s"
"{%a/%b} mut:%b vmut:%b vesc:%s esc:%s"
(Format.pp_print_list
~pp_sep:(fun f () -> Format.fprintf f ", ")
(fun f x ->
Expand All @@ -615,6 +622,10 @@ let f ~fast p =
others
st.possibly_mutable.(Var.idx x)
st.variable_possibly_mutable.(Var.idx x)
(match st.variable_may_escape.(Var.idx x) with
| Escape -> "Y"
| Escape_constant -> "y"
| No -> "n")
(match st.may_escape.(Var.idx x) with
| Escape -> "Y"
| Escape_constant -> "y"
Expand Down
9 changes: 5 additions & 4 deletions compiler/tests-compiler/gh1494.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ let () =
in
Util.compile_and_run prog;
[%expect {|
undefined
undefined |}];
7
7 |}];
let program = Util.compile_and_parse prog in
Util.print_fun_decl program (Some "bug");
[%expect {|
[%expect
{|
function bug(param){
var g = [0, function(x){return function(_c_){return _c_;};}];
return [0, function(param){return g[1].call(null, 1);}, g];
return [0, function(param){return caml_call1(g[1], 1);}, g];
}
//end |}]

0 comments on commit 130b8f4

Please sign in to comment.