Skip to content

Commit dba922b

Browse files
authored
flambda-backend: Oclassic/O2/O3 etc (ocaml-flambda#104)
1 parent f88af3e commit dba922b

File tree

6 files changed

+125
-79
lines changed

6 files changed

+125
-79
lines changed

driver/compenv.ml

+3-17
Original file line numberDiff line numberDiff line change
@@ -337,23 +337,9 @@ let read_one_param ppf position name v =
337337
Int_arg_helper.parse v
338338
"Bad syntax in OCAMLPARAM for 'inline-max-depth'"
339339
inline_max_depth
340-
341-
| "Oclassic" ->
342-
set "Oclassic" [ classic_inlining ] v
343-
| "O2" ->
344-
if check_bool ppf "O2" v then begin
345-
default_simplify_rounds := 2;
346-
use_inlining_arguments_set o2_arguments;
347-
use_inlining_arguments_set ~round:0 o1_arguments
348-
end
349-
350-
| "O3" ->
351-
if check_bool ppf "O3" v then begin
352-
default_simplify_rounds := 3;
353-
use_inlining_arguments_set o3_arguments;
354-
use_inlining_arguments_set ~round:1 o2_arguments;
355-
use_inlining_arguments_set ~round:0 o1_arguments
356-
end
340+
| "Oclassic" -> if check_bool ppf "Oclassic" v then Clflags.set_oclassic ()
341+
| "O2" -> if check_bool ppf "O2" v then Clflags.set_o2 ()
342+
| "O3" -> if check_bool ppf "O3" v then Clflags.set_o3 ()
357343
| "unbox-closures" ->
358344
set "unbox-closures" [ unbox_closures ] v
359345
| "unbox-closures-factor" ->

driver/main_args.ml

+3-10
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ module Default = struct
22142214
module Native = struct
22152215
let _S = set keep_asm_file
22162216
let _clambda_checks () = clambda_checks := true
2217-
let _classic_inlining () = classic_inlining := true
2217+
let _classic_inlining () = set_oclassic ()
22182218
let _compact = clear optimize_for_speed
22192219
let _dalloc = set dump_regalloc
22202220
let _davail () = dump_avail := true
@@ -2300,15 +2300,8 @@ module Default = struct
23002300
collected, then checked all at once for illegal combinations, and then
23012301
transformed into the settings of the individual parameters.
23022302
*)
2303-
let _o2 () =
2304-
default_simplify_rounds := 2;
2305-
use_inlining_arguments_set o2_arguments;
2306-
use_inlining_arguments_set ~round:0 o1_arguments
2307-
let _o3 () =
2308-
default_simplify_rounds := 3;
2309-
use_inlining_arguments_set o3_arguments;
2310-
use_inlining_arguments_set ~round:1 o2_arguments;
2311-
use_inlining_arguments_set ~round:0 o1_arguments
2303+
let _o2 () = Clflags.set_o2 ()
2304+
let _o3 () = Clflags.set_o3 ()
23122305
let _remove_unused_arguments = set remove_unused_arguments
23132306
let _rounds n = simplify_rounds := (Some n)
23142307
let _unbox_closures = set unbox_closures

driver/optcompile.ml

+1-7
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ let (|>>) (x, y) f = (x, f y)
3232
(** Native compilation backend for .ml files. *)
3333

3434
let flambda i backend typed =
35-
if !Clflags.classic_inlining then begin
36-
Clflags.default_simplify_rounds := 1;
37-
Clflags.use_inlining_arguments_set Clflags.classic_arguments;
38-
Clflags.unbox_free_vars_of_closures := false;
39-
Clflags.unbox_specialised_args := false
40-
end;
4135
typed
4236
|> Profile.(record transl)
4337
(Translmod.transl_implementation_flambda i.module_name)
@@ -67,7 +61,7 @@ let flambda i backend typed =
6761
Compilenv.save_unit_info (cmx i))
6862

6963
let clambda i backend typed =
70-
Clflags.use_inlining_arguments_set Clflags.classic_arguments;
64+
Clflags.set_oclassic ();
7165
typed
7266
|> Profile.(record transl)
7367
(Translmod.transl_store_implementation i.module_name)

parsing/builtin_attributes.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,15 @@ let clflags_attribute_with_int_payload attr ~name clflags_ref =
337337
| Some i -> clflags_ref := i
338338
| None -> ()
339339
end
340-
340+
341341
let nolabels_attribute attr =
342342
clflags_attribute_without_payload attr
343343
~name:"nolabels" Clflags.classic
344344

345345
let flambda_o3_attribute attr =
346346
clflags_attribute_without_payload' attr
347347
~name:"flambda_o3"
348-
~f:(fun () ->
349-
if Config.flambda then begin
350-
Clflags.use_inlining_arguments_set Clflags.o3_arguments
351-
end)
348+
~f:(fun () -> if Config.flambda then Clflags.set_o3 ())
352349

353350
let inline_attribute attr =
354351
if String.equal attr.attr_name.txt "inline"
@@ -367,7 +364,7 @@ let inline_attribute attr =
367364
Clflags.Float_arg_helper.parse s err_msg Clflags.inline_threshold
368365
| None -> warn_payload attr.attr_loc attr.attr_name.txt err_msg
369366
end
370-
367+
371368
let afl_inst_ratio_attribute attr =
372369
clflags_attribute_with_int_payload attr
373370
~name:"afl_inst_ratio" Clflags.afl_inst_ratio

utils/clflags.ml

+111-11
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,80 @@ module Flambda2 = struct
551551
let threshold = ref (F.default Default.threshold)
552552

553553
let report_bin = ref false
554+
555+
type inlining_arguments = {
556+
max_depth : int option;
557+
call_cost : float option;
558+
alloc_cost : float option;
559+
prim_cost : float option;
560+
branch_cost : float option;
561+
indirect_call_cost : float option;
562+
poly_compare_cost : float option;
563+
small_function_size : int option;
564+
large_function_size : int option;
565+
threshold : float option;
566+
}
567+
568+
let use_inlining_arguments_set ?round (arg : inlining_arguments) =
569+
let set_int = set_int_arg round in
570+
let set_float = set_float_arg round in
571+
set_int max_depth Default.max_depth arg.max_depth;
572+
set_float call_cost Default.call_cost arg.call_cost;
573+
set_float alloc_cost Default.alloc_cost arg.alloc_cost;
574+
set_float prim_cost Default.prim_cost arg.prim_cost;
575+
set_float branch_cost Default.branch_cost arg.branch_cost;
576+
set_float indirect_call_cost
577+
Default.indirect_call_cost arg.indirect_call_cost;
578+
set_float poly_compare_cost
579+
Default.poly_compare_cost arg.poly_compare_cost;
580+
set_int small_function_size
581+
Default.small_function_size arg.small_function_size;
582+
set_int large_function_size
583+
Default.large_function_size arg.large_function_size;
584+
set_float threshold Default.threshold arg.threshold
585+
586+
let oclassic_arguments = {
587+
max_depth = None;
588+
call_cost = None;
589+
alloc_cost = None;
590+
prim_cost = None;
591+
branch_cost = None;
592+
indirect_call_cost = None;
593+
poly_compare_cost = None;
594+
(* We set the small and large function sizes to the same value here to
595+
recover "classic mode" semantics (no speculative inlining). *)
596+
small_function_size = Some Default.small_function_size;
597+
large_function_size = Some Default.small_function_size;
598+
(* [threshold] matches the current compiler's default. (The factor of
599+
8 in that default is accounted for by [cost_divisor], above.) *)
600+
threshold = Some 10.;
601+
}
602+
603+
let o2_arguments = {
604+
max_depth = Some 2;
605+
call_cost = Some (2.0 *. Default.call_cost);
606+
alloc_cost = Some (2.0 *. Default.alloc_cost);
607+
prim_cost = Some (2.0 *. Default.prim_cost);
608+
branch_cost = Some (2.0 *. Default.branch_cost);
609+
indirect_call_cost = Some (2.0 *. Default.indirect_call_cost);
610+
poly_compare_cost = Some (2.0 *. Default.poly_compare_cost);
611+
small_function_size = Some (2 * Default.small_function_size);
612+
large_function_size = Some (4 * Default.large_function_size);
613+
threshold = Some 25.;
614+
}
615+
616+
let o3_arguments = {
617+
max_depth = Some 3;
618+
call_cost = Some (3.0 *. Default.call_cost);
619+
alloc_cost = Some (3.0 *. Default.alloc_cost);
620+
prim_cost = Some (3.0 *. Default.prim_cost);
621+
branch_cost = Some (3.0 *. Default.branch_cost);
622+
indirect_call_cost = Some (3.0 *. Default.indirect_call_cost);
623+
poly_compare_cost = Some (3.0 *. Default.poly_compare_cost);
624+
small_function_size = Some (3 * Default.small_function_size);
625+
large_function_size = Some (8 * Default.large_function_size);
626+
threshold = Some 50.;
627+
}
554628
end
555629

556630
let oclassic_flags () =
@@ -560,13 +634,6 @@ module Flambda2 = struct
560634
Expert.fallback_inlining_heuristic := true;
561635
backend_cse_at_toplevel := false
562636

563-
let o1_flags () =
564-
cse_depth := 2;
565-
join_points := true;
566-
unbox_along_intra_function_control_flow := true;
567-
Expert.fallback_inlining_heuristic := false;
568-
backend_cse_at_toplevel := false
569-
570637
let o2_flags () =
571638
cse_depth := 2;
572639
join_points := true;
@@ -582,6 +649,43 @@ module Flambda2 = struct
582649
backend_cse_at_toplevel := false
583650
end
584651

652+
let is_flambda2 () =
653+
Config.flambda2 && !native_code
654+
655+
let set_oclassic () =
656+
if is_flambda2 () then begin
657+
Flambda2.Inlining.use_inlining_arguments_set
658+
Flambda2.Inlining.oclassic_arguments;
659+
Flambda2.oclassic_flags ()
660+
end else begin
661+
classic_inlining := true;
662+
default_simplify_rounds := 1;
663+
use_inlining_arguments_set classic_arguments;
664+
unbox_free_vars_of_closures := false;
665+
unbox_specialised_args := false
666+
end
667+
668+
let set_o2 () =
669+
if is_flambda2 () then begin
670+
Flambda2.Inlining.use_inlining_arguments_set Flambda2.Inlining.o2_arguments;
671+
Flambda2.o2_flags ()
672+
end else begin
673+
default_simplify_rounds := 2;
674+
use_inlining_arguments_set o2_arguments;
675+
use_inlining_arguments_set ~round:0 o1_arguments
676+
end
677+
678+
let set_o3 () =
679+
if is_flambda2 () then begin
680+
Flambda2.Inlining.use_inlining_arguments_set Flambda2.Inlining.o3_arguments;
681+
Flambda2.o3_flags ()
682+
end else begin
683+
default_simplify_rounds := 3;
684+
use_inlining_arguments_set o3_arguments;
685+
use_inlining_arguments_set ~round:1 o2_arguments;
686+
use_inlining_arguments_set ~round:0 o1_arguments
687+
end
688+
585689
(* This is used by the -stop-after option. *)
586690
module Compiler_pass = struct
587691
(* If you add a new pass, the following must be updated:
@@ -671,10 +775,6 @@ let set_save_ir_after pass enabled =
671775
in
672776
save_ir_after := new_passes
673777

674-
675-
let is_flambda2 () =
676-
Config.flambda2 && !native_code
677-
678778
module String = Misc.Stdlib.String
679779

680780
let arg_spec = ref []

utils/clflags.mli

+4-28
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,6 @@ module Float_arg_helper : sig
4343
val get : key:int -> parsed -> float
4444
end
4545

46-
type inlining_arguments = {
47-
inline_call_cost : int option;
48-
inline_alloc_cost : int option;
49-
inline_prim_cost : int option;
50-
inline_branch_cost : int option;
51-
inline_indirect_cost : int option;
52-
inline_lifting_benefit : int option;
53-
inline_branch_factor : float option;
54-
inline_max_depth : int option;
55-
inline_max_unroll : int option;
56-
inline_threshold : float option;
57-
inline_toplevel_threshold : int option;
58-
}
59-
60-
val classic_arguments : inlining_arguments
61-
val o1_arguments : inlining_arguments
62-
val o2_arguments : inlining_arguments
63-
val o3_arguments : inlining_arguments
64-
65-
(** Set all the inlining arguments for a round.
66-
The default is set if no round is provided. *)
67-
val use_inlining_arguments_set : ?round:int -> inlining_arguments -> unit
68-
6946
val objfiles : string list ref
7047
val ccobjs : string list ref
7148
val dllibs : string list ref
@@ -324,13 +301,12 @@ module Flambda2 : sig
324301

325302
val report_bin : bool ref
326303
end
327-
328-
val oclassic_flags : unit -> unit
329-
val o1_flags : unit -> unit
330-
val o2_flags : unit -> unit
331-
val o3_flags : unit -> unit
332304
end
333305

306+
val set_oclassic : unit -> unit
307+
val set_o2 : unit -> unit
308+
val set_o3 : unit -> unit
309+
334310
module Compiler_pass : sig
335311
type t = Parsing | Typing | Scheduling | Emit
336312
val of_string : string -> t option

0 commit comments

Comments
 (0)