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

SCM Repository

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

Annotation of /branches/pure-cfg/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 824 - (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 180 val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list)
16 : jhr 188 -> HighIL.assign 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 197 fun pruneDim d = (case TU.pruneDim d
30 :     of (Ty.DimConst n) => n
31 :     | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
32 :     (* end case *))
33 :    
34 : jhr 407 fun pruneShape sv = (case TU.pruneShape(MV.toShape sv)
35 : jhr 426 of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd)
36 : jhr 183 | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
37 :     (* end case *))
38 :    
39 : jhr 426 fun dimVarToTensor dv = DstTy.tensorTy[pruneDim(MV.toDim dv)]
40 : jhr 683 fun dimVarToMatrix dv = let
41 :     val d = pruneDim(MV.toDim dv)
42 :     in
43 :     DstTy.tensorTy[d, d] (* square matrix type *)
44 :     end
45 : jhr 407 fun shapeVarToTensor sv = pruneShape sv
46 : jhr 197
47 : jhr 407 fun assign (y, rator, xs) = [(y, IL.OP(rator, xs))]
48 :    
49 : jhr 695 fun basisFn name (y, [], xs) = [(y, IL.APPLY(name, xs))]
50 :    
51 : jhr 407 fun simpleOp rator (y, [], xs) = assign (y, rator, xs)
52 :    
53 :     fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)
54 :    
55 :     fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)
56 :    
57 : jhr 517 fun kernel h (y, [], []) = assign(y, Op.Kernel(h, 0), [])
58 : jhr 197
59 : jhr 184 (* build a table that maps Basis variables to their translation functions *)
60 : jhr 197 val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assign list) VTbl.hash_table = let
61 : jhr 194 val tbl = VTbl.mkTable (128, Fail "Translate table")
62 : jhr 180 in
63 : jhr 194 List.app (VTbl.insert tbl) [
64 : jhr 392 (BV.lt_ii, simpleOp(Op.LT DstTy.IntTy)),
65 : jhr 426 (BV.lt_rr, simpleOp(Op.LT(DstTy.realTy))),
66 : jhr 392 (BV.lte_ii, simpleOp(Op.LTE DstTy.IntTy)),
67 : jhr 426 (BV.lte_rr, simpleOp(Op.LTE(DstTy.realTy))),
68 : jhr 392 (BV.gte_ii, simpleOp(Op.GTE DstTy.IntTy)),
69 : jhr 426 (BV.gte_rr, simpleOp(Op.GTE(DstTy.realTy))),
70 : jhr 392 (BV.gt_ii, simpleOp(Op.GT DstTy.IntTy)),
71 : jhr 426 (BV.gt_rr, simpleOp(Op.GT(DstTy.realTy))),
72 : jhr 392 (BV.equ_bb, simpleOp(Op.EQ DstTy.BoolTy)),
73 :     (BV.equ_ii, simpleOp(Op.EQ DstTy.IntTy)),
74 :     (BV.equ_ss, simpleOp(Op.EQ DstTy.StringTy)),
75 : jhr 426 (BV.equ_rr, simpleOp(Op.EQ(DstTy.realTy))),
76 : jhr 392 (BV.neq_bb, simpleOp(Op.NEQ DstTy.BoolTy)),
77 :     (BV.neq_ii, simpleOp(Op.NEQ DstTy.IntTy)),
78 :     (BV.neq_ss, simpleOp(Op.NEQ DstTy.StringTy)),
79 : jhr 426 (BV.neq_rr, simpleOp(Op.NEQ(DstTy.realTy))),
80 : jhr 824 (BV.add_ii, simpleOp(Op.Add DstTy.IntTy)),
81 :     (BV.add_tt, tensorOp Op.Add),
82 :     (BV.sub_ii, simpleOp(Op.Sub DstTy.IntTy)),
83 :     (BV.sub_tt, tensorOp Op.Sub),
84 :     (BV.mul_ii, simpleOp(Op.Mul DstTy.IntTy)),
85 :     (BV.mul_rr, simpleOp(Op.Mul(DstTy.realTy))),
86 :     (BV.mul_rt, tensorOp Op.Scale),
87 :     (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
88 :     (BV.div_ii, simpleOp(Op.Div DstTy.IntTy)),
89 :     (BV.div_rr, simpleOp(Op.Div DstTy.realTy)),
90 :     (BV.div_tr, tensorOp Op.InvScale),
91 :     (BV.exp_ri, simpleOp(Op.Power)),
92 :     (BV.exp_rr, basisFn ILBasis.pow),
93 : jhr 392 (BV.neg_i, simpleOp(Op.Neg DstTy.IntTy)),
94 : jhr 194 (BV.neg_t, tensorOp Op.Neg),
95 : jhr 197 (BV.neg_f, fn (y, _, xs) => assign(y, Op.NegField, xs)),
96 : jhr 407 (BV.op_at, fn (y, [_, dv, sv], xs) =>
97 :     assign(y, Op.Probe(shapeVarToTensor sv, dimVarToTensor dv), xs)),
98 : jhr 517 (BV.op_convolve, fn (y, [_, dv, _], xs) =>
99 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), xs)),
100 : jhr 197 (BV.op_D, fn (y, _, xs) => assign(y, Op.DiffField, xs)),
101 : jhr 730 (BV.op_norm, fn (y, [sv], xs) => (case shapeVarToTensor sv
102 :     of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
103 :     | ty => assign(y, Op.Norm ty, xs)
104 : jhr 719 (* end case *))),
105 : jhr 194 (BV.op_not, simpleOp Op.Not),
106 : jhr 695 (BV.fn_atan2, basisFn ILBasis.atan2),
107 : jhr 197 (BV.fn_CL, fn (y, _, xs) => assign(y, Op.CL, xs)),
108 : jhr 517 (BV.fn_convolve, fn (y, [_, dv, _], [h, img]) =>
109 :     assign(y, Op.Field(pruneDim(MV.toDim dv)), [img, h])),
110 : jhr 695 (BV.fn_cos, basisFn ILBasis.cos),
111 : jhr 766 (BV.op_cross, simpleOp Op.Cross),
112 : jhr 685 (BV.fn_cross, simpleOp Op.Cross),
113 : jhr 820 (BV.op_outer, fn (y, [dv1, dv2], xs) => let
114 :     val d1 = pruneDim(MV.toDim dv1)
115 :     val d2 = pruneDim(MV.toDim dv2)
116 :     in
117 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
118 :     end),
119 :     (BV.fn_outer, fn (y, [dv1, dv2], xs) => let
120 :     val d1 = pruneDim(MV.toDim dv1)
121 :     val d2 = pruneDim(MV.toDim dv2)
122 :     in
123 :     assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)
124 :     end),
125 : jhr 807 (BV.op_inner, fn (y, [sh1, sh2, _], xs) => let
126 :     val ty1 as DstTy.TensorTy dd1 = pruneShape sh1
127 :     val ty2 as DstTy.TensorTy dd2 = pruneShape sh2
128 :     val rator = (case (dd1, dd2)
129 :     of ([d], [d']) => Op.Dot ty1
130 : jhr 817 | ([d1], [d1', d2]) => Op.MulVecMat ty2
131 :     | ([d1, d2], [d2']) => Op.MulMatVec ty1
132 : jhr 807 | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
133 :     | _ => raise Fail "unsupported inner-product type"
134 :     (* end case *))
135 :     in
136 :     assign (y, rator, xs)
137 :     end),
138 : jhr 197 (BV.fn_dot, vectorOp Op.Dot),
139 : jhr 683 (BV.fn_evals, fn _ => raise Fail "evals not implemented yet"), (* FIXME *)
140 :     (BV.fn_evecs, fn _ => raise Fail "evecs not implemented yet"), (* FIXME *)
141 : jhr 407 (BV.fn_inside, fn (y, [_, dv, _], xs) =>
142 : jhr 548 assign(y, Op.Inside(pruneDim(MV.toDim dv)), xs)),
143 : jhr 754 (BV.lerp3, tensorOp Op.Lerp),
144 :     (BV.lerp5, fn (y, [sv], [a, b, x0, x, x1]) => let
145 :     val t1 = IL.Var.new("t1", DstTy.realTy)
146 :     val t2 = IL.Var.new("t2", DstTy.realTy)
147 :     val t3 = IL.Var.new("t3", DstTy.realTy)
148 :     in [
149 :     (t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
150 :     (t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
151 :     (t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
152 :     (y, IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
153 :     ] end),
154 : jhr 263 (BV.fn_max, simpleOp Op.Max),
155 :     (BV.fn_min, simpleOp Op.Min),
156 : jhr 197 (BV.fn_modulate, vectorOp Op.Mul),
157 : jhr 750 (BV.fn_normalize, vectorOp Op.Normalize),
158 : jhr 683 (BV.fn_outer, fn _ => raise Fail "outer not implemented yet"), (* FIXME *)
159 : jhr 695 (BV.fn_pow, basisFn ILBasis.pow),
160 : jhr 197 (BV.fn_principleEvec, vectorOp Op.PrincipleEvec),
161 : jhr 695 (BV.fn_sin, basisFn ILBasis.sin),
162 : jhr 811 (BV.fn_sqrt, basisFn ILBasis.sqrt),
163 : jhr 695 (BV.fn_tan, basisFn ILBasis.tan),
164 : jhr 683 (BV.fn_trace, fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
165 : jhr 197 (BV.kn_bspln3, kernel Kernel.bspln3),
166 :     (BV.kn_bspln5, kernel Kernel.bspln5),
167 :     (BV.kn_ctmr, kernel Kernel.ctmr),
168 :     (BV.kn_tent, kernel Kernel.tent),
169 : jhr 754 (BV.kn_c1tent, kernel Kernel.tent),
170 : jhr 762 (BV.i2r, simpleOp Op.IntToReal),
171 :     (BV.identity, fn (y, [dv], []) =>
172 : jhr 822 assign(y, Op.Identity(pruneDim(MV.toDim dv)), [])),
173 :     (BV.zero, fn (y, [sv], []) =>
174 :     assign(y, Op.Zero(shapeVarToTensor sv), []))
175 : jhr 180 ];
176 :     tbl
177 :     end
178 :    
179 : jhr 194 fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
180 : jhr 197 of SOME transFn => transFn(y, mvs, xs)
181 : jhr 407 | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
182 : jhr 180 (* end case *))
183 : jhr 730 handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
184 :     Var.uniqueNameOf f, ", ...)\n"]); raise ex)
185 : jhr 180
186 :     end

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