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 3563 - (view) (download)

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 : jhr 3562 structure DstIR = MidIR
24 :     structure DstV = DstIR.Var
25 : jhr 3561
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 : jhr 3562 fun cnt (DstIR.V{useCnt, ...}) = !useCnt
36 :     fun incUse (DstIR.V{useCnt, ...}) = (useCnt := !useCnt + 2)
37 : jhr 3561 fun use x = (incUse x; x)
38 : jhr 3562 fun getUse e=String.concat["\n\t Varname "^DstIR.Var.name e," -use ",iTos(cnt e)]
39 : jhr 3561
40 :     (*dictionary to lookup mapp*)
41 :     fun lkupIndexSingle(e1,mapp,str)=(case (lookup e1 mapp)
42 :     of SOME l=>l
43 :     | _=> raise Fail(str^iTos(e1))
44 :     (*endcase*))
45 :    
46 :     (* mkMapp:dict*params*var list ->dict*params*var list
47 :     * countmapp dictionary keeps track of which ids have been used
48 :     * mapp id the dictionary of the new ids
49 :     *)
50 : jhr 3562 fun mkMapp (countmapp, params, args) = let
51 :     val n = length params
52 :     val ix = List.tabulate(n, fn e => e)
53 :     fun m ([], _, mapp, p, _, a, _) = (mapp, p, a)
54 :     | m (i::ix, j, mapp, p, p1::params, a, a1::arg) = (case (lookup i countmapp)
55 :     of SOME _ => let
56 :     val mapp2 = insert(i,j) mapp
57 :     in
58 :     m (ix, j+1, mapp2, p@[p1], params, a@[a1], arg)
59 :     end
60 :     | _ => m (ix, j, mapp, p, params, a, arg)
61 :     (* end case *))
62 :     | m (_, _, _, _, _, _, []) = raise Fail (String.concat[
63 :     "incorrect number of params:more params:", Int.toString n,
64 :     " args:", Int.toString(length args)
65 :     ])
66 :     | m (_, _, _, _, [], _, _) = raise Fail ("incorrect number of params:more args")
67 :     in
68 :     m (ix, 0, empty, [], params, [], args)
69 :     end
70 : jhr 3561
71 :     (*getIdCount: ein_exp ->dict
72 :     *rewrite ids in exp using mapp
73 :     *)
74 : jhr 3562 fun getIdCount b = let
75 :     fun rewrite (b, mapp) = (case b
76 :     of E.Tensor(id, _) => insert(id,1) mapp
77 :     | E.Conv(v, _, h, _) => insert(h,1) (insert(v,1) mapp)
78 :     | E.Probe(e1, e2) => rewrite (e2, rewrite (e1, mapp))
79 :     | E.Value _ => err "should not be here"
80 :     | E.Img _ => err "should not be here"
81 :     | E.Krn _ => err "should not be here"
82 :     | E.Sum(_, e1) => rewrite (e1, mapp)
83 :     | E.Op1(_, e1) => rewrite (e1, mapp)
84 :     | E.Op2(_, e1, e2) => rewrite (e2, rewrite (e1, mapp))
85 :     | E.Opn(_, es) => List.foldl rewrite mapp ex
86 :     | _ => mapp
87 :     (* end case *))
88 : jhr 3561 end
89 : jhr 3562 in
90 :     rewrite (b, empty)
91 :     end
92 : jhr 3561
93 :     (*rewriteParam:dict*ein_exp ->ein_exp
94 :     *rewrite ids in exp using mapp
95 :     *)
96 : jhr 3562 fun rewriteParam (mapp, e) = let
97 :     fun getId id = lkupIndexSingle(id,mapp,"Mapp doesn't have Param Id ")
98 :     fun rewriteExp b = (case b
99 :     of E.Tensor(id, alpha) => E.Tensor(getId id, alpha)
100 :     | E.Probe(E.Conv(v, alpha, h, dx), t) =>
101 :     E.Probe(E.Conv(getId v, alpha,getId h,dx),rewriteExp t)
102 :     | E.Sum(sx ,e1) => E.Sum(sx,rewriteExp e1)
103 :     | E.Op1(op1, e1) => E.Op1(op1,rewriteExp e1)
104 :     | E.Op2(op2, e1,e2) => E.Op2(op2,rewriteExp e1,rewriteExp e2)
105 :     | E.Opn(opn, es) => E.Opn(opn,List.map rewriteExp es)
106 :     (* end case *))
107 :     in
108 : jhr 3561 rewriteExp e
109 : jhr 3562 end
110 : jhr 3561
111 :     (* cleanParams:var*ein_exp*param*index* var list ->code
112 :     *cleans params
113 :     *)
114 :     fun clean (y, body, params, index, args) = let
115 : jhr 3562 val countmapp = getIdCount body
116 :     val (mapp, Nparams, Nargs) = mkMapp (countmapp, params, args)
117 :     val Nargs = List.map use Nargs
118 :     val Nbody = rewriteParam (mapp, body)
119 :     in
120 :     (y, DstIR.EINAPP(Ein.EIN{params=Nparams, index=index, body=Nbody}, Nargs))
121 :     end
122 : jhr 3561
123 :     end (* CleanParam *)

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