Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/bind-assigned-variables-in-js-passes.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix a value read from a ref or `@val` global into a tuple, record or variant, or passed to a local function, changing when the ref or global is assigned later.
3 changes: 3 additions & 0 deletions compiler/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ let is_array_function (e : J.expression) =
Ext_ident.is_js ident
| _ -> false

let var_may_change ~assignable (id : Ident.t) =
Ext_ident.is_js id || Set_ident.mem id assignable

let rec no_side_effect_expression_desc (x : J.expression_desc) =
match x with
| Undefined _ | Null | Bool _ | Var _ -> true
Expand Down
7 changes: 7 additions & 0 deletions compiler/core/js_analyzer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ val free_variables_of_expression : J.expression -> Set_ident.t

val is_array_function : J.expression -> bool

val var_may_change : assignable:Set_ident.t -> Ident.t -> bool
(** Whether reading the variable [id] after other code may give another value
than reading it now: [id] is a JS global, such as a [@val] external, which
JS code may assign, or one of [assignable], the local variables some
[Lassign] assigns (see [Lam_stats.assignable]), which the other code or a
closure it calls may assign. *)

(* val no_side_effect_expression_desc :
J.expression_desc -> bool *)

Expand Down
6 changes: 4 additions & 2 deletions compiler/core/js_pass_external_shadow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ let add_binding (ident : Ident.t) names =
else String_set.add (Ext_ident.convert ident.name) names

(* A JS [let], [const] or function declaration is in scope in its whole block,
including before the declaration. *)
let add_declarations (block : J.block) names =
including before the declaration. A [Block] statement is printed without
braces, so its declarations are in the enclosing block. *)
let rec add_declarations (block : J.block) names =
Ext_list.fold_left block names (fun acc (st : J.statement) ->
match st.statement_desc with
| Variable {ident} -> add_binding ident acc
| Block block -> add_declarations block acc
| _ -> acc)

(* The cases of a switch are printed without braces, so they share one scope. *)
Expand Down
22 changes: 18 additions & 4 deletions compiler/core/js_pass_flatten_and_mark_dead.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ let super = Js_record_map.super
let add_substitue substitution (ident : Ident.t) (e : J.expression) =
Hash_ident.replace substitution ident e

let subst_map (substitution : J.expression Hash_ident.t) =
(* Whether [e] is a variable that may hold another value when it is read
later, see [Js_analyzer.var_may_change]. *)
let is_changing_var ~assignable (e : J.expression) =
match e.expression_desc with
| Var (Id id) -> Js_analyzer.var_may_change ~assignable id
| _ -> false

let subst_map (assignable : Set_ident.t)
(substitution : J.expression Hash_ident.t) =
{
super with
statement =
Expand Down Expand Up @@ -208,8 +216,14 @@ let subst_map (substitution : J.expression Hash_ident.t) =
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
match x.expression_desc with
| Var _ | Number _ | Str _ | Template_literal _ | J.Bool _
| Undefined _ ->
| Undefined _
when not (is_changing_var ~assignable x) ->

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent new element bindings from shadowing JS globals

When a JS global has the same name as the generated element temporary, binding its read introduces a self-reference and throws a ReferenceError. For example, using @val external globalCounter: int = "t_0" in the new globalTuple fixture produces let t_0 = t_0 instead of reading the global. Js_pass_external_shadow.program runs before this pass in lam_compile_main.ml, so it cannot protect against the newly introduced binding, and Ext_pp_scope.add_ident only avoids reserved global names for suffixed names (i > 0). Run shadow protection after introducing these bindings, or ensure their names cannot capture raw JS globals.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed at the top level: with @val external tZero: int = "t_0", %%private(let t = (tZero, "b")) compiled to let t_0 = t_0;, which throws on load. Inside a function the printer renames the binding, so the globalTuple fixture itself was fine. Master already did the same for an element that is an expression (let t_0 = t_0 + 1 | 0;).

Fixed in 5b097c0 by running Js_pass_external_shadow after the passes that add bindings. Since Js_pass_flatten_and_mark_dead returns them in a Block that is printed without braces, the pass now also collects the declarations of nested blocks; otherwise a read of the global inside a function in the same scope stayed bare. The test file has a top-level case for this.

(* TODO: check the optimization *)
(* The element stays in the block, and [expression] below
replaces a later [ident[i]] with it. A JS global or a
variable in [assignable] is bound like other expressions
instead: by the time of that read, an assignment may have
changed it. *)
(i + 1, x :: e, acc)
| _ ->
(* tradeoff,
Expand Down Expand Up @@ -306,8 +320,8 @@ let subst_map (substitution : J.expression Hash_ident.t) =
there is no need to add another pass
*)

let program (js : J.program) =
let obj = subst_map (Hash_ident.create 32) in
let program ~assignable (js : J.program) =
let obj = subst_map assignable (Hash_ident.create 32) in
let js = obj.program obj js in
mark_dead_code js
(* |> mark_dead_code *)
Expand Down
6 changes: 5 additions & 1 deletion compiler/core/js_pass_flatten_and_mark_dead.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@

(** A pass to mark some declarations in JS IR as dead code *)

val program : J.program -> J.program
val program : assignable:Set_ident.t -> J.program -> J.program
(** [assignable] are the variables some [Lassign] assigns, see
[Lam_stats.assignable]. An element of a block that is one of them or a JS
global is bound before the block instead of replacing a later read of the
block's field. *)
27 changes: 18 additions & 9 deletions compiler/core/js_pass_tailcall_inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ let rec substituted_truth (consts : J.expression Map_ident.t) (e : J.expression)

(* [vars] maps a parameter to the variable argument that replaces it, [consts]
to the constant argument that replaces it. Both apply only to parameters
that are never assigned, so every occurrence of such a parameter is a read.
that are never assigned, so every occurrence of such a parameter is a read,
and [vars] only to variables for which [Js_analyzer.var_may_change] is
false (see [inline_call]), so reading one in place of the parameter gives
the value it had at the call.
A conditional whose condition becomes a constant keeps only the branch it
takes, and the negation of such a condition becomes a boolean. *)
let substitute_arguments (vars : Ident.t Map_ident.t)
Expand Down Expand Up @@ -117,11 +120,17 @@ let is_substitutable_constant (e : J.expression) =
if it is substitued, the assignment will align the value which is incorrect
*)

let inline_call (immutable_list : bool list) params (args : J.expression list)
processed_blocks =
(* [assignable] are the variables some [Lassign] assigns, see
[Lam_stats.assignable]. An argument that is one of them or a JS global
([Js_analyzer.var_may_change]) is bound like other expressions: where the
inlined body reads the parameter, the variable may already hold a value
that the body, a function it calls or a later assignment stored. *)
let inline_call (assignable : Set_ident.t) (immutable_list : bool list) params
(args : J.expression list) processed_blocks =
let substitute param (arg : J.expression) (vars, consts, acc) =
match arg.expression_desc with
| Var (Id id) -> (Map_ident.add param id vars, consts, acc)
| Var (Id id) when not (Js_analyzer.var_may_change ~assignable id) ->
(Map_ident.add param id vars, consts, acc)
| _ when is_substitutable_constant arg ->
(vars, Map_ident.add param arg consts, acc)
| _ -> (vars, consts, S.define_variable ~kind:Variable param arg :: acc)
Expand Down Expand Up @@ -170,7 +179,7 @@ let inline_call (immutable_list : bool list) params (args : J.expression list)
*)
let super = Js_record_map.super

let subst (export_set : Set_ident.t) stats =
let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats =
{
super with
statement =
Expand Down Expand Up @@ -254,7 +263,7 @@ let subst (export_set : Set_ident.t) stats =
self.block self body
(* see #278 before changes*)
in
inline_call no_tailcall params args processed_blocks
inline_call assignable no_tailcall params args processed_blocks
(* Ext_list.fold_right2
params args processed_blocks
(fun param arg acc ->
Expand Down Expand Up @@ -293,13 +302,13 @@ let subst (export_set : Set_ident.t) stats =
self.block self body
(* see #278 before changes*)
in
inline_call no_tailcall params args processed_blocks
inline_call assignable no_tailcall params args processed_blocks
| x :: xs -> self.statement self x :: self.block self xs
| [] -> []);
}

let tailcall_inline (program : J.program) =
let tailcall_inline ~assignable (program : J.program) =
let stats = Js_pass_get_used.get_stats program in
let export_set = program.export_set in
let obj = subst export_set stats in
let obj = subst export_set assignable stats in
obj.program obj program
5 changes: 4 additions & 1 deletion compiler/core/js_pass_tailcall_inline.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
]}
*)

val tailcall_inline : J.program -> J.program
val tailcall_inline : assignable:Set_ident.t -> J.program -> J.program
(** [assignable] are the variables some [Lassign] assigns, see
[Lam_stats.assignable]. An argument that is one of them or a JS global is
bound to the parameter instead of replacing it in the inlined body. *)
3 changes: 1 addition & 2 deletions compiler/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ let rec delay_safe ~assignable (e : J.expression) =
| Json_literal _ | Fun _
| Var (Qualified _) ->
true
| Var (Id id) ->
(not (Ext_ident.is_js id)) && not (Set_ident.mem id assignable)
| Var (Id id) -> not (Js_analyzer.var_may_change ~assignable id)
| Bin (Eq, _, _) -> false
| Bin (_, a, b) ->
Js_analyzer.no_side_effect_expression e
Expand Down
12 changes: 8 additions & 4 deletions compiler/core/lam_compile_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,14 @@ let compile ?on_optimized_lambda (output_prefix : string)
let export_set = Set_ident.of_list meta_exports in
let js : J.program = {exports = meta_exports; export_set; block = body} in
js |> j "initial" |> Js_pass_flatten.program |> j "flatten"
|> Js_pass_external_shadow.program |> j "external_shadow"
|> Js_pass_tailcall_inline.tailcall_inline |> j "inline_and_shake"
|> Js_pass_record_rest.program |> j "record_rest"
|> Js_pass_flatten_and_mark_dead.program |> j "flatten_and_mark_dead"
|> Js_pass_tailcall_inline.tailcall_inline ~assignable:meta.assignable
|> j "inline_and_shake" |> Js_pass_record_rest.program |> j "record_rest"
|> Js_pass_flatten_and_mark_dead.program ~assignable:meta.assignable
|> j "flatten_and_mark_dead"
(* After the passes that add bindings, so that a JS global read in one of
them, as in [let t_0 = t_0], is not captured by the binding *)
|> Js_pass_external_shadow.program
|> j "external_shadow"
|> (fun js ->
ignore @@ Js_pass_scope.program js;
js)
Expand Down
3 changes: 2 additions & 1 deletion compiler/core/lam_stats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ type t = {
awaited_units: Set_ident.t;
assignable: Set_ident.t;
(** The variables some [Lassign] assigns. [Lam_compile] never delays
reading one of them past other code. *)
reading one of them past other code, and neither do
[Js_pass_tailcall_inline] and [Js_pass_flatten_and_mark_dead]. *)
}

let pp = Format.fprintf
Expand Down
3 changes: 2 additions & 1 deletion compiler/core/lam_stats.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type t = {
them use the flat exports too. *)
assignable: Set_ident.t;
(** The variables some [Lassign] assigns. [Lam_compile] never delays
reading one of them past other code. *)
reading one of them past other code, and neither do
[Js_pass_tailcall_inline] and [Js_pass_flatten_and_mark_dead]. *)
}

val print : Format.formatter -> t -> unit
Expand Down
Loading
Loading