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 3978 - (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 : jhr 3478 * Translation for basis operations in Simple AST to HighIR code
9 : jhr 3471 *)
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 : jhr 3478 val translate : (HighIR.var * Var.t * SimpleTypes.meta_arg list * HighIR.var list)
18 :     -> HighIR.assignment list
19 : jhr 3471
20 :     end = struct
21 :    
22 :     structure BV = BasisVars
23 : jhr 3478 structure IR = HighIR
24 :     structure DstTy = HighTypes
25 : jhr 3471 structure Op = HighOps
26 :     structure Ty = SimpleTypes
27 :     structure VTbl = Var.Tbl
28 : jhr 3478 structure Mk = MkOperators
29 : jhr 3471
30 :     fun trType (Ty.TY ty) = TranslateTy.tr ty
31 :     | trType _ = raise Fail "expected type"
32 :     fun dimVarToInt (Ty.DIM d) = d
33 :     | dimVarToInt _ = raise Fail "expected dim"
34 :     fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
35 :     fun dimVarToMatrix dv = let
36 :     val d = dimVarToInt dv
37 :     in
38 :     DstTy.tensorTy[d, d] (* square matrix type *)
39 :     end
40 :     fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
41 :     | shapeVarToTensor _ = raise Fail "expected shape"
42 :    
43 : jhr 3478 fun assign (y, rator, xs) = [IR.ASSGN(y, IR.OP(rator, xs))]
44 : jhr 3471
45 :     fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
46 :    
47 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
48 :    
49 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
50 :    
51 :     fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
52 :    
53 :     (* utility functions for synthesizing eigenvector/eigenvalue code *)
54 :     fun eigenVec (rator, dim) = let
55 : jhr 3478 val ty = DstTy.SeqTy(DstTy.realTy, SOME dim)
56 : jhr 3471 in
57 :     fn (y, _, [m]) => let
58 : jhr 3478 val v = IR.Var.new("evals", ty)
59 : jhr 3471 in
60 : jhr 3478 [IR.MASSGN([v, y], rator, [m])]
61 : jhr 3471 end
62 :     end
63 :     fun eigenVal (rator, dim) = let
64 : jhr 3478 val ty = DstTy.SeqTy(DstTy.vecTy dim, SOME dim)
65 : jhr 3471 in
66 :     fn (y, _, [m]) => let
67 : jhr 3478 val v = IR.Var.new("evecs", ty)
68 : jhr 3471 in
69 : jhr 3478 [IR.MASSGN([y, v], rator, [m])]
70 : jhr 3471 end
71 :     end
72 :    
73 : jhr 3478 fun assignEin (y, rator, xs) = IR.ASSGN(y, IR.EINAPP(rator, xs))
74 :    
75 :     fun simpleEOp rator (y, _, xs) = [assignEin(y, rator, xs)]
76 :    
77 : jhr 3498 fun dist (y, d, xs) = let
78 :     val t0 = IR.Var.new("t0", DstTy.TensorTy[d])
79 :     in [
80 :     assignEin(t0, Mk.subTT[d], xs),
81 :     assignEin(y, Mk.normT[d], [t0])]
82 :     end
83 : cchiw 3494
84 : jhr 3471 (* build a table that maps Basis variables to their translation functions *)
85 : jhr 3478 val tbl : ((IR.var * Ty.meta_arg list * IR.var list) -> IR.assignment list) VTbl.hash_table = let
86 : jhr 3471 val tbl = VTbl.mkTable (128, Fail "Translate table")
87 : jhr 3478 fun insert (id, def) = (case VTbl.find tbl id
88 : jhr 3498 of NONE => VTbl.insert tbl (id, def)
89 :     | SOME _ => raise Fail("duplicate definition of " ^ Var.nameOf id)
90 :     (* end case *))
91 : jhr 3471 in
92 :     List.app insert [
93 :     (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
94 :     (BV.lt_rr, simpleOp(Op.LT DstTy.realTy)),
95 :     (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
96 :     (BV.lte_rr, simpleOp(Op.LTE DstTy.realTy)),
97 :     (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
98 :     (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
99 :     (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
100 :     (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
101 :     (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
102 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
103 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
104 :     (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
105 :     (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
106 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
107 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
108 :     (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
109 : jhr 3476 (BV.add_ii, simpleOp Op.IAdd),
110 :     (BV.sub_ii, simpleOp Op.ISub),
111 :     (BV.mul_ii, simpleOp Op.IMul),
112 :     (BV.div_ii, simpleOp Op.IDiv),
113 : jhr 3478 (BV.op_mod, simpleOp Op.IMod),
114 :     (BV.neg_i, simpleOp Op.INeg),
115 :     (BV.add_tt, fn (y, [shp], xs) => let
116 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
117 :     val rator = Mk.addTT(dd1)
118 :     in
119 :     [assignEin(y, rator, xs)]
120 :     end),
121 :     (BV.add_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
122 :     [assignEin(y, Mk.addFF(d, dd), xs)]),
123 :     (BV.add_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
124 :     [assignEin(y, Mk.addTF(d, dd), [s, f])]),
125 :     (BV.add_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
126 :     [assignEin(y, Mk.addTF(d, dd), xs)]),
127 :     (BV.sub_tt, fn (y, [shp], xs) => let
128 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
129 :     val rator = Mk.subTT dd1
130 :     in
131 :     [assignEin(y, rator, xs)]
132 :     end),
133 :     (BV.sub_ff, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
134 :     [assignEin(y, Mk.subFF(d, dd), xs)]),
135 :     (BV.sub_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [x1, x2]) =>
136 :     [assignEin(y, Mk.subFT(d, dd), [x2, x1])]),
137 :     (BV.sub_tf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
138 :     [assignEin(y, Mk.subTF(d, dd), xs)]),
139 :     (BV.mul_rr, fn (y, _, args) => [assignEin(y, Mk.mulRR, args)]),
140 :     (BV.mul_rt, fn (y, [shp], xs) => let
141 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
142 :     val rator = Mk.mulRT(dd1)
143 :     in
144 :     [assignEin(y, rator, xs)]
145 :     end),
146 :     (BV.mul_tr, fn (y, [shp], [t, r]) => let
147 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
148 :     val rator = Mk.mulRT dd1
149 :     in
150 :     [assignEin(y, rator, [r, t])]
151 :     end),
152 :     (BV.mul_rf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
153 :     [assignEin(y, Mk.mulRF(d, dd), xs)]),
154 :     (BV.mul_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [f, s]) =>
155 :     [assignEin(y, Mk.mulRF(d, dd), [s, f])]),
156 :     (BV.mul_ss, fn (y, [_, Ty.DIM d], xs) =>
157 :     [assignEin(y, Mk.mulSS d, xs)]),
158 :     (BV.mul_sf, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
159 :     [assignEin(y, Mk.mulSF(d, dd), xs)]),
160 :     (BV.mul_fs, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [s, f]) =>
161 :     [assignEin(y, Mk.mulSF(d, dd), [f, s])]),
162 :     (BV.mul_st, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
163 :     [assignEin(y, Mk.mulST(d, dd), List.rev xs)]),
164 :     (BV.mul_ts, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
165 :     [assignEin(y, Mk.mulST(d, dd), xs)]),
166 :     (BV.div_rr, fn (y, _, args) => [assignEin(y, Mk.divRR, args)]),
167 :     (BV.div_tr, fn (y, [shp], xs) => let
168 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
169 :     val rator = Mk.divTR dd1
170 :     in
171 :     [assignEin(y, rator, xs)]
172 :     end),
173 :     (BV.div_fr, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
174 :     [assignEin(y, Mk.divFR(d, dd), xs)]),
175 :     (BV.div_fs, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd], xs) =>
176 :     [assignEin(y, Mk.divFS(d, dd), xs)]),
177 :     (BV.div_ss, fn (y, [_, Ty.DIM d], xs) =>
178 :     [assignEin(y, Mk.divSS d, xs)]),
179 : cchiw 3978 (BV.pow_ri, simpleOp Op.Power),
180 : jhr 3519 (BV.pow_rr, fn (y, _, args) =>
181 : cchiw 3978 assign(y, Op.MathFn MathFns.POW, args)),
182 : jhr 3498 (BV.pow_si, fn (y, [_, Ty.DIM d1], [f, n]) => let
183 : jhr 3587 fun getN x = (case IR.Var.getDef x
184 :     of IR.LIT(Literal.Int n) => IntInf.toInt n
185 :     | _ => raise Fail "impossible"
186 : jhr 3483 (* end case *))
187 :     in
188 : cchiw 3978 [assignEin(y, Mk.powFI(d1, getN n), [f])]
189 : jhr 3483 end),
190 : jhr 3478 (BV.curl2D, simpleEOp Mk.curl2d),
191 :     (BV.curl3D, simpleEOp Mk.curl3d),
192 : jhr 3471 (BV.convolve_vk, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
193 : jhr 3478 [assignEin(y, Mk.conv(d, dd), xs)]),
194 : jhr 3645 (BV.convolve_kv, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
195 :     [assignEin(y, Mk.conv(d, dd), [v, k])]),
196 : jhr 3478 (BV.neg_t, fn (y, [shp], xs) => let
197 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor shp
198 :     val rator = Mk.negTT dd1
199 :     in
200 :     [assignEin(y, rator, xs)]
201 :     end),
202 :     (BV.neg_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
203 :     [assignEin(y, Mk.negFF(d, dd), xs)]),
204 :     (BV.op_probe, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
205 :     [assignEin(y, (Mk.probe(dd, d)), xs)]),
206 :     (BV.op_D, fn (y, [_, Ty.DIM d], xs) =>
207 :     if (d = 2) orelse (d = 3)
208 :     then [assignEin(y, Mk.grad [d], xs)]
209 :     else raise Fail "unsupported gradient"),
210 :     (BV.op_Dotimes, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
211 :     [assignEin(y, Mk.dotimes(d1, dd@[d2]), xs)]),
212 :     (BV.op_Ddot, fn (y, [_, Ty.DIM d1, Ty.SHAPE dd, Ty.DIM d2], xs) =>
213 :     [assignEin(y, Mk.divergence(d1, dd), xs)] ),
214 : jhr 3498 (BV.op_norm_t, fn (y, [sv], xs) => let
215 :     val DstTy.TensorTy dd1 = shapeVarToTensor sv
216 :     in
217 :     case (dd1,xs)
218 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
219 :     | _ => [assignEin(y, Mk.normT dd1, xs)]
220 :     (* end case *)
221 :     end),
222 :     (BV.op_norm_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) => (case (dd1, xs)
223 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
224 :     | _ => [assignEin(y, Mk.normF(d1, dd1), xs)]
225 :     (* end case *))),
226 : jhr 3478 (BV.op_not, simpleOp Op.Not),
227 :     (BV.op_cross3_tt, simpleEOp Mk.cross3TT),
228 :     (BV.op_cross2_tt, simpleEOp Mk.cross2TT),
229 :     (BV.op_cross2_ff, simpleEOp Mk.cross2FF),
230 :     (BV.op_cross3_ff, simpleEOp Mk.cross3FF),
231 : cchiw 3514 (BV.op_outer_tt, fn (y, [sh1, sh2, _], xs) => let
232 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
233 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
234 :     in
235 :     [assignEin(y, (Mk.outerTT(dd1, dd2)), xs)]
236 :     end),
237 : jhr 3519 (BV.op_outer_tf, fn (y, [_, Ty.DIM d, sh1, Ty.SHAPE dd2, _], xs) => let
238 : cchiw 3514 val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
239 :     in
240 :     [assignEin(y, Mk.outerTF(d, dd1, dd2), xs)]
241 :     end),
242 : jhr 3519 (BV.op_outer_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd1, sh2, _], xs) => let
243 : cchiw 3514 val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
244 :     in
245 :     [assignEin(y, Mk.outerFT(d, dd1, dd2), xs)]
246 :     end),
247 : jhr 3519 (BV.op_outer_ff, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) =>
248 : cchiw 3514 [assignEin(y, Mk.outerFF(d, dd1, dd2), xs)]),
249 : jhr 3478 (BV.op_inner_tt, fn (y, [sh1, sh2, _], xs) => let
250 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
251 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
252 :     in
253 :     [assignEin(y, (Mk.innerTT(dd1, dd2)), xs)]
254 :     end),
255 :     (BV.op_inner_tf, fn (y, [_, Ty.DIM d, sh1, Ty.SHAPE dd2, _], xs) =>let
256 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
257 : cchiw 3496 in
258 : jhr 3478 [assignEin(y, Mk.innerTF(dd1, d, dd2), xs)]
259 :     end),
260 :     (BV.op_inner_ft, fn (y, [_, Ty.DIM d, Ty.SHAPE dd1, sh2, _], xs) =>let
261 :     val ty1 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
262 :     in
263 :     [assignEin(y, Mk.innerFT(dd1, d, dd2), xs)]
264 :     end),
265 :     (BV.op_inner_ff, fn (y, [_, _, Ty.DIM d, Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) =>
266 :     [assignEin(y, Mk.innerFF(dd1, d, dd2), xs)]),
267 :     (BV.op_colon_tt, fn (y, [sh1, sh2, _], xs) => let
268 :     val ty1 as DstTy.TensorTy dd1 = shapeVarToTensor sh1
269 :     val ty2 as DstTy.TensorTy dd2 = shapeVarToTensor sh2
270 :     in
271 :     [assignEin(y, Mk.colonTT(dd1, dd2), xs)]
272 :     end),
273 :     (BV.op_colon_ft, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
274 :     [assignEin(y, Mk.colonFT(d, dd1, dd2), xs)]),
275 :     (BV.op_colon_tf, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
276 :     [assignEin(y, Mk.colonTF(d, dd1, dd2), xs)]),
277 :     (BV.op_colon_ff, fn (y, [_, Ty.SHAPE dd1, Ty.DIM d, Ty.SHAPE dd2, _], xs) =>
278 :     [assignEin(y, Mk.colonFF(d, dd1, dd2), xs)]),
279 :     (* modulate is vector * vector pointwise multiplication *)
280 :     (BV.fn_modulate, fn (y, [Ty.DIM dd1], xs) =>
281 :     [assignEin(y, (Mk.modulate dd1), xs)]),
282 : jhr 3498 (BV.fn_normalize_t, fn (y, [shp], xs) => let
283 :     val DstTy.TensorTy dd1 = shapeVarToTensor shp
284 :     in
285 :     case (dd1, xs)
286 :     (* FIXME: this assignment is incorrect (there should be an absolute-value), but I don't
287 :     * think that this case should ever arise. Same for fn_normalize_f.
288 :     *)
289 :     of ([], [x]) => [IR.ASSGN(y, IR.VAR x)]
290 : jhr 3701 | (_, [_]) => [assignEin(y, Mk.normalizeTT dd1, xs@xs)]
291 : jhr 3498 (* end case *)
292 :     end),
293 :     (BV.fn_normalize_f, fn (y, [ _,Ty.DIM d1, Ty.SHAPE dd1], xs) => (case (dd1, xs)
294 :     of ([], [arg0]) => [IR.ASSGN(y, IR.VAR arg0)]
295 :     | (_, [arg0]) => [assignEin(y, Mk.normalizeFF(d1, dd1), xs@xs)]
296 :     (* end case *))),
297 : jhr 3478 (BV.fn_trace_t, fn (y, [Ty.DIM d], xs) =>
298 :     [assignEin(y, (Mk.traceT d), xs)]),
299 :     (BV.fn_trace_f, fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
300 :     [assignEin(y, Mk.traceF(d, dd), xs)]),
301 :     (BV.fn_transpose_t, fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
302 :     [assignEin(y, (Mk.transposeT [d1, d2]), xs)]),
303 :     (BV.fn_transpose_f, fn (y, [_, Ty.DIM d1, Ty.DIM d2, Ty.DIM d3], xs) =>
304 :     [assignEin(y, (Mk.transposeF (d1, d2, d3)), xs)]),
305 :     (BV.fn_det2_t, simpleEOp (Mk.det2T)),
306 :     (BV.fn_det3_t, simpleEOp (Mk.det3T)),
307 :     (BV.fn_det2_f, simpleEOp (Mk.det2F)),
308 :     (BV.fn_det3_f, simpleEOp (Mk.det3F)),
309 : jhr 3498 (BV.fn_sqrt_r, fn (y, _, xs) =>
310 :     [assignEin(y, Mk.sqrtR, xs)]),
311 : jhr 3483 (BV.fn_sqrt_s, fn (y, [_, Ty.DIM d1], xs) =>
312 : jhr 3478 [assignEin(y, Mk.sqrtF d1, xs)]),
313 : cchiw 3494 (BV.fn_cos_r, fn (y, _, xs) =>
314 : jhr 3498 [assignEin(y, Mk.cosR, xs)]),
315 : jhr 3483 (BV.fn_cos_s, fn (y, [_, Ty.DIM d1], xs) =>
316 : jhr 3478 [assignEin(y, Mk.cosF d1, xs)]),
317 : cchiw 3494 (BV.fn_acos_r, fn (y, _, xs) =>
318 :     [assignEin(y, Mk.acosR, xs)]),
319 : jhr 3483 (BV.fn_acos_s, fn (y, [_, Ty.DIM d1], xs) =>
320 : jhr 3478 [assignEin(y, Mk.acosF d1, xs)]),
321 : cchiw 3494 (BV.fn_sin_r, fn (y, _, xs) =>
322 : jhr 3498 [assignEin(y, Mk.sinR, xs)]),
323 : jhr 3483 (BV.fn_sin_s, fn (y, [_, Ty.DIM d1], xs) =>
324 : jhr 3478 [assignEin(y, Mk.sinF d1, xs)]),
325 : jhr 3498 (BV.fn_asin_r, fn (y, _, xs) =>
326 :     [assignEin(y, Mk.asinR, xs)]),
327 : jhr 3483 (BV.fn_asin_s, fn (y, [_, Ty.DIM d1], xs) =>
328 : jhr 3478 [assignEin(y, Mk.asinF d1, xs)]),
329 : cchiw 3494 (BV.fn_tan_r, fn (y, _, xs) =>
330 : jhr 3498 [assignEin(y, Mk.tanR, xs)]),
331 : jhr 3483 (BV.fn_tan_s, fn (y, [_, Ty.DIM d1], xs) =>
332 : jhr 3478 [assignEin(y, Mk.tanF d1, xs)]),
333 : jhr 3498 (BV.fn_atan_r, fn (y, _, xs) =>
334 :     [assignEin(y, Mk.atanR, xs)]),
335 : jhr 3483 (BV.fn_atan_s, fn (y, [_, Ty.DIM d1], xs) =>
336 : jhr 3478 [assignEin(y, Mk.atanF d1, xs)]),
337 : jhr 3483 (BV.fn_exp_r, fn (y, _, xs) =>
338 :     [assignEin(y, Mk.expT, xs)]),
339 :     (BV.fn_exp_s, fn (y, [_, Ty.DIM d1], xs) =>
340 : jhr 3478 [assignEin(y, Mk.expF d1, xs)]),
341 :     (BV.fn_inside, fn (y, [_, Ty.DIM d, _], xs) => assign(y, Op.Inside d, xs)),
342 : jhr 3830 (* FIXME:
343 :     (BV.clamp_trr, fn (y, [sv, dv], [x, lo, hi]) => let
344 :     in [
345 :     ] end),
346 :     *)
347 :     (BV.clamp_ttt, tensorOp Op.Clamp),
348 : jhr 3471 (BV.lerp3, tensorOp Op.Lerp),
349 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
350 : jhr 3478 val t1 = IR.Var.new("t1", DstTy.realTy)
351 :     val t2 = IR.Var.new("t2", DstTy.realTy)
352 :     val t3 = IR.Var.new("t3", DstTy.realTy)
353 : jhr 3471 in [
354 : jhr 3478 assignEin(t1, Mk.subRR, [x, x0]),
355 :     assignEin(t2, Mk.subRR, [x1, x0]),
356 :     assignEin(t3, Mk.divRR, [t1, t2]),
357 :     IR.ASSGN(y, IR.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
358 : jhr 3471 ] end),
359 :     (BV.evals2x2, eigenVal (Op.Eigen2x2, 2)),
360 :     (BV.evals3x3, eigenVal (Op.Eigen3x3, 3)),
361 :     (BV.evecs2x2, eigenVec (Op.Eigen2x2, 2)),
362 :     (BV.evecs3x3, eigenVec (Op.Eigen3x3, 3)),
363 : jhr 3498 (BV.fn_abs_i, simpleOp (Op.Abs DstTy.IntTy)),
364 :     (BV.fn_abs_r, simpleOp (Op.Abs DstTy.realTy)),
365 : jhr 3483 (BV.fn_max_i, simpleOp (Op.Max DstTy.IntTy)),
366 :     (BV.fn_max_r, simpleOp (Op.Max DstTy.realTy)),
367 :     (BV.fn_min_i, simpleOp (Op.Min DstTy.IntTy)),
368 :     (BV.fn_min_r, simpleOp (Op.Min DstTy.realTy)),
369 : jhr 3471 (BV.kn_bspln3, kernel Kernel.bspln3),
370 :     (BV.kn_bspln5, kernel Kernel.bspln5),
371 :     (BV.kn_ctmr, kernel Kernel.ctmr),
372 :     (BV.kn_c4hexic, kernel Kernel.c4hexic),
373 :     (BV.kn_tent, kernel Kernel.tent),
374 :     (BV.i2r, simpleOp Op.IntToReal),
375 : jhr 3478 (BV.identity, fn (y, [Ty.DIM d], xs) =>
376 :     [assignEin(y, Mk.identity d, xs)]),
377 : jhr 3492 (* FIXME: eventually zero should be implemented by Ein
378 : jhr 3478 (BV.zero, fn (y, [Ty.SHAPE dd], []) =>
379 :     [assignEin(y, Mk.zeros dd, [])]),
380 : jhr 3492 *)
381 :     (BV.zero, fn (y, [sv], []) =>
382 :     assign(y, Op.Zero(shapeVarToTensor sv), [])),
383 : jhr 3471 (BV.nan, fn (y, [Ty.SHAPE dd], []) => let
384 : jhr 3478 val nan = IR.LIT(Literal.Real(RealLit.nan))
385 :     fun mk (y, [], stms) = IR.ASSGN(y, nan) :: stms
386 : jhr 3471 | mk (y, d::dd, stms) = let
387 :     val ty = shapeVarToTensor(Ty.SHAPE dd)
388 : jhr 3478 val zs = List.tabulate(d, fn _ => IR.Var.new("_nan", ty))
389 : jhr 3471 in
390 : jhr 3478 IR.ASSGN(y, IR.CONS(zs, IR.Var.ty y)) ::
391 : jhr 3471 List.foldl (fn (z, stms) => mk(z, dd, stms)) stms zs
392 :     end
393 :     in
394 :     List.rev (mk (y, dd, []))
395 :     end),
396 : jhr 3519 (* sequence operations *)
397 : jhr 3471 (BV.subscript, fn (y, [tv, Ty.DIM d], args) =>
398 :     assign(y,
399 : jhr 3508 Op.Subscript(DstTy.SeqTy(trType tv, SOME d)),
400 : jhr 3471 args)),
401 :     (BV.dynSubscript, fn (y, [tv], args) =>
402 : jhr 3508 assign(y, Op.Subscript(DstTy.SeqTy(trType tv, NONE)), args)),
403 :     (BV.at_Td, fn (y, [tv], args) => assign(y, Op.Prepend(trType tv), args)),
404 :     (BV.at_dT, fn (y, [tv], args) => assign(y, Op.Append(trType tv), args)),
405 :     (BV.at_dd, fn (y, [tv], args) => assign(y, Op.Concat(trType tv), args)),
406 : jhr 3519 (BV.range, fn (y, _, args) => assign(y, Op.Range, args)),
407 : jhr 3508 (BV.fn_length, fn (y, [tv], [s]) => assign(y, Op.Length(trType tv), [s])),
408 : jhr 3471 (* image operations *)
409 :     (BV.fn_size, fn (y, [Ty.DIM d, _], [img]) => let
410 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
411 : jhr 3471 (* we extract each dimension separately and then build the sequence value *)
412 : jhr 3478 val dims = List.tabulate(d, fn i => IR.Var.new("i"^Int.toString i, DstTy.IntTy))
413 : jhr 3471 fun mkStms ([], _, stms) = stms (* in reverse order! *)
414 :     | mkStms (d::dr, i, stms) = mkStms (dr, i+1,
415 : jhr 3478 IR.ASSGN(d, IR.OP(Op.ImageDim(info, i), [img])) :: stms)
416 : jhr 3471 in
417 :     List.revAppend (mkStms (dims, 0, []), [
418 : jhr 3478 IR.ASSGN(y, IR.SEQ(dims, DstTy.SeqTy(DstTy.intTy, SOME d)))
419 : jhr 3471 ])
420 :     end),
421 :     (BV.image_border, fn (y, _, args as [img, _]) => let
422 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
423 : jhr 3471 in
424 :     assign(y, Op.BorderCtlDefault info, args)
425 :     end),
426 :     (BV.image_clamp, fn (y, _, args as [img]) => let
427 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
428 : jhr 3471 in
429 :     assign(y, Op.BorderCtlClamp info, args)
430 :     end),
431 :     (BV.image_mirror, fn (y, _, args as [img]) => let
432 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
433 : jhr 3471 in
434 :     assign(y, Op.BorderCtlMirror info, args)
435 :     end),
436 :     (BV.image_wrap, fn (y, _, args as [img]) => let
437 : jhr 3478 val DstTy.ImageTy info = IR.Var.ty img
438 : jhr 3471 in
439 :     assign(y, Op.BorderCtlWrap info, args)
440 : jhr 3484 end),
441 : jhr 3498 (BV.dist2_t, fn (y, _, xs) => dist(y, 2, xs)),
442 :     (BV.dist3_t, fn (y, _, xs) => dist(y, 3, xs)),
443 : jhr 3519 (* reduction operators *)
444 : jhr 3484 (BV.red_all, fn (y, _, args as [e, v, k]) =>
445 : jhr 3498 assign(y, Op.R_All(IR.Var.ty v), args)),
446 : jhr 3484 (BV.red_exists, fn (y, _, args as [e, v, k]) =>
447 : jhr 3498 assign(y, Op.R_Exists(IR.Var.ty v), args)),
448 : jhr 3484 (BV.red_max, fn (y, _, args as [e, v, k]) =>
449 : jhr 3498 assign(y, Op.R_Max(IR.Var.ty v), args)),
450 : jhr 3484 (BV.red_mean, fn (y, _, args as [e, v, k]) =>
451 : jhr 3498 assign(y, Op.R_Mean(IR.Var.ty v), args)),
452 : jhr 3484 (BV.red_min, fn (y, _, args as [e, v, k]) =>
453 : jhr 3498 assign(y, Op.R_Min(IR.Var.ty v), args)),
454 : jhr 3484 (BV.red_product, fn (y, _, args as [e, v, k]) =>
455 : jhr 3498 assign(y, Op.R_Product(IR.Var.ty v),args)),
456 : jhr 3484 (BV.red_sum, fn (y, _, args as [e, v, k]) =>
457 : jhr 3498 assign(y, Op.R_Sum(IR.Var.ty v), args)),
458 : jhr 3484 (BV.red_variance, fn (y, _, args as [e, v, k]) =>
459 : jhr 3498 assign(y, Op.R_Variance(IR.Var.ty v), args)),
460 : jhr 3519 (* spatial queries *)
461 : jhr 3484 (BV.fn_sphere_im, fn (y, [tv], args as [p, s]) =>
462 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
463 : jhr 3484 (BV.fn_sphere1_r, fn (y, [tv], args as [p, s]) =>
464 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
465 : jhr 3484 (BV.fn_sphere2_t, fn (y, [tv], args as [p, s]) =>
466 : jhr 3498 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
467 : jhr 3484 (BV.fn_sphere3_t, fn (y, [tv], args as [p, s]) =>
468 : jhr 3511 assign(y, Op.SphereQuery(IR.Var.ty p, trType tv), args)),
469 : jhr 3519 (* math functions that have not been lifted *)
470 :     (BV.fn_atan2_rr, fn (y, _, args) =>
471 : jhr 3511 assign(y, Op.MathFn MathFns.ATAN2, args)),
472 : jhr 3519 (BV.fn_ceil_r, fn (y, _, args) =>
473 : jhr 3511 assign(y, Op.MathFn MathFns.CEIL, args)),
474 : jhr 3519 (BV.fn_floor_r, fn (y, _, args) =>
475 : jhr 3511 assign(y, Op.MathFn MathFns.FLOOR, args)),
476 : jhr 3519 (BV.fn_fmod_rr, fn (y, _, args) =>
477 : jhr 3511 assign(y, Op.MathFn MathFns.FMOD, args)),
478 : jhr 3519 (BV.fn_erf_r, fn (y, _, args) =>
479 : jhr 3511 assign(y, Op.MathFn MathFns.ERF, args)),
480 : jhr 3519 (BV.fn_erfc_r, fn (y, _, args) =>
481 : jhr 3511 assign(y, Op.MathFn MathFns.ERFC, args)),
482 : jhr 3519 (BV.fn_log_r, fn (y, _, args) =>
483 : jhr 3511 assign(y, Op.MathFn MathFns.LOG, args)),
484 : jhr 3519 (BV.fn_log10_r, fn (y, _, args) =>
485 : jhr 3511 assign(y, Op.MathFn MathFns.LOG10, args)),
486 : jhr 3519 (BV.fn_log2_r, fn (y, _, args) =>
487 : jhr 3511 assign(y, Op.MathFn MathFns.LOG2, args)),
488 : jhr 3519 (BV.fn_pow_rr, fn (y, _, args) =>
489 : jhr 3511 assign(y, Op.MathFn MathFns.POW, args))
490 : jhr 3471 ];
491 :     tbl
492 :     end
493 :    
494 :     fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
495 :     of SOME transFn => transFn(y, mvs, xs)
496 :     | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
497 :     (* end case *))
498 : jhr 3478 handle ex => (print(concat["translate (", IR.Var.toString y, ", ",
499 : jhr 3471 Var.uniqueNameOf f, ", ...)\n"]); raise ex)
500 :    
501 :     end

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