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 2749 - (download) (annotate)
Wed Oct 1 23:11:09 2014 UTC (5 years, 11 months ago) by cchiw
File size: 9131 byte(s)
added projection function name
(*This function transitions low-il operators to tree-il operators *)
 (*creates the Tree-IL operators*)
structure LowOpToTreeOp = struct
 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


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

    (*assignment, store-statement, and store-expression*)

    fun mkStmt(lhsExp,lhsKind,rator,nSize,oSize,A,splitTy,ops)= (case (rator nSize)
        of DstOp.sumVec _ => (SOME( addSca ops),NONE)
        | DstOp.dotVec _  => (SOME( addSca ops),NONE)
        | _               => (case lhsKind
            of TreeIL.VK_Local => (SOME (Dst.E_Mux(A,nSize,oSize,splitTy,ops)),NONE)
            | _=> (NONE,SOME(Dst.S_StoreVec(lhsExp,A,nSize,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,DstV.ty a)
            of (TreeIL.VK_Local,Ty.TensorTy [nv]) =>
                if (nv=nSize) then t
                else (*raise Fail "Vector operation is not the right size"*)
                   Dst.E_LoadVec(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]
    and mkOps(lhsKind,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 = Dst.E_Op(rator nSize, argScas@argsLd)
                createOps (es, count+1,offset + IntInf.fromInt nSize, code@[exp])
        createOps (pieces, 0,indexAt, [])
    and mkCons(pieces,args)= let
        fun createOps ([], _,_, _,_) = []
        | createOps (nSize::es, count,offset,arg, code)=
            [Dst.E_Cons(Ty.TensorTy [nSize], nSize,List.take(arg,nSize))]@
                createOps (es, count+1,offset + IntInf.fromInt nSize, List.drop(arg,nSize),code)
            createOps (pieces, 0,0,args, [])
    and mkFillCons(nSize,nOrig,args)=let
        val nArg=length(args)
        in if(nSize>nArg) then let
            val n=nSize-nArg
            val newArgs=List.tabulate(n, (fn _=>Dst.E_Lit(Literal.Int 0)))
                Dst.E_Cons(Ty.TensorTy [nSize], nOrig,args@newArgs)
            else raise Fail( "more args then new size")

    (*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)
    | 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,rator,nSize,oSize,pieces,argScas,argV',isFill)= let

        val splitTy=DstTy.vectorLength pieces

        (*val (indexAt', argVecs)=isMatrix2(argV',[])*)
        val indexAt'=0
        val argVecs=argV'

        val lhsKind=DstV.kind lhs
        val _ =testp ["\n\t LHS ",Dst.kindToString lhsKind,"- Var:",DstV.name lhs]
        val aligned= isAligned isFill
        val indexAt=IntInf.fromInt indexAt'

        (*Create Ld Expressions and Tree Ops*)
        val ops=mkOps(lhsKind,rator,oSize,pieces,argScas,argVecs,aligned,indexAt)
        mkStmt(Dst.E_Var lhs,lhsKind,rator,nSize,oSize,aligned,splitTy,ops)

    (*isFIll is true, and there is one piece, use mkFillCons*)
    fun consVecToTree(nSize,oSize,[piece],args,true)= let
            val op1= mkFillCons(piece,oSize,args)
            val aligned= isAligned true
            val splitTy=DstTy.vectorLength [nSize]
        | consVecToTree(nSize,oSize,pieces,args,isFill)= let
            val ops= mkCons(pieces,args)
            val aligned= isAligned isFill
            val splitTy=DstTy.vectorLength pieces

    fun consArrToTree(lhs,nSize,oSize,pieces,args,isFill)= let
        val name=SrcV.name lhs
        val ty=DstTy.tensorTy [oSize]
        val varA=Dst.Var.new(name^Int.toString(oSize), Dst.VK_Global,ty)
        val s1=Dst.S_Cons(varA,ty,ty,args)
         val A = isAligned isFill

        (*return load expression *)
        fun mkLoad([],offset,xs,exps)=(xs,exps)
        | mkLoad(c::cSize,offset,xs,exps)=let
            val e= Dst.E_LoadArr(A, c,oSize,Dst.E_Var varA, Dst.E_Lit(Literal.Int offset))
            val x=Dst.Var.new(name^Int.toString(c), Dst.VK_Local,Ty.TensorTy [c])
                mkLoad(cSize,offset + IntInf.fromInt c,xs@[x],exps@[e])

        val (xs,ldexps)=mkLoad(pieces,0,[],[])
        val stmt=ListPair.map  (fn(x,e)=>Dst.S_Assign([x],e)) (xs,ldexps)

        val splitTy=DstTy.vectorLength pieces
        val xsVar=List.map (fn v=>Dst.E_Var v) xs

        val rhs=Dst.E_Mux(A, nSize,oSize,splitTy, xsVar)
        val xss= [Dst.E_Var varA]@xsVar
        val _ =print(String.concat(["\n\t vars: "]@ List.map Dst.toString xss))

            ([varA]@xs,rhs,List.rev ([s1]@stmt))



    (*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 *))

ViewVC Help
Powered by ViewVC 1.0.0