Skip to content
Open
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
19 changes: 9 additions & 10 deletions compiler/backend/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions compiler/backend/lam_analysis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 12 additions & 22 deletions compiler/backend/lam_beta_reduce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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 =
Expand All @@ -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 ()
Expand All @@ -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
Expand All @@ -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
10 changes: 2 additions & 8 deletions compiler/backend/lam_beta_reduce.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions compiler/backend/lam_beta_reduce_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions compiler/backend/lam_beta_reduce_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
20 changes: 6 additions & 14 deletions compiler/backend/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions compiler/backend/lam_id_kind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions compiler/backend/lam_id_kind.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 3 additions & 6 deletions compiler/backend/lam_pass_collect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
5 changes: 2 additions & 3 deletions compiler/backend/lam_pass_count.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/backend/lam_pass_count.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 4 additions & 9 deletions compiler/backend/lam_pass_deep_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading