From 97881820b7473409389501e6b8ec5eabe1a272c7 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Dec 2026 10:09:17 +0000 Subject: [PATCH 1/2] Remove the alias pass's tracking of assigned variables Why: #9424 taught the alias pass and two helpers about `Lassign`s in bodies inlined from other modules: the pass collected the variables such a body assigns, `Lam_bounded_vars.rewrite` renamed the assigned variable, and `Lam_pass_collect` recorded nothing for a mutable `let`. Since a body that updates a local ref is no longer stored (warning 113), no inlined body assigns a variable, and only `Lam_pass_sroa`, which runs after every inlining pass, makes `Lassign`s. `Lam_check.check` checks this after each pass before sroa under `-check-lam`, which the compiler tests use. So the set was always empty, no inlined body had an assignment to rename, and no mutable `let` existed when the facts were collected. The code goes, and `Lam_bounded_vars.rewrite` now fails on an assignment instead of renaming it. The generated JavaScript doesn't change. Review: Why not keep renaming the assignment, in case? A body with an assignment that reaches inlining is already a bug: the passes before sroa would move reads of the variable past it. Failing there names the variable. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- compiler/backend/lam_bounded_vars.ml | 20 +++----- compiler/backend/lam_id_kind.ml | 6 +-- compiler/backend/lam_id_kind.mli | 6 +-- compiler/backend/lam_pass_collect.ml | 9 ++-- compiler/backend/lam_pass_lets_dce.ml | 3 +- compiler/backend/lam_pass_remove_alias.ml | 50 ++++++------------- .../ounit_lam_bounded_vars_tests.ml | 30 ----------- 7 files changed, 28 insertions(+), 96 deletions(-) diff --git a/compiler/backend/lam_bounded_vars.ml b/compiler/backend/lam_bounded_vars.ml index f2294396e6..e4d1564c7f 100644 --- a/compiler/backend/lam_bounded_vars.ml +++ b/compiler/backend/lam_bounded_vars.ml @@ -190,20 +190,12 @@ let rewrite (map : _ Hash_ident.t) (lam : Lambda.t) : Lambda.t = let l1 = aux l1 in let l2 = aux l2 in Lambda.while_ l1 l2 - | Lassign (v, l) -> - let v = - match Hash_ident.find_opt map v with - | Some (Lvar v) -> v - | Some _ -> - (* Only let-bound variables are assigned, never the parameters [map] - may replace with another term. *) - Ext_fmt.failwithf ~loc:__LOC__ - "%s/%d is assigned, but inlining replaces it with a term that is \ - not a variable" - (Ident.name v) v.stamp - | None -> v - in - Lambda.assign v (aux l) + | Lassign (v, _) -> + (* Only [Lam_pass_sroa] makes assignments, after every pass that + inlines, and no body another module stores has one (checked + by [Lam_check.check]) *) + Ext_fmt.failwithf ~loc:__LOC__ ": %s/%d is assigned in a body to inline" + (Ident.name v) v.stamp in aux lam diff --git a/compiler/backend/lam_id_kind.ml b/compiler/backend/lam_id_kind.ml index 94d1ed08a4..ffacbc2862 100644 --- a/compiler/backend/lam_id_kind.ml +++ b/compiler/backend/lam_id_kind.ml @@ -50,11 +50,7 @@ type t = } | Parameter (** For this case, it can help us determine whether it should be inlined or not *) - | NA - (** Not such information is associated with an identifier, it is immutable, - if you only associate a property to an identifier - we should consider [Lassign] - *) + | NA (** Nothing is known about the identifier *) let pp = Format.fprintf diff --git a/compiler/backend/lam_id_kind.mli b/compiler/backend/lam_id_kind.mli index b08b5fae1d..77c3fc2917 100644 --- a/compiler/backend/lam_id_kind.mli +++ b/compiler/backend/lam_id_kind.mli @@ -59,10 +59,6 @@ type t = } | Parameter (** For this case, it can help us determine whether it should be inlined or not *) - | NA - (** Not such information is associated with an identifier, it is immutable, - if you only associate a property to an identifier - we should consider [Lassign] - *) + | NA (** Nothing is known about the identifier *) val print : Format.formatter -> t -> unit diff --git a/compiler/backend/lam_pass_collect.ml b/compiler/backend/lam_pass_collect.ml index d667ef7fcd..579f8f9575 100644 --- a/compiler/backend/lam_pass_collect.ml +++ b/compiler/backend/lam_pass_collect.ml @@ -90,12 +90,9 @@ let collect_info (meta : Lam_stats.t) (lam : Lambda.t) = (* functor ? *) List.iter (fun p -> Hash_ident.replace meta.ident_tbl p Parameter) params; collect l - | Llet (Variable, _, arg, body) -> - (* An [Lassign] may change the variable, so what it is bound to says - nothing about its value later. *) - collect arg; - collect body - | Llet ((Strict | Alias | StrictOpt), ident, arg, body) -> + | Llet ((Strict | Alias | StrictOpt | Variable), ident, arg, body) -> + (* No [Lassign] changes a variable yet: only [Lam_pass_sroa], which + runs later, makes them (checked by [Lam_check.check]) *) collect_bind Lam_non_rec ident arg; collect body | Lletrec (bindings, body) -> diff --git a/compiler/backend/lam_pass_lets_dce.ml b/compiler/backend/lam_pass_lets_dce.ml index 6621d1fddd..ac75838725 100644 --- a/compiler/backend/lam_pass_lets_dce.ml +++ b/compiler/backend/lam_pass_lets_dce.ml @@ -156,7 +156,8 @@ let apply_lets ~assignable occ lambda = let simplify_lets (lam : Lambda.t) : Lambda.t = (* The count and the rewrite reduce immediately applied functions alike. - See [Lam_pass_remove_alias.simplify_alias] for [assignable]. *) + [assignable] is empty: this pass runs before [Lam_pass_sroa], the only + one that makes [Lassign]s (see [Lam_check.no_assign]). *) let assignable = Lambda_traverse.assigned_variables lam in let occ = Lam_pass_count.collect_occurs ~assignable lam in apply_lets ~assignable occ lam diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index 95c0f24bb8..84afc0bc11 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -54,21 +54,6 @@ let is_const_some (cst : Lambda.structured_constant) : bool = let max_inline_depth = 4 let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = - (* Inlining keeps the reads of assigned variables in order (see - [Lam_beta_reduce]), so it needs the variables some [Lassign] assigns. - Only [Lam_pass_sroa], which runs later, makes [Lassign]s, so [lam] has - none yet; computing them here keeps the inlining sound if that changes. *) - let assignable = ref (Lambda_traverse.assigned_variables lam) in - let current_meta () = {meta with assignable = !assignable} in - (* A body inlined from another module was optimized by [Lam_pass_sroa] - there, so it may assign variables, and inlining renamed them. They are - added before the inlined body is simplified further, or inlining a call in - it could move a read of one past an assignment. *) - let record_assigned (lam : Lambda.t) = - assignable := - Set_ident.union !assignable (Lambda_traverse.assigned_variables lam); - lam - in let inline_depth = ref 0 in let rec simpl (lam : Lambda.t) : Lambda.t = match lam with @@ -173,10 +158,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = Lam_closure.is_closed_with_map Set_ident.empty params body in simpl - (record_assigned - (Lam_beta_reduce.propagate_beta_reduce_with_map - ~result_not_nested:ap_result_not_nested (current_meta ()) - param_map params body args)) + (Lam_beta_reduce.propagate_beta_reduce_with_map + ~result_not_nested:ap_result_not_nested meta param_map params body + args) | _ -> let fn = simpl l1 in let args' = Ext_list.map_sharing args simpl in @@ -221,10 +205,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = (* Check: recursive applying may result in non-termination *) (* Ir_log.dwarn __LOC__ "beta .. %s/%d" v.name v.stamp ; *) simpl - (record_assigned - (Lam_beta_reduce.propagate_beta_reduce - ~result_not_nested:ap_result_not_nested (current_meta ()) - params body ap_args)) + (Lam_beta_reduce.propagate_beta_reduce + ~result_not_nested:ap_result_not_nested meta params body + ap_args) else if Lam_analysis.ok_to_inline_fun_when_app m ap_args then let param_map = Lam_closure.is_closed_with_map meta.export_idents params body @@ -234,10 +217,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = | false, (_, param_map) | true, (true, param_map) -> ( match rec_flag with | Lam_rec -> - record_assigned - (Lam_beta_reduce.propagate_beta_reduce_with_map - ~result_not_nested:ap_result_not_nested (current_meta ()) - param_map params body ap_args) + Lam_beta_reduce.propagate_beta_reduce_with_map + ~result_not_nested:ap_result_not_nested meta param_map params + body ap_args | Lam_self_rec -> normal () | Lam_non_rec -> if @@ -252,10 +234,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = incr inline_depth; let inlined = simpl - (record_assigned - (Lam_beta_reduce.propagate_beta_reduce_with_map - ~result_not_nested:ap_result_not_nested - (current_meta ()) param_map params body ap_args)) + (Lam_beta_reduce.propagate_beta_reduce_with_map + ~result_not_nested:ap_result_not_nested meta param_map + params body ap_args) in decr inline_depth; inlined)) @@ -275,10 +256,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = Lam_closure.is_closed_with_map Set_ident.empty params body in simpl - (record_assigned - (Lam_beta_reduce.propagate_beta_reduce_with_map - ~result_not_nested:ap_result_not_nested (current_meta ()) - param_map params body args)) + (Lam_beta_reduce.propagate_beta_reduce_with_map + ~result_not_nested:ap_result_not_nested meta param_map params body + args) | Lstringswitch (l, sw, d) -> ( let known_constant = match l with diff --git a/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml b/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml index 974698f7c3..2d2aae547c 100644 --- a/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml +++ b/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml @@ -33,35 +33,6 @@ let test_for_start_is_renamed_once _ = (Hash_ident.length map) | _ -> OUnit.assert_failure "a for loop with a let in its start" -let r = Ident.create "r" - -let outer = Ident.create "outer" - -(* [let r = 1; r = r; outer = r]: an assignment to a bound variable and one to - a variable bound outside *) -let assignments = - Lambda.let_ Variable r (int 1) - (Lambda.seq - (Lambda.assign r (Lambda.var r)) - (Lambda.assign outer (Lambda.var r))) - -(* An assignment follows its variable's fresh name, as a read does. *) -let test_assign_is_renamed _ = - match Lam_bounded_vars.rewrite (Hash_ident.create 4) assignments with - | Llet - ( Variable, - r', - _, - Lsequence - (Lassign (assigned, Lvar r_use), Lassign (outer', Lvar r_use_2)) ) -> - OUnit.assert_bool "the variable is renamed" (not (Ident.same r' r)); - OUnit.assert_bool "its assignment follows" (Ident.same assigned r'); - OUnit.assert_bool "its first use follows" (Ident.same r_use r'); - OUnit.assert_bool "its second use follows" (Ident.same r_use_2 r'); - OUnit.assert_bool "a variable bound outside keeps its name" - (Ident.same outer' outer) - | _ -> OUnit.assert_failure "a let with two assignments" - let rewrite lam = Lam_bounded_vars.rewrite (Hash_ident.create 4) lam let assert_exit msg expected actual = @@ -160,7 +131,6 @@ let suites = __FILE__ >::: [ "for_start_is_renamed_once" >:: test_for_start_is_renamed_once; - "assign_is_renamed" >:: test_assign_is_renamed; "exit_is_renamed_per_copy" >:: test_exit_is_renamed_per_copy; "outer_exit_is_kept" >:: test_outer_exit_is_kept; "exit_scopes_over_the_body_only" From fb365099f68acebeb19d4f2b0348a7e11fd4e76a Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Dec 2026 10:12:20 +0000 Subject: [PATCH 2/2] Stop passing assigned variables through the Lambda analyses Why: `delay_safe`, `motion`, `refine_let`, the beta reductions, the let pass, the occurrence count and `deep_flatten` took the set of variables some `Lassign` assigns, and each caller computed it from the term. All of them run before `Lam_pass_sroa`, the only pass that makes `Lassign`s, and `Lam_check.check` checks under `-check-lam` that no pass before sroa makes one. So the set was always empty. Sroa itself kept a set of the fields it had turned into assigned variables, for the `refine_let` of a read-only field. That field's initializer runs before its block exists, so it can't read or change a field this pass assigns: those are bound in the block's body or inside the initializer. Blocks are replaced from the inside out, so a field of an enclosing block is still a record field there, which `delay_safe` already takes to change. The set made no difference there either. The argument goes, and a variable counts as unchanging in these analyses. `Lam_stats.assignable`, which the JavaScript generation and the JavaScript passes use after sroa, stays. The generated JavaScript doesn't change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- compiler/backend/lam_analysis.ml | 19 ++++++------- compiler/backend/lam_analysis.mli | 8 +++--- compiler/backend/lam_beta_reduce.ml | 34 ++++++++--------------- compiler/backend/lam_beta_reduce.mli | 10 ++----- compiler/backend/lam_beta_reduce_util.ml | 11 ++------ compiler/backend/lam_beta_reduce_util.mli | 9 ++---- compiler/backend/lam_pass_count.ml | 5 ++-- compiler/backend/lam_pass_count.mli | 2 +- compiler/backend/lam_pass_deep_flatten.ml | 13 +++------ compiler/backend/lam_pass_lets_dce.ml | 32 ++++++++------------- compiler/backend/lam_pass_sroa.ml | 30 +++++++------------- compiler/backend/lam_util.ml | 20 +++++-------- compiler/backend/lam_util.mli | 4 +-- 13 files changed, 68 insertions(+), 129 deletions(-) diff --git a/compiler/backend/lam_analysis.ml b/compiler/backend/lam_analysis.ml index 5bc8512340..d64cbf156f 100644 --- a/compiler/backend/lam_analysis.ml +++ b/compiler/backend/lam_analysis.ml @@ -27,16 +27,15 @@ let no_side_effects = Lambda_effects.no_side_effects (* The Lambda counterpart of [Lam_compile.delay_safe]: [lam] has no side effects and reads nothing that other code can change, so evaluating it later gives the same value. Fields of records, which may be mutable, - arrays, dictionaries and the variables in [assignable] can change. *) -let rec delay_safe ~assignable (lam : Lambda.t) : bool = + arrays and dictionaries can change. A variable can't: only + [Lam_pass_sroa] makes [Lassign]s (checked by [Lam_check.check]), and the code + it moves can't reach the variables it assigns (see [Lam_pass_sroa.replace]). *) +let rec delay_safe (lam : Lambda.t) : bool = match lam with - | Lconst _ | Lfunction _ | Lglobal_module _ -> true - | Lvar id -> not (Set_ident.mem id assignable) - | Lifthenelse (a, b, c) -> - delay_safe ~assignable a && delay_safe ~assignable b - && delay_safe ~assignable c + | Lconst _ | Lfunction _ | Lglobal_module _ | Lvar _ -> true + | Lifthenelse (a, b, c) -> delay_safe a && delay_safe b && delay_safe c | Lprim {primitive; args} -> ( - List.for_all (delay_safe ~assignable) args + List.for_all delay_safe args && match primitive with | Pdivint | Pmodint | Pdivbigint | Pmodbigint | Ppowbigint -> @@ -85,8 +84,8 @@ let reads_callee (primitive : Lambda.primitive) = type motion = Anywhere | Reads | Effects -let motion ~assignable lam : motion = - if delay_safe ~assignable lam then Anywhere +let motion lam : motion = + if delay_safe lam then Anywhere else if no_side_effects lam then Reads else Effects diff --git a/compiler/backend/lam_analysis.mli b/compiler/backend/lam_analysis.mli index da3b2c1224..a90e4433f3 100644 --- a/compiler/backend/lam_analysis.mli +++ b/compiler/backend/lam_analysis.mli @@ -27,10 +27,10 @@ val no_side_effects : Lambda.t -> bool (** No side effect, but it might depend on data store *) -val delay_safe : assignable:Set_ident.t -> Lambda.t -> bool +val delay_safe : Lambda.t -> bool (** No side effect, and does not depend on data store: evaluating it later, - after other code, gives the same value. [assignable] are the variables - some [Lassign] assigns. *) + after other code, gives the same value. No variable is assigned yet + where it is used (checked by [Lam_check.check]). *) val reads_callee : Lambda.primitive -> bool (** The call of an external function or class, which reads it from its global @@ -42,7 +42,7 @@ type motion = | Reads (** no side effects, but reads state that may change *) | Effects -val motion : assignable:Set_ident.t -> Lambda.t -> motion +val motion : Lambda.t -> motion val conflict : motion -> motion -> bool (** Whether code of the two motions can give other results when it runs in diff --git a/compiler/backend/lam_beta_reduce.ml b/compiler/backend/lam_beta_reduce.ml index 34238a4fd8..3de1485e60 100644 --- a/compiler/backend/lam_beta_reduce.ml +++ b/compiler/backend/lam_beta_reduce.ml @@ -97,8 +97,8 @@ let read_before_effects param (body : Lambda.t) = nothing when it reads no state that may change. One that only reads may still move when no argument to its right has side effects ([effects_after]) and [body] reads it before any. *) -let can_move ~assignable ~effects_after param body (arg : Lambda.t) = - Lam_analysis.delay_safe ~assignable arg +let can_move ~effects_after param body (arg : Lambda.t) = + Lam_analysis.delay_safe arg || Lam_analysis.no_side_effects arg && (not effects_after) && read_before_effects param body @@ -233,10 +233,7 @@ let inlined_body ~result_not_nested params body args = let propagate_beta_reduce ~result_not_nested (meta : Lam_stats.t) (params : Ident.t list) (body : Lambda.t) (args : Lambda.t list) = let body, args = inlined_body ~result_not_nested params body args in - let assignable = meta.assignable in - match - Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args - with + match Lam_beta_reduce_util.simple_beta_reduce params body args with | Some x -> x | None -> let rest_bindings, rev_new_params = @@ -245,8 +242,7 @@ let propagate_beta_reduce ~result_not_nested (meta : Lam_stats.t) match arg with | Lconst c when not (Lambda.const_has_identity c) -> (rest_bindings, arg :: acc) - | Lvar _ when can_move ~assignable ~effects_after old_param body arg - -> + | Lvar _ when can_move ~effects_after old_param body arg -> (rest_bindings, arg :: acc) | _ -> let p = Ident.rename old_param in @@ -272,16 +268,13 @@ let propagate_beta_reduce ~result_not_nested (meta : Lam_stats.t) | Lprim {primitive = Psome | Psome_not_nest; args = [v]; _} -> Hash_ident.replace meta.ident_tbl param (Normal_optional v) | _ -> ()); - Lam_util.refine_let ~assignable ~kind:Strict param arg l) + Lam_util.refine_let ~kind:Strict param arg l) new_body rest_bindings let propagate_beta_reduce_with_map ~result_not_nested (meta : Lam_stats.t) (map : Lam_var_stats.stats Map_ident.t) params body args = let body, args = inlined_body ~result_not_nested params body args in - let assignable = meta.assignable in - match - Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args - with + match Lam_beta_reduce_util.simple_beta_reduce params body args with | Some x -> x | None -> let rest_bindings, rev_new_params = @@ -295,7 +288,7 @@ let propagate_beta_reduce_with_map ~result_not_nested (meta : Lam_stats.t) | Lconst c when not (Lambda.const_has_identity c) -> (rest_bindings, arg :: acc) | Lvar _ -> - if can_move ~assignable ~effects_after old_param body arg then + if can_move ~effects_after old_param body arg then (rest_bindings, arg :: acc) else bind () | Lglobal_module _ -> bind () @@ -304,7 +297,7 @@ let propagate_beta_reduce_with_map ~result_not_nested (meta : Lam_stats.t) Lam_analysis.no_side_effects arg && Lam_var_stats.top_and_used_zero_or_one (Map_ident.find old_param map) - && can_move ~assignable ~effects_after old_param body arg + && can_move ~effects_after old_param body arg then (rest_bindings, arg :: acc) else bind ()) ([], []) params @@ -326,19 +319,16 @@ let propagate_beta_reduce_with_map ~result_not_nested (meta : Lam_stats.t) | Lprim {primitive = Psome | Psome_not_nest; args = [v]} -> Hash_ident.replace meta.ident_tbl param (Normal_optional v) | _ -> ()); - Lam_util.refine_let ~assignable ~kind:Strict param arg l) + Lam_util.refine_let ~kind:Strict param arg l) new_body rest_bindings -let no_names_beta_reduce ~assignable params body args = +let no_names_beta_reduce params body args = let body, args = unmark_arguments params body args in - match - Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args - with + match Lam_beta_reduce_util.simple_beta_reduce params body args with | Some x -> x | None -> (* Fold right, so that the first parameter's binding is outermost and the arguments evaluate in call order *) List.fold_right2 - (fun param arg l -> - Lam_util.refine_let ~assignable ~kind:Strict param arg l) + (fun param arg l -> Lam_util.refine_let ~kind:Strict param arg l) params args body diff --git a/compiler/backend/lam_beta_reduce.mli b/compiler/backend/lam_beta_reduce.mli index 324237cd76..ad72c43b78 100644 --- a/compiler/backend/lam_beta_reduce.mli +++ b/compiler/backend/lam_beta_reduce.mli @@ -24,19 +24,13 @@ (** Beta reduction of lambda IR *) -val no_names_beta_reduce : - assignable:Set_ident.t -> - Ident.t list -> - Lambda.t -> - Lambda.t list -> - Lambda.t +val no_names_beta_reduce : Ident.t list -> Lambda.t -> Lambda.t list -> Lambda.t (* Compile-time beta-reduction of functions immediately applied: Lapply(Lfunction(Curried, params, body), args, loc) -> let param1 = arg1 in ... let paramN = argN in body Lapply(Lfunction(Tupled, params, body), [Lprim(Pmakeblock(args))], loc) -> let param1 = arg1 in ... let paramN = argN in body - Assumes |args| = |params|. [assignable] are the variables some [Lassign] - assigns. + Assumes |args| = |params|. This function is used while counting used vars, no new names are generated to make this more complex diff --git a/compiler/backend/lam_beta_reduce_util.ml b/compiler/backend/lam_beta_reduce_util.ml index de64519a68..e657caf88f 100644 --- a/compiler/backend/lam_beta_reduce_util.ml +++ b/compiler/backend/lam_beta_reduce_util.ml @@ -67,7 +67,7 @@ let seq_unused param_hash params body = | _ -> false ) params args' ]} *) -let simple_beta_reduce ~assignable params body args = +let simple_beta_reduce params body args = (* Substitutions and their [used] bits belong to this reduction: compiler requests run concurrently on several domains. *) let param_hash : value Hash_ident.t = Hash_ident.create 20 in @@ -89,7 +89,7 @@ let simple_beta_reduce ~assignable params body args = evaluate ~conditional exp.rank exp.motion; exp.lambda | None -> - evaluate ~conditional body_rank (Lam_analysis.motion ~assignable opt); + evaluate ~conditional body_rank (Lam_analysis.motion opt); opt in (* Substitutes in the operands from left to right. The operands from @@ -139,12 +139,7 @@ let simple_beta_reduce ~assignable params body args = List.iteri (fun rank (p, a) -> Hash_ident.add param_hash p - { - lambda = a; - used = false; - rank; - motion = Lam_analysis.motion ~assignable a; - }) + {lambda = a; used = false; rank; motion = Lam_analysis.motion a}) (List.combine params args) in match (body : Lambda.t) with diff --git a/compiler/backend/lam_beta_reduce_util.mli b/compiler/backend/lam_beta_reduce_util.mli index c4cc5d287e..691db8aa68 100644 --- a/compiler/backend/lam_beta_reduce_util.mli +++ b/compiler/backend/lam_beta_reduce_util.mli @@ -23,12 +23,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) val simple_beta_reduce : - assignable:Set_ident.t -> - Ident.t list -> - Lambda.t -> - Lambda.t list -> - Lambda.t option + Ident.t list -> Lambda.t -> Lambda.t list -> Lambda.t option (** The reduction of a body that is a single primitive or call on parameters and constants, substituting the arguments for the parameters, if it - evaluates the arguments to the same effect as the call does. [assignable] - are the variables some [Lassign] assigns. *) + evaluates the arguments to the same effect as the call does. *) diff --git a/compiler/backend/lam_pass_count.ml b/compiler/backend/lam_pass_count.ml index 0df8167add..db68582c65 100644 --- a/compiler/backend/lam_pass_count.ml +++ b/compiler/backend/lam_pass_count.ml @@ -41,7 +41,7 @@ let absorb_info (x : used_info) (y : used_info) = locally let-bound variables and is reset when crossing functions and loops, so uses of outer bindings are marked as captured. The optimizer uses the captured flag to restrict inlining without inflating the occurrence count. *) -let collect_occurs ~assignable lam : occ_tbl = +let collect_occurs lam : occ_tbl = let occ : occ_tbl = Hash_ident.create 16 in (* Current use count of a variable. *) @@ -138,8 +138,7 @@ let collect_occurs ~assignable lam : occ_tbl = {ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _} when List.compare_lengths params args = 0 && Lambda.lfunction_can_be_inlined lfunction -> - count bv - (Lam_beta_reduce.no_names_beta_reduce ~assignable params body args) + count bv (Lam_beta_reduce.no_names_beta_reduce params body args) | Lapply {ap_func = l1; ap_args = ll; _} -> count bv l1; List.iter (count bv) ll diff --git a/compiler/backend/lam_pass_count.mli b/compiler/backend/lam_pass_count.mli index 83fef38a57..9175e809e8 100644 --- a/compiler/backend/lam_pass_count.mli +++ b/compiler/backend/lam_pass_count.mli @@ -25,4 +25,4 @@ type occ_tbl = used_info Hash_ident.t val dummy_info : unit -> used_info -val collect_occurs : assignable:Set_ident.t -> Lambda.t -> occ_tbl +val collect_occurs : Lambda.t -> occ_tbl diff --git a/compiler/backend/lam_pass_deep_flatten.ml b/compiler/backend/lam_pass_deep_flatten.ml index 2a4b9026b3..f59cc76e5b 100644 --- a/compiler/backend/lam_pass_deep_flatten.ml +++ b/compiler/backend/lam_pass_deep_flatten.ml @@ -100,14 +100,13 @@ let rec eliminate_tuple (id : Ident.t) (lam : Lambda.t) acc = - also for function compilation, flattening should be done first - [compile_group] and [compile] become mutually recursive function *) -let lambda_of_groups ~assignable ~(rev_bindings : Lam_group.t list) - (result : Lambda.t) : Lambda.t = +let lambda_of_groups ~(rev_bindings : Lam_group.t list) (result : Lambda.t) : + Lambda.t = List.fold_left (fun acc (x : Lam_group.t) -> match x with | Nop l -> Lambda.seq l acc - | Single (kind, ident, lam) -> - Lam_util.refine_let ~assignable ~kind ident lam acc + | Single (kind, ident, lam) -> Lam_util.refine_let ~kind ident lam acc | Recursive bindings -> Lambda.letrec bindings acc) result rev_bindings @@ -158,9 +157,6 @@ let regroups_binding (str : Lambda.let_kind) (id : Ident.t) (arg : Lambda.t) = | _ -> false let deep_flatten (lam : Lambda.t) : Lambda.t = - (* See [Lam_pass_remove_alias.simplify_alias] *) - let assignable = Lambda_traverse.assigned_variables lam in - let lambda_of_groups = lambda_of_groups ~assignable in let rec flatten (acc : Lam_group.t list) (lam : Lambda.t) : Lambda.t * Lam_group.t list = match lam with @@ -231,8 +227,7 @@ let deep_flatten (lam : Lambda.t) : Lambda.t = and aux (lam : Lambda.t) : Lambda.t = match lam with | Llet (str, id, arg, body) when not (regroups_binding str id arg) -> - Lam_util.refine_let ~original:lam ~assignable ~kind:str id (aux arg) - (aux body) + Lam_util.refine_let ~original:lam ~kind:str id (aux arg) (aux body) | Llet _ -> let res, groups = flatten [] lam in lambda_of_groups res ~rev_bindings:groups diff --git a/compiler/backend/lam_pass_lets_dce.ml b/compiler/backend/lam_pass_lets_dce.ml index ac75838725..148f12e8aa 100644 --- a/compiler/backend/lam_pass_lets_dce.ml +++ b/compiler/backend/lam_pass_lets_dce.ml @@ -11,8 +11,8 @@ (***********************************************************************) (* Adapted for Javascript backend : Hongbo Zhang, *) -let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info) - lam : Lambda.t = +let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lambda.t + = let subst : Lambda.t Hash_ident.t = Hash_ident.create 16 in let string_table : string Hash_ident.t = Hash_ident.create 16 in let used v = (count_var v).times > 0 in @@ -22,10 +22,7 @@ let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info) match Hash_ident.find_opt subst v with | Some lam -> lam | None -> lam) - | Llet ((Strict | Alias | StrictOpt), v, Lvar w, l2) - when not (Set_ident.mem w assignable) -> - (* A variable some [Lassign] assigns may change between the binding and - a read of [v], so [v] keeps its own binding. *) + | Llet ((Strict | Alias | StrictOpt), v, Lvar w, l2) -> Hash_ident.add subst v (simplif (Lambda.var w)); simplif l2 | Llet (Alias, v, l1, l2) -> ( @@ -96,9 +93,7 @@ let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info) Hash_ident.add string_table v s; (* we need move [simplif lbody] later, since adding Hash does have side effect *) Lambda.let_ Alias v l1 (simplif lbody) - | _ -> - Lam_util.refine_let ~original:lam ~assignable ~kind v l1 - (simplif lbody) + | _ -> Lam_util.refine_let ~original:lam ~kind v l1 (simplif lbody) (* TODO: check if it is correct rollback to [StrictOpt]? *)) | Llet (((Strict | Variable) as kind), v, l1, l2) -> ( if not (used v) then @@ -112,14 +107,12 @@ let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info) | Strict, Lconst (Const_string s) -> Hash_ident.add string_table v s; Lambda.let_ Alias v l1 (simplif l2) - | _ -> - Lam_util.refine_let ~original:lam ~assignable ~kind v l1 (simplif l2)) + | _ -> Lam_util.refine_let ~original:lam ~kind v l1 (simplif l2)) | Lapply {ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _} when List.compare_lengths params args = 0 && Lambda.lfunction_can_be_inlined lfunction -> - simplif - (Lam_beta_reduce.no_names_beta_reduce ~assignable params body args) + simplif (Lam_beta_reduce.no_names_beta_reduce params body args) | Lprim {primitive = Pstringadd; args = [l; r]; loc} -> ( let l' = simplif l in let r' = simplif r in @@ -146,18 +139,15 @@ let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info) simplif lam (* To transform let-bound references into variables *) -let apply_lets ~assignable occ lambda = +let apply_lets occ lambda = let count_var v = match Hash_ident.find_opt occ v with | None -> Lam_pass_count.dummy_info () | Some v -> v in - lets_helper ~assignable count_var lambda + lets_helper count_var lambda let simplify_lets (lam : Lambda.t) : Lambda.t = - (* The count and the rewrite reduce immediately applied functions alike. - [assignable] is empty: this pass runs before [Lam_pass_sroa], the only - one that makes [Lassign]s (see [Lam_check.no_assign]). *) - let assignable = Lambda_traverse.assigned_variables lam in - let occ = Lam_pass_count.collect_occurs ~assignable lam in - apply_lets ~assignable occ lam + (* The count and the rewrite reduce immediately applied functions alike *) + let occ = Lam_pass_count.collect_occurs lam in + apply_lets occ lam diff --git a/compiler/backend/lam_pass_sroa.ml b/compiler/backend/lam_pass_sroa.ml index b5c97333b0..c427d7002f 100644 --- a/compiler/backend/lam_pass_sroa.ml +++ b/compiler/backend/lam_pass_sroa.ml @@ -97,9 +97,7 @@ let fields_for_block block info field_count = | Some names -> Array.map (fun name -> Ident.create (Ident.name block ^ "_" ^ name)) names -(* [assignable] holds the variables assigned so far. A replaced block adds the - fields it assigns. *) -let replace_block ~assignable ~block ~info ~initializers body = +let replace ~block ~info ~initializers body = match initializers with | [] -> None | _ -> @@ -111,11 +109,6 @@ let replace_block ~assignable ~block ~info ~initializers body = else let fields = fields_for_block block info field_count in let body = rewrite block fields uses body in - Array.iteri - (fun index use -> - if use.read && use.written then - assignable := Set_ident.add fields.(index) !assignable) - uses; let rec bind_fields index initializers body = match initializers with | [] -> body @@ -128,22 +121,19 @@ let replace_block ~assignable ~block ~info ~initializers body = still needs a mutable scalar binding. *) if not use.read then discard_value init body else if not use.written then - Lam_util.refine_let ~assignable:!assignable ~kind:Strict - fields.(index) init body + (* [init] runs before the block exists, so it neither reads nor + changes a variable this pass assigns: those are bound in the + body, or inside [init] itself. No other variable is assigned + (checked by [Lam_check.check]), and a field of an enclosing block + is still a record field here, as enclosing blocks are + replaced later. *) + Lam_util.refine_let ~kind:Strict fields.(index) init body else Lambda.let_ Variable fields.(index) init body in Some (bind_fields 0 initializers body) -let replace ~block ~info ~initializers body = - let assignable = ref (Lambda_traverse.assigned_variables body) in - replace_block ~assignable ~block ~info ~initializers body - let simplify (lam : Lambda.t) = - (* Blocks are replaced from the inside out, so when a block is replaced the - set has every assignment in its initializers and its body. A field of an - enclosing block is not in it yet, and is still a field of a record, which - [refine_let] takes to change as well. *) - let assignable = ref (Lambda_traverse.assigned_variables lam) in + (* Blocks are replaced from the inside out *) let rec simplify (lam : Lambda.t) = match lam with | Llet (kind, block, init, body) -> ( @@ -153,7 +143,7 @@ let simplify (lam : Lambda.t) = | ( (Strict | StrictOpt), Lambda.Lprim {primitive = Pmakeblock info; args = initializers} ) when not (Lambda.is_immutable_block info) -> ( - match replace_block ~assignable ~block ~info ~initializers body' with + match replace ~block ~info ~initializers body' with | Some replacement -> replacement | None -> if init' == init && body' == body then lam diff --git a/compiler/backend/lam_util.ml b/compiler/backend/lam_util.ml index e29663f4d4..6036d03590 100644 --- a/compiler/backend/lam_util.ml +++ b/compiler/backend/lam_util.ml @@ -51,10 +51,9 @@ evaluation strategy downstream, so we keep its predicate intentionally syntactic and narrow. *) (* [original] is the binding this one was taken apart from, if any. When - nothing is refined it is handed back untouched rather than rebuilt. - [assignable] are the variables some [Lassign] assigns. *) -let refine_let ?original ~assignable ~kind param (arg : Lambda.t) (l : Lambda.t) - : Lambda.t = + nothing is refined it is handed back untouched rather than rebuilt. *) +let refine_let ?original ~kind param (arg : Lambda.t) (l : Lambda.t) : Lambda.t + = (* A JSX element whose only argument is its tag keeps the tag bound: a function bound by [let] gets the binding's name, which React shows *) let keeps_binding = function @@ -69,7 +68,7 @@ let refine_let ?original ~assignable ~kind param (arg : Lambda.t) (l : Lambda.t) match callee with | Anywhere -> true | Reads -> Lam_analysis.no_side_effects arg - | Effects -> Lam_analysis.delay_safe ~assignable arg + | Effects -> Lam_analysis.delay_safe arg in (* SafeAlias is the predicate that justifies the (Alias) rewrite let[k] x = e in body --> let[Alias] x = e in body @@ -79,13 +78,8 @@ let refine_let ?original ~assignable ~kind param (arg : Lambda.t) (l : Lambda.t) the single eager evaluation promised by [Strict]/[StrictOpt]. *) let rec is_safe_to_alias (lam : Lambda.t) = match lam with - | Lvar v -> - (* var --> emitting multiple reads is identical to the original eager - evaluation, so codegen may inline them freely, unless an [Lassign] - may change the variable between the binding and a read. *) - not (Set_ident.mem v assignable) - | Lconst _ -> - (* const --> emitting multiple `const` reads is identical to the + | Lvar _ | Lconst _ -> + (* var/const --> emitting multiple `const` reads is identical to the original eager evaluation, so codegen may inline them freely. *) true | Lprim {primitive = Ptemplate [_]; args = []; _} -> @@ -132,7 +126,7 @@ let refine_let ?original ~assignable ~kind param (arg : Lambda.t) (l : Lambda.t) Lapply {ap_func = fn; ap_args = [Lvar w]; ap_loc; ap_result_not_nested} ) when Ident.same w param && (not (Lam_hit.hit_variable param fn)) - && may_follow (Lam_analysis.motion ~assignable fn) -> + && may_follow (Lam_analysis.motion fn) -> (* For a function call such as `{ let x = value; someFn(x) }`, we can rewrite to `someFn(value)` as long as the callee does not capture `x` and `value` may run after it: not `{ let x = c.n; next()(x) }`, where diff --git a/compiler/backend/lam_util.mli b/compiler/backend/lam_util.mli index a2fa6dc64e..99be5d6059 100644 --- a/compiler/backend/lam_util.mli +++ b/compiler/backend/lam_util.mli @@ -54,14 +54,12 @@ val alias_ident_or_global : val refine_let : ?original:Lambda.t -> - assignable:Set_ident.t -> kind:Lambda.let_kind -> Ident.t -> Lambda.t -> Lambda.t -> Lambda.t (** [original] is the binding being rebuilt, when there is one. It is returned - unchanged if no refinement applies. [assignable] are the variables some - [Lassign] assigns. *) + unchanged if no refinement applies. *) val not_function : Lambda.t -> bool