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

SCM Repository

[diderot] View of /branches/ein16/src/compiler/high-il/normalize.sml
ViewVC logotype

View of /branches/ein16/src/compiler/high-il/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4196 - (download) (annotate)
Wed Jul 13 03:36:42 2016 UTC (3 years, 8 months ago) by cchiw
File size: 6420 byte(s)
added to synthetic testing
(* normalize.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure Normalize : sig

    val transform : HighIL.program -> HighIL.program

  end = struct

    structure IL = HighIL
    structure Op = HighOps
    structure V = IL.Var
    structure Ty = HighILTypes
    structure ST = Stats
    structure NE = NormalizeEin
    structure P = Printer
    structure Order = OrderEin
    structure App = App
    structure S = SummationEin

  (********** Counters for statistics **********)
    val cntInsideScale          = ST.newCounter "high-opt:inside-scale"
    val cntInsideOffset         = ST.newCounter "high-opt:inside-offset"
    val cntInsideNeg            = ST.newCounter "high-opt:inside-meg"
    val cntInsideCurl           = ST.newCounter "high-opt:inside-curl"
    val cntInsideDiff           = ST.newCounter "high-opt:inside-diff"
    val cntProbeAdd             = ST.newCounter "high-opt:probe-add"
    val cntProbeSub             = ST.newCounter "high-opt:probe-sub"
    val cntProbeScale           = ST.newCounter "high-opt:probe-scale"
    val cntProbeOffset          = ST.newCounter "high-opt:probe-offset"
    val cntProbeNeg             = ST.newCounter "high-opt:probe-neg"
    val cntProbeCurl            = ST.newCounter "high-opt:probe-curl"
    val cntDiffField            = ST.newCounter "high-opt:diff-field"
    val cntDiffAdd              = ST.newCounter "high-opt:diff-add"
    val cntDiffScale            = ST.newCounter "high-opt:diff-scale"
    val cntDiffOffset           = ST.newCounter "high-opt:diff-offset"
    val cntDiffNeg              = ST.newCounter "high-opt:diff-neg"
    val cntCurlScale            = ST.newCounter "high-opt:curl-scale"
    val cntCurlNeg              = ST.newCounter "high-opt:curl-neg"
    val cntUnused               = ST.newCounter "high-opt:unused"
    val firstCounter            = cntInsideScale
    val lastCounter             = cntUnused

    val testslim=0
    val testing=0
    fun testb e=(case testslim
        of 1 => ((String.concat e);1)
        | _ => 1
        (*end case*))
    fun testp e=(case testing
        of 1 => ((String.concat e);1)
        | _ => 1
    (*end case*))

    structure UnusedElim = UnusedElimFn (
        structure IL = IL
        val cntUnused = cntUnused)

    fun useCount (IL.V{useCnt, ...}) = !useCnt

  (* adjust a variable's use count *)
    fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
    fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
    fun use x = (incUse x; x)

    fun isEin x = (case V.binding x
        of IL.VB_RHS(rhs as IL.EINAPP(e, arg)) => (SOME rhs)
	    | IL.VB_RHS(IL.VAR x') => isEin x'
	    | _ => NONE
	  (* end case *))



    (*doNormalize:EIN->EIN
    * Orders EIN, normalizes it, then cleans the summation
    *)
    fun doNormalize(e,args)=let
         (* val _ = print(String.concat["\n\n**do normalize:", P.printerE(e)])*)
          val ordered = Order.orderfn e

          val (n, change) = NE.normalize(ordered,args)

          val e' = S.cleanSummation n

          in
            e'
          end


    fun nameCnt e= String.concat[V.toString e, "(",Int.toString(useCount e),")"]
    fun nameCnts e=String.concat (List.map nameCnt e)

    (*foundEIN:EIN.Params*int*int*EIN*HighIL Vars* rhs list* EIN  *HighIL Var
        ->int*EIN*int*rhs list
    * Orders EIN, normalizes it, then cleans the summation
    *orig-original EIN 
    *)
    fun foundEin(paramCount,place,change,newE,newArgs,done,newEinApp,orig,lhs)= (case (List.nth(paramCount, place))
        of Ein.TEN(0,_)=> (change,orig, place+1,  done@[newEinApp])
        | _ => let
            val rtnArgs=done@newArgs

            val (c,subst)=App.app(orig,place,newE,newArgs,done)
            in (case c
            of 0 => ("(\n c is zero)";(1,subst, place+length(newArgs), rtnArgs))
              |_ => ("c nonzero" ;  use lhs;List.map use newArgs ;decUse newEinApp;
                    (1,subst, place+length(newArgs), rtnArgs))
                (*end case*))
            end
        (*end case*))

  (* doRHS: HighIL.var * rhs -> (var * rhs) list option
   * Looks at each argument to the original EINAPP.
   * If it is another EIN APP calls foundEIN to do application
   * "place"-The Param-id for the EIN operator.
   * Keeps track of the place of the argument in substitution.
   *)
    fun doRHS (lhs, IL.EINAPP (ein, args))=let
           (* val _ = print(String.concat["\n\n**dorhs:", P.printerE(ein)]) *)
          fun rewrite(0,_,_, [], _)= NONE
            | rewrite(_,orig,_, [], args')= SOME[(lhs,IL.EINAPP(doNormalize(orig,args'), args'))]
            | rewrite(change,orig, place, e::es,args')=(("\ncnt:";Int.toString(length(e::es)));(case (isEin e)
                 of NONE => rewrite(change,orig, place+1, es, args'@[e])
                  | SOME(IL.EINAPP (newE,newA)) => let
                    (* val _ = print(String.concat["\n\n**found newE:", P.printerE(newE)])*)
                      val Ein.EIN{params, index, body}=orig
                      val (change',e',place',done')=foundEin(params,place,change,newE,newA,args',e,orig,lhs)
                      in rewrite(change',e',place',es,done')
                      end
                  | _ => raise Fail"isEin did not work"
                  (* end case *)))


       val rtn=rewrite(0,ein, 0,args,[])
        val _ ="\nafter dorhs"
       in rtn   end
      | doRHS _ = NONE
(*
    fun doRHS' arg = (case doRHS arg
        of NONE=> let
            val (lhs,rhs) = arg
            in (case (V.ty lhs, rhs)
                of (Ty.FieldTy,  _)=> NONE
                | (Ty.TensorTy _ ,IL.EINAPP _) =>
                    let
                    val l=[arg]
                    val x=  List.foldr (fn (stm, stms) => handleEinHigh.expandEinOp stm @ stms) [] l

                    in (case x
                        of [_]=> NONE
                        |  _ => SOME x
                        (*end case*))
                    end
                | _ => NONE
                (*end case*))
            end
        | SOME l =>SOME l
        (* end case *))

*)


    structure Rewrite = RewriteFn (
      struct
        structure IL = IL
        val doAssign = doRHS

        fun doMAssign _ = NONE
        val elimUnusedVars = UnusedElim.reduce
      end)

    val transform = Rewrite.transform

  end

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