Skip to content

Commit

Permalink
Compiler: Take into account block mutability in flow analysis (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon authored Sep 13, 2024
1 parent b2f3bae commit 886855c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
9 changes: 6 additions & 3 deletions compiler/lib/flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,13 @@ let rec block_escape st x =
if not (Code.Var.ISet.mem st.may_escape y)
then (
Code.Var.ISet.add st.may_escape y;
Code.Var.ISet.add st.possibly_mutable y;
match st.defs.(Var.idx y) with
| Expr (Block (_, l, _, _)) -> Array.iter l ~f:(fun z -> block_escape st z)
| _ -> ()))
| Expr (Block (_, l, _, mut)) ->
(match mut with
| Immutable -> ()
| Maybe_mutable -> Code.Var.ISet.add st.possibly_mutable y);
Array.iter l ~f:(fun z -> block_escape st z)
| _ -> Code.Var.ISet.add st.possibly_mutable y))
(Var.Tbl.get st.known_origins x)

let expr_escape st _x e =
Expand Down
20 changes: 12 additions & 8 deletions compiler/lib/global_flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ module Domain = struct
then (
st.may_escape.(idx) <- s;
match st.defs.(idx) with
| Expr (Block (_, a, _, _)) -> (
| Expr (Block (_, a, _, mut)) -> (
Array.iter ~f:(fun y -> variable_escape ~update ~st ~approx s y) a;
match s with
| Escape ->
match s, mut with
| Escape, Maybe_mutable ->
Var.ISet.add st.possibly_mutable x;
update ~children:true x
| Escape_constant | No -> ())
| (Escape_constant | No), _ | Escape, Immutable -> ())
| Expr (Closure (params, _)) ->
List.iter
~f:(fun y ->
Expand Down Expand Up @@ -386,10 +386,14 @@ module Domain = struct
| Values { known; _ } ->
Var.Set.iter
(fun x ->
if not (Var.ISet.mem st.possibly_mutable x)
then (
Var.ISet.add st.possibly_mutable x;
update ~children:true x))
match st.defs.(Var.idx x) with
| Expr (Block (_, _, _, Maybe_mutable)) ->
if not (Var.ISet.mem st.possibly_mutable x)
then (
Var.ISet.add st.possibly_mutable x;
update ~children:true x)
| Expr (Block (_, _, _, Immutable)) | Expr (Closure _) -> ()
| Phi _ | Expr _ -> assert false)
known
end

Expand Down

0 comments on commit 886855c

Please sign in to comment.