annotate src/fftw-3.3.5/genfft/annotate.ml @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents
children
rev   line source
cannam@127 1 (*
cannam@127 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
cannam@127 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
cannam@127 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
cannam@127 5 *
cannam@127 6 * This program is free software; you can redistribute it and/or modify
cannam@127 7 * it under the terms of the GNU General Public License as published by
cannam@127 8 * the Free Software Foundation; either version 2 of the License, or
cannam@127 9 * (at your option) any later version.
cannam@127 10 *
cannam@127 11 * This program is distributed in the hope that it will be useful,
cannam@127 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@127 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@127 14 * GNU General Public License for more details.
cannam@127 15 *
cannam@127 16 * You should have received a copy of the GNU General Public License
cannam@127 17 * along with this program; if not, write to the Free Software
cannam@127 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@127 19 *
cannam@127 20 *)
cannam@127 21
cannam@127 22 (* Here, we take a schedule (produced by schedule.ml) ordering a
cannam@127 23 sequence of instructions, and produce an annotated schedule. The
cannam@127 24 annotated schedule has the same ordering as the original schedule,
cannam@127 25 but is additionally partitioned into nested blocks of temporary
cannam@127 26 variables. The partitioning is computed via a heuristic algorithm.
cannam@127 27
cannam@127 28 The blocking allows the C code that we generate to consist of
cannam@127 29 nested blocks that help communicate variable lifetimes to the
cannam@127 30 compiler. *)
cannam@127 31
cannam@127 32 open Schedule
cannam@127 33 open Expr
cannam@127 34 open Variable
cannam@127 35
cannam@127 36 type annotated_schedule =
cannam@127 37 Annotate of variable list * variable list * variable list * int * aschedule
cannam@127 38 and aschedule =
cannam@127 39 ADone
cannam@127 40 | AInstr of assignment
cannam@127 41 | ASeq of (annotated_schedule * annotated_schedule)
cannam@127 42
cannam@127 43 let addelem a set = if not (List.memq a set) then a :: set else set
cannam@127 44 let union l =
cannam@127 45 let f x = addelem x (* let is source of polymorphism *)
cannam@127 46 in List.fold_right f l
cannam@127 47
cannam@127 48 (* set difference a - b *)
cannam@127 49 let diff a b = List.filter (fun x -> not (List.memq x b)) a
cannam@127 50
cannam@127 51 let rec minimize f = function
cannam@127 52 [] -> failwith "minimize"
cannam@127 53 | [n] -> n
cannam@127 54 | n :: rest ->
cannam@127 55 let x = minimize f rest in
cannam@127 56 if (f x) >= (f n) then n else x
cannam@127 57
cannam@127 58 (* find all variables used inside a scheduling unit *)
cannam@127 59 let rec find_block_vars = function
cannam@127 60 Done -> []
cannam@127 61 | (Instr (Assign (v, x))) -> v :: (find_vars x)
cannam@127 62 | Par a -> List.flatten (List.map find_block_vars a)
cannam@127 63 | Seq (a, b) -> (find_block_vars a) @ (find_block_vars b)
cannam@127 64
cannam@127 65 let uniq l =
cannam@127 66 List.fold_right (fun a b -> if List.memq a b then b else a :: b) l []
cannam@127 67
cannam@127 68 let has_related x = List.exists (Variable.same_class x)
cannam@127 69
cannam@127 70 let rec overlap a b = Util.count (fun y -> has_related y b) a
cannam@127 71
cannam@127 72 (* reorder a list of schedules so as to maximize overlap of variables *)
cannam@127 73 let reorder l =
cannam@127 74 let rec loop = function
cannam@127 75 [] -> []
cannam@127 76 | (a, va) :: b ->
cannam@127 77 let c =
cannam@127 78 List.map
cannam@127 79 (fun (a, x) -> ((a, x), (overlap va x, List.length x))) b in
cannam@127 80 let c' =
cannam@127 81 Sort.list
cannam@127 82 (fun (_, (a, la)) (_, (b, lb)) ->
cannam@127 83 la < lb || a > b)
cannam@127 84 c in
cannam@127 85 let b' = List.map (fun (a, _) -> a) c' in
cannam@127 86 a :: (loop b') in
cannam@127 87 let l' = List.map (fun x -> x, uniq (find_block_vars x)) l in
cannam@127 88 (* start with smallest block --- does this matter ? *)
cannam@127 89 match l' with
cannam@127 90 [] -> []
cannam@127 91 | _ ->
cannam@127 92 let m = minimize (fun (_, x) -> (List.length x)) l' in
cannam@127 93 let l'' = Util.remove m l' in
cannam@127 94 loop (m :: l'')
cannam@127 95
cannam@127 96 (* remove Par blocks *)
cannam@127 97 let rec linearize = function
cannam@127 98 | Seq (a, Done) -> linearize a
cannam@127 99 | Seq (Done, a) -> linearize a
cannam@127 100 | Seq (a, b) -> Seq (linearize a, linearize b)
cannam@127 101
cannam@127 102 (* try to balance nested Par blocks *)
cannam@127 103 | Par [a] -> linearize a
cannam@127 104 | Par l ->
cannam@127 105 let n2 = (List.length l) / 2 in
cannam@127 106 let rec loop n a b =
cannam@127 107 if n = 0 then
cannam@127 108 (List.rev b, a)
cannam@127 109 else
cannam@127 110 match a with
cannam@127 111 [] -> failwith "loop"
cannam@127 112 | x :: y -> loop (n - 1) y (x :: b)
cannam@127 113 in let (a, b) = loop n2 (reorder l) []
cannam@127 114 in linearize (Seq (Par a, Par b))
cannam@127 115
cannam@127 116 | x -> x
cannam@127 117
cannam@127 118 let subset a b =
cannam@127 119 List.for_all (fun x -> List.exists (fun y -> x == y) b) a
cannam@127 120
cannam@127 121 let use_same_vars (Assign (av, ax)) (Assign (bv, bx)) =
cannam@127 122 is_temporary av &&
cannam@127 123 is_temporary bv &&
cannam@127 124 (let va = Expr.find_vars ax and vb = Expr.find_vars bx in
cannam@127 125 subset va vb && subset vb va)
cannam@127 126
cannam@127 127 let store_to_same_class (Assign (av, ax)) (Assign (bv, bx)) =
cannam@127 128 is_locative av &&
cannam@127 129 is_locative bv &&
cannam@127 130 Variable.same_class av bv
cannam@127 131
cannam@127 132 let loads_from_same_class (Assign (av, ax)) (Assign (bv, bx)) =
cannam@127 133 match (ax, bx) with
cannam@127 134 | (Load a), (Load b) when
cannam@127 135 Variable.is_locative a && Variable.is_locative b
cannam@127 136 -> Variable.same_class a b
cannam@127 137 | _ -> false
cannam@127 138
cannam@127 139 (* extract instructions from schedule *)
cannam@127 140 let rec sched_to_ilist = function
cannam@127 141 | Done -> []
cannam@127 142 | Instr a -> [a]
cannam@127 143 | Seq (a, b) -> (sched_to_ilist a) @ (sched_to_ilist b)
cannam@127 144 | _ -> failwith "sched_to_ilist" (* Par blocks removed by linearize *)
cannam@127 145
cannam@127 146 let rec find_friends friendp insn friends foes = function
cannam@127 147 | [] -> (friends, foes)
cannam@127 148 | a :: b ->
cannam@127 149 if (a == insn) || (friendp a insn) then
cannam@127 150 find_friends friendp insn (a :: friends) foes b
cannam@127 151 else
cannam@127 152 find_friends friendp insn friends (a :: foes) b
cannam@127 153
cannam@127 154 (* schedule all instructions in the equivalence class determined
cannam@127 155 by friendp at the point where the last one
cannam@127 156 is executed *)
cannam@127 157 let rec delay_friends friendp sched =
cannam@127 158 let rec recur insns = function
cannam@127 159 | Done -> (Done, insns)
cannam@127 160 | Instr a ->
cannam@127 161 let (friends, foes) = find_friends friendp a [] [] insns in
cannam@127 162 (Schedule.sequentially friends), foes
cannam@127 163 | Seq (a, b) ->
cannam@127 164 let (b', insnsb) = recur insns b in
cannam@127 165 let (a', insnsa) = recur insnsb a in
cannam@127 166 (Seq (a', b')), insnsa
cannam@127 167 | _ -> failwith "delay_friends"
cannam@127 168 in match recur (sched_to_ilist sched) sched with
cannam@127 169 | (s, []) -> s (* assert that all insns have been used *)
cannam@127 170 | _ -> failwith "delay_friends"
cannam@127 171
cannam@127 172 (* schedule all instructions in the equivalence class determined
cannam@127 173 by friendp at the point where the first one
cannam@127 174 is executed *)
cannam@127 175 let rec anticipate_friends friendp sched =
cannam@127 176 let rec recur insns = function
cannam@127 177 | Done -> (Done, insns)
cannam@127 178 | Instr a ->
cannam@127 179 let (friends, foes) = find_friends friendp a [] [] insns in
cannam@127 180 (Schedule.sequentially friends), foes
cannam@127 181 | Seq (a, b) ->
cannam@127 182 let (a', insnsa) = recur insns a in
cannam@127 183 let (b', insnsb) = recur insnsa b in
cannam@127 184 (Seq (a', b')), insnsb
cannam@127 185 | _ -> failwith "anticipate_friends"
cannam@127 186 in match recur (sched_to_ilist sched) sched with
cannam@127 187 | (s, []) -> s (* assert that all insns have been used *)
cannam@127 188 | _ -> failwith "anticipate_friends"
cannam@127 189
cannam@127 190 let collect_buddy_stores buddy_list sched =
cannam@127 191 let rec recur sched delayed_stores = match sched with
cannam@127 192 | Done -> (sched, delayed_stores)
cannam@127 193 | Instr (Assign (v, x)) ->
cannam@127 194 begin
cannam@127 195 try
cannam@127 196 let buddies = List.find (List.memq v) buddy_list in
cannam@127 197 let tmp = Variable.make_temporary () in
cannam@127 198 let i = Seq(Instr (Assign (tmp, x)),
cannam@127 199 Instr (Assign (v, Times (NaN MULTI_A, Load tmp))))
cannam@127 200 and delayed_stores = (v, Load tmp) :: delayed_stores in
cannam@127 201 try
cannam@127 202 (Seq (i,
cannam@127 203 Instr (Assign
cannam@127 204 (List.hd buddies,
cannam@127 205 Times (NaN MULTI_B,
cannam@127 206 Plus (List.map
cannam@127 207 (fun buddy ->
cannam@127 208 List.assq buddy
cannam@127 209 delayed_stores)
cannam@127 210 buddies))) )))
cannam@127 211 , delayed_stores
cannam@127 212 with Not_found -> (i, delayed_stores)
cannam@127 213 with Not_found -> (sched, delayed_stores)
cannam@127 214 end
cannam@127 215 | Seq (a, b) ->
cannam@127 216 let (newa, delayed_stores) = recur a delayed_stores in
cannam@127 217 let (newb, delayed_stores) = recur b delayed_stores in
cannam@127 218 (Seq (newa, newb), delayed_stores)
cannam@127 219 | _ -> failwith "collect_buddy_stores"
cannam@127 220 in let (sched, _) = recur sched [] in
cannam@127 221 sched
cannam@127 222
cannam@127 223 let schedule_for_pipeline sched =
cannam@127 224 let update_readytimes t (Assign (v, _)) ready_times =
cannam@127 225 (v, (t + !Magic.pipeline_latency)) :: ready_times
cannam@127 226 and readyp t ready_times (Assign (_, x)) =
cannam@127 227 List.for_all
cannam@127 228 (fun var ->
cannam@127 229 try
cannam@127 230 (List.assq var ready_times) <= t
cannam@127 231 with Not_found -> false)
cannam@127 232 (List.filter Variable.is_temporary (Expr.find_vars x))
cannam@127 233 in
cannam@127 234 let rec recur sched t ready_times delayed_instructions =
cannam@127 235 let (ready, not_ready) =
cannam@127 236 List.partition (readyp t ready_times) delayed_instructions
cannam@127 237 in match ready with
cannam@127 238 | a :: b ->
cannam@127 239 let (sched, t, ready_times, delayed_instructions) =
cannam@127 240 recur sched (t+1) (update_readytimes t a ready_times)
cannam@127 241 (b @ not_ready)
cannam@127 242 in
cannam@127 243 (Seq (Instr a, sched)), t, ready_times, delayed_instructions
cannam@127 244 | _ -> (match sched with
cannam@127 245 | Done -> (sched, t, ready_times, delayed_instructions)
cannam@127 246 | Instr a ->
cannam@127 247 if (readyp t ready_times a) then
cannam@127 248 (sched, (t+1), (update_readytimes t a ready_times),
cannam@127 249 delayed_instructions)
cannam@127 250 else
cannam@127 251 (Done, t, ready_times, (a :: delayed_instructions))
cannam@127 252 | Seq (a, b) ->
cannam@127 253 let (a, t, ready_times, delayed_instructions) =
cannam@127 254 recur a t ready_times delayed_instructions
cannam@127 255 in
cannam@127 256 let (b, t, ready_times, delayed_instructions) =
cannam@127 257 recur b t ready_times delayed_instructions
cannam@127 258 in (Seq (a, b)), t, ready_times, delayed_instructions
cannam@127 259 | _ -> failwith "schedule_for_pipeline")
cannam@127 260 in let rec recur_until_done sched t ready_times delayed_instructions =
cannam@127 261 let (sched, t, ready_times, delayed_instructions) =
cannam@127 262 recur sched t ready_times delayed_instructions
cannam@127 263 in match delayed_instructions with
cannam@127 264 | [] -> sched
cannam@127 265 | _ ->
cannam@127 266 (Seq (sched,
cannam@127 267 (recur_until_done Done (t+1) ready_times
cannam@127 268 delayed_instructions)))
cannam@127 269 in recur_until_done sched 0 [] []
cannam@127 270
cannam@127 271 let rec rewrite_declarations force_declarations
cannam@127 272 (Annotate (_, _, declared, _, what)) =
cannam@127 273 let m = !Magic.number_of_variables in
cannam@127 274
cannam@127 275 let declare_it declared =
cannam@127 276 if (force_declarations || List.length declared >= m) then
cannam@127 277 ([], declared)
cannam@127 278 else
cannam@127 279 (declared, [])
cannam@127 280
cannam@127 281 in match what with
cannam@127 282 ADone -> Annotate ([], [], [], 0, what)
cannam@127 283 | AInstr i ->
cannam@127 284 let (u, d) = declare_it declared
cannam@127 285 in Annotate ([], u, d, 0, what)
cannam@127 286 | ASeq (a, b) ->
cannam@127 287 let ma = rewrite_declarations false a
cannam@127 288 and mb = rewrite_declarations false b
cannam@127 289 in let Annotate (_, ua, _, _, _) = ma
cannam@127 290 and Annotate (_, ub, _, _, _) = mb
cannam@127 291 in let (u, d) = declare_it (declared @ ua @ ub)
cannam@127 292 in Annotate ([], u, d, 0, ASeq (ma, mb))
cannam@127 293
cannam@127 294 let annotate list_of_buddy_stores schedule =
cannam@127 295 let rec analyze live_at_end = function
cannam@127 296 Done -> Annotate (live_at_end, [], [], 0, ADone)
cannam@127 297 | Instr i -> (match i with
cannam@127 298 Assign (v, x) ->
cannam@127 299 let vars = (find_vars x) in
cannam@127 300 Annotate (Util.remove v (union live_at_end vars), [v], [],
cannam@127 301 0, AInstr i))
cannam@127 302 | Seq (a, b) ->
cannam@127 303 let ab = analyze live_at_end b in
cannam@127 304 let Annotate (live_at_begin_b, defined_b, _, depth_a, _) = ab in
cannam@127 305 let aa = analyze live_at_begin_b a in
cannam@127 306 let Annotate (live_at_begin_a, defined_a, _, depth_b, _) = aa in
cannam@127 307 let defined = List.filter is_temporary (defined_a @ defined_b) in
cannam@127 308 let declarable = diff defined live_at_end in
cannam@127 309 let undeclarable = diff defined declarable
cannam@127 310 and maxdepth = max depth_a depth_b in
cannam@127 311 Annotate (live_at_begin_a, undeclarable, declarable,
cannam@127 312 List.length declarable + maxdepth,
cannam@127 313 ASeq (aa, ab))
cannam@127 314 | _ -> failwith "really_analyze"
cannam@127 315
cannam@127 316 in
cannam@127 317 let () = Util.info "begin annotate" in
cannam@127 318 let x = linearize schedule in
cannam@127 319
cannam@127 320 let x =
cannam@127 321 if (!Magic.schedule_for_pipeline && !Magic.pipeline_latency > 0) then
cannam@127 322 schedule_for_pipeline x
cannam@127 323 else
cannam@127 324 x
cannam@127 325 in
cannam@127 326
cannam@127 327 let x =
cannam@127 328 if !Magic.reorder_insns then
cannam@127 329 linearize(anticipate_friends use_same_vars x)
cannam@127 330 else
cannam@127 331 x
cannam@127 332 in
cannam@127 333
cannam@127 334 (* delay stores to the real and imaginary parts of the same number *)
cannam@127 335 let x =
cannam@127 336 if !Magic.reorder_stores then
cannam@127 337 linearize(delay_friends store_to_same_class x)
cannam@127 338 else
cannam@127 339 x
cannam@127 340 in
cannam@127 341
cannam@127 342 (* move loads of the real and imaginary parts of the same number *)
cannam@127 343 let x =
cannam@127 344 if !Magic.reorder_loads then
cannam@127 345 linearize(anticipate_friends loads_from_same_class x)
cannam@127 346 else
cannam@127 347 x
cannam@127 348 in
cannam@127 349
cannam@127 350 let x = collect_buddy_stores list_of_buddy_stores x in
cannam@127 351 let x = analyze [] x in
cannam@127 352 let res = rewrite_declarations true x in
cannam@127 353 let () = Util.info "end annotate" in
cannam@127 354 res
cannam@127 355
cannam@127 356 let rec dump print (Annotate (_, _, _, _, code)) =
cannam@127 357 dump_code print code
cannam@127 358 and dump_code print = function
cannam@127 359 | ADone -> ()
cannam@127 360 | AInstr x -> print ((assignment_to_string x) ^ "\n")
cannam@127 361 | ASeq (a, b) -> dump print a; dump print b