annotate src/especialize.sml @ 1079:d069b193ed6b

Especialize uses a termination measure based on number of arguments introduced
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Dec 2009 19:26:52 -0500
parents b9321bcefb42
children a4979e31e4bf
rev   line source
adamc@443 1 (* Copyright (c) 2008, Adam Chlipala
adamc@443 2 * All rights reserved.
adamc@443 3 *
adamc@443 4 * Redistribution and use in source and binary forms, with or without
adamc@443 5 * modification, are permitted provided that the following conditions are met:
adamc@443 6 *
adamc@443 7 * - Redistributions of source code must retain the above copyright notice,
adamc@443 8 * this list of conditions and the following disclaimer.
adamc@443 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@443 10 * this list of conditions and the following disclaimer in the documentation
adamc@443 11 * and/or other materials provided with the distribution.
adamc@443 12 * - The names of contributors may not be used to endorse or promote products
adamc@443 13 * derived from this software without specific prior written permission.
adamc@443 14 *
adamc@443 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@443 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@443 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@443 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@443 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@443 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@443 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@443 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@443 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@443 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@443 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@443 26 *)
adamc@443 27
adamc@443 28 structure ESpecialize :> ESPECIALIZE = struct
adamc@443 29
adamc@443 30 open Core
adamc@443 31
adamc@443 32 structure E = CoreEnv
adamc@443 33 structure U = CoreUtil
adamc@443 34
adamc@479 35 type skey = exp
adamc@453 36
adamc@453 37 structure K = struct
adamc@479 38 type ord_key = exp list
adamc@479 39 val compare = Order.joinL U.Exp.compare
adamc@443 40 end
adamc@443 41
adamc@453 42 structure KM = BinaryMapFn(K)
adamc@443 43 structure IM = IntBinaryMap
adamc@482 44 structure IS = IntBinarySet
adamc@443 45
adamc@626 46 val freeVars = U.Exp.foldB {kind = fn (_, _, xs) => xs,
adamc@488 47 con = fn (_, _, xs) => xs,
adamc@488 48 exp = fn (bound, e, xs) =>
adamc@488 49 case e of
adamc@488 50 ERel x =>
adamc@488 51 if x >= bound then
adamc@488 52 IS.add (xs, x - bound)
adamc@488 53 else
adamc@488 54 xs
adamc@488 55 | _ => xs,
adamc@488 56 bind = fn (bound, b) =>
adamc@488 57 case b of
adamc@488 58 U.Exp.RelE _ => bound + 1
adamc@488 59 | _ => bound}
adamc@488 60 0 IS.empty
adamc@479 61
adamc@522 62 val isPoly = U.Decl.exists {kind = fn _ => false,
adamc@522 63 con = fn _ => false,
adamc@522 64 exp = fn ECAbs _ => true
adamc@522 65 | _ => false,
adamc@522 66 decl = fn _ => false}
adamc@522 67
adamc@488 68 fun positionOf (v : int, ls) =
adamc@488 69 let
adamc@488 70 fun pof (pos, ls) =
adamc@488 71 case ls of
adamc@488 72 [] => raise Fail "Defunc.positionOf"
adamc@488 73 | v' :: ls' =>
adamc@488 74 if v = v' then
adamc@488 75 pos
adamc@488 76 else
adamc@488 77 pof (pos + 1, ls')
adamc@488 78 in
adamc@488 79 pof (0, ls)
adamc@488 80 end
adamc@488 81
adamc@1079 82 fun squish fvs =
adamc@626 83 U.Exp.mapB {kind = fn _ => fn k => k,
adamc@488 84 con = fn _ => fn c => c,
adamc@488 85 exp = fn bound => fn e =>
adamc@479 86 case e of
adamc@488 87 ERel x =>
adamc@488 88 if x >= bound then
adamc@1079 89 ERel (positionOf (x - bound, fvs) + bound)
adamc@488 90 else
adamc@488 91 e
adamc@488 92 | _ => e,
adamc@488 93 bind = fn (bound, b) =>
adamc@488 94 case b of
adamc@488 95 U.Exp.RelE _ => bound + 1
adamc@488 96 | _ => bound}
adamc@488 97 0
adamc@453 98
adamc@443 99 type func = {
adamc@443 100 name : string,
adamc@453 101 args : int KM.map,
adamc@443 102 body : exp,
adamc@443 103 typ : con,
adamc@443 104 tag : string
adamc@443 105 }
adamc@443 106
adamc@443 107 type state = {
adamc@443 108 maxName : int,
adamc@443 109 funcs : func IM.map,
adamc@1079 110 decls : (string * int * con * exp * string) list,
adamc@1079 111 specialized : bool IM.map
adamc@443 112 }
adamc@443 113
adamc@488 114 fun default (_, x, st) = (x, st)
adamc@443 115
adamc@800 116 structure SS = BinarySetFn(struct
adamc@800 117 type ord_key = string
adamc@800 118 val compare = String.compare
adamc@800 119 end)
adamc@800 120
adamc@800 121 val mayNotSpec = ref SS.empty
adamc@800 122
adamc@1079 123 fun specialize' specialized file =
adamc@443 124 let
adamc@488 125 fun bind (env, b) =
adamc@488 126 case b of
adamc@521 127 U.Decl.RelE xt => xt :: env
adamc@521 128 | _ => env
adamc@488 129
adamc@488 130 fun exp (env, e, st : state) =
adamc@482 131 let
adamc@721 132 (*val () = Print.prefaces "exp" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 133 (e, ErrorMsg.dummySpan))]*)
adamc@721 134
adamc@488 135 fun getApp e =
adamc@482 136 case e of
adamc@488 137 ENamed f => SOME (f, [])
adamc@482 138 | EApp (e1, e2) =>
adamc@488 139 (case getApp (#1 e1) of
adamc@482 140 NONE => NONE
adamc@488 141 | SOME (f, xs) => SOME (f, xs @ [e2]))
adamc@482 142 | _ => NONE
adamc@482 143 in
adamc@482 144 case getApp e of
adamc@721 145 NONE => ((*Print.prefaces "No" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 146 (e, ErrorMsg.dummySpan))];*)
adamc@721 147 (e, st))
adamc@488 148 | SOME (f, xs) =>
adamc@485 149 case IM.find (#funcs st, f) of
adamc@485 150 NONE => (e, st)
adamc@485 151 | SOME {name, args, body, typ, tag} =>
adamc@488 152 let
adamc@721 153 (*val () = Print.prefaces "Consider" [("e", CorePrint.p_exp CoreEnv.empty
adamc@721 154 (e, ErrorMsg.dummySpan))]*)
adamc@721 155
adamc@488 156 val functionInside = U.Con.exists {kind = fn _ => false,
adamc@488 157 con = fn TFun _ => true
adamc@488 158 | CFfi ("Basis", "transaction") => true
adamc@794 159 | CFfi ("Basis", "eq") => true
adamc@794 160 | CFfi ("Basis", "num") => true
adamc@794 161 | CFfi ("Basis", "ord") => true
adamc@794 162 | CFfi ("Basis", "show") => true
adamc@794 163 | CFfi ("Basis", "read") => true
adamc@794 164 | CFfi ("Basis", "sql_injectable_prim") => true
adamc@794 165 | CFfi ("Basis", "sql_injectable") => true
adamc@488 166 | _ => false}
adamc@488 167 val loc = ErrorMsg.dummySpan
adamc@488 168
adamc@1079 169 fun findSplit av (xs, typ, fxs, fvs) =
adamc@488 170 case (#1 typ, xs) of
adamc@488 171 (TFun (dom, ran), e :: xs') =>
adamc@1078 172 let
adamc@1079 173 val av = case #1 e of
adamc@1079 174 ERel _ => av
adamc@1079 175 | _ => false
adamc@1078 176 in
adamc@1079 177 if functionInside dom orelse (av andalso case #1 e of
adamc@1079 178 ERel _ => true
adamc@1079 179 | _ => false) then
adamc@1079 180 findSplit av (xs',
adamc@1079 181 ran,
adamc@1079 182 e :: fxs,
adamc@1079 183 IS.union (fvs, freeVars e))
adamc@1078 184 else
adamc@1079 185 (rev fxs, xs, fvs)
adamc@1078 186 end
adamc@1079 187 | _ => (rev fxs, xs, fvs)
adamc@488 188
adamc@1079 189 val (fxs, xs, fvs) = findSplit true (xs, typ, [], IS.empty)
adamc@1079 190
adamc@1079 191 val fxs' = map (squish (IS.listItems fvs)) fxs
adamc@488 192 in
adamc@800 193 (*Print.preface ("fxs'", Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs');*)
adamc@1079 194 if List.all (fn (ERel _, _) => true
adamc@1079 195 | _ => false) fxs'
adamc@1079 196 orelse (IS.numItems fvs >= length fxs
adamc@1079 197 andalso IS.exists (fn n => functionInside (#2 (List.nth (env, n)))) fvs) then
adamc@488 198 (e, st)
adamc@488 199 else
adamc@1079 200 case (KM.find (args, fxs'),
adamc@1079 201 SS.member (!mayNotSpec, name) orelse IM.find (#specialized st, f) = SOME true) of
adamc@800 202 (SOME f', _) =>
adamc@485 203 let
adamc@488 204 val e = (ENamed f', loc)
adamc@488 205 val e = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 206 e fvs
adamc@1079 207 val e = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 208 e xs
adamc@488 209 in
adamc@488 210 (*Print.prefaces "Brand new (reuse)"
adamc@721 211 [("e'", CorePrint.p_exp CoreEnv.empty e)];*)
adamc@488 212 (#1 e, st)
adamc@488 213 end
adamc@818 214 | (_, true) => ((*Print.prefaces ("No(" ^ name ^ ")")
adamc@818 215 [("fxs'",
adamc@818 216 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')];*)
adamc@818 217 (e, st))
adamc@800 218 | (NONE, false) =>
adamc@488 219 let
adamc@800 220 (*val () = Print.prefaces "New one"
adamc@800 221 [("f", Print.PD.string (Int.toString f)),
adamc@800 222 ("mns", Print.p_list Print.PD.string
adamc@800 223 (SS.listItems (!mayNotSpec)))]*)
adamc@800 224
adamc@818 225 (*val () = Print.prefaces ("Yes(" ^ name ^ ")")
adamc@818 226 [("fxs'",
adamc@818 227 Print.p_list (CorePrint.p_exp CoreEnv.empty) fxs')]*)
adamc@818 228
adamc@1079 229 fun subBody (body, typ, fxs') =
adamc@1079 230 case (#1 body, #1 typ, fxs') of
adamc@488 231 (_, _, []) => SOME (body, typ)
adamc@1079 232 | (EAbs (_, _, _, body'), TFun (_, typ'), x :: fxs'') =>
adamc@488 233 let
adamc@1079 234 val body'' = E.subExpInExp (0, x) body'
adamc@488 235 in
adamc@488 236 subBody (body'',
adamc@488 237 typ',
adamc@1079 238 fxs'')
adamc@488 239 end
adamc@488 240 | _ => NONE
adamc@488 241 in
adamc@1079 242 case subBody (body, typ, fxs') of
adamc@488 243 NONE => (e, st)
adamc@488 244 | SOME (body', typ') =>
adamc@488 245 let
adamc@488 246 val f' = #maxName st
adamc@488 247 val args = KM.insert (args, fxs', f')
adamc@488 248 val funcs = IM.insert (#funcs st, f, {name = name,
adamc@488 249 args = args,
adamc@488 250 body = body,
adamc@488 251 typ = typ,
adamc@488 252 tag = tag})
adamc@1079 253
adamc@1079 254 val specialized = IM.insert (#specialized st, f', false)
adamc@1079 255 val specialized = case IM.find (specialized, f) of
adamc@1079 256 NONE => specialized
adamc@1079 257 | SOME _ => IM.insert (specialized, f, true)
adamc@1079 258
adamc@488 259 val st = {
adamc@488 260 maxName = f' + 1,
adamc@488 261 funcs = funcs,
adamc@1079 262 decls = #decls st,
adamc@1079 263 specialized = specialized
adamc@488 264 }
adamc@487 265
adamc@488 266 (*val () = Print.prefaces "specExp"
adamc@488 267 [("f", CorePrint.p_exp env (ENamed f, loc)),
adamc@488 268 ("f'", CorePrint.p_exp env (ENamed f', loc)),
adamc@488 269 ("xs", Print.p_list (CorePrint.p_exp env) xs),
adamc@488 270 ("fxs'", Print.p_list
adamc@488 271 (CorePrint.p_exp E.empty) fxs'),
adamc@488 272 ("e", CorePrint.p_exp env (e, loc))]*)
adamc@488 273 val (body', typ') = IS.foldl (fn (n, (body', typ')) =>
adamc@488 274 let
adamc@521 275 val (x, xt) = List.nth (env, n)
adamc@488 276 in
adamc@488 277 ((EAbs (x, xt, typ', body'),
adamc@488 278 loc),
adamc@488 279 (TFun (xt, typ'), loc))
adamc@488 280 end)
adamc@488 281 (body', typ') fvs
adamc@800 282 val mns = !mayNotSpec
adamc@800 283 val () = mayNotSpec := SS.add (mns, name)
adamc@800 284 (*val () = Print.preface ("body'", CorePrint.p_exp CoreEnv.empty body')*)
adamc@488 285 val (body', st) = specExp env st body'
adamc@800 286 val () = mayNotSpec := mns
adamc@482 287
adamc@488 288 val e' = (ENamed f', loc)
adamc@488 289 val e' = IS.foldr (fn (arg, e) => (EApp (e, (ERel arg, loc)), loc))
adamc@488 290 e' fvs
adamc@1079 291 val e' = foldl (fn (arg, e) => (EApp (e, arg), loc))
adamc@488 292 e' xs
adamc@488 293 (*val () = Print.prefaces "Brand new"
adamc@721 294 [("e'", CorePrint.p_exp CoreEnv.empty e'),
adamc@721 295 ("e", CorePrint.p_exp CoreEnv.empty (e, loc)),
adamc@721 296 ("body'", CorePrint.p_exp CoreEnv.empty body')]*)
adamc@488 297 in
adamc@488 298 (#1 e',
adamc@488 299 {maxName = #maxName st,
adamc@488 300 funcs = #funcs st,
adamc@1079 301 decls = (name, f', typ', body', tag) :: #decls st,
adamc@1079 302 specialized = #specialized st})
adamc@488 303 end
adamc@485 304 end
adamc@488 305 end
adamc@485 306 end
adamc@482 307
adamc@626 308 and specExp env = U.Exp.foldMapB {kind = default, con = default, exp = exp, bind = bind} env
adamc@482 309
adamc@626 310 val specDecl = U.Decl.foldMapB {kind = default, con = default, exp = exp, decl = default, bind = bind}
adamc@482 311
adamc@521 312 fun doDecl (d, (st : state, changed)) =
adamc@488 313 let
adamc@521 314 (*val befor = Time.now ()*)
adamc@482 315
adamc@453 316 val funcs = #funcs st
adamc@453 317 val funcs =
adamc@453 318 case #1 d of
adamc@453 319 DValRec vis =>
adamc@453 320 foldl (fn ((x, n, c, e, tag), funcs) =>
adamc@453 321 IM.insert (funcs, n, {name = x,
adamc@453 322 args = KM.empty,
adamc@453 323 body = e,
adamc@453 324 typ = c,
adamc@453 325 tag = tag}))
adamc@453 326 funcs vis
adamc@453 327 | _ => funcs
adamc@453 328
adamc@453 329 val st = {maxName = #maxName st,
adamc@453 330 funcs = funcs,
adamc@1079 331 decls = [],
adamc@1079 332 specialized = #specialized st}
adamc@453 333
adamc@482 334 (*val () = Print.prefaces "decl" [("d", CorePrint.p_decl CoreEnv.empty d)]*)
adamc@521 335
adamc@522 336 val (d', st) =
adamc@522 337 if isPoly d then
adamc@522 338 (d, st)
adamc@522 339 else
adamc@1078 340 (mayNotSpec := SS.empty(*(case #1 d of
adamc@800 341 DValRec vis => foldl (fn ((x, _, _, _, _), mns) =>
adamc@800 342 SS.add (mns, x)) SS.empty vis
adamc@800 343 | DVal (x, _, _, _, _) => SS.singleton x
adamc@1078 344 | _ => SS.empty)*);
adamc@800 345 specDecl [] st d
adamc@800 346 before mayNotSpec := SS.empty)
adamc@521 347
adamc@482 348 (*val () = print "/decl\n"*)
adamc@443 349
adamc@443 350 val funcs = #funcs st
adamc@443 351 val funcs =
adamc@443 352 case #1 d of
adamc@443 353 DVal (x, n, c, e as (EAbs _, _), tag) =>
adamc@443 354 IM.insert (funcs, n, {name = x,
adamc@453 355 args = KM.empty,
adamc@443 356 body = e,
adamc@443 357 typ = c,
adamc@443 358 tag = tag})
adamc@469 359 | DVal (_, n, _, (ENamed n', _), _) =>
adamc@469 360 (case IM.find (funcs, n') of
adamc@469 361 NONE => funcs
adamc@469 362 | SOME v => IM.insert (funcs, n, v))
adamc@443 363 | _ => funcs
adamc@443 364
adamc@453 365 val (changed, ds) =
adamc@443 366 case #decls st of
adamc@453 367 [] => (changed, [d'])
adamc@453 368 | vis =>
adamc@453 369 (true, case d' of
adamc@453 370 (DValRec vis', _) => [(DValRec (vis @ vis'), ErrorMsg.dummySpan)]
adamc@453 371 | _ => [(DValRec vis, ErrorMsg.dummySpan), d'])
adamc@443 372 in
adamc@802 373 (*Print.prefaces "doDecl" [("d", CorePrint.p_decl E.empty d),
adamc@802 374 ("d'", CorePrint.p_decl E.empty d')];*)
adamc@521 375 (ds, ({maxName = #maxName st,
adamc@453 376 funcs = funcs,
adamc@1079 377 decls = [],
adamc@1079 378 specialized = #specialized st}, changed))
adamc@443 379 end
adamc@443 380
adamc@1079 381 val (ds, (st, changed)) = ListUtil.foldlMapConcat doDecl
adamc@521 382 ({maxName = U.File.maxName file + 1,
adamc@488 383 funcs = IM.empty,
adamc@1079 384 decls = [],
adamc@1079 385 specialized = specialized},
adamc@488 386 false)
adamc@488 387 file
adamc@443 388 in
adamc@1079 389 (changed, ds, #specialized st)
adamc@443 390 end
adamc@443 391
adamc@1079 392 fun specializeL specialized file =
adamc@453 393 let
adamc@721 394 val file = ReduceLocal.reduce file
adamc@721 395 (*val () = Print.prefaces "Intermediate" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 396 (*val file = ReduceLocal.reduce file*)
adamc@1079 397 val (changed, file, specialized) = specialize' specialized file
adamc@520 398 (*val file = ReduceLocal.reduce file
adamc@520 399 val file = CoreUntangle.untangle file
adamc@488 400 val file = Shake.shake file*)
adamc@453 401 in
adamc@488 402 (*print "Round over\n";*)
adamc@453 403 if changed then
adamc@520 404 let
adamc@721 405 (*val file = ReduceLocal.reduce file*)
adamc@802 406 (*val () = Print.prefaces "Pre-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 407 val file = CoreUntangle.untangle file
adamc@802 408 (*val () = Print.prefaces "Post-untangle" [("file", CorePrint.p_file CoreEnv.empty file)]*)
adamc@520 409 val file = Shake.shake file
adamc@520 410 in
adamc@520 411 (*print "Again!\n";*)
adamc@1079 412 specializeL specialized file
adamc@520 413 end
adamc@453 414 else
adamc@453 415 file
adamc@453 416 end
adamc@453 417
adamc@1079 418 val specialize = specializeL IM.empty
adamc@1079 419
adamc@443 420 end