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

SCM Repository

[diderot] Diff of /branches/pure-cfg/src/compiler/mid-to-low/mid-to-low.sml
ViewVC logotype

Diff of /branches/pure-cfg/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 851, Tue Apr 19 15:52:07 2011 UTC revision 852, Tue Apr 19 16:33:09 2011 UTC
# Line 81  Line 81 
81                  end                  end
82            (* end case *))            (* end case *))
83    
84      fun imul (r, a, b) = (r, DstIL.OP(DstOp.Mul DstTy.intTy, [a, b]))      fun imul (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.Mul DstTy.intTy, [a, b]))
85      fun iadd (r, a, b) = (r, DstIL.OP(DstOp.Add DstTy.intTy, [a, b]))      fun iadd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.Add DstTy.intTy, [a, b]))
86        fun ilit (r : DstIL.var, n) = (r, DstIL.LIT(Literal.Int(IntInf.fromInt n)))
87    
88    (* expand the EvalKernel operations into vector operations.  The parameters    (* expand the EvalKernel operations into vector operations.  The parameters
89     * are     * are
# Line 172  Line 173 
173              coeffVecs @ evalCode              coeffVecs @ evalCode
174            end            end
175    
176      (* add code to handle the offset and stride when addressing non-scalar image data *)
177        fun adjustForStrideAndOffset (1, _, ix, code) = (ix, code)
178          | adjustForStrideAndOffset (stride, 0, ix, code) = let
179              val offp = DstIL.Var.new ("offp", DstTy.intTy)
180              val stride' = DstIL.Var.new ("stride", DstTy.intTy)
181              in
182                (offp, [imul(offp, stride', ix), ilit(stride', stride)])
183              end
184          | adjustForStrideAndOffset (stride, offset, ix, code) = let
185              val offp = DstIL.Var.new ("offp", DstTy.intTy)
186              val stride' = DstIL.Var.new ("stride", DstTy.intTy)
187              val offset' = DstIL.Var.new ("offset", DstTy.intTy)
188              val t = DstIL.Var.new ("t", DstTy.intTy)
189              val code = [
190                      imul(offp, stride', t),
191                      ilit (stride', stride),
192                      iadd(t, offset', ix),
193                      ilit (offset', offset)
194                    ]
195              in
196                (offp, code)
197              end
198    
199    (* compute the load address for a given set of voxels indices.  For the    (* compute the load address for a given set of voxels indices.  For the
200     * operation     * operation
201     *     *
202     *    VoxelAddress<info>(i_1, ..., i_d)     *    VoxelAddress<info,offset>(i_1, ..., i_d)
203     *     *
204     * the address is given by     * the address is given by
205     *     *
206     *    base + i_1 + N_1 * (i_2 + N_2 * (... + N_{d-1} * i_d) ...))     *    base + stride * (offset + i_1 + N_1 * (i_2 + N_2 * (... + N_{d-1} * i_d) ...))
207     *     *
208     * where     * where
209     *    base    -- base address of the image data     *    base    -- base address of the image data
210       *    stride  -- number of samples per voxel
211       *    offset  -- offset of sample being addressed
212     *    N_i     -- size of ith axis in elements     *    N_i     -- size of ith axis in elements
213     *     *
214     * Note that we are following the Nrrd convention that the axes are ordered     * Note that we are following the Nrrd convention that the axes are ordered
# Line 190  Line 216 
216     * arithmetic, where the offset will be automatically scaled by the size of the     * arithmetic, where the offset will be automatically scaled by the size of the
217     * elements.     * elements.
218     *)     *)
219      fun expandVoxelAddress (result, info, img::ix1::indices) = let      fun expandVoxelAddress (result, info, offset, [img, ix]) = let
220            val ImageInfo.ImgInfo{dim, ty=(d, rTy), sizes=sizes, ...} = info            val dim = ImageInfo.dim info
221              val sizes = ImageInfo.sizes info
222              val stride = ImageInfo.stride info
223              val shape = ImageInfo.voxelShape info
224              val (offp, code) = adjustForStrideAndOffset (stride, offset, ix, [])
225              val addrTy = DstTy.AddrTy info
226              val base = DstIL.Var.new ("imgBaseAddr", addrTy)
227              val code = (result, DstIL.OP(DstOp.Add addrTy, [base, offp])) ::
228                    (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
229                    code
230              in
231                List.rev code
232              end
233          | expandVoxelAddress (result, info, offset, img::ix1::indices) = let
234              val dim = ImageInfo.dim info
235              val sizes = ImageInfo.sizes info
236              val stride = ImageInfo.stride info
237              val shape = ImageInfo.voxelShape info
238            (* get N_1 ... N_{d-1} *)
239            val sizes = List.take (sizes, List.length sizes - 1)            val sizes = List.take (sizes, List.length sizes - 1)
240          (* generate the address computation code in reverse order *)          (* generate the address computation code in reverse order *)
241            fun gen (d, [n], [ix]) = let            fun gen (d, [n], [ix]) = let
# Line 199  Line 243 
243                  val t = DstIL.Var.new ("t", DstTy.intTy)                  val t = DstIL.Var.new ("t", DstTy.intTy)
244                  val code = [                  val code = [
245                          imul(t, n', ix),                          imul(t, n', ix),
246                          (n', DstIL.LIT(Literal.Int(IntInf.fromInt n)))                          ilit(n', n)
247                        ]                        ]
248                  in                  in
249                    (t, code)                    (t, code)
# Line 211  Line 255 
255                  val (t, code) = gen (d+1, ns, ixs)                  val (t, code) = gen (d+1, ns, ixs)
256                  val code =                  val code =
257                        imul(t2, n', t1) ::                        imul(t2, n', t1) ::
258                        (n', DstIL.LIT(Literal.Int(IntInf.fromInt n))) ::                        ilit(n', n) ::
259                        iadd(t1, ix, t) :: code                        iadd(t1, ix, t) :: code
260                  in                  in
261                    (t2, code)                    (t2, code)
262                  end                  end
 (* FIXME: what about non-scalar images?  
           val voxSzb = ImageInfo.voxelSzB info  
 *)  
263            val (tmp, code) = gen (0, sizes, indices)            val (tmp, code) = gen (0, sizes, indices)
264            val offset = DstIL.Var.new ("offp", DstTy.intTy)            val (offp, code) = adjustForStrideAndOffset (stride, offset, ix1, code)
265            val addrTy = DstTy.AddrTy info            val addrTy = DstTy.AddrTy info
266            val base = DstIL.Var.new ("imgBaseAddr", addrTy)            val base = DstIL.Var.new ("imgBaseAddr", addrTy)
267            val code = (result, DstIL.OP(DstOp.Add addrTy, [base, offset])) ::            val code = (result, DstIL.OP(DstOp.Add addrTy, [base, offp])) ::
268                  (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::                  (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
                 iadd(offset, ix1, tmp) ::  
269                  code                  code
270            in            in
271              List.rev code              List.rev code
# Line 273  Line 313 
313                | SrcOp.Trunc d => assign (DstOp.Trunc d)                | SrcOp.Trunc d => assign (DstOp.Trunc d)
314                | SrcOp.IntToReal => assign (DstOp.IntToReal)                | SrcOp.IntToReal => assign (DstOp.IntToReal)
315                | SrcOp.RealToInt d => assign (DstOp.RealToInt d)                | SrcOp.RealToInt d => assign (DstOp.RealToInt d)
316                | SrcOp.VoxelAddress dim => (case SrcIL.Var.binding(hd args)                | SrcOp.VoxelAddress(info, offset) => expandVoxelAddress (y, info, offset, args')
                    of SrcIL.VB_RHS(SrcIL.OP(SrcOp.LoadImage info, _)) =>  
                         expandVoxelAddress (y, info, args')  
                     | vb => raise Fail(concat[  
                           "bad binding in ", SrcOp.toString rator, "(", SrcIL.Var.toString(hd args),  
                           "==", SrcIL.vbToString vb, ", ...)"  
                         ])  
                   (* end case *))  
317                | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))                | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
318                | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)                | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
319                | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)                | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)

Legend:
Removed from v.851  
changed lines
  Added in v.852

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