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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/low-il/check-low-il.sml
ViewVC logotype

View of /branches/charisee/src/compiler/low-il/check-low-il.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3195 - (download) (annotate)
Wed May 20 21:02:12 2015 UTC (4 years, 3 months ago) by cchiw
File size: 7053 byte(s)
does not use vectors, benchmarks all faster
(* check-low-il.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure CheckOps : OPERATOR_TY = struct

    structure Op = LowOps
    structure Ty = LowILTypes

    type rator = Op.rator
    type ty = Ty.ty

    val vec3Ty = Ty.vecTy 3

  (* utility function for synthesizing eigenvector/eigenvalue signature *)
    fun eigenSig dim = let
          val tplTy = Ty.TupleTy[
                  Ty.SeqTy(Ty.realTy, dim),
                  Ty.SeqTy(Ty.vecTy dim, dim)
                ]
          in
            (tplTy, [Ty.TensorTy[dim, dim]])
          end

  (* Return the signature of a LowIL operator. *)
    fun sigOf rator = (case rator
           of  Op.IAdd  => (Ty.intTy, [Ty.intTy,Ty.intTy])
            | Op.ISub => (Ty.intTy, [Ty.intTy,Ty.intTy])
            | Op.IMul  => (Ty.intTy, [Ty.intTy,Ty.intTy])
            | Op.IDiv => (Ty.intTy, [Ty.intTy,Ty.intTy])
            | Op.INeg  =>(Ty.intTy, [Ty.intTy])
            | Op.Abs ty => (ty, [ty])
            | Op.LT ty => (Ty.BoolTy, [ty, ty])
            | Op.LTE ty => (Ty.BoolTy, [ty, ty])
            | Op.EQ ty => (Ty.BoolTy, [ty, ty])
            | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
            | Op.GT ty => (Ty.BoolTy, [ty, ty])
            | Op.GTE ty => (Ty.BoolTy, [ty, ty])
            | Op.Not => (Ty.BoolTy, [Ty.BoolTy])
            | Op.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.Clamp ty => (ty, [ty, ty, ty])
            | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
            | Op.Sqrt => (Ty.realTy, [Ty.realTy])
            | Op.Cosine => (Ty.realTy, [Ty.realTy])
            | Op.ArcCosine => (Ty.realTy, [Ty.realTy])
            | Op.Sine => (Ty.realTy, [Ty.realTy])
            | Op.PrincipleEvec _ => raise Fail "Op.PrincipleEvec unimplemented"
            | Op.EigenVecs2x2 => eigenSig 2
            | Op.EigenVecs3x3 => eigenSig 3
            | Op.EigenVals2x2 => (Ty.SeqTy(Ty.realTy, 2), [Ty.TensorTy[2,2]])
            | Op.EigenVals3x3 => (Ty.SeqTy(Ty.realTy, 3), [Ty.TensorTy[3,3]])
            | Op.Zero ty => (ty, [])
            | Op.Select(ty as Ty.TupleTy tys, i) => (List.nth(tys, i-1), [ty])
            | Op.Index(ty as Ty.TensorTy[d], _) => (Ty.realTy, [ty])
            | Op.Index(ty as Ty.SeqTy(elemTy, _), _) => (elemTy, [ty])
            | Op.Subscript(ty as Ty.TensorTy dd) => (Ty.realTy, ty :: List.map (fn _ => Ty.intTy) dd)
            | Op.Subscript(ty as Ty.SeqTy(elemTy, d)) => (elemTy, [ty, Ty.intTy])
            | Op.Ceiling d => (Ty.vecTy d, [Ty.vecTy d])
            (*| Op.Floor d => (Ty.vecTy d, [Ty.vecTy d])*)
	    | Op.Floor 1 => (Ty.TensorTy [], [Ty.TensorTy []])
            | Op.Floor d => (Ty.TensorTy [d], [Ty.TensorTy [d]])
            (*| Op.Norm(ty as Ty.TensorTy _) => (Ty.realTy, [ty])*)
            | Op.Normalize d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.Round d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.Trunc d => (Ty.vecTy d, [Ty.vecTy d])
            | Op.IntToReal => (Ty.realTy, [Ty.intTy])
            (*| Op.RealToInt 1 => (Ty.IntTy, [Ty.realTy])*)
            | Op.RealToInt 1 => (Ty.IntTy, [Ty.TensorTy []])
            | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.TensorTy[d]])
(* FIXME: the type of RealToInt should be
            | Op.RealToInt d => (Ty.SeqTy(Ty.IntTy, d), [Ty.SeqTy(Ty.realTy, d)])
*)
            | Op.ImageAddress info => (Ty.AddrTy info, [Ty.ImageTy info])
            | Op.LoadVoxels(info, n) => (Ty.vecTy n, [Ty.AddrTy info])
            | Op.Transform info => let
                val dim = ImageInfo.dim info
                in (case dim
                    of 1=>(Ty.TensorTy[], [Ty.ImageTy info])
                    | _ =>(Ty.TensorTy[dim,dim], [Ty.ImageTy info])
                    (*end case*))
                end
            | Op.Translate info  => let
                val dim = ImageInfo.dim info
                in (case dim
		 of 1 => (Ty.TensorTy[], [Ty.ImageTy info])
		  | _ => (Ty.TensorTy[dim], [Ty.ImageTy info])
		(*end case*))
		end
                (*real or int argument?*)
            | Op.IndexTensor ( _ ,_, Ty.TensorTy argTy)
                => (Ty.TensorTy [],[Ty.TensorTy argTy])
            | Op.IndexTensor ( _ ,_,  IntTy) (*fix me *)
                => (Ty.TensorTy [],[ IntTy])
            | Op. ProjectLast ( _ ,resultTy,_, Ty.TensorTy argTy)
                => (Ty.TensorTy [resultTy],[Ty.TensorTy argTy])
            | Op. ProjectFirst ( _ ,resultTy,_, Ty.TensorTy argTy)
                    => (Ty.TensorTy [resultTy],[Ty.TensorTy argTy])
	    | Op.addSca => (Ty.TensorTy([]), [Ty.TensorTy([]),Ty.TensorTy([])])
	    | Op.subSca => (Ty.TensorTy([]), [Ty.TensorTy([]),Ty.TensorTy([])])
	    | Op.prodSca => (Ty.TensorTy([]), [Ty.TensorTy([]),Ty.TensorTy([])])
	    | Op.divSca =>(Ty.TensorTy([]), [Ty.TensorTy([]),Ty.TensorTy([])])
	    | Op.powRat _ => (Ty.TensorTy([]), [Ty.TensorTy([])])
	    | Op.powInt =>(Ty.TensorTy([]), [Ty.TensorTy([]),Ty.intTy])
	    | Op.addVec d=>(Ty.TensorTy([d]),[Ty.TensorTy([d]),Ty.TensorTy([d])])
	    | Op.subVec d=>(Ty.TensorTy([d]),[Ty.TensorTy([d]),Ty.TensorTy([d])])
	    | Op.prodVec d=>(Ty.TensorTy([d]),[Ty.TensorTy([d]),Ty.TensorTy([d])])
	    | Op.prodScaV d=>(Ty.TensorTy([d]),[Ty.TensorTy([]),Ty.TensorTy([d])])
	    | Op.sumVec d =>(Ty.TensorTy([]),[Ty.TensorTy([d])])
	    | Op.dotVec d=>(Ty.TensorTy([]),[Ty.TensorTy([d]),Ty.TensorTy([d])])
	  (*imgAddr:  shape of IMG, dim, Args:  dim number of ints*)
            | Op.imgAddr(info , ty,dim) => let
                val ptrTy=Ty.AddrTy info
                in
                  (ptrTy, [ptrTy,Ty.intTy])
                end
          (*imgLoad, dim, int-2*support:Args:addrVar*)
	    | Op.imgLoad (info ,i,j)=>(Ty.TensorTy([j]), [Ty.AddrTy info])
	    | Op.baseAddr info =>(Ty.AddrTy info,[Ty.ImageTy info])
	    | Op.Kernel _ => (Ty.KernelTy, [])
            | Op.Inside(info, _) => (Ty.BoolTy, [Ty.vecTy(ImageInfo.dim info), Ty.ImageTy info])
            | Op.Input(Inputs.INP{ty, ...}) => (ty, [])
            | Op.LoadImage(ty, _, _) => (ty, [])
            | Op.Print tys => (Ty.TupleTy[], tys)
            | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
          (* end case *))

    fun typeOfCons (_, []) = false
      | typeOfCons (Ty.SeqTy(ty, n), tys) =
          List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
      | typeOfCons (expectedTy, tys as ty1::_) =
          if List.all (fn ty => Ty.same(ty1, ty)) tys
            then (case (expectedTy, ty1)
               of (Ty.SeqTy(_, n), Ty.IntTy) => (n = List.length tys)
                | (Ty.TensorTy dd, Ty.TensorTy dd') => (dd = List.length tys :: dd')
                (*|(Ty.IntTy, Ty.TensorTy [n])=> true*)
                |(Ty.TensorTy [n],Ty.IntTy)=> true
                | _ => false
              (* end case *))
            else false

  end

structure CheckLowIL = CheckILFn (
    structure IL = LowIL
    structure OpTy = CheckOps)

structure LowPP = SSAPPFn (LowIL)

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