@@ -5,6 +5,8 @@ module DLL = Flambda_backend_utils.Doubly_linked_list
55
66let gi_debug = true
77
8+ let gi_rng = Random.State. make [| 4 ; 6 ; 2 |]
9+
810let bool_of_param param_name =
911 bool_of_param ~guard: (gi_debug, " gi_debug" ) param_name
1012
@@ -43,11 +45,17 @@ let log_cfg_with_infos : indent:int -> Cfg_with_infos.t -> unit =
4345
4446(* CR xclerc for xclerc: add more heuristics *)
4547module Priority_heuristics = struct
46- type t = Interval_length
48+ type t =
49+ | Interval_length
50+ | Random_for_testing
4751
48- let all = [Interval_length ]
52+ let all = [Interval_length ; Random_for_testing ]
53+
54+ let to_string = function
55+ | Interval_length -> " interval_length"
56+ | Random_for_testing -> " random"
4957
50- let to_string = function Interval_length -> " interval_length "
58+ let random () = Random.State. int gi_rng 10_000
5159
5260 let value =
5361 let available_heuristics () =
@@ -64,6 +72,7 @@ module Priority_heuristics = struct
6472 | Some id -> (
6573 match String. lowercase_ascii id with
6674 | "interval_length" | "interval-length" -> Interval_length
75+ | "random" -> Random_for_testing
6776 | _ ->
6877 fatal " unknown heuristics %S (possible values: %s)" id
6978 (available_heuristics () )))
@@ -75,13 +84,24 @@ module Selection_heuristics = struct
7584 | First_available
7685 | Best_fit
7786 | Worst_fit
87+ | Random_for_testing
7888
79- let all = [First_available ; Best_fit ; Worst_fit ]
89+ let all = [First_available ; Best_fit ; Worst_fit ; Random_for_testing ]
8090
8191 let to_string = function
8292 | First_available -> " first_available"
8393 | Best_fit -> " best_fit"
8494 | Worst_fit -> " worst_fit"
95+ | Random_for_testing -> " random"
96+
97+ let include_in_random = function
98+ | Random_for_testing | Worst_fit -> false
99+ | First_available | Best_fit -> true
100+
101+ let random =
102+ let all = List. filter all ~f: include_in_random in
103+ let len = List. length all in
104+ fun () -> List. nth all (Random.State. int gi_rng len)
85105
86106 let value =
87107 let available_heuristics () =
@@ -100,6 +120,7 @@ module Selection_heuristics = struct
100120 | "first_available" | "first-available" -> First_available
101121 | "best_fit" | "best-fit" -> Best_fit
102122 | "worst_fit" | "worst-fit" -> Worst_fit
123+ | "random" -> Random_for_testing
103124 | _ ->
104125 fatal " unknown heuristics %S (possible values: %s)" id
105126 (available_heuristics () )))
@@ -109,12 +130,16 @@ module Spilling_heuristics = struct
109130 type t =
110131 | Flat_uses
111132 | Hierarchical_uses
133+ | Random_for_testing
112134
113- let all = [Flat_uses ; Hierarchical_uses ]
135+ let all = [Flat_uses ; Hierarchical_uses ; Random_for_testing ]
114136
115137 let to_string = function
116138 | Flat_uses -> " flat_uses"
117139 | Hierarchical_uses -> " hierarchical_uses"
140+ | Random_for_testing -> " random"
141+
142+ let random () = Random.State. bool gi_rng
118143
119144 let value =
120145 let available_heuristics () =
@@ -132,6 +157,7 @@ module Spilling_heuristics = struct
132157 match String. lowercase_ascii id with
133158 | "flat_uses" | "flat-uses" -> Flat_uses
134159 | "hierarchical_uses" | "hierarchical-uses" -> Hierarchical_uses
160+ | "random" -> Random_for_testing
135161 | _ ->
136162 fatal " unknown heuristics %S (possible values: %s)" id
137163 (available_heuristics () )))
@@ -705,7 +731,14 @@ module Hardware_registers = struct
705731 let find_available : t -> Reg.t -> Interval.t -> available =
706732 fun t reg interval ->
707733 let with_no_overlap =
708- match Lazy. force Selection_heuristics. value with
734+ let heuristic =
735+ match Lazy. force Selection_heuristics. value with
736+ | Selection_heuristics. Random_for_testing ->
737+ Selection_heuristics. random ()
738+ | heuristic -> heuristic
739+ in
740+ match heuristic with
741+ | Selection_heuristics. Random_for_testing -> assert false
709742 | Selection_heuristics. First_available ->
710743 if gi_debug
711744 then
0 commit comments