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 2690 - (download) (annotate)
Fri Aug 29 15:41:22 2014 UTC (5 years, 1 month ago) by cchiw
File size: 7096 byte(s)
clean up tree-il
(*This function transitions low-il operators to tree-il operators *)
 (*creates the Tree-IL operators*)
structure LowOpToTreeOp = struct
local

    structure SrcOp = LowOps
    structure DstOp = TreeOps
    structure SrcTy = LowILTypes
    structure DstTy = TreeILTypes
    structure Dst = TreeIL
    structure VTree = TreeIL.Var
    structure VLow = LowIL.Var
        structure VTree = TreeIL.Var
    structure Ty=TreeIL.Ty



in

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


(*
assumes that if it is a local then it is a vector.
not always true
maybe just check size

*)
        (**************************************)
    (*creates a load when it is not a local variable *)
    fun  getArg(t,count,aligned, nSize, oSize,offset)=let
        fun getExp (Dst.E_Var a) =(case ( VTree.kind a,VTree.ty a)
            of (TreeIL.VK_Local,Ty.TensorTy [nv]) =>
                if (nv=nSize) then t
                else  Dst.E_LoadArr(aligned, nSize, oSize,t, Dst.E_Lit(Literal.Int offset))
            |(TreeIL.VK_Local,_) =>t
            | (_,_) =>  Dst.E_LoadArr(aligned, nSize,oSize,t, Dst.E_Lit(Literal.Int offset))
            (*end case*))
        | getExp (Dst.E_Mux(_,_,_,_,ops))= List.nth(ops,count)
        | getExp _ = t

        val exp =getExp t 
        val _ =testp ["\n\t Arg",Dst.toString t, "====>>",Dst.toString exp]
        in
            exp
        end
        (**************************************)

    fun getLHS(lhs,cnt,A,nSize,oSize,indexAt,splitTy,ops) =(case VTree.kind lhs
        of TreeIL.VK_Local =>(SOME (Dst.E_Mux(A,nSize,oSize,splitTy,ops)),NONE)
        | _=> (NONE,SOME(Dst.S_StoreVec(Dst.E_Var lhs,A,nSize,oSize,splitTy,ops)))
    (*end case*))


    (**************************************)
    (*Create Op and Ld expressions*)

    fun mkOps(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(a,count, aligned, nSize, oSize, offset))  argVecs
            val exp = Dst.E_Op(rator nSize, argScas@argsLd)
            in
                createOps (es, count+1,offset + IntInf.fromInt nSize, code@[exp])
            end
    in
        createOps (pieces, 0,indexAt, [])
    end


    (**************************************)


    (*addSca function used for sumVec function *)
    fun addSca [e1]=e1
      | addSca(e1::e2::es)= addSca([Dst.E_Op(DstOp.addSca,[e1,e2])]@es)


    (**************************************)

        (*assignment, store-statement, and store-expression*)
    fun mkAssn(lhs,cnt,rator,nSize,oSize,pieces,isFill,aligned,splitTy,ops,indexAt)= (case rator nSize
        of DstOp.sumVec _ =>(SOME( addSca ops),NONE)
        | DstOp.dotVec _ => (SOME( addSca ops),NONE)
        | _=>getLHS(lhs,cnt,aligned,nSize,oSize,indexAt,splitTy,ops)

        (*end case*))

    (* This will create assignments with multiple mkVec*)
    (*x=[MkVec 2(Add 2), mkVec 4(Add 4)] Multiple mkVecs *)

    (**************************************)
    (*If it is matrix then we need to add offset to indexAt*)
    fun matInt( n, m,a, b )=(m*b+a)
    fun isMatrix2([Dst.E_Op(DstOp.IndexTensor(_,DstTy.TensorTy [_], DstTy.indexTy [fast], DstTy.TensorTy[argTyY,argTyX]),m)],rest)= let
        val indexAt=matInt(argTyY,argTyX,0,fast)
        in
            (indexAt,rest@m)
        end
    | isMatrix2(Dst.E_Op(DstOp.IndexTensor(_,DstTy.TensorTy [_], DstTy.indexTy [fast], DstTy.TensorTy[argTyY,argTyX]),m)::es,rest)=
        isMatrix2(es, rest@m)
    | isMatrix2(a,[])=(0, a)
    | isMatrix2 _=raise Fail"Should be projection"


    (**************************************)

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

        (**************************************)
    (*Main functions*)
    fun vecToTree(lhs,cnt,rator,nSize,oSize,pieces,argScas,argV',isFill)= let

        val splitTy=DstTy.vectorLength pieces
        val (indexAt', argVecs)=isMatrix2(argV',[])

        val _ =  testp ["\n\t ",Dst.kindToString(VTree.kind lhs)]
        val aligned= isAligned isFill
        val indexAt=IntInf.fromInt indexAt'

        (*Create Ld Expressions and Tree Ops*)
        val ops=mkOps(rator,oSize,pieces,argScas,argVecs,aligned,indexAt)

        (* create statement*)
        val expA=mkAssn(lhs,cnt,rator,nSize,oSize,pieces,isFill,aligned,splitTy,ops, indexAt)

    in
        expA 
    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.Transform V=>    DstOp.Transform V
        | 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
        (*Still need to sort *)
        | SrcOp.IndexTensor e=> DstOp.IndexTensor e
    
        | SrcOp.imgAddr(v,indexAt, dim)=>DstOp.imgAddr(v, indexAt, dim)
        | SrcOp.imgLoad(v,dim,n)=>DstOp.imgLoad(v,dim,n)

    | 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