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 703 - (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 : jhr 649 val cntProbeAdd = ST.newCounter "high-opt:probe-add"
24 :     val cntProbeScale = ST.newCounter "high-opt:probe-scale"
25 :     val cntProbeNeg = ST.newCounter "high-opt:probe-neg"
26 :     val cntDiffField = ST.newCounter "high-opt:diff-field"
27 :     val cntDiffAdd = ST.newCounter "high-opt:diff-add"
28 :     val cntDiffScale = ST.newCounter "high-opt:diff-scale"
29 :     val cntDiffNeg = ST.newCounter "high-opt:diff-neg"
30 : jhr 320 val cntUnused = ST.newCounter "high-opt:unused"
31 : jhr 649 val firstCounter = cntProbeAdd
32 : jhr 338 val lastCounter = cntUnused
33 : jhr 320
34 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
35 :    
36 : jhr 649 (* adjust a variable's use count *)
37 :     fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
38 : jhr 287 fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
39 :    
40 : jhr 372 fun getRHS x = (case V.binding x
41 :     of IL.VB_RHS(IL.OP arg) => SOME arg
42 :     | _ => NONE
43 :     (* end case *))
44 : jhr 287
45 :     (* optimize the rhs of an assignment, returning NONE if there is no change *)
46 : jhr 649 fun doRHS (lhs, IL.OP rhs) = (case rhs
47 :     of (Op.Probe(domTy, rngTy), [f, pos]) => (case getRHS f
48 :     of SOME(Op.Field _, _) => NONE (* direct probe does not need rewrite *)
49 :     | SOME(Op.AddField, [f, g]) => raise Fail "Probe(f+g)"
50 :     | SOME(Op.ScaleField, [s, f]) => raise Fail "Probe(s*f)"
51 : jhr 703 | SOME(Op.NegField, [f']) => let
52 :     (* rewrite to -(f'@pos) *)
53 :     val lhs' = IL.Var.copy lhs
54 :     in
55 :     ST.tick cntProbeNeg;
56 :     decUse f;
57 :     incUse lhs'; incUse f';
58 :     SOME[
59 :     (lhs', IL.OP(Op.Probe(domTy, rngTy), [f', pos])),
60 :     (lhs, IL.OP(Op.Neg rngTy, [lhs']))
61 :     ]
62 :     end
63 : jhr 649 | _ => raise Fail(concat[
64 :     "bogus field binding ", V.toString f, " = ", IL.vbToString(V.binding f)
65 :     ])
66 :     (* end case *))
67 :     | (Op.DiffField, [f]) => (case (getRHS f)
68 :     of SOME(Op.Field dim, [v, h]) => (case getRHS h
69 :     of SOME(Op.Kernel(kernel, k), []) => let
70 :     val h' = IL.Var.copy h
71 :     in
72 :     ST.tick cntDiffField;
73 :     decUse f;
74 :     incUse h'; incUse v;
75 :     SOME[
76 :     (h', IL.OP(Op.Kernel(kernel, k+1), [])),
77 :     (lhs, IL.OP(Op.Field dim, [v, h']))
78 :     ]
79 :     end
80 :     | _ => raise Fail(concat[
81 :     "bogus kernel binding ", V.toString h, " = ", IL.vbToString(V.binding h)
82 :     ])
83 : jhr 516 (* end case *))
84 : jhr 649 | SOME(Op.AddField, [f, g]) => raise Fail "Diff(f+g)"
85 :     | SOME(Op.ScaleField, [s, f]) => raise Fail "Diff(s*f)"
86 :     | SOME(Op.NegField, [f]) => raise Fail "Diff(-f)"
87 :     | _ => NONE
88 : jhr 287 (* end case *))
89 :     | _ => NONE
90 :     (* end case *))
91 : jhr 649 | doRHS _ = NONE
92 : jhr 287
93 : jhr 320 (* simplify expressions *)
94 : jhr 649 fun simplify (nd as IL.ND{kind=IL.ASSIGN{stm=(y, rhs), ...}, ...}) =
95 :     if (useCount y = 0)
96 :     then () (* skip unused assignments *)
97 :     else (case doRHS(y, rhs)
98 :     of SOME[] => IL.CFG.deleteNode nd
99 :     | SOME assigns => (
100 :     List.app (fn (y, rhs) => V.setBinding(y, IL.VB_RHS rhs)) assigns;
101 :     IL.CFG.splice (nd, IL.CFG.mkBlock assigns))
102 :     | NONE => ()
103 :     (* end case *))
104 : jhr 508 | simplify _ = ()
105 : jhr 320
106 :     (* reduce the code by removing variables with use counts of 0 *)
107 : jhr 508 local
108 :     fun checkVar (y, _) = (useCount y > 0) orelse (ST.tick cntUnused; false)
109 :     in
110 :     fun reduce (nd as IL.ND{kind, ...}) = (case kind
111 :     of IL.JOIN{phis, ...} => let
112 :     fun doVar (y, xs) = if (useCount y = 0)
113 :     then (
114 :     ST.tick cntUnused;
115 :     List.app decUse xs;
116 :     false)
117 :     else true
118 :     in
119 :     phis := List.filter doVar (!phis)
120 :     end
121 :     | IL.ASSIGN{stm=(y, rhs), ...} =>
122 :     if (useCount y = 0)
123 :     then (
124 :     ST.tick cntUnused;
125 :     case rhs
126 :     of IL.VAR x => decUse x
127 :     | IL.LIT _ => ()
128 :     | IL.OP(_, xs) => List.app decUse xs
129 : jhr 695 | IL.APPLY(_, xs) => List.app decUse xs
130 : jhr 508 | IL.CONS xs => List.app decUse xs
131 :     (* end case *);
132 :     IL.CFG.deleteNode nd)
133 :     else ()
134 :     | _ => ()
135 :     (* end case *))
136 :     end (* local *)
137 : jhr 320
138 : jhr 508 fun loopToFixPt f cfg = let
139 :     fun loop n = let
140 :     val () = IL.CFG.apply f cfg
141 : jhr 338 val n' = Stats.sum{from=firstCounter, to=lastCounter}
142 : jhr 320 in
143 : jhr 508 if (n = n') then () else loop n'
144 : jhr 320 end
145 :     in
146 : jhr 508 loop (Stats.sum{from=firstCounter, to=lastCounter})
147 : jhr 320 end
148 :    
149 : jhr 613 fun optimize (prog as IL.Program{globals, globalInit, initially, strands}) = let
150 : jhr 508 fun doCFG cfg = (
151 :     loopToFixPt simplify cfg;
152 :     loopToFixPt reduce cfg)
153 : jhr 491 val globInitNodes = doCFG globalInit
154 : jhr 508 fun doMethod (IL.Method{body, ...}) = doCFG body
155 :     fun doStrand (IL.Strand{stateInit, methods, ...}) = (
156 :     doCFG stateInit;
157 :     List.app doMethod methods)
158 : jhr 499 val _ = List.app doStrand strands
159 : jhr 320 in
160 :     IL.Program{
161 :     globals = globals,
162 :     globalInit = globalInit,
163 : jhr 613 initially = initially, (* FIXME: we should optimize this code *)
164 : jhr 499 strands = strands
165 : jhr 320 }
166 :     end
167 :    
168 : jhr 287 end

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