2011-05-02 16:43:32 +02:00
|
|
|
(** A scheduler that tries to minimize interference between variables, in
|
|
|
|
order to have a more efficient memory allocation. *)
|
|
|
|
open Idents
|
|
|
|
open Minils
|
|
|
|
open Mls_utils
|
|
|
|
open Misc
|
|
|
|
open Sgraph
|
|
|
|
|
|
|
|
|
|
|
|
let eq_clock eq =
|
2011-07-21 08:50:45 +02:00
|
|
|
eq.eq_rhs.e_base_ck
|
2011-05-02 16:43:32 +02:00
|
|
|
|
|
|
|
module Cost =
|
|
|
|
struct
|
2011-05-02 17:59:12 +02:00
|
|
|
open Interference_graph
|
|
|
|
open Interference
|
|
|
|
|
2011-10-20 16:52:50 +02:00
|
|
|
|
2011-05-02 16:43:32 +02:00
|
|
|
|
|
|
|
(** Remove from the elements the elements whose value is zero or negative. *)
|
|
|
|
let remove_null m =
|
|
|
|
let check_not_null k d m =
|
2011-05-02 17:59:12 +02:00
|
|
|
if d > 0 then IvarEnv.add k d m else m
|
2011-05-02 16:43:32 +02:00
|
|
|
in
|
2011-05-02 17:59:12 +02:00
|
|
|
IvarEnv.fold check_not_null m IvarEnv.empty
|
2011-05-02 16:43:32 +02:00
|
|
|
|
|
|
|
(** Returns the list of variables killed by an equation (ie vars
|
|
|
|
used by the equation and with use count equal to 1). *)
|
2011-05-02 17:59:12 +02:00
|
|
|
let killed_vars eq env =
|
|
|
|
let is_killed iv acc =
|
|
|
|
try
|
|
|
|
if IvarEnv.find iv env = 1 then acc + 1 else acc
|
|
|
|
with
|
2011-07-21 08:50:45 +02:00
|
|
|
| Not_found ->
|
|
|
|
Format.printf "Var not found in kill_vars %s@." (ivar_to_string iv); assert false
|
2011-05-02 16:43:32 +02:00
|
|
|
in
|
2011-05-02 17:59:12 +02:00
|
|
|
IvarSet.fold is_killed (all_ivars_set (InterfRead.read eq)) 0
|
2011-05-02 16:43:32 +02:00
|
|
|
|
|
|
|
(** Initialize the costs data structure. *)
|
2011-05-02 17:59:12 +02:00
|
|
|
let init_cost uses inputs =
|
|
|
|
let env = IvarSet.fold (add_uses uses) !World.memories IvarEnv.empty in
|
|
|
|
let inputs = List.map (fun vd -> Ivar vd.v_ident) inputs in
|
|
|
|
List.fold_left (fun env iv -> add_uses uses iv env) env inputs
|
2011-05-02 16:43:32 +02:00
|
|
|
|
2011-05-02 17:59:12 +02:00
|
|
|
(** [update_cost eq uses env] updates the costs data structure
|
2011-05-02 16:43:32 +02:00
|
|
|
after eq has been chosen as the next equation to be scheduled.
|
|
|
|
It updates uses and adds the new variables defined by this equation.
|
|
|
|
*)
|
2011-05-02 17:59:12 +02:00
|
|
|
let update_cost eq uses env =
|
|
|
|
let env = IvarSet.fold decr_uses (all_ivars_set (InterfRead.read eq)) env in
|
|
|
|
IvarSet.fold (add_uses uses) (InterfRead.def eq) env
|
2011-05-02 16:43:32 +02:00
|
|
|
|
|
|
|
(** Returns the next equation, chosen from the list of equations rem_eqs *)
|
2011-05-02 17:59:12 +02:00
|
|
|
let next_equation rem_eqs ck env =
|
2011-11-21 01:38:08 +01:00
|
|
|
let bonus eq = match eq.eq_rhs.e_desc with
|
|
|
|
| Eapp ({a_op = (Eupdate _ | Efield_update _) },_,_) -> 1
|
|
|
|
| _ -> 0
|
|
|
|
in
|
2011-10-20 16:52:50 +02:00
|
|
|
let cost eq =
|
|
|
|
let nb_killed_vars = killed_vars eq env in
|
|
|
|
let nb_def_vars = IvarSet.cardinal (all_ivars_set (InterfRead.def eq)) in
|
2011-11-21 01:38:08 +01:00
|
|
|
let b = bonus eq in
|
|
|
|
if verbose_mode then
|
|
|
|
Format.eprintf "(%d,%d,%d)%a@." nb_killed_vars nb_def_vars b Mls_printer.print_eq eq;
|
|
|
|
nb_def_vars - nb_killed_vars + b
|
|
|
|
|
2011-10-20 16:52:50 +02:00
|
|
|
in
|
|
|
|
let eqs_wcost = List.map (fun eq -> (eq, cost eq)) rem_eqs in
|
|
|
|
let compare_eqs_wcost (_,c1) (_,c2) = compare c1 c2 in
|
|
|
|
let sorted_eqs_wcost = List.stable_sort compare_eqs_wcost eqs_wcost in
|
|
|
|
let rec min_same_ck sorted_eqs = match sorted_eqs with
|
|
|
|
| [] -> Misc.internal_error "no next equation to schedule"
|
|
|
|
| [(eq,_)] -> eq
|
|
|
|
| (eq1,c1)::(eq2,c2)::l ->
|
|
|
|
if (c2 > c1) || (Clocks.same_control (eq_clock eq1) ck)
|
|
|
|
then eq1 (* choosen since either the last with min cost or min and right clock *)
|
|
|
|
else min_same_ck ((eq2,c2)::l)
|
|
|
|
in
|
|
|
|
min_same_ck sorted_eqs_wcost
|
2011-05-02 16:43:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
(** Returns the list of 'free' nodes in the dependency graph (nodes without
|
|
|
|
predecessors). *)
|
|
|
|
let free_eqs node_list =
|
|
|
|
let is_free n =
|
|
|
|
(List.length n.g_depends_on) = 0
|
|
|
|
in
|
|
|
|
List.map (fun n -> n.g_containt) (List.filter is_free node_list)
|
|
|
|
|
|
|
|
let rec node_for_eq eq nodes_list =
|
|
|
|
match nodes_list with
|
|
|
|
| [] -> raise Not_found
|
|
|
|
| n::nodes_list ->
|
|
|
|
if eq = n.g_containt then
|
|
|
|
n
|
|
|
|
else
|
|
|
|
node_for_eq eq nodes_list
|
|
|
|
|
|
|
|
(** Remove an equation from the dependency graph. All the edges to
|
|
|
|
other nodes are removed. *)
|
|
|
|
let remove_eq eq node_list =
|
|
|
|
let n = node_for_eq eq node_list in
|
|
|
|
List.iter (remove_depends n) n.g_depends_on;
|
|
|
|
List.iter (fun n2 -> remove_depends n2 n) n.g_depends_by;
|
|
|
|
List.filter (fun n2 -> n.g_tag <> n2.g_tag) node_list
|
|
|
|
|
|
|
|
(** Main function to schedule a node. *)
|
|
|
|
let schedule eq_list inputs node_list =
|
2011-05-02 17:59:12 +02:00
|
|
|
let uses = Interference.compute_uses eq_list in
|
2011-11-21 01:38:08 +01:00
|
|
|
Interference.print_debug_ivar_env "uses" uses;
|
2011-05-02 16:43:32 +02:00
|
|
|
let rec schedule_aux rem_eqs sched_eqs node_list ck costs =
|
|
|
|
match rem_eqs with
|
2011-10-17 18:10:38 +02:00
|
|
|
| [] ->
|
|
|
|
if List.length node_list <> 0 then
|
|
|
|
Misc.internal_error "Node is unschedulable";
|
|
|
|
sched_eqs
|
2011-05-02 16:43:32 +02:00
|
|
|
| _ ->
|
|
|
|
(* First choose the next equation to schedule depending on costs*)
|
|
|
|
let eq = Cost.next_equation rem_eqs ck costs in
|
|
|
|
(* remove it from the dependency graph *)
|
|
|
|
let node_list = remove_eq eq node_list in
|
|
|
|
(* update the list of equations ready to be scheduled *)
|
|
|
|
let rem_eqs = free_eqs node_list in
|
|
|
|
(* compute new costs for the next step *)
|
2011-05-02 17:59:12 +02:00
|
|
|
let costs = Cost.update_cost eq uses costs in
|
2011-05-02 16:43:32 +02:00
|
|
|
schedule_aux rem_eqs (eq::sched_eqs) node_list (eq_clock eq) costs
|
|
|
|
in
|
2011-05-02 17:59:12 +02:00
|
|
|
let costs = Cost.init_cost uses inputs in
|
2011-05-02 16:43:32 +02:00
|
|
|
let rem_eqs = free_eqs node_list in
|
|
|
|
List.rev (schedule_aux rem_eqs [] node_list Clocks.Cbase costs)
|
|
|
|
|
|
|
|
let schedule_contract c =
|
|
|
|
c
|
|
|
|
|
|
|
|
let node _ () f =
|
2011-05-02 17:59:12 +02:00
|
|
|
Interference.World.init f;
|
2011-05-02 16:43:32 +02:00
|
|
|
let contract = optional schedule_contract f.n_contract in
|
|
|
|
let node_list, _ = DataFlowDep.build f.n_equs in
|
2011-05-02 17:59:12 +02:00
|
|
|
let f = { f with n_equs = schedule f.n_equs f.n_input node_list; n_contract = contract } in
|
2011-05-02 16:43:32 +02:00
|
|
|
f, ()
|
|
|
|
|
|
|
|
let program p =
|
|
|
|
let funs = { Mls_mapfold.defaults with Mls_mapfold.node_dec = node } in
|
|
|
|
let p, () = Mls_mapfold.program_it funs () p in
|
|
|
|
p
|