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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/high-to-mid/clean-params.sml
ViewVC logotype

View of /branches/vis15/src/compiler/high-to-mid/clean-params.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3561 - (download) (annotate)
Sun Jan 10 15:18:34 2016 UTC (4 years, 4 months ago) by jhr
Original Path: branches/vis15/src/compiler/high-to-mid/clean-param.sml
File size: 6060 byte(s)
working on merge
(* clean-param.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 *)

(*
*cleanParam.sml cleans the parameters in an EIN expression.
*Cleaning parameters is simple.
*We keep track of all the paramids used in subexpression(getIdCount()),
*remap the param ids(mkMap)
*and choosing the mid-il args that are used, and then lastly rewrites the body.
*)
structure CleanParams : sig

    val clean ? -> ?

  end = struct

    structure E = Ein
    structure DstIL = MidIL
    structure DstV = DstIL.Var

(* FIXME *)
    fun insert (key, value) d =fn s =>
        if s = key then SOME value
        else d s
    fun lookup k d = d k
    val empty =fn key =>NONE

    fun err str=raise Fail str
    fun iTos i =Int.toString i
    fun setEin(params,index,body)=Ein.EIN{params=params, index=index, body=body}
    fun cnt(DstIL.V{useCnt, ...}) = !useCnt
    fun incUse (DstIL.V{useCnt, ...}) = (useCnt := !useCnt + 2)
    fun use x = (incUse x; x)
    fun getUse e=String.concat["\n\t Varname "^DstIL.Var.name e," -use ",iTos(cnt e)]
    fun assignEinApp(y,params,index,body,args)= (y,DstIL.EINAPP(setEin(params,index,body),args))
    
    (*dictionary to lookup mapp*)
    fun lkupIndexSingle(e1,mapp,str)=(case (lookup e1 mapp)
        of SOME l=>l
        | _=> raise Fail(str^iTos(e1))
        (*endcase*))

    (* mkMapp:dict*params*var list ->dict*params*var list
    * countmapp dictionary keeps track of which ids have been used
    * mapp id the dictionary of the new ids
    *)
    fun mkMapp(countmapp,params,args)=let
        val n=(length params)
        val ix=List.tabulate(n, (fn e=> e))
        fun m([],_,mapp,p, _, a, _)=(mapp,p,a)
          | m(i::ix,j,mapp,p, p1::params, a, a1::arg)=(case (lookup i countmapp)
            of SOME _=>let
                val _ =testp["\nInserting into dictionary",iTos i,"=>",iTos j]
                val mapp2=insert(i,j) mapp
                in
                    m(ix,j+1,mapp2,p@[p1],params, a@[a1],arg)
                end
            | _ =>  m(ix,j,mapp,p, params, a, arg)
            (*end case*))
        | m (_,_,_,_,_,_,[]) = raise Fail (String.concat["incorrect number of params:more params:",Int.toString(n),
            "args:",Int.toString(length(args))])
        | m (_,_,_,_,[],_,_) = raise Fail ("incorrect number of params:more args")
        (*| m _ = raise Fail ("incorrect number of params:unsure")*)
        val _ =testp["ix created up to",iTos(n),"length of params",
                iTos(length params),"lengths of args",iTos(length args)]
        val  (mapp,Nparams,Nargs)=m(ix, 0, empty, [],params,[],args)
        in
            (mapp,Nparams,Nargs)
        end 

    (*getIdCount: ein_exp ->dict
    *rewrite ids in exp using mapp
    *)
    fun getIdCount b =let
        fun rewriteExp(b,mapp)=let
            fun iterList([],mapp)=mapp
            | iterList(e1::es,mapp)=let
                val mapp1=rewriteExp(e1,mapp)
                in iterList(es,mapp1) end
            in (case b
                of E.B _                 => mapp
                | E.Tensor(id,_)       => insert(id,1) mapp
                | E.G _                => mapp
                | E.Field _            => mapp
                | E.Lift _             => mapp
                | E.Conv(v,_,h,_)      => insert(h,1) (insert(v,1) mapp)
                | E.Partial _          => mapp
                | E.Apply _            => mapp
                | E.Probe(e1,e2)       => iterList ([e1,e2],mapp)
                | E.Value _            => err "Should not be here"
                | E.Img _              => err  "should not be here"
                | E.Krn _              => err "Should not be here"
                | E.Sum(_ ,e1)         => rewriteExp (e1,mapp)
                | E.Op1(_,e1)          => rewriteExp (e1,mapp)
                | E.Op2(_,e1,e2)       => iterList ([e1,e2],mapp)
                | E.Opn(_, es)         => iterList (es,mapp)
                (*end case*))
            end
    in
        rewriteExp(b,empty)
    end

    (*rewriteParam:dict*ein_exp ->ein_exp
    *rewrite ids in exp using mapp
    *)
    fun rewriteParam(mapp,e)=let
        fun getId id  = lkupIndexSingle(id,mapp,"Mapp doesn't have Param Id ")
        fun rewriteExp b=(case b
            of  E.B _              => b
            | E.Tensor(id,alpha)   => E.Tensor(getId id, alpha)
            | E.G _ => b
            | E.Field _            => raise Fail "should have been swept"
            | E.Lift _             => raise Fail "should have been swept"
            | E.Conv _             => raise Fail "should have been swept"
            | E.Partial _          => raise Fail "should have been swept"
            | E.Apply _            => raise Fail "should have been swept"
            | E.Probe(E.Conv(v,alpha,h,dx),t)
                                  => E.Probe(E.Conv(getId v, alpha,getId h,dx),rewriteExp t)
            | E.Probe _            => raise Fail "should have been swept"
            | E.Value e1           => raise Fail"Should not be here"
            | E.Img _              => raise Fail "should not be here"
            | E.Krn _              => raise Fail"Should not be here"
            | E.Sum(sx ,e1)        => E.Sum(sx,rewriteExp e1)
            | E.Op1(op1, e1)       => E.Op1(op1,rewriteExp e1)
            | E.Op2(op2, e1,e2)    => E.Op2(op2,rewriteExp e1,rewriteExp e2)
            | E.Opn(opn, es)       => E.Opn(opn,List.map rewriteExp es)
            (*end case*))
        in
            rewriteExp e 
        end

    (* cleanParams:var*ein_exp*param*index* var list ->code
    *cleans params
    *)
    fun clean (y, body, params, index, args) = let
        val countmapp=getIdCount body
        val (mapp,Nparams,Nargs)=mkMapp(countmapp,params,args)
        val Nargs =List.map (fn e=> use e) Nargs
        val Nbody=rewriteParam(mapp,body)
        val einapp= assignEinApp(y,Nparams,index,Nbody,Nargs)
        in
            einapp
        end

  end (* CleanParam *)

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