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

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