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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 420 (* check-mid-il.sml
2 :     *
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 420 * All rights reserved.
7 :     *)
8 :    
9 :     structure CheckOps : OPERATOR_TY = struct
10 :    
11 :     structure Op = MidOps
12 :     structure Ty = MidILTypes
13 :    
14 :     type rator = Op.rator
15 :     type ty = Ty.ty
16 :    
17 : jhr 1116 val vec3Ty = Ty.vecTy 3
18 : jhr 420
19 : jhr 1640 (* utility function for synthesizing eigenvector/eigenvalue signature *)
20 :     fun eigenSig dim = let
21 :     val tplTy = Ty.TupleTy[
22 :     Ty.SeqTy(Ty.realTy, dim),
23 :     Ty.SeqTy(Ty.vecTy dim, dim)
24 :     ]
25 :     in
26 :     (tplTy, [Ty.TensorTy[dim, dim]])
27 :     end
28 :    
29 : jhr 420 (* Return the signature of a MidIL operator. *)
30 :     fun sigOf rator = (case rator
31 : jhr 2356 of Op.Add ty => (ty, [ty, ty])
32 :     | Op.Sub ty => (ty, [ty, ty])
33 :     | Op.Mul ty => (ty, [ty, ty])
34 :     | Op.Div ty => (ty, [ty, ty])
35 :     | Op.Neg ty => (ty, [ty])
36 :     | Op.Abs ty => (ty, [ty])
37 :     | Op.LT ty => (Ty.BoolTy, [ty, ty])
38 :     | Op.LTE ty => (Ty.BoolTy, [ty, ty])
39 :     | Op.EQ ty => (Ty.BoolTy, [ty, ty])
40 :     | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
41 :     | Op.GT ty => (Ty.BoolTy, [ty, ty])
42 :     | Op.GTE ty => (Ty.BoolTy, [ty, ty])
43 :     | Op.Not => (Ty.BoolTy, [Ty.BoolTy])
44 :     | Op.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
45 :     | Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
46 :     | Op.Clamp ty => (ty, [ty, ty, ty])
47 :     | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
48 :     | Op.Dot d => (Ty.realTy, [Ty.vecTy d, Ty.vecTy d])
49 :     | Op.MulVecMat(d1, d2) => (Ty.vecTy d2, [Ty.vecTy d1, Ty.TensorTy[d1, d2]])
50 :     | Op.MulMatVec(d1, d2) => (Ty.vecTy d1, [Ty.TensorTy[d1, d2], Ty.vecTy d2])
51 :     | Op.MulMatMat(d1, d2, d3) => (Ty.TensorTy[d1, d3], [Ty.TensorTy[d1, d2], Ty.TensorTy[d2, d3]])
52 :     | Op.MulVecTen3(d1, d2, d3) => (Ty.TensorTy[d2, d3], [Ty.vecTy d1, Ty.TensorTy[d1, d2, d3]])
53 :     | Op.MulTen3Vec(d1, d2, d3) => (Ty.TensorTy[d1, d2], [Ty.TensorTy[d1, d2, d3], Ty.vecTy d3])
54 :     | Op.ColonMul(ty1 as Ty.TensorTy dd1, ty2 as Ty.TensorTy(d21::d22::dd2)) => let
55 :     fun last2 ([d1, d2], prefix) = (prefix, d1, d2)
56 :     | last2 (d::dd, prefix) = last2(dd, d::prefix)
57 :     | last2 _ = raise Fail("sigOf: invalid operator " ^ Op.toString rator)
58 :     val (prefix, d11, d12) = last2 (dd1, [])
59 :     in
60 :     if (d11 <> d21) orelse (d12 <> d22)
61 :     then raise Fail("sigOf: invalid operator " ^ Op.toString rator)
62 :     else (Ty.TensorTy(List.revAppend(prefix, dd2)), [ty1, ty2])
63 :     end
64 :     | Op.Cross => (vec3Ty, [vec3Ty, vec3Ty])
65 :     | Op.Norm(ty as Ty.TensorTy _) => (Ty.realTy, [ty])
66 :     | Op.Normalize d => (Ty.vecTy d, [Ty.vecTy d])
67 :     | Op.Scale(ty as Ty.TensorTy(_::_)) => (ty, [Ty.realTy, ty])
68 :     | Op.PrincipleEvec _ => raise Fail "Op.PrincipleEvec unimplemented"
69 : jhr 1640 | Op.EigenVecs2x2 => eigenSig 2
70 :     | Op.EigenVecs3x3 => eigenSig 3
71 :     | Op.EigenVals2x2 => (Ty.SeqTy(Ty.realTy, 2), [Ty.TensorTy[2,2]])
72 :     | Op.EigenVals3x3 => (Ty.SeqTy(Ty.realTy, 3), [Ty.TensorTy[3,3]])
73 : jhr 2356 | Op.Identity d => (Ty.TensorTy[d,d], [])
74 :     | Op.Zero ty => (ty, [])
75 :     | Op.Trace d => (Ty.realTy, [Ty.TensorTy[d, d]])
76 :     | Op.Transpose(d1, d2) => (Ty.TensorTy[d2, d1], [Ty.TensorTy[d1, d2]])
77 : jhr 1640 | Op.Select(ty as Ty.TupleTy tys, i) => (List.nth(tys, i-1), [ty])
78 : jhr 2356 | Op.Index(ty as Ty.TensorTy[d], _) => (Ty.realTy, [ty])
79 :     | Op.Index(ty as Ty.SeqTy(elemTy, _), _) => (elemTy, [ty])
80 :     | Op.Subscript(ty as Ty.TensorTy dd) => (Ty.realTy, ty :: List.map (fn _ => Ty.intTy) dd)
81 :     | Op.Subscript(ty as Ty.SeqTy(elemTy, d)) => (elemTy, [ty, Ty.intTy])
82 :     | Op.Ceiling d => (Ty.vecTy d, [Ty.vecTy d])
83 :     | Op.Floor d => (Ty.vecTy d, [Ty.vecTy d])
84 :     | Op.Round d => (Ty.vecTy d, [Ty.vecTy d])
85 :     | Op.Trunc d => (Ty.vecTy d, [Ty.vecTy d])
86 :     | Op.IntToReal => (Ty.realTy, [Ty.intTy])
87 :     | Op.RealToInt 1 => (Ty.IntTy, [Ty.realTy])
88 :     | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.TensorTy[d]])
89 : jhr 1640 (* FIXME: the type of RealToInt should be
90 : jhr 2356 | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.SeqTy(Ty.realTy, d)])
91 : jhr 1640 *)
92 : jhr 2356 | Op.VoxelAddress(info, offset) => let
93 :     val dim = ImageInfo.dim info
94 :     in
95 :     (Ty.AddrTy info, Ty.ImageTy info :: List.tabulate(dim, fn _ => Ty.intTy))
96 :     end
97 :     | Op.LoadVoxels(info, n) => (Ty.vecTy n, [Ty.AddrTy info])
98 :     | Op.PosToImgSpace info => let
99 :     val dim = ImageInfo.dim info
100 :     in
101 :     (Ty.vecTy dim, [Ty.ImageTy info, Ty.vecTy dim])
102 :     end
103 :     | Op.TensorToWorldSpace(info, ty) => (ty, [Ty.ImageTy info, ty])
104 :     | Op.EvalKernel(d, _, _) => (Ty.vecTy d, [Ty.vecTy d])
105 :     | Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
106 : jhr 2636 | Op.LoadImage(ty, _, _) => (ty, [])
107 :     | Op.Input(Inputs.INP{ty, ...}) => (ty, [])
108 : jhr 1640 | Op.Print tys => (Ty.TupleTy[], tys)
109 : jhr 2356 | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
110 :     (* end case *))
111 : jhr 420
112 : jhr 1116 fun typeOfCons (_, []) = false
113 : jhr 2636 | typeOfCons (Ty.SeqTy(ty, n), tys) =
114 :     List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
115 : jhr 1116 | typeOfCons (expectedTy, tys as ty1::_) =
116 : jhr 2356 if List.all (fn ty => Ty.same(ty1, ty)) tys
117 :     then (case (expectedTy, ty1)
118 :     of (Ty.SeqTy(_, n), Ty.IntTy) => (n = List.length tys)
119 :     | (Ty.TensorTy dd, Ty.TensorTy dd') => (dd = List.length tys :: dd')
120 :     | _ => false
121 :     (* end case *))
122 :     else false
123 : jhr 420
124 :     end
125 :    
126 :     structure CheckMidIL = CheckILFn (
127 :     structure IL = MidIL
128 :     structure OpTy = CheckOps)
129 : jhr 1116
130 :     structure MidPP = SSAPPFn (MidIL)
131 :    

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