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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/high-il/check-high-il.sml
ViewVC logotype

Annotation of /trunk/src/compiler/high-il/check-high-il.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 411 (* check-high-il.sml
2 : jhr 404 *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 404 * All rights reserved.
7 :     *)
8 :    
9 : jhr 405 structure CheckOps : OPERATOR_TY = struct
10 : jhr 404
11 :     structure Op = HighOps
12 :     structure Ty = HighILTypes
13 :    
14 : jhr 405 type rator = Op.rator
15 :     type ty = Ty.ty
16 :    
17 : jhr 1640 (* utility function for synthesizing eigenvector/eigenvalue signature *)
18 :     fun eigenSig dim = let
19 :     val tplTy = Ty.TupleTy[
20 :     Ty.SeqTy(Ty.realTy, dim),
21 :     Ty.SeqTy(Ty.vecTy dim, dim)
22 :     ]
23 :     in
24 :     (tplTy, [Ty.TensorTy[dim, dim]])
25 :     end
26 :    
27 : jhr 420 (* Return the signature of a HighIL operator. *)
28 : jhr 412 fun sigOf rator = (case rator
29 : jhr 2356 of Op.Add ty => (ty, [ty, ty])
30 :     | Op.Sub ty => (ty, [ty, ty])
31 :     | Op.Mul ty => (ty, [ty, ty])
32 :     | Op.Div ty => (ty, [ty, ty])
33 :     | Op.Neg ty => (ty, [ty])
34 :     | Op.Abs ty => (ty, [ty])
35 :     | Op.LT ty => (Ty.BoolTy, [ty, ty])
36 :     | Op.LTE ty => (Ty.BoolTy, [ty, ty])
37 :     | Op.EQ ty => (Ty.BoolTy, [ty, ty])
38 :     | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
39 :     | Op.GT ty => (Ty.BoolTy, [ty, ty])
40 :     | Op.GTE ty => (Ty.BoolTy, [ty, ty])
41 :     | Op.Power => (Ty.realTy, [Ty.realTy, Ty.IntTy])
42 :     | Op.Not => (Ty.BoolTy, [Ty.BoolTy])
43 :     | Op.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
44 :     | Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
45 :     | Op.Clamp ty => (ty, [ty, ty, ty])
46 :     | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
47 :     | Op.Dot ty => (Ty.realTy, [ty, ty])
48 :     | Op.MulVecMat(ty as Ty.TensorTy[d1, d2]) => (Ty.TensorTy[d2], [Ty.TensorTy[d1], ty])
49 :     | Op.MulMatVec(ty as Ty.TensorTy[d1, d2]) => (Ty.TensorTy[d1], [ty, Ty.TensorTy[d2]])
50 :     | Op.MulMatMat(ty1 as Ty.TensorTy[d1, d2], ty2 as Ty.TensorTy[d2', d3]) =>
51 :     (Ty.TensorTy[d1, d3], [ty1, ty2])
52 :     | Op.MulVecTen3(ty as Ty.TensorTy[d1, d2, d3]) =>
53 :     (Ty.TensorTy[d2, d3], [Ty.TensorTy[d1], ty])
54 :     | Op.MulTen3Vec(ty as Ty.TensorTy[d1, d2, d3]) =>
55 :     (Ty.TensorTy[d1, d2], [ty, Ty.TensorTy[d3]])
56 :     | Op.ColonMul(ty1 as Ty.TensorTy dd1, ty2 as Ty.TensorTy(d21::d22::dd2)) => let
57 :     fun last2 ([d1, d2], prefix) = (prefix, d1, d2)
58 :     | last2 (d::dd, prefix) = last2(dd, d::prefix)
59 :     | last2 _ = raise Fail("sigOf: invalid operator " ^ Op.toString rator)
60 :     val (prefix, d11, d12) = last2 (dd1, [])
61 :     in
62 :     if (d11 <> d21) orelse (d12 <> d22)
63 :     then raise Fail("sigOf: invalid operator " ^ Op.toString rator)
64 :     else (Ty.TensorTy(List.revAppend(prefix, dd2)), [ty1, ty2])
65 :     end
66 :     | Op.Cross => (Ty.vec3Ty, [Ty.vec3Ty, Ty.vec3Ty])
67 :     | Op.Outer(ty as Ty.TensorTy[d1, d2]) => (ty, [Ty.TensorTy[d1], Ty.TensorTy[d2]])
68 :     | Op.Norm ty => (Ty.realTy, [ty])
69 :     | Op.Normalize ty => (ty, [ty])
70 :     | Op.Scale ty => (ty, [Ty.realTy, ty])
71 :     | Op.PrincipleEvec(resTy as Ty.TensorTy[d]) => (resTy, [Ty.TensorTy[d,d]])
72 : jhr 1640 | Op.Eigen2x2 => eigenSig 2
73 :     | Op.Eigen3x3 => eigenSig 3
74 : jhr 2356 | Op.Identity d => (Ty.TensorTy[d,d], [])
75 :     | Op.Zero ty => (ty, [])
76 :     | Op.Trace ty => (Ty.realTy, [ty])
77 :     | Op.Transpose(d1, d2) => (Ty.TensorTy[d2, d1], [Ty.TensorTy[d1, d2]])
78 :     | Op.Slice(ty as Ty.TensorTy dd, m) => let
79 :     val (resDims, idxDims) = let
80 :     fun f (d, false, (rds, ids)) = (d::rds, ids)
81 :     | f (_, true, (rds, ids)) = (rds, Ty.IntTy::ids)
82 :     in
83 :     ListPair.foldr f ([], []) (dd, m)
84 :     end
85 :     in
86 :     (Ty.TensorTy resDims, ty::idxDims)
87 :     end
88 :     | Op.TensorSub(ty as Ty.TensorTy dd) =>
89 :     (Ty.realTy, ty :: List.map (fn _ => Ty.IntTy) dd)
90 :     | Op.Select(ty as Ty.TupleTy tys, i) =>
91 : jhr 1640 if (1 <= i) andalso (i <= length tys)
92 :     then (List.nth(tys, i-1), [ty])
93 :     else raise Fail("sigOf: invalid operator " ^ Op.toString rator)
94 : jhr 2356 | Op.SeqSub(ty as Ty.SeqTy(elemTy, _)) => (elemTy, [ty, Ty.IntTy])
95 :     | Op.IntToReal => (Ty.realTy, [Ty.IntTy])
96 :     | Op.TruncToInt => (Ty.IntTy, [Ty.realTy])
97 :     | Op.RoundToInt => (Ty.IntTy, [Ty.realTy])
98 :     | Op.CeilToInt => (Ty.IntTy, [Ty.realTy])
99 :     | Op.FloorToInt => (Ty.IntTy, [Ty.realTy])
100 :     | Op.Kernel _ => (Ty.KernelTy, [])
101 :     | Op.Field dim => (Ty.FieldTy, [Ty.ImageTy dim, Ty.KernelTy])
102 :     | Op.Inside dim => (Ty.BoolTy, [Ty.TensorTy[dim], Ty.FieldTy])
103 :     | Op.AddField => (Ty.FieldTy, [Ty.FieldTy, Ty.FieldTy])
104 :     | Op.SubField => (Ty.FieldTy, [Ty.FieldTy, Ty.FieldTy])
105 :     | Op.ScaleField => (Ty.FieldTy, [Ty.realTy, Ty.FieldTy])
106 :     | Op.OffsetField => (Ty.FieldTy, [Ty.FieldTy, Ty.realTy])
107 :     | Op.NegField => (Ty.FieldTy, [Ty.FieldTy])
108 :     | Op.DiffField => (Ty.FieldTy, [Ty.FieldTy])
109 :     | Op.CurlField 2 => (Ty.FieldTy, [Ty.FieldTy])
110 :     | Op.CurlField 3 => (Ty.FieldTy, [Ty.FieldTy])
111 :     | Op.Probe(domTy, rngTy) => (rngTy, [Ty.FieldTy, domTy])
112 : jhr 2636 | Op.LoadImage(ty, _, _) => (ty, [])
113 :     | Op.Input(Inputs.INP{ty, ...}) => (ty, [])
114 : jhr 1640 | Op.Print tys => (Ty.TupleTy[], tys)
115 : jhr 2356 | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
116 :     (* end case *))
117 : jhr 404
118 : jhr 1116 fun typeOfCons (Ty.TensorTy dd', (ty1 as Ty.TensorTy dd)::r) =
119 : jhr 2356 if List.all (fn ty => Ty.same(ty1, ty)) r
120 :     then (dd' = (List.length r + 1)::dd)
121 :     else false
122 : jhr 2636 | typeOfCons (Ty.SeqTy(ty, n), tys) =
123 :     List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
124 : jhr 1116 | typeOfCons _ = false
125 : jhr 412
126 : jhr 404 end
127 : jhr 405
128 :     structure CheckHighIL = CheckILFn (
129 :     structure IL = HighIL
130 :     structure OpTy = CheckOps)
131 : jhr 1116
132 :     structure HighPP = SSAPPFn (HighIL)
133 :    

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