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

SCM Repository

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

Annotation of /trunk/src/compiler/high-il/high-opt.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 372 - (view) (download)

1 : jhr 287 (* high-opt.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * 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 structure Census = CensusFn (IL)
23 :    
24 :     (********** Counters for statistics **********)
25 :     val cntConstConvolve = ST.newCounter "high-opt:const-convolve"
26 :     val cntConstField = ST.newCounter "high-opt:const-field"
27 :     val cntConstDiff = ST.newCounter "high-opt:const-diff"
28 :     val cntUnused = ST.newCounter "high-opt:unused"
29 : jhr 338 val firstCounter = cntConstConvolve
30 :     val lastCounter = cntUnused
31 : jhr 320
32 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
33 :    
34 : jhr 287 (* decrement a variable's use count *)
35 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
36 :    
37 : jhr 372 fun getRHS x = (case V.binding x
38 :     of IL.VB_RHS(IL.OP arg) => SOME arg
39 :     | _ => NONE
40 :     (* end case *))
41 : jhr 287
42 :     (* optimize the rhs of an assignment, returning NONE if there is no change *)
43 : jhr 320 fun doRHS rhs = (case rhs
44 : jhr 372 of IL.OP(Op.Convolve, [v, h]) => (case (getRHS v, getRHS h)
45 :     of (SOME(Op.LoadImage v', []), SOME(Op.Kernel h', [])) => (
46 : jhr 320 ST.tick cntConstConvolve;
47 : jhr 287 decUse v; decUse h;
48 : jhr 338 SOME(IL.OP(Op.Field(F.convolve(v', h')), [])))
49 : jhr 287 | _ => raise Fail "non-constant Convolve"
50 :     (* end case *))
51 : jhr 372 | IL.OP(Op.AddField, [f, g]) => (case (getRHS f, getRHS g)
52 :     of (SOME(Op.Field f', []), SOME(Op.Field g', [])) => (
53 : jhr 320 ST.tick cntConstField;
54 : jhr 287 decUse f; decUse g;
55 : jhr 338 SOME(IL.OP(Op.Field(F.SUM(f', g')), [])))
56 : jhr 287 | _ => NONE
57 :     (* end case *))
58 : jhr 372 | IL.OP(Op.NegField, [f]) => (case (getRHS f)
59 :     of SOME(Op.Field f', []) => (
60 : jhr 320 ST.tick cntConstField;
61 : jhr 338 decUse f;
62 :     SOME(IL.OP(Op.Field(F.neg f'), [])))
63 : jhr 287 | _ => NONE
64 :     (* end case *))
65 : jhr 372 | IL.OP(Op.DiffField, [f]) => (case (getRHS f)
66 :     of SOME(Op.Field f', []) => (
67 : jhr 320 ST.tick cntConstDiff;
68 : jhr 338 decUse f;
69 :     SOME(IL.OP(Op.Field(F.diff f'), [])))
70 : jhr 287 | _ => raise Fail "non-constant DiffField"
71 :     (* end case *))
72 :     | _ => NONE
73 :     (* end case *))
74 :    
75 : jhr 320 (* simplify expressions *)
76 :     fun simplify nodes = let
77 :     fun doAssign (y, rhs) = (case doRHS rhs
78 : jhr 372 of SOME rhs' => (V.setBinding(y, IL.VB_RHS rhs'); (y, rhs'))
79 : jhr 320 | NONE => (y, rhs)
80 :     (* end case *))
81 : jhr 368 fun doNode (IL.ND{kind=IL.BLOCK{body, ...}, ...}) =
82 :     body := List.map doAssign (!body)
83 : jhr 320 | doNode _ = ()
84 :     in
85 :     List.app doNode nodes
86 :     end
87 :    
88 :     (* reduce the code by removing variables with use counts of 0 *)
89 :     fun reduce nodes = let
90 :     fun checkVar (y, _) = (useCount y > 0) orelse (ST.tick cntUnused; false)
91 : jhr 338 fun doNode (IL.ND{kind, ...}) = (case kind
92 :     of IL.JOIN{phis, ...} => let
93 :     fun doVar (y, xs) = if (useCount y = 0)
94 :     then (
95 :     ST.tick cntUnused;
96 :     List.app decUse xs;
97 :     false)
98 :     else true
99 : jhr 320 in
100 : jhr 338 phis := List.filter doVar (!phis)
101 :     end
102 :     | IL.BLOCK{body, ...} => let
103 :     (* check for unused lhs variables in reverse order *)
104 :     fun doAssigns [] = []
105 :     | doAssigns ((y, rhs)::r) = let
106 :     val r = doAssigns r
107 :     in
108 :     if (useCount y = 0)
109 :     then (
110 :     ST.tick cntUnused;
111 :     case rhs
112 :     of IL.VAR x => decUse x
113 : jhr 340 | IL.LIT _ => ()
114 : jhr 338 | IL.OP(_, xs) => List.app decUse xs
115 :     | IL.CONS xs => List.app decUse xs
116 :     (* end case *);
117 :     r)
118 :     else (y, rhs)::r
119 :     end
120 :     in
121 :     body := doAssigns (!body)
122 :     end
123 :     | _ => ()
124 :     (* end case *))
125 : jhr 320 in
126 :     List.app doNode (List.rev nodes)
127 :     end
128 :    
129 : jhr 338 fun loopToFixPt f prog = let
130 : jhr 320 fun loop (n, prog) = let
131 : jhr 338 val () = f prog
132 :     val n' = Stats.sum{from=firstCounter, to=lastCounter}
133 : jhr 320 in
134 : jhr 338 if (n = n') then () else loop(n', prog)
135 : jhr 320 end
136 :     in
137 : jhr 338 loop (Stats.sum{from=firstCounter, to=lastCounter}, prog)
138 : jhr 320 end
139 :    
140 :     fun optimize (prog as IL.Program{globals, globalInit, actors}) = let
141 :     val _ = Census.init prog
142 :     fun doStmt stm = let
143 :     val nodes = IL.sortNodes stm
144 :     in
145 :     loopToFixPt simplify nodes;
146 :     loopToFixPt reduce nodes;
147 :     nodes
148 :     end
149 :     val globInitNodes = doStmt globalInit
150 :     fun doMethod (IL.Method{body, ...}) = let
151 :     val nodes = IL.sortNodes body
152 :     in
153 :     loopToFixPt simplify nodes;
154 : jhr 372 loopToFixPt reduce nodes
155 : jhr 320 end
156 :     fun doActor (IL.Actor{stateInit, methods, ...}) = let
157 :     val nodes = IL.sortNodes stateInit
158 :     in
159 :     loopToFixPt simplify nodes;
160 :     loopToFixPt reduce nodes;
161 : jhr 372 List.app doMethod methods
162 : jhr 320 end
163 :     val _ = List.app doActor actors
164 :     in
165 :     IL.Program{
166 :     globals = globals,
167 :     globalInit = globalInit,
168 :     actors = actors
169 :     }
170 :     end
171 :    
172 : jhr 287 end

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