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

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