annotate src/fftw-3.3.5/genfft/to_alist.ml @ 168:ceec0dd9ec9c

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 07 Feb 2020 11:51:13 +0000
parents 7867fa7e1b6b
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 (*************************************************************
cannam@127 23 * Conversion of the dag to an assignment list
cannam@127 24 *************************************************************)
cannam@127 25 (*
cannam@127 26 * This function is messy. The main problem is that we want to
cannam@127 27 * inline dag nodes conditionally, depending on how many times they
cannam@127 28 * are used. The Right Thing to do would be to modify the
cannam@127 29 * state monad to propagate some of the state backwards, so that
cannam@127 30 * we know whether a given node will be used again in the future.
cannam@127 31 * This modification is trivial in a lazy language, but it is
cannam@127 32 * messy in a strict language like ML.
cannam@127 33 *
cannam@127 34 * In this implementation, we just do the obvious thing, i.e., visit
cannam@127 35 * the dag twice, the first to count the node usages, and the second to
cannam@127 36 * produce the output.
cannam@127 37 *)
cannam@127 38
cannam@127 39 open Monads.StateMonad
cannam@127 40 open Monads.MemoMonad
cannam@127 41 open Expr
cannam@127 42
cannam@127 43 let fresh = Variable.make_temporary
cannam@127 44 let node_insert x = Assoctable.insert Expr.hash x
cannam@127 45 let node_lookup x = Assoctable.lookup Expr.hash (==) x
cannam@127 46 let empty = Assoctable.empty
cannam@127 47
cannam@127 48 let fetchAl =
cannam@127 49 fetchState >>= (fun (al, _, _) -> returnM al)
cannam@127 50
cannam@127 51 let storeAl al =
cannam@127 52 fetchState >>= (fun (_, visited, visited') ->
cannam@127 53 storeState (al, visited, visited'))
cannam@127 54
cannam@127 55 let fetchVisited = fetchState >>= (fun (_, v, _) -> returnM v)
cannam@127 56
cannam@127 57 let storeVisited visited =
cannam@127 58 fetchState >>= (fun (al, _, visited') ->
cannam@127 59 storeState (al, visited, visited'))
cannam@127 60
cannam@127 61 let fetchVisited' = fetchState >>= (fun (_, _, v') -> returnM v')
cannam@127 62 let storeVisited' visited' =
cannam@127 63 fetchState >>= (fun (al, visited, _) ->
cannam@127 64 storeState (al, visited, visited'))
cannam@127 65 let lookupVisitedM' key =
cannam@127 66 fetchVisited' >>= fun table ->
cannam@127 67 returnM (node_lookup key table)
cannam@127 68 let insertVisitedM' key value =
cannam@127 69 fetchVisited' >>= fun table ->
cannam@127 70 storeVisited' (node_insert key value table)
cannam@127 71
cannam@127 72 let counting f x =
cannam@127 73 fetchVisited >>= (fun v ->
cannam@127 74 match node_lookup x v with
cannam@127 75 Some count ->
cannam@127 76 let incr_cnt =
cannam@127 77 fetchVisited >>= (fun v' ->
cannam@127 78 storeVisited (node_insert x (count + 1) v'))
cannam@127 79 in
cannam@127 80 begin
cannam@127 81 match x with
cannam@127 82 (* Uminus is always inlined. Visit child *)
cannam@127 83 Uminus y -> f y >> incr_cnt
cannam@127 84 | _ -> incr_cnt
cannam@127 85 end
cannam@127 86 | None ->
cannam@127 87 f x >> fetchVisited >>= (fun v' ->
cannam@127 88 storeVisited (node_insert x 1 v')))
cannam@127 89
cannam@127 90 let with_varM v x =
cannam@127 91 fetchAl >>= (fun al -> storeAl ((v, x) :: al)) >> returnM (Load v)
cannam@127 92
cannam@127 93 let inlineM = returnM
cannam@127 94
cannam@127 95 let with_tempM x = match x with
cannam@127 96 | Load v when Variable.is_temporary v -> inlineM x (* avoid trivial moves *)
cannam@127 97 | _ -> with_varM (fresh ()) x
cannam@127 98
cannam@127 99 (* declare a temporary only if node is used more than once *)
cannam@127 100 let with_temp_maybeM node x =
cannam@127 101 fetchVisited >>= (fun v ->
cannam@127 102 match node_lookup node v with
cannam@127 103 Some count ->
cannam@127 104 if (count = 1 && !Magic.inline_single) then
cannam@127 105 inlineM x
cannam@127 106 else
cannam@127 107 with_tempM x
cannam@127 108 | None ->
cannam@127 109 failwith "with_temp_maybeM")
cannam@127 110 type fma =
cannam@127 111 NO_FMA
cannam@127 112 | FMA of expr * expr * expr (* FMA (a, b, c) => a + b * c *)
cannam@127 113 | FMS of expr * expr * expr (* FMS (a, b, c) => -a + b * c *)
cannam@127 114 | FNMS of expr * expr * expr (* FNMS (a, b, c) => a - b * c *)
cannam@127 115
cannam@127 116 let good_for_fma (a, b) =
cannam@127 117 let good = function
cannam@127 118 | NaN I -> true
cannam@127 119 | NaN CONJ -> true
cannam@127 120 | NaN _ -> false
cannam@127 121 | Times(NaN _, _) -> false
cannam@127 122 | Times(_, NaN _) -> false
cannam@127 123 | _ -> true
cannam@127 124 in good a && good b
cannam@127 125
cannam@127 126 let build_fma l =
cannam@127 127 if (not !Magic.enable_fma) then NO_FMA
cannam@127 128 else match l with
cannam@127 129 | [a; Uminus (Times (b, c))] when good_for_fma (b, c) -> FNMS (a, b, c)
cannam@127 130 | [Uminus (Times (b, c)); a] when good_for_fma (b, c) -> FNMS (a, b, c)
cannam@127 131 | [Uminus a; Times (b, c)] when good_for_fma (b, c) -> FMS (a, b, c)
cannam@127 132 | [Times (b, c); Uminus a] when good_for_fma (b, c) -> FMS (a, b, c)
cannam@127 133 | [a; Times (b, c)] when good_for_fma (b, c) -> FMA (a, b, c)
cannam@127 134 | [Times (b, c); a] when good_for_fma (b, c) -> FMA (a, b, c)
cannam@127 135 | _ -> NO_FMA
cannam@127 136
cannam@127 137 let children_fma l = match build_fma l with
cannam@127 138 | FMA (a, b, c) -> Some (a, b, c)
cannam@127 139 | FMS (a, b, c) -> Some (a, b, c)
cannam@127 140 | FNMS (a, b, c) -> Some (a, b, c)
cannam@127 141 | NO_FMA -> None
cannam@127 142
cannam@127 143
cannam@127 144 let rec visitM x =
cannam@127 145 counting (function
cannam@127 146 | Load v -> returnM ()
cannam@127 147 | Num a -> returnM ()
cannam@127 148 | NaN a -> returnM ()
cannam@127 149 | Store (v, x) -> visitM x
cannam@127 150 | Plus a -> (match children_fma a with
cannam@127 151 None -> mapM visitM a >> returnM ()
cannam@127 152 | Some (a, b, c) ->
cannam@127 153 (* visit fma's arguments twice to make sure they are not inlined *)
cannam@127 154 visitM a >> visitM a >>
cannam@127 155 visitM b >> visitM b >>
cannam@127 156 visitM c >> visitM c)
cannam@127 157 | Times (a, b) -> visitM a >> visitM b
cannam@127 158 | CTimes (a, b) -> visitM a >> visitM b
cannam@127 159 | CTimesJ (a, b) -> visitM a >> visitM b
cannam@127 160 | Uminus a -> visitM a)
cannam@127 161 x
cannam@127 162
cannam@127 163 let visit_rootsM = mapM visitM
cannam@127 164
cannam@127 165
cannam@127 166 let rec expr_of_nodeM x =
cannam@127 167 memoizing lookupVisitedM' insertVisitedM'
cannam@127 168 (function x -> match x with
cannam@127 169 | Load v ->
cannam@127 170 if (Variable.is_temporary v) then
cannam@127 171 inlineM (Load v)
cannam@127 172 else if (Variable.is_locative v && !Magic.inline_loads) then
cannam@127 173 inlineM (Load v)
cannam@127 174 else if (Variable.is_constant v && !Magic.inline_loads_constants) then
cannam@127 175 inlineM (Load v)
cannam@127 176 else
cannam@127 177 with_tempM (Load v)
cannam@127 178 | Num a ->
cannam@127 179 if !Magic.inline_constants then
cannam@127 180 inlineM (Num a)
cannam@127 181 else
cannam@127 182 with_temp_maybeM x (Num a)
cannam@127 183 | NaN a -> inlineM (NaN a)
cannam@127 184 | Store (v, x) ->
cannam@127 185 expr_of_nodeM x >>=
cannam@127 186 (if !Magic.trivial_stores then with_tempM else inlineM) >>=
cannam@127 187 with_varM v
cannam@127 188
cannam@127 189 | Plus a ->
cannam@127 190 begin
cannam@127 191 match build_fma a with
cannam@127 192 FMA (a, b, c) ->
cannam@127 193 expr_of_nodeM a >>= fun a' ->
cannam@127 194 expr_of_nodeM b >>= fun b' ->
cannam@127 195 expr_of_nodeM c >>= fun c' ->
cannam@127 196 with_temp_maybeM x (Plus [a'; Times (b', c')])
cannam@127 197 | FMS (a, b, c) ->
cannam@127 198 expr_of_nodeM a >>= fun a' ->
cannam@127 199 expr_of_nodeM b >>= fun b' ->
cannam@127 200 expr_of_nodeM c >>= fun c' ->
cannam@127 201 with_temp_maybeM x
cannam@127 202 (Plus [Times (b', c'); Uminus a'])
cannam@127 203 | FNMS (a, b, c) ->
cannam@127 204 expr_of_nodeM a >>= fun a' ->
cannam@127 205 expr_of_nodeM b >>= fun b' ->
cannam@127 206 expr_of_nodeM c >>= fun c' ->
cannam@127 207 with_temp_maybeM x
cannam@127 208 (Plus [a'; Uminus (Times (b', c'))])
cannam@127 209 | NO_FMA ->
cannam@127 210 mapM expr_of_nodeM a >>= fun a' ->
cannam@127 211 with_temp_maybeM x (Plus a')
cannam@127 212 end
cannam@127 213 | CTimes (Load _ as a, b) when !Magic.generate_bytw ->
cannam@127 214 expr_of_nodeM b >>= fun b' ->
cannam@127 215 with_tempM (CTimes (a, b'))
cannam@127 216 | CTimes (a, b) ->
cannam@127 217 expr_of_nodeM a >>= fun a' ->
cannam@127 218 expr_of_nodeM b >>= fun b' ->
cannam@127 219 with_tempM (CTimes (a', b'))
cannam@127 220 | CTimesJ (Load _ as a, b) when !Magic.generate_bytw ->
cannam@127 221 expr_of_nodeM b >>= fun b' ->
cannam@127 222 with_tempM (CTimesJ (a, b'))
cannam@127 223 | CTimesJ (a, b) ->
cannam@127 224 expr_of_nodeM a >>= fun a' ->
cannam@127 225 expr_of_nodeM b >>= fun b' ->
cannam@127 226 with_tempM (CTimesJ (a', b'))
cannam@127 227 | Times (a, b) ->
cannam@127 228 expr_of_nodeM a >>= fun a' ->
cannam@127 229 expr_of_nodeM b >>= fun b' ->
cannam@127 230 begin
cannam@127 231 match a' with
cannam@127 232 Num a'' when !Magic.strength_reduce_mul && Number.is_two a'' ->
cannam@127 233 (inlineM b' >>= fun b'' ->
cannam@127 234 with_temp_maybeM x (Plus [b''; b'']))
cannam@127 235 | _ -> with_temp_maybeM x (Times (a', b'))
cannam@127 236 end
cannam@127 237 | Uminus a ->
cannam@127 238 expr_of_nodeM a >>= fun a' ->
cannam@127 239 inlineM (Uminus a'))
cannam@127 240 x
cannam@127 241
cannam@127 242 let expr_of_rootsM = mapM expr_of_nodeM
cannam@127 243
cannam@127 244 let peek_alistM roots =
cannam@127 245 visit_rootsM roots >> expr_of_rootsM roots >> fetchAl
cannam@127 246
cannam@127 247 let wrap_assign (a, b) = Expr.Assign (a, b)
cannam@127 248
cannam@127 249 let to_assignments dag =
cannam@127 250 let () = Util.info "begin to_alist" in
cannam@127 251 let al = List.rev (runM ([], empty, empty) peek_alistM dag) in
cannam@127 252 let res = List.map wrap_assign al in
cannam@127 253 let () = Util.info "end to_alist" in
cannam@127 254 res
cannam@127 255
cannam@127 256
cannam@127 257 (* dump alist in `dot' format *)
cannam@127 258 let dump print alist =
cannam@127 259 let vs v = "\"" ^ (Variable.unparse v) ^ "\"" in
cannam@127 260 begin
cannam@127 261 print "digraph G {\n";
cannam@127 262 print "\tsize=\"6,6\";\n";
cannam@127 263
cannam@127 264 (* all input nodes have the same rank *)
cannam@127 265 print "{ rank = same;\n";
cannam@127 266 List.iter (fun (Expr.Assign (v, x)) ->
cannam@127 267 List.iter (fun y ->
cannam@127 268 if (Variable.is_locative y) then print("\t" ^ (vs y) ^ ";\n"))
cannam@127 269 (Expr.find_vars x))
cannam@127 270 alist;
cannam@127 271 print "}\n";
cannam@127 272
cannam@127 273 (* all output nodes have the same rank *)
cannam@127 274 print "{ rank = same;\n";
cannam@127 275 List.iter (fun (Expr.Assign (v, x)) ->
cannam@127 276 if (Variable.is_locative v) then print("\t" ^ (vs v) ^ ";\n"))
cannam@127 277 alist;
cannam@127 278 print "}\n";
cannam@127 279
cannam@127 280 (* edges *)
cannam@127 281 List.iter (fun (Expr.Assign (v, x)) ->
cannam@127 282 List.iter (fun y -> print("\t" ^ (vs y) ^ " -> " ^ (vs v) ^ ";\n"))
cannam@127 283 (Expr.find_vars x))
cannam@127 284 alist;
cannam@127 285
cannam@127 286 print "}\n";
cannam@127 287 end
cannam@127 288