Skip to content

Commit 5cd2520

Browse files
chambartmshinwell
andauthored
flambda-backend: Disable inlining of recursive functions by default (#372)
Co-authored-by: Mark Shinwell <[email protected]>
1 parent e98b277 commit 5cd2520

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

driver/compenv.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,20 @@ let read_one_param ppf position name v =
399399
| "flambda2-expert-max-unboxing-depth" ->
400400
int_setter ppf "flambda2-expert-max-unboxing-depth"
401401
Flambda2.Expert.max_unboxing_depth v
402+
| "flambda2-expert-can-inline-recursive-functions" ->
403+
set "flambda2-expert-can-inline-recursive-functions"
404+
[Flambda2.Expert.can_inline_recursive_functions] v
405+
| "no-flambda2-expert-can-inline-recursive-functions" ->
406+
clear "flambda2-expert-can-inline-recursive-functions"
407+
[Flambda2.Expert.can_inline_recursive_functions] v
402408
| "flambda2-inline-max-depth" ->
403409
Int_arg_helper.parse v
404410
"Bad syntax in OCAMLPARAM for 'flambda2-inline-max-depth'"
405411
Flambda2.Inlining.max_depth
412+
| "flambda2-inline-max-rec-depth" ->
413+
Int_arg_helper.parse v
414+
"Bad syntax in OCAMLPARAM for 'flambda2-inline-max-rec-depth'"
415+
Flambda2.Inlining.max_rec_depth
406416
| "flambda2-inline-call-cost" ->
407417
Float_arg_helper.parse v
408418
"Bad syntax in OCAMLPARAM for 'flambda2-inline-call-cost'"

driver/main_args.ml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,21 @@ let mk_flambda2_expert_max_unboxing_depth f =
10581058
Flambda2.Expert.Default.max_unboxing_depth
10591059
;;
10601060

1061+
let mk_flambda2_expert_can_inline_recursive_functions f =
1062+
"-flambda2-expert-can-inline-recursive-functions", Arg.Unit f,
1063+
Printf.sprintf " Consider inlining\n\
1064+
\ recursive functions (default %s) (Flambda 2 only)"
1065+
(format_default Flambda2.Expert.Default.can_inline_recursive_functions)
1066+
;;
1067+
1068+
let mk_no_flambda2_expert_can_inline_recursive_functions f =
1069+
"-no-flambda2-expert-can-inline-recursive-functions", Arg.Unit f,
1070+
Printf.sprintf " Only inline recursive\n\
1071+
\ functions if forced to so do by an attribute\n\
1072+
\ (default %s) (Flambda 2 only)"
1073+
(format_not_default Flambda2.Expert.Default.can_inline_recursive_functions)
1074+
;;
1075+
10611076
let mk_flambda2_debug_permute_every_name f =
10621077
"-flambda2-debug-permute-every-name", Arg.Unit f,
10631078
Printf.sprintf " Permute every name to test name\n\
@@ -1097,6 +1112,14 @@ let mk_flambda2_inline_max_depth f =
10971112
Clflags.Flambda2.Inlining.Default.max_depth
10981113
;;
10991114

1115+
let mk_flambda2_inline_max_rec_depth f =
1116+
"-flambda2-inline-max-rec-depth", Arg.String f,
1117+
Printf.sprintf "<int>|<round>=<int>[,...]\n\
1118+
\ Maximum depth of search for inlining opportunities inside\n\
1119+
\ inlined recursive functions (default %d) (Flambda 2 only)"
1120+
Clflags.Flambda2.Inlining.Default.max_rec_depth
1121+
;;
1122+
11001123
let mk_flambda2_inline_cost arg descr ~default f =
11011124
Printf.sprintf "-flambda2-inline-%s-cost" arg,
11021125
Arg.String f,
@@ -1455,12 +1478,15 @@ module type Optcommon_options = sig
14551478
val _no_flambda2_expert_phantom_lets : unit -> unit
14561479
val _flambda2_expert_max_block_size_for_projections : int -> unit
14571480
val _flambda2_expert_max_unboxing_depth : int -> unit
1481+
val _flambda2_expert_can_inline_recursive_functions : unit -> unit
1482+
val _no_flambda2_expert_can_inline_recursive_functions : unit -> unit
14581483
val _flambda2_debug_permute_every_name : unit -> unit
14591484
val _no_flambda2_debug_permute_every_name : unit -> unit
14601485
val _flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
14611486
val _no_flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
14621487

14631488
val _flambda2_inline_max_depth : string -> unit
1489+
val _flambda2_inline_max_rec_depth : string -> unit
14641490
val _flambda2_inline_call_cost : string -> unit
14651491
val _flambda2_inline_alloc_cost : string -> unit
14661492
val _flambda2_inline_prim_cost : string -> unit
@@ -1849,6 +1875,10 @@ struct
18491875
F._flambda2_expert_max_block_size_for_projections;
18501876
mk_flambda2_expert_max_unboxing_depth
18511877
F._flambda2_expert_max_unboxing_depth;
1878+
mk_flambda2_expert_can_inline_recursive_functions
1879+
F._flambda2_expert_can_inline_recursive_functions;
1880+
mk_no_flambda2_expert_can_inline_recursive_functions
1881+
F._no_flambda2_expert_can_inline_recursive_functions;
18521882
mk_flambda2_debug_permute_every_name
18531883
F._flambda2_debug_permute_every_name;
18541884
mk_no_flambda2_debug_permute_every_name
@@ -1859,6 +1889,7 @@ struct
18591889
F._no_flambda2_debug_concrete_types_only_on_canonicals;
18601890

18611891
mk_flambda2_inline_max_depth F._flambda2_inline_max_depth;
1892+
mk_flambda2_inline_max_rec_depth F._flambda2_inline_max_rec_depth;
18621893
mk_flambda2_inline_alloc_cost F._flambda2_inline_alloc_cost;
18631894
mk_flambda2_inline_branch_cost F._flambda2_inline_branch_cost;
18641895
mk_flambda2_inline_call_cost F._flambda2_inline_call_cost;
@@ -2038,6 +2069,10 @@ module Make_opttop_options (F : Opttop_options) = struct
20382069
F._flambda2_expert_max_block_size_for_projections;
20392070
mk_flambda2_expert_max_unboxing_depth
20402071
F._flambda2_expert_max_unboxing_depth;
2072+
mk_flambda2_expert_can_inline_recursive_functions
2073+
F._flambda2_expert_can_inline_recursive_functions;
2074+
mk_no_flambda2_expert_can_inline_recursive_functions
2075+
F._no_flambda2_expert_can_inline_recursive_functions;
20412076
mk_flambda2_debug_permute_every_name
20422077
F._flambda2_debug_permute_every_name;
20432078
mk_no_flambda2_debug_permute_every_name
@@ -2048,6 +2083,7 @@ module Make_opttop_options (F : Opttop_options) = struct
20482083
F._no_flambda2_debug_concrete_types_only_on_canonicals;
20492084

20502085
mk_flambda2_inline_max_depth F._flambda2_inline_max_depth;
2086+
mk_flambda2_inline_max_rec_depth F._flambda2_inline_max_rec_depth;
20512087
mk_flambda2_inline_alloc_cost F._flambda2_inline_alloc_cost;
20522088
mk_flambda2_inline_branch_cost F._flambda2_inline_branch_cost;
20532089
mk_flambda2_inline_call_cost F._flambda2_inline_call_cost;
@@ -2390,6 +2426,10 @@ module Default = struct
23902426
Flambda2.Expert.max_block_size_for_projections := Some size
23912427
let _flambda2_expert_max_unboxing_depth depth =
23922428
Flambda2.Expert.max_unboxing_depth := depth
2429+
let _flambda2_expert_can_inline_recursive_functions () =
2430+
Flambda2.Expert.can_inline_recursive_functions := true
2431+
let _no_flambda2_expert_can_inline_recursive_functions () =
2432+
Flambda2.Expert.can_inline_recursive_functions := false
23932433
let _flambda2_debug_permute_every_name =
23942434
set Flambda2.Debug.permute_every_name
23952435
let _no_flambda2_debug_permute_every_name =
@@ -2404,6 +2444,10 @@ module Default = struct
24042444
"Syntax: -flambda2-inline-max-depth <int> | <round>=<int>[,...]"
24052445
Flambda2.Inlining.max_depth
24062446

2447+
let _flambda2_inline_max_rec_depth spec =
2448+
Int_arg_helper.parse spec
2449+
"Syntax: -flambda2-inline-max-rec-depth <int> | <round>=<int>[,...]"
2450+
Flambda2.Inlining.max_rec_depth
24072451
let _flambda2_inline_alloc_cost spec =
24082452
Float_arg_helper.parse spec
24092453
"Syntax: -flambda2-inline-alloc-cost <float> | <round>=<float>[,...]"

driver/main_args.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,15 @@ module type Optcommon_options = sig
241241
val _no_flambda2_expert_phantom_lets : unit -> unit
242242
val _flambda2_expert_max_block_size_for_projections : int -> unit
243243
val _flambda2_expert_max_unboxing_depth : int -> unit
244+
val _flambda2_expert_can_inline_recursive_functions : unit -> unit
245+
val _no_flambda2_expert_can_inline_recursive_functions : unit -> unit
244246
val _flambda2_debug_permute_every_name : unit -> unit
245247
val _no_flambda2_debug_permute_every_name : unit -> unit
246248
val _flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
247249
val _no_flambda2_debug_concrete_types_only_on_canonicals : unit -> unit
248250

249251
val _flambda2_inline_max_depth : string -> unit
252+
val _flambda2_inline_max_rec_depth : string -> unit
250253
val _flambda2_inline_call_cost : string -> unit
251254
val _flambda2_inline_alloc_cost : string -> unit
252255
val _flambda2_inline_prim_cost : string -> unit

utils/clflags.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ module Flambda2 = struct
496496
let phantom_lets = true
497497
let max_block_size_for_projections = None
498498
let max_unboxing_depth = 3
499+
let can_inline_recursive_functions = false
499500
end
500501

501502
let code_id_and_symbol_scoping_checks =
@@ -506,6 +507,8 @@ module Flambda2 = struct
506507
let max_block_size_for_projections =
507508
ref Default.max_block_size_for_projections
508509
let max_unboxing_depth = ref Default.max_unboxing_depth
510+
let can_inline_recursive_functions =
511+
ref Default.can_inline_recursive_functions
509512
end
510513

511514
module Debug = struct
@@ -524,6 +527,7 @@ module Flambda2 = struct
524527
let cost_divisor = 8.
525528

526529
let max_depth = 1
530+
let max_rec_depth = 0
527531

528532
let call_cost = 5. /. cost_divisor
529533
let alloc_cost = 7. /. cost_divisor
@@ -544,6 +548,7 @@ module Flambda2 = struct
544548
module I = Int_arg_helper
545549

546550
let max_depth = ref (I.default Default.max_depth)
551+
let max_rec_depth = ref (I.default Default.max_rec_depth)
547552

548553
let call_cost = ref (F.default Default.call_cost)
549554
let alloc_cost = ref (F.default Default.alloc_cost)
@@ -564,6 +569,7 @@ module Flambda2 = struct
564569

565570
type inlining_arguments = {
566571
max_depth : int option;
572+
max_rec_depth : int option;
567573
call_cost : float option;
568574
alloc_cost : float option;
569575
prim_cost : float option;
@@ -579,6 +585,7 @@ module Flambda2 = struct
579585
let set_int = set_int_arg round in
580586
let set_float = set_float_arg round in
581587
set_int max_depth Default.max_depth arg.max_depth;
588+
set_int max_rec_depth Default.max_rec_depth arg.max_rec_depth;
582589
set_float call_cost Default.call_cost arg.call_cost;
583590
set_float alloc_cost Default.alloc_cost arg.alloc_cost;
584591
set_float prim_cost Default.prim_cost arg.prim_cost;
@@ -595,6 +602,7 @@ module Flambda2 = struct
595602

596603
let oclassic_arguments = {
597604
max_depth = None;
605+
max_rec_depth = None;
598606
call_cost = None;
599607
alloc_cost = None;
600608
prim_cost = None;
@@ -612,6 +620,7 @@ module Flambda2 = struct
612620

613621
let o2_arguments = {
614622
max_depth = Some 2;
623+
max_rec_depth = Some 0;
615624
call_cost = Some (2.0 *. Default.call_cost);
616625
alloc_cost = Some (2.0 *. Default.alloc_cost);
617626
prim_cost = Some (2.0 *. Default.prim_cost);
@@ -625,6 +634,7 @@ module Flambda2 = struct
625634

626635
let o3_arguments = {
627636
max_depth = Some 3;
637+
max_rec_depth = Some 0;
628638
call_cost = Some (3.0 *. Default.call_cost);
629639
alloc_cost = Some (3.0 *. Default.alloc_cost);
630640
prim_cost = Some (3.0 *. Default.prim_cost);

utils/clflags.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ module Flambda2 : sig
253253
val phantom_lets : bool
254254
val max_block_size_for_projections : int option
255255
val max_unboxing_depth : int
256+
val can_inline_recursive_functions : bool
256257
end
257258

258259
val code_id_and_symbol_scoping_checks : bool ref
@@ -261,6 +262,7 @@ module Flambda2 : sig
261262
val phantom_lets : bool ref
262263
val max_block_size_for_projections : int option ref
263264
val max_unboxing_depth : int ref
265+
val can_inline_recursive_functions : bool ref
264266
end
265267

266268
module Debug : sig
@@ -276,6 +278,7 @@ module Flambda2 : sig
276278
module Inlining : sig
277279
module Default : sig
278280
val max_depth : int
281+
val max_rec_depth : int
279282

280283
val call_cost : float
281284
val alloc_cost : float
@@ -293,6 +296,7 @@ module Flambda2 : sig
293296
end
294297

295298
val max_depth : Int_arg_helper.parsed ref
299+
val max_rec_depth : Int_arg_helper.parsed ref
296300

297301
val call_cost : Float_arg_helper.parsed ref
298302
val alloc_cost : Float_arg_helper.parsed ref

0 commit comments

Comments
 (0)