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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3476 - (view) (download)

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

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