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

SCM Repository

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

Annotation of /branches/charisee/src/compiler/high-il/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2584 - (view) (download)

1 : jhr 1232 (* normalize.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure Normalize : sig
8 :    
9 :     val transform : HighIL.program -> HighIL.program
10 :    
11 :     end = struct
12 :    
13 : cchiw 2507
14 : jhr 1232 structure IL = HighIL
15 :     structure Op = HighOps
16 :     structure V = IL.Var
17 : jhr 2356 structure Ty = HighILTypes
18 : jhr 1232 structure ST = Stats
19 : cchiw 2400 structure NE = NormalizeEin
20 : cchiw 2499 structure P=Printer
21 : cchiw 2515 structure Order=OrderEin
22 :     structure mk=mkOperators
23 : cchiw 2507 structure App=App
24 : jhr 1232
25 : cchiw 2507
26 : cchiw 2400 (********** Counters for statistics **********)
27 : jhr 2356 val cntInsideScale = ST.newCounter "high-opt:inside-scale"
28 :     val cntInsideOffset = ST.newCounter "high-opt:inside-offset"
29 :     val cntInsideNeg = ST.newCounter "high-opt:inside-meg"
30 :     val cntInsideCurl = ST.newCounter "high-opt:inside-curl"
31 :     val cntInsideDiff = ST.newCounter "high-opt:inside-diff"
32 :     val cntProbeAdd = ST.newCounter "high-opt:probe-add"
33 :     val cntProbeSub = ST.newCounter "high-opt:probe-sub"
34 :     val cntProbeScale = ST.newCounter "high-opt:probe-scale"
35 :     val cntProbeOffset = ST.newCounter "high-opt:probe-offset"
36 :     val cntProbeNeg = ST.newCounter "high-opt:probe-neg"
37 :     val cntProbeCurl = ST.newCounter "high-opt:probe-curl"
38 :     val cntDiffField = ST.newCounter "high-opt:diff-field"
39 :     val cntDiffAdd = ST.newCounter "high-opt:diff-add"
40 :     val cntDiffScale = ST.newCounter "high-opt:diff-scale"
41 :     val cntDiffOffset = ST.newCounter "high-opt:diff-offset"
42 :     val cntDiffNeg = ST.newCounter "high-opt:diff-neg"
43 :     val cntCurlScale = ST.newCounter "high-opt:curl-scale"
44 :     val cntCurlNeg = ST.newCounter "high-opt:curl-neg"
45 :     val cntUnused = ST.newCounter "high-opt:unused"
46 :     val firstCounter = cntInsideScale
47 : jhr 1232 val lastCounter = cntUnused
48 :    
49 : cchiw 2507
50 :    
51 :    
52 :    
53 :    
54 : jhr 1232 structure UnusedElim = UnusedElimFn (
55 : jhr 2356 structure IL = IL
56 :     val cntUnused = cntUnused)
57 : jhr 1232
58 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
59 :    
60 :     (* adjust a variable's use count *)
61 :     fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
62 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
63 : jhr 2356 fun use x = (incUse x; x)
64 : jhr 1232
65 : cchiw 2507
66 : jhr 1232 fun getRHS x = (case V.binding x
67 : jhr 2356 of IL.VB_RHS(IL.OP arg) => SOME arg
68 :     | IL.VB_RHS(IL.VAR x') => getRHS x'
69 : cchiw 2507 (*| IL.VB_RHS(IL.EINAPP (e,arg))=>*)
70 : jhr 2356 | _ => NONE
71 :     (* end case *))
72 : jhr 1232
73 : cchiw 2507
74 :    
75 :    
76 :     fun isEin x = (case V.binding x
77 : cchiw 2508 of IL.VB_RHS(IL.EINAPP (e,arg))=>SOME(IL.EINAPP (e,arg))
78 : cchiw 2525 | IL.VB_RHS(IL.VAR x') => isEin x'
79 : cchiw 2507 |_=>NONE
80 :     (* end case *))
81 :    
82 :    
83 : jhr 2356 (* get the binding of a kernel variable *)
84 :     fun getKernelRHS h = (case getRHS h
85 :     of SOME(Op.Kernel(kernel, k), []) => (kernel, k)
86 :     | _ => raise Fail(concat[
87 :     "bogus kernel binding ", V.toString h, " = ", IL.vbToString(V.binding h)
88 :     ])
89 :     (* end case *))
90 :    
91 : jhr 1232 (* optimize the rhs of an assignment, returning NONE if there is no change *)
92 : cchiw 2445
93 : cchiw 2502 (*args are vars, how do I find if an einapp applied to an einapp?*)
94 :     (*where to add type checker*)
95 : cchiw 2400
96 : cchiw 2502
97 : cchiw 2507 fun doNormalize e=(let
98 : cchiw 2515 val ordered=Order.orderfn(e)
99 :     val (n,change)=NE.normalize(ordered)
100 : cchiw 2507 in (case change
101 : cchiw 2584 of 0 =>ordered
102 : cchiw 2507 | _ => let
103 : cchiw 2515 val str=String.concat ["\n Ordered:\n",P.printerE(ordered),"\n =>",P.printerE(n)]
104 : cchiw 2507 val p=print str
105 :     in n
106 :     end
107 :     (*end case*))
108 :     end)
109 :    
110 :    
111 : cchiw 2525 fun printX(IL.ASSGN (x, IL.OP(opss,args)))= let
112 : cchiw 2507
113 : cchiw 2525 val a= print(String.concat(["\n",V.toString x,"==",Op.toString opss," : "]))
114 :     in print (String.concatWith "," (List.map V.toString args)) end
115 :     | printX(IL.ASSGN(x,IL.LIT _))= print(String.concat["\n :",V.toString x,"==...Lit"])
116 :     | printX(IL.ASSGN(x,IL.CONS (_, varl)))= let
117 :     val y= List.map (fn e1=> V.toString e1) varl
118 :     in print(String.concat[ "\n",(V.toString x),"==",(String.concatWith "," y)]) end
119 :     | printX(IL.ASSGN (x, _))=print(String.concat["\n",V.toString x,"==","CONS"])
120 : cchiw 2507
121 : cchiw 2525
122 :    
123 : cchiw 2521 fun check params _=1
124 :    
125 : cchiw 2507 fun doRHS (lhs, IL.EINAPP (ein, args))=(let
126 : cchiw 2525 val x= print (String.concat[V.toString lhs,"=="," ---Current: ", P.printerE(ein)])
127 :     val g= print (String.concatWith "," (List.map V.toString args))
128 : cchiw 2507
129 : cchiw 2508 fun rewrite(0,_,_, [], _)= (print "No Subst ";NONE)
130 :     | rewrite(_,orig,_, [], done)= let
131 : cchiw 2507 val n=doNormalize orig
132 : cchiw 2508 val r=print(String.concat["\n Return:\t",P.printerE(n)])
133 : cchiw 2507 in
134 :     SOME[(lhs,IL.EINAPP(n, done))]
135 :     end
136 :     | rewrite(change,orig, place, e::es,done)=let
137 :     val v =isEin e
138 : cchiw 2521 val g=print(String.concat["\n checking argument First:", Int.toString(place)])
139 : cchiw 2507 in case v
140 : cchiw 2521
141 : cchiw 2507 of NONE=> (rewrite(change,orig, place+1, es, done@[e]))
142 : cchiw 2521 | SOME(IL.EINAPP (e2,arg1))=>let
143 :     val Ein.EIN{params, index, body}=orig
144 :     val g=print(String.concat["checking argument:", Int.toString(place), "number of params",Int.toString(length(params)),"\n"])
145 :     val t=List.nth(params, place)
146 :     in (case t
147 : cchiw 2525 of Ein.TEN(0,_)=> (rewrite(change,orig, place+1, es, done@[e]))
148 : cchiw 2521 | _=> let
149 :     val m= print(String.concat["\n Apply at ", Int.toString(place),":--",P.printerE(e2),
150 : cchiw 2508 "\n"])
151 : cchiw 2521 val (c,subst)=App.app(orig,place,e2)
152 :     val m= print(String.concat["\n","Subst Result:\t", P.printerE(subst), "\n"])
153 :     in (case c of 0 => rewrite(1,subst, place+length(arg1), es,done@arg1)
154 : cchiw 2553 |_=> (incUse lhs;(*decUse e;*)rewrite(1,subst, place+length(arg1), es,done@arg1)))
155 : cchiw 2521 end
156 :     (*end case*))
157 :     end
158 : cchiw 2507 end
159 :    
160 :     val m= rewrite(0,ein, 0,args,[])
161 : cchiw 2515 val t=print "\n\n\n"
162 : cchiw 2507 in m end )
163 :    
164 :    
165 :    
166 : cchiw 2496 (*
167 : cchiw 2445 | doRHS (lhs, IL.OP rhs) = (case rhs
168 : jhr 2356 of (Op.Inside dim, [pos, f]) => (case getRHS f
169 : cchiw 2445 of SOME(Op.Field _, _) => NONE (* direct inside test does not need rewrite
170 : jhr 2356 | SOME(Op.OffsetField, [f', _]) => (
171 :     ST.tick cntInsideOffset;
172 :     decUse f;
173 : cchiw 2445 SOME[(lhs, IL.OP(Op.Inside dim, [pos, use f']))])*)
174 : jhr 2356 | _ => raise Fail(concat[
175 :     "inside: bogus field binding ", V.toString f, " = ", IL.vbToString(V.binding f)
176 :     ])
177 : cchiw 2445 (* end case *))
178 : jhr 2356 | _ => NONE
179 :     (* end case *))
180 : jhr 1232
181 : cchiw 2400 *)
182 : cchiw 2496 | doRHS _ = NONE
183 : cchiw 2400
184 :    
185 : cchiw 2496
186 : jhr 2356 structure Rewrite = RewriteFn (
187 :     struct
188 :     structure IL = IL
189 :     val doAssign = doRHS
190 :     fun doMAssign _ = NONE
191 :     val elimUnusedVars = UnusedElim.reduce
192 :     end)
193 : jhr 1232
194 : jhr 2356 val transform = Rewrite.transform
195 : jhr 1232
196 :     end

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