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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2830 - (download) (annotate)
Tue Nov 18 17:48:05 2014 UTC (4 years, 10 months ago) by cchiw
File size: 7627 byte(s)
lerp and clamp
(*This function transitions low-il operators to tree-il operators *)
 (*creates the Tree-IL operators*)
structure LowOpToTreeOp = struct
local
 structure Src = LowIL
    structure SrcOp = LowOps
    structure SrcTy = LowILTypes
 structure SrcV = Src.Var
    structure DstOp = TreeOps
    structure DstTy = TreeILTypes
    structure Dst = TreeIL
    structure DstV = Dst.Var
    structure Ty=TreeIL.Ty

in

    val testing=1
    fun testp t=(case testing
        of 1=>(print(String.concat t);1)
         |_ =>1
    (*end case*))

    (**************************************)
    (*assignment, store-statement*)
    fun mkStmt(lhs,lhsKind,origrator,isFill,oSize,A,splitTy,ops)= (case origrator
        of SrcOp.sumVec _ => Dst.S_Assign([lhs], addSca ops)
        | _               => (case lhsKind
            of TreeIL.VK_Local  =>  Dst.S_Assign([lhs], Dst.E_Mux(A,isFill,oSize,splitTy,ops))
            | _ => Dst.S_StoreVec(Dst.E_Var lhs,0,A,isFill,oSize,Ty.TensorTy[oSize],splitTy, ops)
            (*end case*))
        (*end case*))
    and addSca [e1]=e1
    | addSca(e1::e2::es)= addSca([Dst.E_Op(DstOp.addSca,[e1,e2])]@es)

    (**************************************)
    (*creates a load when it is not a local variable *)
    fun getArg(lhsKind,t,count,aligned, nSize, oSize,offset)=let
        fun getExp (Dst.E_Var a) =(case (DstV.kind a)
            of TreeIL.VK_Local =>t
            | _  => Dst.E_LoadArr(aligned, nSize,oSize,t, Dst.E_Lit(Literal.Int offset))
            (*end case*))
         | getExp(Dst.E_State _)=t 
        | getExp (Dst.E_Mux(_,_,_,_,ops))= List.nth(ops,count)
        | getExp a1 = raise Fail("Arg to vector op is not Mux or Var. Found: "^Dst.toString a1)
    in
        getExp t
    end
    and  mkOps(lhsKind,oldrator,rator,oSize,pieces,argScas,argVecs,aligned,indexAt)= let
        fun createOps ([], _,_, code) = code
        | createOps (nSize::es, count,offset, code)=let
            val argsLd =List.map (fn a =>getArg(lhsKind,a,count, aligned ,nSize, oSize, offset))  argVecs
            val exp = (nSize, argScas@argsLd)
            in
                createOps (es, count+1,offset + IntInf.fromInt nSize, code@[exp])
            end
        in (case (oldrator,pieces,argVecs)
            of (SrcOp.ProjectTensor(_,_,Ty.indexTy[i],Ty.TensorTy[_,argTyX]),_,[a]) => let
                (*Use Load expression instead of Project Operator *)
                val j=i*argTyX
                fun mkLoad ([], _, code) = code
                | mkLoad (nSize::es, offset, code)=let
                    val exps=[Dst.E_LoadArr(aligned, nSize,oSize,a, Dst.E_Lit(Literal.Int offset))]
                    in
                        mkLoad (es, offset + IntInf.fromInt nSize, code@exps)
                    end
                in
                    mkLoad (pieces, IntInf.fromInt j, [])
                end
            | (SrcOp.sumVec _,[nSize],[a]) =>
                [Dst.E_Op(DstOp.sumVec(nSize,oSize), [getArg(lhsKind,a,0, aligned, nSize, oSize, 0)])]
            | (SrcOp.sumVec _,_,_) =>let
                val code=createOps (pieces, 0,indexAt, [])
                in
                    List.map (fn(nSize,args)=> Dst.E_Op(DstOp.sumVec(nSize,nSize),args)) code
                end
            | _ => let
                val code=createOps (pieces, 0,indexAt, [])
                in
                    List.map (fn(nSize,args)=> Dst.E_Op(rator nSize,args)) code
                end
            (*end case*))
        end
    (**************************************)

    fun isAligned isFill = (case isFill
        of true => false
        | _     => true
    (*end case*))

        (**************************************)
    (*Main functions*)
    fun vecToTree(lhs,origrator,rator,nSize,oSize,pieces,argScas,argVecs,isFill)= let
        val splitTy=DstTy.vectorLength pieces
        val aligned= isAligned isFill
     
        val indexAt=IntInf.fromInt 0
        (*Create Ld Expressions and Tree Ops*)
        val lhsKind=DstV.kind lhs
        val ops=mkOps(lhsKind,origrator,rator,oSize,pieces,argScas,argVecs,aligned,indexAt)
    in
        mkStmt(lhs,lhsKind,origrator,isFill,oSize,aligned,splitTy,ops)
    end

    (* consVecToTree()
     * Takes Cons of a vector and returns Mux Expression
     *)
    fun consVecToTree(_,oSize,[nSize],args,true)= let
        val nArg=length(args)
        val n=nSize-nArg
        val newArgs=List.tabulate(n, (fn _=>Dst.E_Lit(Literal.Int 0)))
        val op1=Dst.E_Cons(nSize, oSize,args@newArgs)
        val aligned= isAligned true
        val splitTy=DstTy.vectorLength [nSize]
        in
            Dst.E_Mux(aligned,true,oSize,splitTy,[op1])
        end
    | consVecToTree(_,_,_,_,true)= raise Fail"In ConsVecToTree-isFill with more than 1 piece"
    | consVecToTree(nSize,oSize,pieces,args,isFill)= let
        val aligned= isAligned isFill
        val splitTy=DstTy.vectorLength pieces
        fun createOps ([], _,_,_) = []
        | createOps (nSize::es, offset,arg, code)=
            [Dst.E_Cons(nSize, nSize,List.take(arg,nSize))]@
            createOps (es, offset + IntInf.fromInt nSize, List.drop(arg,nSize),code)
        val ops= createOps (pieces, 0,args, [])
        in
            Dst.E_Mux(aligned,isFill,oSize,splitTy,ops)
        end
        (**************************************)

    (*Low-IL operators to Tree-IL operators*)
    fun expandOp rator=(case rator
        of SrcOp.IAdd  =>    DstOp.IAdd
        | SrcOp.ISub  =>    DstOp.ISub
        | SrcOp.IMul  =>    DstOp.IMul
        | SrcOp.IDiv  =>    DstOp.IDiv
        | SrcOp.INeg  =>    DstOp.INeg
        | SrcOp.Abs ty =>    DstOp.Abs ty
        | SrcOp.LT ty =>    DstOp.LT  ty
        | SrcOp.LTE ty =>    DstOp.LTE  ty
        | SrcOp.EQ ty =>    DstOp.EQ  ty
        | SrcOp.NEQ ty =>    DstOp.NEQ  ty
        | SrcOp.GT ty =>    DstOp.GT  ty
        | SrcOp.GTE ty =>    DstOp.GTE  ty
        | SrcOp.Not =>    DstOp.Not
        | SrcOp.Max =>    DstOp.Max
        | SrcOp.Min =>    DstOp.Min
        | SrcOp.Clamp ty =>    DstOp.Clamp ty
        | SrcOp.Lerp ty =>    DstOp.Lerp  ty
        | SrcOp.Zero ty =>    DstOp.Zero  ty
        | SrcOp.PrincipleEvec ty =>    DstOp.PrincipleEvec ty
        | SrcOp.EigenVals2x2 =>    DstOp.EigenVals2x2
        | SrcOp.EigenVals3x3 =>    DstOp.EigenVals3x3
        | SrcOp.Select(ty as SrcTy.TupleTy tys, i)  =>    DstOp.Select( ty, i)
        | SrcOp.Index (ty, i ) =>    DstOp.Index ( ty, i)
        | SrcOp.Subscript ty =>    DstOp.Subscript  ty
        | SrcOp.Ceiling d =>    DstOp.Ceiling d
        | SrcOp.Floor d =>    DstOp.Floor d
        | SrcOp.Round d =>    DstOp.Round d
        | SrcOp.Trunc d =>    DstOp.Trunc d
        | SrcOp.IntToReal =>    DstOp.IntToReal
        | SrcOp.RealToInt d =>    DstOp.RealToInt d
        (*| SrcOp.VoxelAddress( info, offset)  => expandVoxelAddress  (y, info, offset, args')
        | SrcOp.LoadVoxels (rty, d ) =>    DstOp.LoadVoxels( rty, d)*)
        (*Maybe should delete?*)
        (*  | SrcOp.LoadImage info =>    DstOp.LoadImage info*)
        | SrcOp.Inside info =>    DstOp.Inside info
        | SrcOp.Translate V=>  DstOp.Translate V
        | SrcOp.addSca =>DstOp.addSca
        | SrcOp.subSca=>DstOp.subSca
        | SrcOp.prodSca=>DstOp.prodSca
        | SrcOp.divSca=>DstOp.divSca
        | SrcOp.Sqrt =>DstOp.Sqrt  
        | SrcOp.imgAddr(v,indexAt, dim)=>DstOp.imgAddr(v, indexAt, dim)
        | SrcOp.baseAddr V => DstOp.baseAddr V
        (*EigenVecs,mkDynamic, Append,Prepend, Concat,Length,ImageAddress,LoadVoxel,Inputs, and Pritns*)
        | rator => raise Fail ("bogus operator " ^ SrcOp.toString rator)
  (* end case *))
end;
end

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