Skip to content

Commit 216be99

Browse files
authored
flambda-backend: Fix flambda_o3 and flambda_oclassic attributes (#536)
* Fix flambda_o3 and flambda_oclassic attributes The implementations of the `flambda_o3` and `flambda_oclassic` attributes were calling into `Clflags`, but the flambda backend implements optimization flags by circumventing `Clflags` entirely. The only obvious solution is to add a hook to `Clflags` so we can simply override what `Clflags.set_o3 ()` does. Fortunately, doing it this way means that the other two ways of setting the flags - the command line and `OCAMLPARAM` - no longer need to do anything special. Tested manually using the included .ml source file. Note that `[@@@flambda_o3]` does nothing if `-Oclassic` is on. This actually reflects existing behavior: `-Oclassic -O3` and `-O3 -Oclassic` are, as far as I can tell, effectively the same as `-Oclassic`. * Make later -O flags completely override earlier ones Also document the test more thoroughly. * Don't have -O* clobber earlier options Now `-flambda2-cse-depth 7 -O3` will do the same thing as `-O3 -flambda2-cse-depth 7`. * Minimize the diff a bit * Install optimization-flag handler in native toplevel * Restore the behavior that -Oclassic implies -linscan * Add missing settings to O levels The explicit setting of `fallback_inlining_heuristic` to `false` in the old `o2_flags` was redundant, but it's easier to review this way. * Formatting
1 parent 4b56e07 commit 216be99

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

driver/compenv.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ let check_bool ppf name s =
193193
"bad value %s for %s" s name;
194194
false
195195

196+
let check_int ppf name s =
197+
match int_of_string s with
198+
| i -> Some i
199+
| exception _ ->
200+
Printf.ksprintf (print_error ppf)
201+
"bad value %s for %s" s name;
202+
None
203+
196204
let decode_compiler_pass ppf v ~name ~filter =
197205
let module P = Clflags.Compiler_pass in
198206
let passes = P.available_pass_names ~filter ~native:!native_code in

driver/compenv.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ val setter :
5454
Format.formatter -> (bool -> 'a) -> string -> 'a ref list -> string -> unit
5555
val int_setter : Format.formatter -> string -> int ref -> string -> unit
5656
val check_bool : Format.formatter -> string -> string -> bool
57+
val check_int : Format.formatter -> string -> string -> int option
5758

5859
(* [is_unit_name name] returns true only if [name] can be used as a
5960
correct module name *)

driver/main_args.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ let mk_inline_max_unroll f =
219219
let mk_classic_inlining f =
220220
"-Oclassic", Arg.Unit f, " Make inlining decisions at function definition \
221221
time rather than at the call site (replicates previous behaviour of the \
222-
compiler)"
222+
compiler). Implies -linscan (and causes -nolinscan to be ignored)."
223223
;;
224224

225225
let mk_inline_cost arg descr default f =

utils/clflags.ml

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -502,23 +502,42 @@ end
502502
let is_flambda2 () =
503503
Config.flambda2 && !native_code
504504

505-
let set_oclassic () =
506-
classic_inlining := true;
507-
default_simplify_rounds := 1;
508-
use_inlining_arguments_set classic_arguments;
509-
unbox_free_vars_of_closures := false;
510-
unbox_specialised_args := false
511-
512-
let set_o2 () =
513-
default_simplify_rounds := 2;
514-
use_inlining_arguments_set o2_arguments;
515-
use_inlining_arguments_set ~round:0 o1_arguments
516-
517-
let set_o3 () =
518-
default_simplify_rounds := 3;
519-
use_inlining_arguments_set o3_arguments;
520-
use_inlining_arguments_set ~round:1 o2_arguments;
521-
use_inlining_arguments_set ~round:0 o1_arguments
505+
module Opt_flag_handler = struct
506+
type t = {
507+
set_oclassic : unit -> unit;
508+
set_o2 : unit -> unit;
509+
set_o3 : unit -> unit;
510+
}
511+
512+
let default =
513+
let set_oclassic () =
514+
classic_inlining := true;
515+
default_simplify_rounds := 1;
516+
use_inlining_arguments_set classic_arguments;
517+
unbox_free_vars_of_closures := false;
518+
unbox_specialised_args := false
519+
in
520+
let set_o2 () =
521+
default_simplify_rounds := 2;
522+
use_inlining_arguments_set o2_arguments;
523+
use_inlining_arguments_set ~round:0 o1_arguments
524+
in
525+
let set_o3 () =
526+
default_simplify_rounds := 3;
527+
use_inlining_arguments_set o3_arguments;
528+
use_inlining_arguments_set ~round:1 o2_arguments;
529+
use_inlining_arguments_set ~round:0 o1_arguments
530+
in
531+
{ set_oclassic; set_o2; set_o3 }
532+
533+
let current = ref default
534+
535+
let set t = current := t
536+
end
537+
538+
let set_oclassic () = (!Opt_flag_handler.current).set_oclassic ()
539+
let set_o2 () = (!Opt_flag_handler.current).set_o2 ()
540+
let set_o3 () = (!Opt_flag_handler.current).set_o3 ()
522541

523542
(* This is used by the -stop-after option. *)
524543
module Compiler_pass = struct

utils/clflags.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ val unboxed_types : bool ref
229229
val insn_sched : bool ref
230230
val insn_sched_default : bool
231231

232+
module Opt_flag_handler : sig
233+
type t = {
234+
set_oclassic : unit -> unit;
235+
set_o2 : unit -> unit;
236+
set_o3 : unit -> unit;
237+
}
238+
239+
val default : t
240+
241+
val set : t -> unit
242+
end
243+
232244
val set_oclassic : unit -> unit
233245
val set_o2 : unit -> unit
234246
val set_o3 : unit -> unit

0 commit comments

Comments
 (0)