Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/high-il/high-opt.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/high-il/high-opt.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 508 - (view) (download)

1 : jhr 287 (* high-opt.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 287 * All rights reserved.
5 :     *
6 :     * Optimization of the HighIL representation of Diderot terms. The main
7 :     * task of this phase is to statically resolve field definitions.
8 :     *)
9 :    
10 :     structure HighOptimizer : sig
11 :    
12 :     val optimize : HighIL.program -> HighIL.program
13 :    
14 :     end = struct
15 :    
16 :     structure IL = HighIL
17 : jhr 338 structure Op = HighOps
18 : jhr 287 structure V = IL.Var
19 : jhr 320 structure ST = Stats
20 : jhr 338 structure F = FieldDef
21 : jhr 287
22 : jhr 320 (********** Counters for statistics **********)
23 :     val cntConstConvolve = ST.newCounter "high-opt:const-convolve"
24 :     val cntConstField = ST.newCounter "high-opt:const-field"
25 :     val cntConstDiff = ST.newCounter "high-opt:const-diff"
26 :     val cntUnused = ST.newCounter "high-opt:unused"
27 : jhr 338 val firstCounter = cntConstConvolve
28 :     val lastCounter = cntUnused
29 : jhr 320
30 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
31 :    
32 : jhr 287 (* decrement a variable's use count *)
33 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
34 :    
35 : jhr 372 fun getRHS x = (case V.binding x
36 :     of IL.VB_RHS(IL.OP arg) => SOME arg
37 :     | _ => NONE
38 :     (* end case *))
39 : jhr 287
40 :     (* optimize the rhs of an assignment, returning NONE if there is no change *)
41 : jhr 320 fun doRHS rhs = (case rhs
42 : jhr 372 of IL.OP(Op.Convolve, [v, h]) => (case (getRHS v, getRHS h)
43 :     of (SOME(Op.LoadImage v', []), SOME(Op.Kernel h', [])) => (
44 : jhr 320 ST.tick cntConstConvolve;
45 : jhr 287 decUse v; decUse h;
46 : jhr 338 SOME(IL.OP(Op.Field(F.convolve(v', h')), [])))
47 : jhr 287 | _ => raise Fail "non-constant Convolve"
48 :     (* end case *))
49 : jhr 372 | IL.OP(Op.AddField, [f, g]) => (case (getRHS f, getRHS g)
50 :     of (SOME(Op.Field f', []), SOME(Op.Field g', [])) => (
51 : jhr 320 ST.tick cntConstField;
52 : jhr 287 decUse f; decUse g;
53 : jhr 338 SOME(IL.OP(Op.Field(F.SUM(f', g')), [])))
54 : jhr 287 | _ => NONE
55 :     (* end case *))
56 : jhr 372 | IL.OP(Op.NegField, [f]) => (case (getRHS f)
57 :     of SOME(Op.Field f', []) => (
58 : jhr 320 ST.tick cntConstField;
59 : jhr 338 decUse f;
60 :     SOME(IL.OP(Op.Field(F.neg f'), [])))
61 : jhr 287 | _ => NONE
62 :     (* end case *))
63 : jhr 372 | IL.OP(Op.DiffField, [f]) => (case (getRHS f)
64 :     of SOME(Op.Field f', []) => (
65 : jhr 320 ST.tick cntConstDiff;
66 : jhr 338 decUse f;
67 :     SOME(IL.OP(Op.Field(F.diff f'), [])))
68 : jhr 287 | _ => raise Fail "non-constant DiffField"
69 :     (* end case *))
70 :     | _ => NONE
71 :     (* end case *))
72 :    
73 : jhr 320 (* simplify expressions *)
74 : jhr 508 local
75 :     fun doAssign (y, rhs) = (case doRHS rhs
76 :     of SOME rhs' => (V.setBinding(y, IL.VB_RHS rhs'); (y, rhs'))
77 :     | NONE => (y, rhs)
78 :     (* end case *))
79 :     in
80 :     fun simplify (nd as IL.ND{kind=IL.ASSIGN{stm=(y, rhs), ...}, ...}) = (case doRHS rhs
81 :     of SOME rhs' => (
82 :     V.setBinding(y, IL.VB_RHS rhs');
83 :     IL.CFG.replaceNode (nd, IL.Node.mkASSIGN(y, rhs')))
84 :     | NONE => ()
85 :     (* end case *))
86 :     | simplify _ = ()
87 :     end (* local *)
88 : jhr 320
89 :     (* reduce the code by removing variables with use counts of 0 *)
90 : jhr 508 local
91 :     fun checkVar (y, _) = (useCount y > 0) orelse (ST.tick cntUnused; false)
92 :     in
93 :     fun reduce (nd as IL.ND{kind, ...}) = (case kind
94 :     of IL.JOIN{phis, ...} => let
95 :     fun doVar (y, xs) = if (useCount y = 0)
96 :     then (
97 :     ST.tick cntUnused;
98 :     List.app decUse xs;
99 :     false)
100 :     else true
101 :     in
102 :     phis := List.filter doVar (!phis)
103 :     end
104 :     | IL.ASSIGN{stm=(y, rhs), ...} =>
105 :     if (useCount y = 0)
106 :     then (
107 :     ST.tick cntUnused;
108 :     case rhs
109 :     of IL.VAR x => decUse x
110 :     | IL.LIT _ => ()
111 :     | IL.OP(_, xs) => List.app decUse xs
112 :     | IL.CONS xs => List.app decUse xs
113 :     (* end case *);
114 :     IL.CFG.deleteNode nd)
115 :     else ()
116 :     | _ => ()
117 :     (* end case *))
118 :     end (* local *)
119 : jhr 320
120 : jhr 508 fun loopToFixPt f cfg = let
121 :     fun loop n = let
122 :     val () = IL.CFG.apply f cfg
123 : jhr 338 val n' = Stats.sum{from=firstCounter, to=lastCounter}
124 : jhr 320 in
125 : jhr 508 if (n = n') then () else loop n'
126 : jhr 320 end
127 :     in
128 : jhr 508 loop (Stats.sum{from=firstCounter, to=lastCounter})
129 : jhr 320 end
130 :    
131 : jhr 499 fun optimize (prog as IL.Program{globals, globalInit, strands}) = let
132 : jhr 508 fun doCFG cfg = (
133 :     loopToFixPt simplify cfg;
134 :     loopToFixPt reduce cfg)
135 : jhr 491 val globInitNodes = doCFG globalInit
136 : jhr 508 fun doMethod (IL.Method{body, ...}) = doCFG body
137 :     fun doStrand (IL.Strand{stateInit, methods, ...}) = (
138 :     doCFG stateInit;
139 :     List.app doMethod methods)
140 : jhr 499 val _ = List.app doStrand strands
141 : jhr 320 in
142 :     IL.Program{
143 :     globals = globals,
144 :     globalInit = globalInit,
145 : jhr 499 strands = strands
146 : jhr 320 }
147 :     end
148 :    
149 : jhr 287 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0