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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 511 - (view) (download)
Original Path: trunk/src/compiler/high-il/high-opt.sml

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 :     fun simplify nodes = let
75 :     fun doAssign (y, rhs) = (case doRHS rhs
76 : jhr 372 of SOME rhs' => (V.setBinding(y, IL.VB_RHS rhs'); (y, rhs'))
77 : jhr 320 | NONE => (y, rhs)
78 :     (* end case *))
79 : jhr 368 fun doNode (IL.ND{kind=IL.BLOCK{body, ...}, ...}) =
80 :     body := List.map doAssign (!body)
81 : jhr 320 | doNode _ = ()
82 :     in
83 :     List.app doNode nodes
84 :     end
85 :    
86 :     (* reduce the code by removing variables with use counts of 0 *)
87 :     fun reduce nodes = let
88 :     fun checkVar (y, _) = (useCount y > 0) orelse (ST.tick cntUnused; false)
89 : jhr 338 fun doNode (IL.ND{kind, ...}) = (case kind
90 :     of IL.JOIN{phis, ...} => let
91 :     fun doVar (y, xs) = if (useCount y = 0)
92 :     then (
93 :     ST.tick cntUnused;
94 :     List.app decUse xs;
95 :     false)
96 :     else true
97 : jhr 320 in
98 : jhr 338 phis := List.filter doVar (!phis)
99 :     end
100 :     | IL.BLOCK{body, ...} => let
101 :     (* check for unused lhs variables in reverse order *)
102 :     fun doAssigns [] = []
103 :     | doAssigns ((y, rhs)::r) = let
104 :     val r = doAssigns r
105 :     in
106 :     if (useCount y = 0)
107 :     then (
108 :     ST.tick cntUnused;
109 :     case rhs
110 :     of IL.VAR x => decUse x
111 : jhr 340 | IL.LIT _ => ()
112 : jhr 338 | IL.OP(_, xs) => List.app decUse xs
113 :     | IL.CONS xs => List.app decUse xs
114 :     (* end case *);
115 :     r)
116 :     else (y, rhs)::r
117 :     end
118 :     in
119 :     body := doAssigns (!body)
120 :     end
121 :     | _ => ()
122 :     (* end case *))
123 : jhr 320 in
124 :     List.app doNode (List.rev nodes)
125 :     end
126 :    
127 : jhr 338 fun loopToFixPt f prog = let
128 : jhr 320 fun loop (n, prog) = let
129 : jhr 338 val () = f prog
130 :     val n' = Stats.sum{from=firstCounter, to=lastCounter}
131 : jhr 320 in
132 : jhr 338 if (n = n') then () else loop(n', prog)
133 : jhr 320 end
134 :     in
135 : jhr 338 loop (Stats.sum{from=firstCounter, to=lastCounter}, prog)
136 : jhr 320 end
137 :    
138 : jhr 511 fun optimize (prog as IL.Program{globals, globalInit, strands}) = let
139 : jhr 320 fun doStmt stm = let
140 :     val nodes = IL.sortNodes stm
141 :     in
142 :     loopToFixPt simplify nodes;
143 :     loopToFixPt reduce nodes;
144 :     nodes
145 :     end
146 :     val globInitNodes = doStmt globalInit
147 :     fun doMethod (IL.Method{body, ...}) = let
148 :     val nodes = IL.sortNodes body
149 :     in
150 :     loopToFixPt simplify nodes;
151 : jhr 372 loopToFixPt reduce nodes
152 : jhr 320 end
153 : jhr 511 fun doStrand (IL.Strand{stateInit, methods, ...}) = let
154 : jhr 320 val nodes = IL.sortNodes stateInit
155 :     in
156 :     loopToFixPt simplify nodes;
157 :     loopToFixPt reduce nodes;
158 : jhr 372 List.app doMethod methods
159 : jhr 320 end
160 : jhr 511 val _ = List.app doStrand strands
161 : jhr 320 in
162 :     IL.Program{
163 :     globals = globals,
164 :     globalInit = globalInit,
165 : jhr 511 strands = strands
166 : jhr 320 }
167 :     end
168 :    
169 : jhr 287 end

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