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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-to-mid/clean-params.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-to-mid/clean-params.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3561 - (view) (download)
Original Path: branches/vis15/src/compiler/high-to-mid/clean-param.sml

1 : jhr 3561 (* clean-param.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     (*
10 :     *cleanParam.sml cleans the parameters in an EIN expression.
11 :     *Cleaning parameters is simple.
12 :     *We keep track of all the paramids used in subexpression(getIdCount()),
13 :     *remap the param ids(mkMap)
14 :     *and choosing the mid-il args that are used, and then lastly rewrites the body.
15 :     *)
16 :     structure CleanParams : sig
17 :    
18 :     val clean ? -> ?
19 :    
20 :     end = struct
21 :    
22 :     structure E = Ein
23 :     structure DstIL = MidIL
24 :     structure DstV = DstIL.Var
25 :    
26 :     (* FIXME *)
27 :     fun insert (key, value) d =fn s =>
28 :     if s = key then SOME value
29 :     else d s
30 :     fun lookup k d = d k
31 :     val empty =fn key =>NONE
32 :    
33 :     fun err str=raise Fail str
34 :     fun iTos i =Int.toString i
35 :     fun setEin(params,index,body)=Ein.EIN{params=params, index=index, body=body}
36 :     fun cnt(DstIL.V{useCnt, ...}) = !useCnt
37 :     fun incUse (DstIL.V{useCnt, ...}) = (useCnt := !useCnt + 2)
38 :     fun use x = (incUse x; x)
39 :     fun getUse e=String.concat["\n\t Varname "^DstIL.Var.name e," -use ",iTos(cnt e)]
40 :     fun assignEinApp(y,params,index,body,args)= (y,DstIL.EINAPP(setEin(params,index,body),args))
41 :    
42 :     (*dictionary to lookup mapp*)
43 :     fun lkupIndexSingle(e1,mapp,str)=(case (lookup e1 mapp)
44 :     of SOME l=>l
45 :     | _=> raise Fail(str^iTos(e1))
46 :     (*endcase*))
47 :    
48 :     (* mkMapp:dict*params*var list ->dict*params*var list
49 :     * countmapp dictionary keeps track of which ids have been used
50 :     * mapp id the dictionary of the new ids
51 :     *)
52 :     fun mkMapp(countmapp,params,args)=let
53 :     val n=(length params)
54 :     val ix=List.tabulate(n, (fn e=> e))
55 :     fun m([],_,mapp,p, _, a, _)=(mapp,p,a)
56 :     | m(i::ix,j,mapp,p, p1::params, a, a1::arg)=(case (lookup i countmapp)
57 :     of SOME _=>let
58 :     val _ =testp["\nInserting into dictionary",iTos i,"=>",iTos j]
59 :     val mapp2=insert(i,j) mapp
60 :     in
61 :     m(ix,j+1,mapp2,p@[p1],params, a@[a1],arg)
62 :     end
63 :     | _ => m(ix,j,mapp,p, params, a, arg)
64 :     (*end case*))
65 :     | m (_,_,_,_,_,_,[]) = raise Fail (String.concat["incorrect number of params:more params:",Int.toString(n),
66 :     "args:",Int.toString(length(args))])
67 :     | m (_,_,_,_,[],_,_) = raise Fail ("incorrect number of params:more args")
68 :     (*| m _ = raise Fail ("incorrect number of params:unsure")*)
69 :     val _ =testp["ix created up to",iTos(n),"length of params",
70 :     iTos(length params),"lengths of args",iTos(length args)]
71 :     val (mapp,Nparams,Nargs)=m(ix, 0, empty, [],params,[],args)
72 :     in
73 :     (mapp,Nparams,Nargs)
74 :     end
75 :    
76 :     (*getIdCount: ein_exp ->dict
77 :     *rewrite ids in exp using mapp
78 :     *)
79 :     fun getIdCount b =let
80 :     fun rewriteExp(b,mapp)=let
81 :     fun iterList([],mapp)=mapp
82 :     | iterList(e1::es,mapp)=let
83 :     val mapp1=rewriteExp(e1,mapp)
84 :     in iterList(es,mapp1) end
85 :     in (case b
86 :     of E.B _ => mapp
87 :     | E.Tensor(id,_) => insert(id,1) mapp
88 :     | E.G _ => mapp
89 :     | E.Field _ => mapp
90 :     | E.Lift _ => mapp
91 :     | E.Conv(v,_,h,_) => insert(h,1) (insert(v,1) mapp)
92 :     | E.Partial _ => mapp
93 :     | E.Apply _ => mapp
94 :     | E.Probe(e1,e2) => iterList ([e1,e2],mapp)
95 :     | E.Value _ => err "Should not be here"
96 :     | E.Img _ => err "should not be here"
97 :     | E.Krn _ => err "Should not be here"
98 :     | E.Sum(_ ,e1) => rewriteExp (e1,mapp)
99 :     | E.Op1(_,e1) => rewriteExp (e1,mapp)
100 :     | E.Op2(_,e1,e2) => iterList ([e1,e2],mapp)
101 :     | E.Opn(_, es) => iterList (es,mapp)
102 :     (*end case*))
103 :     end
104 :     in
105 :     rewriteExp(b,empty)
106 :     end
107 :    
108 :     (*rewriteParam:dict*ein_exp ->ein_exp
109 :     *rewrite ids in exp using mapp
110 :     *)
111 :     fun rewriteParam(mapp,e)=let
112 :     fun getId id = lkupIndexSingle(id,mapp,"Mapp doesn't have Param Id ")
113 :     fun rewriteExp b=(case b
114 :     of E.B _ => b
115 :     | E.Tensor(id,alpha) => E.Tensor(getId id, alpha)
116 :     | E.G _ => b
117 :     | E.Field _ => raise Fail "should have been swept"
118 :     | E.Lift _ => raise Fail "should have been swept"
119 :     | E.Conv _ => raise Fail "should have been swept"
120 :     | E.Partial _ => raise Fail "should have been swept"
121 :     | E.Apply _ => raise Fail "should have been swept"
122 :     | E.Probe(E.Conv(v,alpha,h,dx),t)
123 :     => E.Probe(E.Conv(getId v, alpha,getId h,dx),rewriteExp t)
124 :     | E.Probe _ => raise Fail "should have been swept"
125 :     | E.Value e1 => raise Fail"Should not be here"
126 :     | E.Img _ => raise Fail "should not be here"
127 :     | E.Krn _ => raise Fail"Should not be here"
128 :     | E.Sum(sx ,e1) => E.Sum(sx,rewriteExp e1)
129 :     | E.Op1(op1, e1) => E.Op1(op1,rewriteExp e1)
130 :     | E.Op2(op2, e1,e2) => E.Op2(op2,rewriteExp e1,rewriteExp e2)
131 :     | E.Opn(opn, es) => E.Opn(opn,List.map rewriteExp es)
132 :     (*end case*))
133 :     in
134 :     rewriteExp e
135 :     end
136 :    
137 :     (* cleanParams:var*ein_exp*param*index* var list ->code
138 :     *cleans params
139 :     *)
140 :     fun clean (y, body, params, index, args) = let
141 :     val countmapp=getIdCount body
142 :     val (mapp,Nparams,Nargs)=mkMapp(countmapp,params,args)
143 :     val Nargs =List.map (fn e=> use e) Nargs
144 :     val Nbody=rewriteParam(mapp,body)
145 :     val einapp= assignEinApp(y,Nparams,index,Nbody,Nargs)
146 :     in
147 :     einapp
148 :     end
149 :    
150 :     end (* CleanParam *)

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