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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3038 - (view) (download)

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 2481 val translate : (HighIL.var * Var.var * SimpleTypes.meta_arg list * HighIL.var list)
16 : jhr 2279 -> 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 2481 structure Ty = SimpleTypes
25 : jhr 194 structure VTbl = Var.Tbl
26 : jhr 180
27 : jhr 2481 fun trType (Ty.TY ty) = TranslateTy.tr ty
28 :     | trType _ = raise Fail "expected type"
29 :     fun dimVarToInt (Ty.DIM d) = d
30 :     | dimVarToInt _ = raise Fail "expected dim"
31 : jhr 2331 fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
32 : jhr 1116 fun dimVarToMatrix dv = let
33 : jhr 2331 val d = dimVarToInt dv
34 : jhr 2279 in
35 :     DstTy.tensorTy[d, d] (* square matrix type *)
36 :     end
37 : jhr 2481 fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
38 :     | shapeVarToTensor _ = raise Fail "expected shape"
39 : jhr 197
40 : jhr 1640 fun assign (y, rator, xs) = [IL.ASSGN(y, IL.OP(rator, xs))]
41 : jhr 407
42 : jhr 1640 fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, xs))]
43 : jhr 1116
44 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
45 :    
46 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
47 :    
48 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
49 :    
50 : jhr 1116 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
51 : jhr 197
52 : jhr 1640 (* utility functions for synthesizing eigenvector/eigenvalue code *)
53 :     fun eigenVec (rator, dim) = let
54 :     val ty = DstTy.SeqTy(DstTy.realTy, dim)
55 :     in
56 :     fn (y, _, [m]) => let
57 :     val v = IL.Var.new("evals", ty)
58 :     in
59 :     [IL.MASSGN([v, y], rator, [m])]
60 :     end
61 :     end
62 :     fun eigenVal (rator, dim) = let
63 :     val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
64 :     in
65 :     fn (y, _, [m]) => let
66 :     val v = IL.Var.new("evecs", ty)
67 :     in
68 :     [IL.MASSGN([y, v], rator, [m])]
69 :     end
70 :     end
71 :    
72 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
73 : jhr 2481 val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
74 : jhr 2279 val tbl = VTbl.mkTable (128, Fail "Translate table")
75 :     val insert = VTbl.insert tbl
76 :     in
77 :     List.app insert [
78 : jhr 2481 (BV.at_Td, fn (y, [tv], args) => assign(y, Op.Prepend(trType tv), args)),
79 :     (BV.at_dT, fn (y, [tv], args) => assign(y, Op.Append(trType tv), args)),
80 :     (BV.at_dd, fn (y, [tv], args) => assign(y, Op.Concat(trType tv), args)),
81 : jhr 2279 (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
82 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
83 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
84 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
85 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
86 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
87 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
88 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
89 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
90 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
91 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
92 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
93 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
94 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
95 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
96 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
97 :     (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)),
98 :     (BV.add_tt, tensorOp Op.Add),
99 :     (BV.add_ff, fn (y, _, [f, g]) => assign(y, Op.AddField, [f, g])),
100 :     (BV.add_fr, fn (y, _, [f, s]) => assign(y, Op.OffsetField, [f, s])),
101 :     (BV.add_rf, fn (y, _, [s, f]) => assign(y, Op.OffsetField, [f, s])),
102 :     (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)),
103 :     (BV.sub_tt, tensorOp Op.Sub),
104 :     (BV.sub_ff, fn (y, _, [f, g]) => assign(y, Op.SubField, [f, g])),
105 :     (BV.sub_fr, fn (y, _, [f, s]) => let
106 :     val s' = IL.Var.copy s
107 :     in [
108 :     IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])),
109 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s']))
110 :     ] end),
111 :     (BV.sub_rf, fn (y, _, [s, f]) => let
112 :     val f' = IL.Var.copy f
113 :     in [
114 :     IL.ASSGN(f', IL.OP(Op.NegField, [f])),
115 :     IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s]))
116 :     ] end),
117 :     (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)),
118 :     (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))),
119 :     (BV.mul_rt, tensorOp Op.Scale),
120 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
121 :     (BV.mul_rf, fn (y, _, [s, f]) => assign(y, Op.ScaleField, [s, f])),
122 :     (BV.mul_fr, fn (y, _, [f, s]) => assign(y, Op.ScaleField, [s, f])),
123 :     (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
124 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
125 :     (BV.div_tr, fn (y, [sv], [x, s]) => let
126 :     val one = IL.Var.new("one", DstTy.realTy)
127 :     val s' = IL.Var.new("sInv", DstTy.realTy)
128 :     in [
129 :     IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
130 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
131 :     IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x]))
132 :     ] end),
133 :     (BV.div_fr, fn (y, _, [f, s]) => let
134 :     val one = IL.Var.new("one", DstTy.realTy)
135 :     val s' = IL.Var.new("sInv", DstTy.realTy)
136 :     in [
137 :     IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
138 :     IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
139 :     IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f]))
140 :     ] end),
141 :     (BV.exp_ri, simpleOp(Op.Power)),
142 :     (BV.exp_rr, basisFn MathFuns.pow),
143 : jhr 2165 (BV.curl2D, fn (y, _, xs) => assign(y, Op.CurlField 2, xs)),
144 :     (BV.curl3D, fn (y, _, xs) => assign(y, Op.CurlField 3, xs)),
145 : jhr 2796 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
146 : jhr 2805 assign(y, Op.Field(DstTy.TensorTy[d], DstTy.TensorTy dd), xs)),
147 : jhr 2796 (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
148 : jhr 2805 assign(y, Op.Field(DstTy.TensorTy[d], DstTy.TensorTy dd), [v, k])),
149 : jhr 2279 (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
150 :     (BV.neg_t, tensorOp Op.Neg),
151 :     (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
152 :     (BV.op_probe, fn (y, [_, dv, sv], xs) =>
153 :     assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
154 :     (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
155 :     (BV.op_Dotimes, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
156 :     (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
157 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
158 :     | ty => assign(y, Op.Norm ty, xs)
159 :     (* end case *))),
160 :     (BV.op_not, simpleOp Op.Not),
161 :     (BV.op_cross, simpleOp Op.Cross),
162 : jhr 2481 (BV.op_outer, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
163 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)),
164 :     (BV.op_inner, fn (y, [Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) => let
165 :     val ty1 = DstTy.TensorTy dd1
166 :     val ty2 = DstTy.TensorTy dd2
167 : jhr 2279 val rator = (case (dd1, dd2)
168 :     of ([d], [d']) => Op.Dot ty1
169 :     | ([d1], [d1', d2]) => Op.MulVecMat ty2
170 :     | ([d1, d2], [d2']) => Op.MulMatVec ty1
171 :     | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
172 :     | ([d1], [d1', d2, d3]) => Op.MulVecTen3 ty2
173 :     | ([d1, d2, d3], [d3']) => Op.MulTen3Vec ty1
174 :     | _ => raise Fail(concat[
175 :     "unsupported inner-product: ",
176 :     DstTy.toString ty1, " * ", DstTy.toString ty2
177 :     ])
178 :     (* end case *))
179 :     in
180 :     assign (y, rator, xs)
181 :     end),
182 :     (BV.op_colon, fn (y, [sh1, sh2, _], xs) => let
183 : jhr 2481 val ty1 = shapeVarToTensor sh1
184 :     val ty2 = shapeVarToTensor sh2
185 : jhr 2279 in
186 :     assign (y, Op.ColonMul(ty1, ty2), xs)
187 :     end),
188 : jhr 2481 (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) =>
189 :     assign(y, Op.Inside d, xs)),
190 : jhr 2279 (BV.clamp_rrr, simpleOp (Op.Clamp DstTy.realTy)),
191 :     (BV.clamp_vvv, vectorOp Op.Clamp),
192 :     (BV.lerp3, tensorOp Op.Lerp),
193 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
194 :     val t1 = IL.Var.new("t1", DstTy.realTy)
195 :     val t2 = IL.Var.new("t2", DstTy.realTy)
196 :     val t3 = IL.Var.new("t3", DstTy.realTy)
197 :     in [
198 :     IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
199 :     IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
200 :     IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
201 :     IL.ASSGN(y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
202 :     ] end),
203 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
204 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
205 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
206 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
207 : jhr 2481 (BV.fn_length, fn (y, [tv], [s]) => assign(y, Op.Length(trType tv), [s])),
208 : jhr 2279 (BV.fn_max, simpleOp Op.Max),
209 :     (BV.fn_min, simpleOp Op.Min),
210 :     (BV.fn_modulate, vectorOp Op.Mul),
211 :     (BV.fn_normalize, vectorOp Op.Normalize),
212 :     (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
213 :     (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
214 : jhr 2481 (BV.fn_transpose, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
215 :     assign(y, Op.Transpose(d1, d2), xs)),
216 : jhr 2279 (BV.kn_bspln3, kernel Kernel.bspln3),
217 :     (BV.kn_bspln5, kernel Kernel.bspln5),
218 :     (BV.kn_ctmr, kernel Kernel.ctmr),
219 :     (BV.kn_c2ctmr, kernel Kernel.ctmr),
220 :     (BV.kn_c4hexic, kernel Kernel.c4hexic),
221 :     (BV.kn_tent, kernel Kernel.tent),
222 :     (BV.kn_c1tent, kernel Kernel.tent),
223 :     (BV.i2r, simpleOp Op.IntToReal),
224 : jhr 2481 (BV.identity, fn (y, [Ty.DIM d], []) =>
225 :     assign(y, Op.Identity d, [])),
226 : jhr 2279 (BV.zero, fn (y, [sv], []) =>
227 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
228 : jhr 3038 (BV.nan, fn (y, [Ty.SHAPE dd], []) => let
229 :     val nan = IL.LIT(Literal.Float(FloatLit.nan))
230 :     fun mk (y, [], stms) = IL.ASSGN(y, nan) :: stms
231 :     | mk (y, d::dd, stms) = let
232 :     val ty = shapeVarToTensor(Ty.SHAPE dd)
233 :     val zs = List.tabulate(d, fn _ => IL.Var.new("_nan", ty))
234 :     in
235 :     IL.ASSGN(y, IL.CONS(IL.Var.ty y, zs)) ::
236 :     List.foldl (fn (z, stms) => mk(z, dd, stms)) stms zs
237 :     end
238 :     in
239 :     List.rev (mk (y, dd, []))
240 :     end),
241 : jhr 2481 (BV.subscript, fn (y, [tv, Ty.DIM d], args) =>
242 : jhr 1640 assign(y,
243 : jhr 2481 Op.SeqSub(DstTy.SeqTy(trType tv, d)),
244 : jhr 2021 args)),
245 : jhr 2279 (BV.dynSubscript, fn (y, [tv], args) =>
246 : jhr 2962 assign(y, Op.SeqSub(DstTy.DynSeqTy(trType tv)), args)),
247 : jhr 2998 (* image operations *)
248 :     (BV.fn_size, fn (y, [Ty.DIM d, _], [img]) => let
249 :     val DstTy.ImageTy info = IL.Var.ty img
250 :     (* we extract each dimension separately and then build the sequence value *)
251 :     val dims = List.tabulate(d, fn i => IL.Var.new("i"^Int.toString i, DstTy.IntTy))
252 :     fun mkStms ([], _, stms) = stms (* in reverse order! *)
253 :     | mkStms (d::dr, i, stms) = mkStms (dr, i+1,
254 :     IL.ASSGN(d, IL.OP(Op.ImageDim(info, i), [img])) :: stms)
255 :     in
256 :     List.revAppend (mkStms (dims, 0, []), [
257 :     IL.ASSGN(y, IL.CONS(DstTy.SeqTy(DstTy.intTy, d), dims))
258 :     ])
259 :     end),
260 :     (BV.image_border, fn (y, _, args as [img, _]) => let
261 :     val DstTy.ImageTy info = IL.Var.ty img
262 :     in
263 :     assign(y, Op.BorderCtlDefault info, args)
264 :     end),
265 :     (BV.image_clamp, fn (y, _, args as [img]) => let
266 :     val DstTy.ImageTy info = IL.Var.ty img
267 :     in
268 :     assign(y, Op.BorderCtlClamp info, args)
269 :     end),
270 :     (BV.image_mirror, fn (y, _, args as [img]) => let
271 :     val DstTy.ImageTy info = IL.Var.ty img
272 :     in
273 :     assign(y, Op.BorderCtlMirror info, args)
274 :     end),
275 :     (BV.image_wrap, fn (y, _, args as [img]) => let
276 :     val DstTy.ImageTy info = IL.Var.ty img
277 :     in
278 :     assign(y, Op.BorderCtlWrap info, args)
279 :     end)
280 : jhr 2279 ];
281 :     (* add C math functions *)
282 :     List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;
283 :     tbl
284 :     end
285 : jhr 180
286 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
287 : jhr 2279 of SOME transFn => transFn(y, mvs, xs)
288 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
289 :     (* end case *))
290 : jhr 1116 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
291 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
292 : jhr 180
293 :     end

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