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