annotate src/fftw-3.3.8/genfft/annotate.ml @ 169:223a55898ab9 tip default

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