Skip to content

Commit

Permalink
Remove Inlining_arguments.unknown (#547)
Browse files Browse the repository at this point in the history
We now want to create every `Code.t` with a correct set of inlining
arguments, so we no longer need `Inlining_arguments.unknown`, which
deferred producing the arguments until after the `Code.t` was created.
  • Loading branch information
lukemaurer authored Jul 9, 2021
1 parent c94b7c2 commit 5e6be1e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 68 deletions.
5 changes: 1 addition & 4 deletions .depend
Original file line number Diff line number Diff line change
Expand Up @@ -5291,6 +5291,7 @@ middle_end/flambda/inlining/call_site_inlining_decision.cmx : \
middle_end/flambda/inlining/call_site_inlining_decision.cmi
middle_end/flambda/inlining/call_site_inlining_decision.cmi : \
middle_end/flambda/simplify/simplify_common.cmi \
middle_end/flambda/compilenv_deps/rec_info.cmi \
middle_end/flambda/types/flambda_type.cmi \
middle_end/flambda/types/kinds/flambda_arity.cmi \
middle_end/flambda/terms/flambda.cmi \
Expand All @@ -5315,13 +5316,9 @@ middle_end/flambda/inlining/function_decl_inlining_decision.cmi : \
middle_end/flambda/simplify/env/downwards_env.cmi \
middle_end/flambda/inlining/metrics/code_size.cmi
middle_end/flambda/inlining/inlining_arguments.cmo : \
middle_end/flambda/types/basic/or_unknown.cmi \
utils/misc.cmi \
utils/clflags.cmi \
middle_end/flambda/inlining/inlining_arguments.cmi
middle_end/flambda/inlining/inlining_arguments.cmx : \
middle_end/flambda/types/basic/or_unknown.cmx \
utils/misc.cmx \
utils/clflags.cmx \
middle_end/flambda/inlining/inlining_arguments.cmi
middle_end/flambda/inlining/inlining_arguments.cmi :
Expand Down
6 changes: 3 additions & 3 deletions middle_end/flambda/from_lambda/closure_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ let close_c_call acc ~let_bound_var (prim : Primitive.description)
~call_kind
dbg
~inline:Default_inline
~inlining_state:(Inlining_state.default)
~inlining_state:(Inlining_state.default ~round:0)
in
Expr_with_acc.create_apply acc apply
in
Expand Down Expand Up @@ -580,7 +580,7 @@ let close_apply acc env ({ kind; func; args; continuation; exn_continuation;
~call_kind
(Debuginfo.from_location loc)
~inline:(LC.inline_attribute inlined)
~inlining_state:(Inlining_state.default)
~inlining_state:(Inlining_state.default ~round:0)
in
Expr_with_acc.create_apply acc apply

Expand Down Expand Up @@ -907,7 +907,7 @@ let close_one_function acc ~external_env ~by_closure_id decl
~recursive
~newer_version_of:None
~cost_metrics
~inlining_arguments:Inlining_arguments.unknown
~inlining_arguments:(Inlining_arguments.create ~round:0)
in
let acc = Acc.add_code ~code_id ~code acc in
let acc = Acc.with_seen_a_function acc true in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ let expression_for_failure acc ~backend exn_cont ~register_const_string
(* These inlining fields should not be used for C calls since
they can't really be inlined anyway. *)
let inline = Inline_attribute.Never_inline in
let inlining_state = Inlining_state.default in
let inlining_state = Inlining_state.default ~round:0 in
Apply.create ~callee ~continuation exn_cont
~args ~call_kind dbg ~inline ~inlining_state
in
Expand Down
79 changes: 32 additions & 47 deletions middle_end/flambda/inlining/inlining_arguments.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

[@@@ocaml.warning "+a-30-40-41-42"]

(* CR lmaurer: Flatten this into the top-level module. *)
module Args = struct
type t = {
max_inlining_depth : int;
Expand Down Expand Up @@ -204,50 +205,34 @@ module Args = struct
}
end

type t = Args.t Or_unknown.t

let unknown = Or_unknown.Unknown

let print ppf = Or_unknown.print Args.print ppf

let get_or_fail t : Args.t =
match t with
| Or_unknown.Unknown ->
Misc.fatal_errorf
"Trying to access an unknown set of inliner arguments. This should not \
happen, usually [meet] should have been called with a known set of \
arguments by this point."
| Or_unknown.Known s -> s

let max_inlining_depth t = (get_or_fail t).max_inlining_depth
let call_cost t = (get_or_fail t).call_cost
let alloc_cost t = (get_or_fail t).alloc_cost
let prim_cost t = (get_or_fail t).prim_cost
let branch_cost t = (get_or_fail t).branch_cost
let indirect_call_cost t = (get_or_fail t).indirect_call_cost
let poly_compare_cost t = (get_or_fail t).poly_compare_cost
let small_function_size t = (get_or_fail t).small_function_size
let large_function_size t = (get_or_fail t).large_function_size
let threshold t = (get_or_fail t).threshold

let meet (t1 : _ Or_unknown.t) (t2 : _ Or_unknown.t) : t =
match t1, t2 with
| Unknown, Unknown -> Unknown
| Known _, Unknown -> t1
| Unknown, Known _ -> t2
| Known args1, Known args2 ->
(* If we are sure that args1 is lower than args2 then
[meet args1 args2 = args1]. In that case we can avoid calling
[Args.meet] and reuse args1 to avoid having to allocate a new Args.t.
The same goes if the are sure that args2 is lower than args1.
*)
if Args.(<=) args1 args2 then
t1
else if Args.(<=) args2 args1 then
t2
else
Known (Args.meet args1 args2)

let create ~round : t = Known (Args.create ~round)

let equal t1 t2 = Or_unknown.equal Args.equal t1 t2
type t = Args.t

let print ppf = Args.print ppf

let max_inlining_depth t = t.Args.max_inlining_depth
let call_cost t = t.Args.call_cost
let alloc_cost t = t.Args.alloc_cost
let prim_cost t = t.Args.prim_cost
let branch_cost t = t.Args.branch_cost
let indirect_call_cost t = t.Args.indirect_call_cost
let poly_compare_cost t = t.Args.poly_compare_cost
let small_function_size t = t.Args.small_function_size
let large_function_size t = t.Args.large_function_size
let threshold t = t.Args.threshold

let meet t1 t2 =
(* If we are sure that args1 is lower than args2 then
[meet args1 args2 = args1]. In that case we can avoid calling
[Args.meet] and reuse args1 to avoid having to allocate a new Args.t.
The same goes if the are sure that args2 is lower than args1.
*)
if Args.(<=) t1 t2 then
t1
else if Args.(<=) t2 t1 then
t2
else
Args.meet t1 t2

let create ~round = Args.create ~round

let equal t1 t2 = Args.equal t1 t2
2 changes: 0 additions & 2 deletions middle_end/flambda/inlining/inlining_arguments.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

type t

val unknown : t

val create : round:int -> t

val print : Format.formatter -> t -> unit
Expand Down
4 changes: 2 additions & 2 deletions middle_end/flambda/inlining/inlining_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type t = {

let increment_depth t = { t with depth = t.depth + 1 }

let default = {
arguments = Inlining_arguments.unknown;
let default ~round = {
arguments = Inlining_arguments.create ~round;
depth = 0
}

Expand Down
2 changes: 1 addition & 1 deletion middle_end/flambda/inlining/inlining_state.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ val print : Format.formatter -> t -> unit

val equal : t -> t -> bool

val default : t
val default : round:int -> t

val create : arguments:Inlining_arguments.t -> depth:int -> t

Expand Down
6 changes: 3 additions & 3 deletions middle_end/flambda/parser/fexpr_to_flambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ let rec expr env (e : Fexpr.expr) : Flambda.Expr.t =
~recursive
~cost_metrics
(* CR poechsel: grab inlining arguments from fexpr. *)
~inlining_arguments:Inlining_arguments.unknown
~inlining_arguments:(Inlining_arguments.create ~round:0)
in
Code code
in
Expand Down Expand Up @@ -894,9 +894,9 @@ let rec expr env (e : Fexpr.expr) : Flambda.Expr.t =
match inlining_state with
| Some { depth; } ->
(* TODO inlining arguments *)
Inlining_state.create ~arguments:Inlining_arguments.unknown ~depth
Inlining_state.create ~arguments:(Inlining_arguments.create ~round:0) ~depth
| None ->
Inlining_state.default
Inlining_state.default ~round:0
in
let exn_continuation = find_exn_cont env exn_continuation in
let apply =
Expand Down
2 changes: 1 addition & 1 deletion middle_end/flambda/parser/flambda_to_fexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ let static_const env (sc : Flambda.Static_const.t) : Fexpr.static_data =
| Immutable_string s -> Immutable_string s

let inlining_state (is : Inlining_state.t) : Fexpr.inlining_state option =
if Inlining_state.equal is Inlining_state.default then None else
if Inlining_state.equal is (Inlining_state.default ~round:0) then None else
let depth = Inlining_state.depth is in
(* TODO: inlining arguments *)
Some { depth; }
Expand Down
5 changes: 1 addition & 4 deletions middle_end/flambda/simplify/env/downwards_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ let create ~round ~backend ~(resolver : resolver)
get_imported_code;
inlined_debuginfo = Debuginfo.none;
can_inline = true;
inlining_state =
Inlining_state.default
|> Inlining_state.with_arguments
(Inlining_arguments.create ~round);
inlining_state = Inlining_state.default ~round;
float_const_prop;
code = Code_id.Map.empty;
at_unit_toplevel = true;
Expand Down

0 comments on commit 5e6be1e

Please sign in to comment.