Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] View of /branches/vis12/src/compiler/mid-il/mid-contract.sml
 [diderot] / branches / vis12 / src / compiler / mid-il / mid-contract.sml # View of /branches/vis12/src/compiler/mid-il/mid-contract.sml

Sun Jan 22 15:23:36 2012 UTC (9 years, 7 months ago) by jhr
File size: 2989 byte(s)
```  Create a branch to implement things that we need for the Vis 2012 paper
```
```(* mid-contract.sml
*
* COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
*
* Contraction phase for MidIL.
*)

structure MidContract : sig

val transform : MidIL.program -> MidIL.program

end = struct

structure IL = MidIL
structure Op = MidOps
structure V = IL.Var
structure ST = Stats

(********** Counters for statistics **********)
val cntEigenVals		= ST.newCounter "mid-opt:eigen-vals"
val cntUnused		= ST.newCounter "mid-opt:unused"
val firstCounter            = cntEigenVals
val lastCounter             = cntUnused

structure UnusedElim = UnusedElimFn (
structure IL = IL
val cntUnused = cntUnused)

fun useCount (IL.V{useCnt, ...}) = !useCnt

(* adjust a variable's use count *)
fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)

(* simplify expressions *)
fun simplify nd = (case IL.Node.kind nd
of IL.MASSIGN{stm=(ys, rator, xs), ...} => (case (ys, rator)
of ([vals, vecs], Op.EigenVecs2x2) =>
if (useCount vecs = 0)
then let
val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals2x2, xs))
in
ST.tick cntEigenVals;
IL.CFG.replaceNode (nd, newNd)
end
else ()
| ([vals, vecs], Op.EigenVecs3x3) =>
if (useCount vecs = 0)
then let
val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals3x3, xs))
in
ST.tick cntEigenVals;
IL.CFG.replaceNode (nd, newNd)
end
else ()
| _ => ()
(* end case *))
| _ => ()
(* end case *))

fun loopToFixPt f = let
fun loop n = let
val () = f ()
val n' = Stats.sum{from=firstCounter, to=lastCounter}
in
if (n = n') then () else loop n'
end
in
loop (Stats.sum{from=firstCounter, to=lastCounter})
end

fun transform (prog as IL.Program{props, globalInit, initially, strands}) = let
fun doCFG cfg = (
loopToFixPt (fn () => IL.CFG.apply simplify cfg);
loopToFixPt (fn () => ignore(UnusedElim.reduce cfg)))
fun doMethod (IL.Method{body, ...}) = doCFG body
fun doStrand (IL.Strand{stateInit, methods, ...}) = (
doCFG stateInit;
List.app doMethod methods)
fun optPass () = (
doCFG globalInit;
List.app doStrand strands)
in
loopToFixPt optPass;
(* FIXME: after optimization, we should filter out any globals that are now unused *)
IL.Program{
props = props,
globalInit = globalInit,
initially = initially,	(* FIXME: we should optimize this code *)
strands = strands
}
end

end
```