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

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