Skip to content

Commit 678d647

Browse files
authored
flambda-backend: Wire in the Flambda 2 inlining flags (#100)
1 parent 1a8febb commit 678d647

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed

driver/main_args.ml

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,78 @@ let mk_no_flambda2_debug_concrete_types_only_on_canonicals f =
10811081
Flambda2.Debug.Default.concrete_types_only_on_canonicals)
10821082
;;
10831083

1084+
let mk_flambda2_inline_max_depth f =
1085+
"-flambda2-inline-max-depth", Arg.String f,
1086+
Printf.sprintf "<int>|<round>=<int>[,...]\n\
1087+
\ Maximum depth of search for inlining opportunities inside\n\
1088+
\ inlined functions (default %d) (Flambda 2 only)"
1089+
Clflags.Flambda2.Inlining.Default.max_depth
1090+
;;
1091+
1092+
let mk_flambda2_inline_cost arg descr ~default f =
1093+
Printf.sprintf "-flambda2-inline-%s-cost" arg,
1094+
Arg.String f,
1095+
Printf.sprintf "<float>|<round>=<float>[,...]\n\
1096+
\ The cost of not removing %s during inlining\n\
1097+
\ (default %.03f, higher = more costly) (Flambda 2 only)"
1098+
descr
1099+
default
1100+
;;
1101+
1102+
let mk_flambda2_inline_call_cost =
1103+
mk_flambda2_inline_cost "call" "a call"
1104+
~default:Clflags.Flambda2.Inlining.Default.call_cost
1105+
1106+
let mk_flambda2_inline_alloc_cost =
1107+
mk_flambda2_inline_cost "alloc" "an allocation"
1108+
~default:Clflags.Flambda2.Inlining.Default.alloc_cost
1109+
1110+
let mk_flambda2_inline_prim_cost =
1111+
mk_flambda2_inline_cost "prim" "a primitive"
1112+
~default:Clflags.Flambda2.Inlining.Default.prim_cost
1113+
1114+
let mk_flambda2_inline_branch_cost =
1115+
mk_flambda2_inline_cost "branch" "a conditional"
1116+
~default:Clflags.Flambda2.Inlining.Default.branch_cost
1117+
1118+
let mk_flambda2_inline_indirect_call_cost =
1119+
mk_flambda2_inline_cost "indirect" "an indirect call"
1120+
~default:Clflags.Flambda2.Inlining.Default.indirect_call_cost
1121+
1122+
let mk_flambda2_inline_poly_compare_cost =
1123+
mk_flambda2_inline_cost "poly-compare" "a polymorphic comparison"
1124+
~default:Clflags.Flambda2.Inlining.Default.poly_compare_cost
1125+
1126+
(* CR-someday mshinwell: We should have a check that the parameters provided by
1127+
the user are sensible, e.g. small_function_size <= large_function_size. *)
1128+
1129+
let mk_flambda2_inline_small_function_size f =
1130+
"-flambda2-inline-small-function-size", Arg.String f,
1131+
Printf.sprintf "<int>|<round>=<int>[,...]\n\
1132+
\ Functions with a cost less than this will always be inlined\n\
1133+
\ unless an attribute instructs otherwise (default %d)\n\
1134+
\ (Flambda 2 only)"
1135+
Clflags.Flambda2.Inlining.Default.small_function_size
1136+
;;
1137+
1138+
let mk_flambda2_inline_large_function_size f =
1139+
"-flambda2-inline-large-function-size", Arg.String f,
1140+
Printf.sprintf "<int>|<round>=<int>[,...]\n\
1141+
\ Functions with a cost greater than this will never be inlined\n\
1142+
\ unless an attribute instructs otherwise (default %d); speculative\n\
1143+
\ inlining will be disabled if equal to the small function size\n\
1144+
\ (Flambda 2 only)"
1145+
Clflags.Flambda2.Inlining.Default.large_function_size
1146+
;;
1147+
1148+
let mk_flambda2_inline_threshold f =
1149+
"-flambda2-inline-threshold", Arg.String f,
1150+
Printf.sprintf "<float>|<round>=<float>[,...]\n\
1151+
\ Aggressiveness of inlining (default %.02f, higher numbers mean\n\
1152+
\ more aggressive) (Flambda 2 only)"
1153+
Clflags.Flambda2.Inlining.Default.threshold
1154+
;;
1155+
10841156
module type Common_options = sig
10851157
val _absname : unit -> unit
10861158
val _alert : string -> unit
@@ -1309,6 +1381,17 @@ module type Optcommon_options = sig
13091381
val _no_flambda2_debug_permute_every_name : unit -> unit
13101382
val _flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
13111383
val _no_flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
1384+
1385+
val _flambda2_inline_max_depth : string -> unit
1386+
val _flambda2_inline_call_cost : string -> unit
1387+
val _flambda2_inline_alloc_cost : string -> unit
1388+
val _flambda2_inline_prim_cost : string -> unit
1389+
val _flambda2_inline_branch_cost : string -> unit
1390+
val _flambda2_inline_indirect_call_cost : string -> unit
1391+
val _flambda2_inline_poly_compare_cost : string -> unit
1392+
val _flambda2_inline_small_function_size : string -> unit
1393+
val _flambda2_inline_large_function_size : string -> unit
1394+
val _flambda2_inline_threshold : string -> unit
13121395
end;;
13131396

13141397
module type Optcomp_options = sig
@@ -1682,6 +1765,19 @@ struct
16821765
mk_no_flambda2_debug_concrete_types_only_on_canonicals
16831766
F._no_flambda2_debug_concrete_types_only_on_canonicals;
16841767

1768+
mk_flambda2_inline_max_depth F._flambda2_inline_max_depth;
1769+
mk_flambda2_inline_alloc_cost F._flambda2_inline_alloc_cost;
1770+
mk_flambda2_inline_branch_cost F._flambda2_inline_branch_cost;
1771+
mk_flambda2_inline_call_cost F._flambda2_inline_call_cost;
1772+
mk_flambda2_inline_prim_cost F._flambda2_inline_prim_cost;
1773+
mk_flambda2_inline_indirect_call_cost F._flambda2_inline_indirect_call_cost;
1774+
mk_flambda2_inline_poly_compare_cost F._flambda2_inline_poly_compare_cost;
1775+
mk_flambda2_inline_small_function_size
1776+
F._flambda2_inline_small_function_size;
1777+
mk_flambda2_inline_large_function_size
1778+
F._flambda2_inline_large_function_size;
1779+
mk_flambda2_inline_threshold F._flambda2_inline_threshold;
1780+
16851781
mk_match_context_rows F._match_context_rows;
16861782
mk_dno_unique_ids F._dno_unique_ids;
16871783
mk_dunique_ids F._dunique_ids;
@@ -1838,6 +1934,19 @@ module Make_opttop_options (F : Opttop_options) = struct
18381934
mk_no_flambda2_debug_concrete_types_only_on_canonicals
18391935
F._no_flambda2_debug_concrete_types_only_on_canonicals;
18401936

1937+
mk_flambda2_inline_max_depth F._flambda2_inline_max_depth;
1938+
mk_flambda2_inline_alloc_cost F._flambda2_inline_alloc_cost;
1939+
mk_flambda2_inline_branch_cost F._flambda2_inline_branch_cost;
1940+
mk_flambda2_inline_call_cost F._flambda2_inline_call_cost;
1941+
mk_flambda2_inline_prim_cost F._flambda2_inline_prim_cost;
1942+
mk_flambda2_inline_indirect_call_cost F._flambda2_inline_indirect_call_cost;
1943+
mk_flambda2_inline_poly_compare_cost F._flambda2_inline_poly_compare_cost;
1944+
mk_flambda2_inline_small_function_size
1945+
F._flambda2_inline_small_function_size;
1946+
mk_flambda2_inline_large_function_size
1947+
F._flambda2_inline_large_function_size;
1948+
mk_flambda2_inline_threshold F._flambda2_inline_threshold;
1949+
18411950
mk_dsource F._dsource;
18421951
mk_dparsetree F._dparsetree;
18431952
mk_dtypedtree F._dtypedtree;
@@ -2160,6 +2269,60 @@ module Default = struct
21602269
set Flambda2.Debug.concrete_types_only_on_canonicals
21612270
let _no_flambda2_debug_concrete_types_only_on_canonicals =
21622271
clear Flambda2.Debug.concrete_types_only_on_canonicals
2272+
2273+
let _flambda2_inline_max_depth spec =
2274+
Int_arg_helper.parse spec
2275+
"Syntax: -flambda2-inline-max-depth <int> | <round>=<int>[,...]"
2276+
Flambda2.Inlining.max_depth
2277+
2278+
let _flambda2_inline_alloc_cost spec =
2279+
Float_arg_helper.parse spec
2280+
"Syntax: -flambda2-inline-alloc-cost <float> | <round>=<float>[,...]"
2281+
Flambda2.Inlining.alloc_cost
2282+
2283+
let _flambda2_inline_branch_cost spec =
2284+
Float_arg_helper.parse spec
2285+
"Syntax: -flambda2-inline-branch-cost <float> | <round>=<float>[,...]"
2286+
Flambda2.Inlining.branch_cost
2287+
2288+
let _flambda2_inline_call_cost spec =
2289+
Float_arg_helper.parse spec
2290+
"Syntax: -flambda2-inline-call-cost <float> | <round>=<float>[,...]"
2291+
Flambda2.Inlining.call_cost
2292+
2293+
let _flambda2_inline_prim_cost spec =
2294+
Float_arg_helper.parse spec
2295+
"Syntax: -flambda2-inline-prim-cost <float> | <round>=<float>[,...]"
2296+
Flambda2.Inlining.prim_cost
2297+
2298+
let _flambda2_inline_indirect_call_cost spec =
2299+
Float_arg_helper.parse spec
2300+
"Syntax: -flambda2-inline-indirect-call-cost <float> | \
2301+
<round>=<float>[,...]"
2302+
Flambda2.Inlining.indirect_call_cost
2303+
2304+
let _flambda2_inline_poly_compare_cost spec =
2305+
Float_arg_helper.parse spec
2306+
"Syntax: -flambda2-inline-poly-compare-cost <float> | \
2307+
<round>=<float>[,...]"
2308+
Flambda2.Inlining.poly_compare_cost
2309+
2310+
let _flambda2_inline_small_function_size spec =
2311+
Int_arg_helper.parse spec
2312+
"Syntax: -flambda2-inline-small-function-size <int> | \
2313+
<round>=<int>[,...]"
2314+
Flambda2.Inlining.small_function_size
2315+
2316+
let _flambda2_inline_large_function_size spec =
2317+
Int_arg_helper.parse spec
2318+
"Syntax: -flambda2-inline-large-function-size <int> | \
2319+
<round>=<int>[,...]"
2320+
Flambda2.Inlining.large_function_size
2321+
2322+
let _flambda2_inline_threshold spec =
2323+
Float_arg_helper.parse spec
2324+
"Syntax: -flambda2-inline-threshold <float> | <round>=<float>[,...]"
2325+
Flambda2.Inlining.threshold
21632326
end
21642327

21652328
module Compiler = struct

driver/main_args.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ module type Optcommon_options = sig
243243
val _no_flambda2_debug_permute_every_name : unit -> unit
244244
val _flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
245245
val _no_flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
246+
247+
val _flambda2_inline_max_depth : string -> unit
248+
val _flambda2_inline_call_cost : string -> unit
249+
val _flambda2_inline_alloc_cost : string -> unit
250+
val _flambda2_inline_prim_cost : string -> unit
251+
val _flambda2_inline_branch_cost : string -> unit
252+
val _flambda2_inline_indirect_call_cost : string -> unit
253+
val _flambda2_inline_poly_compare_cost : string -> unit
254+
val _flambda2_inline_small_function_size : string -> unit
255+
val _flambda2_inline_large_function_size : string -> unit
256+
val _flambda2_inline_threshold : string -> unit
246257
end;;
247258

248259
module type Optcomp_options = sig

utils/clflags.ml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,43 @@ module Flambda2 = struct
502502
ref Default.concrete_types_only_on_canonicals
503503
end
504504

505+
module Inlining = struct
506+
module Default = struct
507+
let cost_divisor = 8.
508+
509+
let max_depth = 1
510+
511+
let call_cost = 5. /. cost_divisor
512+
let alloc_cost = 7. /. cost_divisor
513+
let prim_cost = 3. /. cost_divisor
514+
let branch_cost = 5. /. cost_divisor
515+
let indirect_call_cost = 4. /. cost_divisor
516+
let poly_compare_cost = 10. /. cost_divisor
517+
518+
let small_function_size = 10
519+
let large_function_size = 10
520+
521+
let threshold = 10.
522+
end
523+
524+
module F = Float_arg_helper
525+
module I = Int_arg_helper
526+
527+
let max_depth = ref (I.default Default.max_depth)
528+
529+
let call_cost = ref (F.default Default.call_cost)
530+
let alloc_cost = ref (F.default Default.alloc_cost)
531+
let prim_cost = ref (F.default Default.prim_cost)
532+
let branch_cost = ref (F.default Default.branch_cost)
533+
let indirect_call_cost = ref (F.default Default.indirect_call_cost)
534+
let poly_compare_cost = ref (F.default Default.poly_compare_cost)
535+
536+
let small_function_size = ref (I.default Default.small_function_size)
537+
let large_function_size = ref (I.default Default.large_function_size)
538+
539+
let threshold = ref (F.default Default.threshold)
540+
end
541+
505542
let oclassic_flags () =
506543
cse_depth := 2;
507544
join_points := false;

utils/clflags.mli

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,38 @@ module Flambda2 : sig
278278
val concrete_types_only_on_canonicals : bool ref
279279
end
280280

281+
module Inlining : sig
282+
module Default : sig
283+
val max_depth : int
284+
285+
val call_cost : float
286+
val alloc_cost : float
287+
val prim_cost : float
288+
val branch_cost : float
289+
val indirect_call_cost : float
290+
val poly_compare_cost : float
291+
292+
val small_function_size : int
293+
val large_function_size : int
294+
295+
val threshold : float
296+
end
297+
298+
val max_depth : Int_arg_helper.parsed ref
299+
300+
val call_cost : Float_arg_helper.parsed ref
301+
val alloc_cost : Float_arg_helper.parsed ref
302+
val prim_cost : Float_arg_helper.parsed ref
303+
val branch_cost : Float_arg_helper.parsed ref
304+
val indirect_call_cost : Float_arg_helper.parsed ref
305+
val poly_compare_cost : Float_arg_helper.parsed ref
306+
307+
val small_function_size : Int_arg_helper.parsed ref
308+
val large_function_size : Int_arg_helper.parsed ref
309+
310+
val threshold : Float_arg_helper.parsed ref
311+
end
312+
281313
val oclassic_flags : unit -> unit
282314
val o1_flags : unit -> unit
283315
val o2_flags : unit -> unit

0 commit comments

Comments
 (0)