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 3574 - (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 3574 structure ISet = IntRedBlackSet
28 : jhr 3561
29 :     (*dictionary to lookup mapp*)
30 : jhr 3564 fun lookupSingleIndex (e1, mapp, str) = (case IMap.find(mapp, e1)
31 :     of SOME l => l
32 :     | _ => raise Fail(str ^ Int.toString e1)
33 :     (* end case *))
34 : jhr 3561
35 :     (* mkMapp:dict*params*var list ->dict*params*var list
36 :     * countmapp dictionary keeps track of which ids have been used
37 :     * mapp id the dictionary of the new ids
38 :     *)
39 : jhr 3574 fun mkMapp (freeParams, params, args) = let
40 :     fun m (_, _, mapp, p, [], a, []) = (mapp, rev p, rev a)
41 :     | m (i, j, mapp, p, p1::params, a, a1::arg) =
42 :     if ISet.member(freeParams, i)
43 :     then let
44 :     val mapp2 = IMap.insert(mapp, i, j)
45 :     in
46 :     m (i+1, j+1, mapp2, p1::p, params, a1::a, arg)
47 :     end
48 :     else m (i+1, j, mapp, p, params, a, arg)
49 :     | m (_, _, _, _, _, _, []) = raise Fail "too many parameters"
50 :     | m (_, _, _, _, [], _, _) = raise Fail "too many args"
51 : jhr 3562 in
52 : jhr 3574 m (0, 0, IMap.empty, [], params, [], args)
53 : jhr 3562 end
54 : jhr 3561
55 : jhr 3574 (* walk the ein expression and compute the set of free parameter indices (i.e., tensor,
56 :     * image, and kernel variables) in the expression.
57 :     *)
58 : jhr 3562 fun getIdCount b = let
59 : jhr 3574 fun walk (b, mapp) = (case b
60 :     of E.Tensor(id, _) => ISet.add(mapp, id)
61 :     | E.Conv(v, _, h, _) => ISet.add(ISet.add(mapp, h), v)
62 :     | E.Probe(e1, e2) => walk (e2, walk (e1, mapp))
63 : jhr 3564 | E.Value _ => raise Fail "unexpected Value"
64 :     | E.Img _ => raise Fail "unexpected Img"
65 :     | E.Krn _ => raise Fail "unexpected Krn"
66 : jhr 3574 | E.Sum(_, e1) => walk (e1, mapp)
67 :     | E.Op1(_, e1) => walk (e1, mapp)
68 :     | E.Op2(_, e1, e2) => walk (e2, walk (e1, mapp))
69 :     | E.Opn(_, es) => List.foldl walk mapp es
70 : jhr 3562 | _ => mapp
71 :     (* end case *))
72 :     in
73 : jhr 3574 walk (b, ISet.empty)
74 : jhr 3562 end
75 : jhr 3561
76 :     (*rewriteParam:dict*ein_exp ->ein_exp
77 :     *rewrite ids in exp using mapp
78 :     *)
79 : jhr 3562 fun rewriteParam (mapp, e) = let
80 : jhr 3574 fun getId id = lookupSingleIndex(id, mapp, "Mapp doesn't have Param Id ")
81 :     fun rewrite b = (case b
82 : jhr 3562 of E.Tensor(id, alpha) => E.Tensor(getId id, alpha)
83 :     | E.Probe(E.Conv(v, alpha, h, dx), t) =>
84 : jhr 3574 E.Probe(E.Conv(getId v, alpha, getId h, dx), rewrite t)
85 :     | E.Sum(sx ,e1) => E.Sum(sx, rewrite e1)
86 :     | E.Op1(op1, e1) => E.Op1(op1, rewrite e1)
87 :     | E.Op2(op2, e1,e2) => E.Op2(op2, rewrite e1, rewrite e2)
88 :     | E.Opn(opn, es) => E.Opn(opn, List.map rewrite es)
89 : jhr 3572 | _ => b
90 : jhr 3562 (* end case *))
91 :     in
92 : jhr 3574 rewrite e
93 : jhr 3562 end
94 : jhr 3561
95 :     (* cleanParams:var*ein_exp*param*index* var list ->code
96 :     *cleans params
97 :     *)
98 :     fun clean (y, body, params, index, args) = let
99 : jhr 3574 val freeParams = getIdCount body
100 :     val (mapp, Nparams, Nargs) = mkMapp (freeParams, params, args)
101 : jhr 3562 val Nbody = rewriteParam (mapp, body)
102 :     in
103 :     (y, DstIR.EINAPP(Ein.EIN{params=Nparams, index=index, body=Nbody}, Nargs))
104 :     end
105 : jhr 3561
106 :     end (* CleanParam *)

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