Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/charisee/src/compiler/translate/translate-basis.sml
ViewVC logotype

Annotation of /branches/charisee/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1640 - (view) (download)
Original Path: trunk/src/compiler/translate/translate-basis.sml

1 : jhr 180 (* translate-basis.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 180 * All rights reserved.
5 :     *
6 :     * Translation for basis operations in Simple AST to HighIL code
7 :     *)
8 :    
9 :     structure TranslateBasis : sig
10 :    
11 : jhr 189 (* translate(lhs, f, mvs, args) translates the application of f (specialized
12 :     * to the instantiated meta variables mvs) to a list of SSA assignments in
13 :     * reverse order.
14 :     *)
15 : jhr 180 val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list)
16 : jhr 1640 -> HighIL.assignment list
17 : jhr 180
18 :     end = struct
19 :    
20 : jhr 194 structure BV = BasisVars
21 : jhr 180 structure IL = HighIL
22 : jhr 392 structure DstTy = HighILTypes
23 : jhr 180 structure Op = HighOps
24 : jhr 183 structure Ty = Types
25 :     structure TU = TypeUtil
26 :     structure MV = MetaVar
27 : jhr 194 structure VTbl = Var.Tbl
28 : jhr 180
29 : jhr 1640 fun pruneTy ty = (case TU.prune ty
30 :     of (Ty.T_Var _) => raise Fail("unresolved type " ^ TU.toString ty)
31 :     | ty => TranslateTy.tr ty
32 :     (* end case *))
33 :    
34 : jhr 197 fun pruneDim d = (case TU.pruneDim d
35 :     of (Ty.DimConst n) => n
36 :     | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
37 :     (* end case *))
38 :    
39 : jhr 407 fun pruneShape sv = (case TU.pruneShape(MV.toShape sv)
40 : jhr 426 of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd)
41 : jhr 183 | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
42 :     (* end case *))
43 :    
44 : jhr 426 fun dimVarToTensor dv = DstTy.tensorTy[pruneDim(MV.toDim dv)]
45 : jhr 1116 fun dimVarToMatrix dv = let
46 :     val d = pruneDim(MV.toDim dv)
47 :     in
48 :     DstTy.tensorTy[d, d] (* square matrix type *)
49 :     end
50 : jhr 407 fun shapeVarToTensor sv = pruneShape sv
51 : jhr 197
52 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
53 : jhr 407
54 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
55 : jhr 1116
56 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
57 :    
58 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
59 :    
60 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
61 :    
62 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
63 : jhr 197
64 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
65 :     fun eigenVec (rator, dim) = let
66 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
67 :     in
68 :     fn (y, _, [m]) => let
69 :     val v = IL.Var.new("evals", ty)
70 :     in
71 :     [IL.MASSGN([v, y], rator, [m])]
72 :     end
73 :     end
74 :     fun eigenVal (rator, dim) = let
75 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
76 :     in
77 :     fn (y, _, [m]) => let
78 :     val v = IL.Var.new("evecs", ty)
79 :     in
80 :     [IL.MASSGN([y, v], rator, [m])]
81 :     end
82 :     end
83 :    
84 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
85 : jhr 1640 val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
86 : jhr 194 val tbl = VTbl.mkTable (128, Fail "Translate table")
87 : jhr 180 in
88 : jhr 194 List.app (VTbl.insert tbl) [
89 : jhr 392 (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
90 : jhr 1295 (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
91 : jhr 392 (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
92 : jhr 1295 (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
93 : jhr 392 (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
94 : jhr 426 (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
95 : jhr 392 (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
96 : jhr 426 (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
97 : jhr 392 (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
98 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
99 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
100 : jhr 426 (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
101 : jhr 392 (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
102 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
103 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
104 : jhr 426 (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
105 : jhr 1116 (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)),
106 :     (BV.add_tt, tensorOp Op.Add),
107 :     (BV.add_ff, fn (y, _, [f, g]) => assign(y, Op.AddField, [f, g])),
108 :     (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)),
109 :     (BV.sub_tt, tensorOp Op.Sub),
110 :     (BV.sub_ff, fn (y, _, [f, g]) => assign(y, Op.SubField, [f, g])),
111 :     (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)),
112 :     (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))),
113 :     (BV.mul_rt, tensorOp Op.Scale),
114 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
115 :     (BV.mul_rf, fn (y, _, [s, f]) => assign(y, Op.ScaleField, [s, f])),
116 :     (BV.mul_fr, fn (y, _, [f, s]) => assign(y, Op.ScaleField, [s, f])),
117 :     (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
118 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
119 :     (BV.div_tr, fn (y, [sv], [x, s]) => let
120 :     val one = IL.Var.new("one", DstTy.realTy)
121 :     val s' = IL.Var.new("sInv", DstTy.realTy)
122 :     in [
123 : jhr 1640 IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
124 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
125 :     IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x]))
126 : jhr 1116 ] end),
127 :     (BV.div_fr, fn (y, _, [f, s]) => let
128 :     val one = IL.Var.new("one", DstTy.realTy)
129 :     val s' = IL.Var.new("sInv", DstTy.realTy)
130 :     in [
131 : jhr 1640 IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
132 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
133 :     IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f]))
134 : jhr 1116 ] end),
135 :     (BV.exp_ri, simpleOp(Op.Power)),
136 :     (BV.exp_rr, basisFn ILBasis.pow),
137 :     (BV.convolve_vk, fn (y, [_, dv, _], xs) =>
138 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)),
139 :     (BV.convolve_kv, fn (y, [_, dv, _], [k, v]) =>
140 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [v, k])),
141 : jhr 392 (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
142 : jhr 194 (BV.neg_t, tensorOp Op.Neg),
143 : jhr 197 (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
144 : jhr 407 (BV.op_at, fn (y, [_, dv, sv], xs) =>
145 : jhr 1116 assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
146 : jhr 197 (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
147 : jhr 1383 (BV.op_Dotimes, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
148 : jhr 1116 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
149 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
150 :     | ty => assign(y, Op.Norm ty, xs)
151 :     (* end case *))),
152 : jhr 194 (BV.op_not, simpleOp Op.Not),
153 : jhr 1116 (BV.fn_atan2, basisFn ILBasis.atan2),
154 :     (BV.fn_convolve, fn (y, [_, dv, _], [h, img]) =>
155 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [img, h])),
156 :     (BV.fn_cos, basisFn ILBasis.cos),
157 :     (BV.op_cross, simpleOp Op.Cross),
158 :     (BV.fn_cross, simpleOp Op.Cross),
159 :     (BV.op_outer, fn (y, [dv1, dv2], xs) => let
160 :     val d1 = pruneDim(MV.toDim dv1)
161 :     val d2 = pruneDim(MV.toDim dv2)
162 :     in
163 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
164 :     end),
165 :     (BV.fn_outer, fn (y, [dv1, dv2], xs) => let
166 :     val d1 = pruneDim(MV.toDim dv1)
167 :     val d2 = pruneDim(MV.toDim dv2)
168 :     in
169 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
170 :     end),
171 :     (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
172 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
173 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
174 :     val rator = (case (dd1, dd2)
175 :     of ([d], [d']) => Op.Dot ty1
176 :     | ([d1], [d1', d2]) => Op.MulVecMat ty2
177 :     | ([d1, d2], [d2']) => Op.MulMatVec ty1
178 :     | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
179 :     | _ => raise Fail "unsupported inner-product type"
180 :     (* end case *))
181 :     in
182 :     assign (y, rator, xs)
183 :     end),
184 : jhr 197 (BV.fn_dot, vectorOp Op.Dot),
185 : jhr 407 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
186 : jhr 1116 assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
187 : jhr 1295 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
188 :     (BV.clamp_vvv, vectorOp Op.Clamp),
189 : jhr 1116 (BV.lerp3, tensorOp Op.Lerp),
190 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
191 :     val t1 = IL.Var.new("t1", DstTy.realTy)
192 :     val t2 = IL.Var.new("t2", DstTy.realTy)
193 :     val t3 = IL.Var.new("t3", DstTy.realTy)
194 :     in [
195 : jhr 1640 IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
196 :     IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
197 :     IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
198 :     IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
199 : jhr 1116 ] end),
200 : jhr 1640 (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
201 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
202 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
203 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
204 : jhr 263 (BV.fn_max, simpleOp Op.Max),
205 :     (BV.fn_min, simpleOp Op.Min),
206 : jhr 197 (BV.fn_modulate, vectorOp Op.Mul),
207 : jhr 1116 (BV.fn_normalize, vectorOp Op.Normalize),
208 :     (BV.fn_outer, fn _ => raise Fail "outer not implemented yet"), (* FIXME *)
209 :     (BV.fn_pow, basisFn ILBasis.pow),
210 : jhr 197 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
211 : jhr 1116 (BV.fn_sin, basisFn ILBasis.sin),
212 :     (BV.fn_sqrt, basisFn ILBasis.sqrt),
213 :     (BV.fn_tan, basisFn ILBasis.tan),
214 :     (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
215 : jhr 197 (BV.kn_bspln3, kernel Kernel.bspln3),
216 :     (BV.kn_bspln5, kernel Kernel.bspln5),
217 :     (BV.kn_ctmr, kernel Kernel.ctmr),
218 : jhr 1116 (BV.kn_c2ctmr, kernel Kernel.ctmr),
219 : jhr 197 (BV.kn_tent, kernel Kernel.tent),
220 : jhr 1116 (BV.kn_c1tent, kernel Kernel.tent),
221 :     (BV.i2r, simpleOp Op.IntToReal),
222 :     (BV.identity, fn (y, [dv], []) =>
223 :     assign(y, Op.Identity(pruneDim(MV.toDim dv)), [])),
224 :     (BV.zero, fn (y, [sv], []) =>
225 : jhr 1640 assign(y, Op.Zero(shapeVarToTensor sv), [])),
226 :     (BV.subscript, fn (y, [tv, dv], args) =>
227 :     assign(y,
228 :     Op.SeqSub(DstTy.SeqTy(pruneTy(MV.toType tv), pruneDim(MV.toDim dv))),
229 :     args))
230 : jhr 180 ];
231 :     tbl
232 :     end
233 :    
234 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
235 : jhr 197 of SOME transFn => transFn(y, mvs, xs)
236 : jhr 407 | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
237 : jhr 180 (* end case *))
238 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
239 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
240 : jhr 180
241 :     end

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