Skip to content

Commit 79633d2

Browse files
authored
Tweak condition forcing linscan (register allocation) (ocaml-flambda#3913)
1 parent 4b42aa3 commit 79633d2

5 files changed

+24
-3
lines changed

asmcomp/asmgen.ml

+10-3
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,15 @@ let write_ir prefix =
156156

157157
let should_emit () = not (should_stop_after Compiler_pass.Linearization)
158158

159-
let should_use_linscan fun_codegen_options =
160-
!use_linscan || List.mem Cmm.Use_linscan_regalloc fun_codegen_options
159+
(* note: `should_use_linscan` relies on the state of the `Reg` module, as the
160+
list of temporaries is retrieved to be compared to the threshold. *)
161+
let should_use_linscan fd =
162+
!use_linscan
163+
|| List.mem Cmm.Use_linscan_regalloc fd.fun_codegen_options
164+
|| List.compare_length_with
165+
(Reg.all_relocatable_regs ())
166+
!Flambda_backend_flags.regalloc_linscan_threshold
167+
> 0
161168

162169
let if_emit_do f x = if should_emit () then f x else ()
163170

@@ -290,7 +297,7 @@ type register_allocator =
290297

291298
let register_allocator fd : register_allocator =
292299
match String.lowercase_ascii !Flambda_backend_flags.regalloc with
293-
| "" | "cfg" -> if should_use_linscan fd.fun_codegen_options then LS else IRC
300+
| "" | "cfg" -> if should_use_linscan fd then LS else IRC
294301
| "gi" -> GI
295302
| "irc" -> IRC
296303
| "ls" -> LS

driver/flambda_backend_args.ml

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ let mk_dcfg_invariants f =
4242
let mk_regalloc f =
4343
"-regalloc", Arg.String f, " Select the register allocator"
4444

45+
let mk_regalloc_linscan_threshold f =
46+
"-regalloc-linscan-threshold",
47+
Arg.Int f,
48+
(Printf.sprintf " Use linscan on functions with more temporaries than the threshold (default is %d)"Flambda_backend_flags.default_regalloc_linscan_threshold)
49+
4550
let mk_regalloc_param f =
4651
"-regalloc-param", Arg.String f, " Pass a parameter to the register allocator"
4752

@@ -725,6 +730,7 @@ module type Flambda_backend_options = sig
725730
val dcfg : unit -> unit
726731
val dcfg_invariants : unit -> unit
727732
val regalloc : string -> unit
733+
val regalloc_linscan_threshold : int -> unit
728734
val regalloc_param : string -> unit
729735
val regalloc_validate : unit -> unit
730736
val no_regalloc_validate : unit -> unit
@@ -860,6 +866,7 @@ struct
860866
mk_dcfg F.dcfg;
861867
mk_dcfg_invariants F.dcfg_invariants;
862868
mk_regalloc F.regalloc;
869+
mk_regalloc_linscan_threshold F.regalloc_linscan_threshold;
863870
mk_regalloc_param F.regalloc_param;
864871
mk_regalloc_validate F.regalloc_validate;
865872
mk_no_regalloc_validate F.no_regalloc_validate;
@@ -1026,6 +1033,7 @@ module Flambda_backend_options_impl = struct
10261033
let dcfg = set' Flambda_backend_flags.dump_cfg
10271034
let dcfg_invariants = set' Flambda_backend_flags.cfg_invariants
10281035
let regalloc x = Flambda_backend_flags.regalloc := x
1036+
let regalloc_linscan_threshold x = Flambda_backend_flags.regalloc_linscan_threshold := x
10291037
let regalloc_param x = Flambda_backend_flags.regalloc_params := x :: !Flambda_backend_flags.regalloc_params
10301038
let regalloc_validate = set' Flambda_backend_flags.regalloc_validate
10311039
let no_regalloc_validate = clear' Flambda_backend_flags.regalloc_validate
@@ -1378,6 +1386,7 @@ module Extra_params = struct
13781386
| "ocamlcfg" -> set' Flambda_backend_flags.use_ocamlcfg
13791387
| "cfg-invariants" -> set' Flambda_backend_flags.cfg_invariants
13801388
| "regalloc" -> set_string Flambda_backend_flags.regalloc
1389+
| "regalloc-linscan-threshold" -> set_int' Flambda_backend_flags.regalloc_linscan_threshold
13811390
| "regalloc-param" -> add_string Flambda_backend_flags.regalloc_params
13821391
| "regalloc-validate" -> set' Flambda_backend_flags.regalloc_validate
13831392
| "vectorize" -> set' Flambda_backend_flags.vectorize

driver/flambda_backend_args.mli

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module type Flambda_backend_options = sig
2929
val dcfg : unit -> unit
3030
val dcfg_invariants : unit -> unit
3131
val regalloc : string -> unit
32+
val regalloc_linscan_threshold : int -> unit
3233
val regalloc_param : string -> unit
3334
val regalloc_validate : unit -> unit
3435
val no_regalloc_validate : unit -> unit

driver/flambda_backend_flags.ml

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ let use_ocamlcfg = ref true (* -[no-]ocamlcfg *)
1717
let dump_cfg = ref false (* -dcfg *)
1818
let cfg_invariants = ref false (* -dcfg-invariants *)
1919
let regalloc = ref "" (* -regalloc *)
20+
let default_regalloc_linscan_threshold = 100_000
21+
let regalloc_linscan_threshold = ref max_int (* -regalloc-linscan-threshold *)
2022
let regalloc_params = ref ([] : string list) (* -regalloc-param *)
2123
let regalloc_validate = ref true (* -[no-]regalloc-validate *)
2224

driver/flambda_backend_flags.mli

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ val use_ocamlcfg : bool ref
1818
val dump_cfg : bool ref
1919
val cfg_invariants : bool ref
2020
val regalloc : string ref
21+
val default_regalloc_linscan_threshold : int
22+
val regalloc_linscan_threshold : int ref
2123
val regalloc_params : string list ref
2224
val regalloc_validate : bool ref
2325

0 commit comments

Comments
 (0)