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

SCM Repository

[diderot] Annotation of /branches/charisee/src/compiler/tree-il/lowOp-to-treeOp.sml
ViewVC logotype

Annotation of /branches/charisee/src/compiler/tree-il/lowOp-to-treeOp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3169 - (view) (download)

1 : cchiw 2844 (*This function transitions low-il operators to tree-il operators
2 :     When there is a LowIL vector op then it breaks it into HW supported TreeIL vector operation
3 :     i.e. A6+B6=> Mux[A4+B4, A2+B2]
4 :     The following variables are used
5 :     isAligned/A:bool-Is the array aligned
6 :     isFill:bool-Is the vector filled with zeros i.e. length 3 vectors represented with length 4
7 :     nSize: int -The Size of the vector operation, (4)
8 :     oSize:int-The Size of the orginal arguments in vector operation if less than new Size. (3)
9 :     pieces: sizes of vector operations. i.e.2->[2], 6->[4,2] 3->[4]
10 :     *)
11 :    
12 : jhr 3169 (* FIXME: add signature!!! *)
13 :     structure LowOpToTreeOp =
14 :     struct
15 :     local
16 :     structure Src = LowIL
17 :     structure SrcOp = LowOps
18 :     structure SrcTy = LowILTypes
19 :     structure SrcV = Src.Var
20 :     structure DstOp = TreeOps
21 :     structure DstTy = TreeILTypes
22 :     structure Dst = TreeIL
23 :     structure DstSV=Dst.StateVar
24 :     structure DstV = Dst.Var
25 :     structure Ty=TreeIL.Ty
26 :     in
27 : cchiw 2637
28 : cchiw 2664 val testing=1
29 : cchiw 2690 fun testp t=(case testing
30 :     of 1=>(print(String.concat t);1)
31 :     |_ =>1
32 : cchiw 2688 (*end case*))
33 : cchiw 2680
34 : cchiw 2691 (**************************************)
35 : cchiw 2844 (* isAlignedLoad: bool* ty->bool
36 :     * Do we Load this array assuming it's aligned?
37 :     * Decides if isAligned is true when creating E_load
38 :     * Currently, it is only true when the vector argument can be represent in the HW
39 :     * Fix here
40 :     *)
41 :     fun isAlignedLoad(isFill,Ty.TensorTy [_]) =(case isFill
42 :     of true => false
43 :     | _ => true
44 :     (*end case*))
45 :     | isAlignedLoad _ =false
46 :    
47 :     (* isAlignedStore: bool*int->bool
48 :     * Do we Store this array assuming it's aligned?
49 :     * Decides if isAligned is true when creating S_Store and E_Mux
50 :     * Currently, it is false always
51 :     * Fix here
52 :     *)
53 :     fun (*isAlignedStore(isFill,1) =(case isFill
54 :     of true => false
55 :     | _ => true
56 :     (*end case*))
57 :     |*)isAlignedStore _ =false(*true*)
58 :    
59 :     (**************************************)
60 :     (*mkStmt:TreeIL.Var* TreeIL.Var.kind*MidIL.Op*bool*int*bool*ty*TreeIL.Exp list ->TreeIL.stmt
61 :     * makes final TreeIL.Stmt
62 :     * When TreeIL.Op is a sumVec is then all ops are added
63 :     * If lhs is a global then S_Store is used
64 :     * Otherwise uses S_Assign
65 :     *)
66 : cchiw 2795 fun mkStmt(lhs,lhsKind,origrator,isFill,oSize,A,splitTy,ops)= (case origrator
67 :     of SrcOp.sumVec _ => Dst.S_Assign([lhs], addSca ops)
68 :     | _ => (case lhsKind
69 :     of TreeIL.VK_Local => Dst.S_Assign([lhs], Dst.E_Mux(A,isFill,oSize,splitTy,ops))
70 : cchiw 2844 | _ => Dst.S_StoreVec(Dst.E_Var lhs,0,A,isFill,oSize,Ty.TensorTy[oSize],splitTy, ops)
71 : cchiw 2791 (*end case*))
72 : cchiw 2795 (*end case*))
73 : cchiw 2691 and addSca [e1]=e1
74 :     | addSca(e1::e2::es)= addSca([Dst.E_Op(DstOp.addSca,[e1,e2])]@es)
75 : cchiw 2680
76 : cchiw 2838
77 :    
78 : cchiw 2844 (**************************************)
79 :     (*getArg:bool*TreeILVar.kind*TreeIL.Exp*int*int*int*int->TreeIL.Exp
80 :     * Gets the argument for the operation
81 :     * if the argument is a mux then we get the piece needed for the operation
82 :     * if the argument is a local var then we assume it is the right size
83 :     * if the argument is a global var or state var then we load the array with offset and sizes
84 :     * otherwise the arg was not loaded probably and should produce an error
85 :     *)
86 :     fun getArg(isFill,lhsKind,t,count, nSize, oSize,offset)=(case t
87 :     of (Dst.E_Var a) =>(case (DstV.kind a)
88 : cchiw 2830 of TreeIL.VK_Local =>t
89 : cchiw 2838 | _ => Dst.E_LoadArr(isAlignedLoad(isFill,DstV.ty a) ,nSize,oSize,t, Dst.E_Lit(Literal.Int offset))
90 : cchiw 2680 (*end case*))
91 : cchiw 2844 | (Dst.E_State a)=> Dst.E_LoadArr(isAlignedLoad(isFill,DstSV.ty a) ,nSize,oSize,t, Dst.E_Lit(Literal.Int offset))
92 :     | (Dst.E_Mux(_,_,_,_,ops))=> List.nth(ops,count)
93 :     | a1 =>( print(String.concat["Warning argument to vector operation is: ",Dst.toString a1]);a1)
94 :     (*| a1 = raise Fail("Arg to vector op is not Mux or Var. Found: "^Dst.toString a1)*)
95 :     (*end case*))
96 : cchiw 2838
97 : cchiw 2844
98 :     (* mkGenericOps:bool.TreeIL.Var.kind*LowILOP*TreeILOP*int*int list*TreeILExp*TreeILExp*int->TreeILExp List
99 :     Take the original vector op and breaks it "pieces" which are HW supported sizes
100 :     gets arguments with getArg().
101 :     and then puts them inside TreeIL op ("rator")
102 :     special case sumVecOp, because it maintains new and old size.
103 :    
104 :     *)
105 :     fun mkGenericOps(isFill,lhsKind,oldrator,rator,oSize,pieces,argScas,argVecs,indexAt)= let
106 :     (*createOps:int list*int*int*DstIL.exp->DstIL.exp list
107 :     * Gets all the Arguments in order
108 :     * i.e. nsize=[4,2]=> [[A4,B4],[A2,B2]]
109 :     *)
110 : cchiw 2680 fun createOps ([], _,_, code) = code
111 : cchiw 2795 | createOps (nSize::es, count,offset, code)=let
112 : cchiw 2838 val argsLd =List.map (fn a =>getArg(isFill,lhsKind,a,count, nSize, oSize, offset)) argVecs
113 : cchiw 2827 val exp = (nSize, argScas@argsLd)
114 : cchiw 2749 in
115 : cchiw 2795 createOps (es, count+1,offset + IntInf.fromInt nSize, code@[exp])
116 : cchiw 2749 end
117 : cchiw 2827 in (case (oldrator,pieces,argVecs)
118 : cchiw 2870 of (SrcOp.sumVec _,[nSize],[a]) =>let
119 :     val args=[getArg(isFill,lhsKind,a,0, nSize, oSize, (0))]
120 :     in
121 :     [Dst.E_Op(DstOp.sumVec([nSize],oSize),args)]
122 :     end
123 : cchiw 2827 | (SrcOp.sumVec _,_,_) =>let
124 :     val code=createOps (pieces, 0,indexAt, [])
125 : cchiw 2870 val args=List.foldr op@ [] (List.map (fn(_,args)=>args) code)
126 : cchiw 2827 in
127 : cchiw 2870 (* List.map (fn(nSize,args)=> Dst.E_Op(DstOp.sumVec([nSize],nSize),args)) code*)
128 :     [Dst.E_Op(DstOp.sumVec(pieces,oSize),args)]
129 :    
130 :    
131 : cchiw 2827 end
132 :     | _ => let
133 :     val code=createOps (pieces, 0,indexAt, [])
134 :     in
135 :     List.map (fn(nSize,args)=> Dst.E_Op(rator nSize,args)) code
136 :     end
137 :     (*end case*))
138 : cchiw 2680 end
139 :    
140 : cchiw 2844 (* Same funciton as above
141 :     *Except this first scans for special case ProjectTensor LowIL Op.
142 :     *ProjectTensor is an op that slices a vector from a higher order tensor
143 :     * That vec could be in pieces
144 :     * So we load each one and the next step wraps a mux around it
145 :     * We can assume the higher order tensor is an array
146 :     * This rewrites the PorjectTensor op as an E_LoadArr.
147 :     *Otherwise hand off to above function
148 :     *)
149 :     fun mkOps(isFill,lhsKind,oldrator,rator,oSize,pieces,argScas,argVecs,indexAt)= (case (oldrator,pieces,argVecs)
150 :     of (SrcOp.ProjectTensor(_,_,indTy,argTy),_,[a]) => let
151 :     val A=isAlignedLoad(isFill,argTy)
152 :     val shift=(case (indTy,argTy)
153 :     of (Ty.indexTy[i],Ty.TensorTy[_,argTyX])=> i*argTyX
154 :     | (Ty.indexTy[j,i],Ty.TensorTy[_,argTyY,argTyX])=> j*(argTyX*argTyY)+i*argTyX
155 :     | _ =>raise Fail "Project Tensor of a unhandled size"
156 :     (*end case *))
157 :     fun mkLoad ([], _, code) = code
158 :     | mkLoad (nSize::es, offset, code)=
159 :     mkLoad (es, offset + IntInf.fromInt nSize,
160 :     code@[Dst.E_LoadArr(A, nSize,oSize,a, Dst.E_Lit(Literal.Int offset))])
161 :     in
162 :     mkLoad (pieces, IntInf.fromInt shift, [])
163 :     end
164 : cchiw 2870 | (SrcOp.sumVec _,[nSize],[a]) => [Dst.E_Op(DstOp.sumVec([nSize],oSize), [getArg(isFill,lhsKind,a,0, nSize, oSize, 0)])]
165 : cchiw 2844 | _ => mkGenericOps(isFill,lhsKind,oldrator,rator,oSize,pieces,argScas,argVecs,indexAt)
166 : cchiw 2838 (*end case*))
167 : cchiw 2680
168 : cchiw 2690 (**************************************)
169 : cchiw 2844 (*vecToTree:TreeIL.Var.kind*LowILOP*TreeILOP*int*int*int list*TreeILExp list *TreeILExp list *bool->TreeILExp List
170 :     *Main function that turns a low-il vector operation into tree-il stmt
171 :     *)
172 : cchiw 2795 fun vecToTree(lhs,origrator,rator,nSize,oSize,pieces,argScas,argVecs,isFill)= let
173 : cchiw 2637 val splitTy=DstTy.vectorLength pieces
174 : cchiw 2844 val aligned= isAlignedStore(isFill,length pieces)
175 : cchiw 2789 val indexAt=IntInf.fromInt 0
176 : cchiw 2676 (*Create Ld Expressions and Tree Ops*)
177 : cchiw 2795 val lhsKind=DstV.kind lhs
178 : cchiw 2844
179 : cchiw 2838 val ops=mkOps(isFill,lhsKind,origrator,rator,oSize,pieces,argScas,argVecs,indexAt)
180 : cchiw 2676 in
181 : cchiw 2795 mkStmt(lhs,lhsKind,origrator,isFill,oSize,aligned,splitTy,ops)
182 : cchiw 2676 end
183 : cchiw 2637
184 : cchiw 2844 (* consVecToTree:int*int*int list*TreeIL.Exp* bool->TreeIL.Exp
185 :     * Takes Cons of a vector and returns TreeIL.exp inside E_Mux.
186 :     * When isFill is true, creates zeros
187 : cchiw 2795 *)
188 :     fun consVecToTree(_,oSize,[nSize],args,true)= let
189 :     val nArg=length(args)
190 :     val n=nSize-nArg
191 :     val newArgs=List.tabulate(n, (fn _=>Dst.E_Lit(Literal.Int 0)))
192 :     val op1=Dst.E_Cons(nSize, oSize,args@newArgs)
193 : cchiw 2844 val aligned= isAlignedStore(true,1)
194 : cchiw 2789 val splitTy=DstTy.vectorLength [nSize]
195 : cchiw 2791 in
196 : cchiw 2795 Dst.E_Mux(aligned,true,oSize,splitTy,[op1])
197 : cchiw 2791 end
198 : cchiw 2795 | consVecToTree(_,_,_,_,true)= raise Fail"In ConsVecToTree-isFill with more than 1 piece"
199 :     | consVecToTree(nSize,oSize,pieces,args,isFill)= let
200 : cchiw 2844 val aligned= isAlignedStore(isFill,length pieces)
201 : cchiw 2795 val splitTy=DstTy.vectorLength pieces
202 : cchiw 2827 fun createOps ([], _,_,_) = []
203 :     | createOps (nSize::es, offset,arg, code)=
204 : cchiw 2795 [Dst.E_Cons(nSize, nSize,List.take(arg,nSize))]@
205 : cchiw 2827 createOps (es, offset + IntInf.fromInt nSize, List.drop(arg,nSize),code)
206 :     val ops= createOps (pieces, 0,args, [])
207 : cchiw 2795 in
208 :     Dst.E_Mux(aligned,isFill,oSize,splitTy,ops)
209 :     end
210 : cchiw 2671 (**************************************)
211 : cchiw 2637
212 :     (*Low-IL operators to Tree-IL operators*)
213 :     fun expandOp rator=(case rator
214 :     of SrcOp.IAdd => DstOp.IAdd
215 :     | SrcOp.ISub => DstOp.ISub
216 :     | SrcOp.IMul => DstOp.IMul
217 :     | SrcOp.IDiv => DstOp.IDiv
218 :     | SrcOp.INeg => DstOp.INeg
219 :     | SrcOp.Abs ty => DstOp.Abs ty
220 :     | SrcOp.LT ty => DstOp.LT ty
221 :     | SrcOp.LTE ty => DstOp.LTE ty
222 :     | SrcOp.EQ ty => DstOp.EQ ty
223 :     | SrcOp.NEQ ty => DstOp.NEQ ty
224 :     | SrcOp.GT ty => DstOp.GT ty
225 :     | SrcOp.GTE ty => DstOp.GTE ty
226 :     | SrcOp.Not => DstOp.Not
227 :     | SrcOp.Max => DstOp.Max
228 :     | SrcOp.Min => DstOp.Min
229 :     | SrcOp.Clamp ty => DstOp.Clamp ty
230 :     | SrcOp.Lerp ty => DstOp.Lerp ty
231 : cchiw 2845 | SrcOp.Sqrt=> DstOp.Sqrt
232 : cchiw 3138 | SrcOp.Cosine=> DstOp.Cosine
233 :     | SrcOp.ArcCosine=> DstOp.ArcCosine
234 :     | SrcOp.Sine=> DstOp.Sine
235 : cchiw 2637 | SrcOp.Zero ty => DstOp.Zero ty
236 :     | SrcOp.PrincipleEvec ty => DstOp.PrincipleEvec ty
237 :     | SrcOp.EigenVals2x2 => DstOp.EigenVals2x2
238 :     | SrcOp.EigenVals3x3 => DstOp.EigenVals3x3
239 : cchiw 2842 | SrcOp.EigenVecs2x2 => DstOp.EigenVecs2x2
240 :     | SrcOp.EigenVecs3x3 => DstOp.EigenVecs3x3
241 : cchiw 2637 | SrcOp.Select(ty as SrcTy.TupleTy tys, i) => DstOp.Select( ty, i)
242 :     | SrcOp.Index (ty, i ) => DstOp.Index ( ty, i)
243 :     | SrcOp.Subscript ty => DstOp.Subscript ty
244 :     | SrcOp.Ceiling d => DstOp.Ceiling d
245 :     | SrcOp.Floor d => DstOp.Floor d
246 :     | SrcOp.Round d => DstOp.Round d
247 :     | SrcOp.Trunc d => DstOp.Trunc d
248 :     | SrcOp.IntToReal => DstOp.IntToReal
249 : jhr 3060 | SrcOp.R r => DstOp.R r
250 : cchiw 2637 | SrcOp.RealToInt d => DstOp.RealToInt d
251 :     (*| SrcOp.VoxelAddress( info, offset) => expandVoxelAddress (y, info, offset, args')
252 :     | SrcOp.LoadVoxels (rty, d ) => DstOp.LoadVoxels( rty, d)*)
253 : cchiw 2680 (*Maybe should delete?*)
254 : cchiw 2691 (* | SrcOp.LoadImage info => DstOp.LoadImage info*)
255 : cchiw 2637 | SrcOp.Inside info => DstOp.Inside info
256 :     | SrcOp.Translate V=> DstOp.Translate V
257 :     | SrcOp.addSca =>DstOp.addSca
258 : jhr 3060 | SrcOp.subSca => DstOp.subSca
259 :     | SrcOp.prodSca => DstOp.prodSca
260 :     | SrcOp.divSca => DstOp.divSca
261 :     | SrcOp.powRat ty => raise Fail"PowRat"
262 :     | SrcOp.powInt => raise Fail"PowInt"
263 : cchiw 2838 (*| SrcOp.Norm ty => (DstOp.Norm ty)*)
264 :     | SrcOp.Normalize d =>(DstOp.Normalize d)
265 : cchiw 2637 | SrcOp.imgAddr(v,indexAt, dim)=>DstOp.imgAddr(v, indexAt, dim)
266 : cchiw 2691 | SrcOp.baseAddr V => DstOp.baseAddr V
267 : cchiw 2637 (*EigenVecs,mkDynamic, Append,Prepend, Concat,Length,ImageAddress,LoadVoxel,Inputs, and Pritns*)
268 : cchiw 2845 (*| SrcOp.Input e=>*)
269 : jhr 3060 | SrcOp.LoadImage args => DstOp.LoadImage args
270 :     | SrcOp.Input inp => DstOp.Input inp
271 :     | SrcOp.Print tys => DstOp.Print tys
272 : cchiw 2637 | rator => raise Fail ("bogus operator " ^ SrcOp.toString rator)
273 : jhr 3060 (* end case *))
274 :     end
275 :    
276 :     end

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