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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/mid-il/mid-contract.sml
ViewVC logotype

View of /branches/charisee/src/compiler/mid-il/mid-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2400 - (download) (annotate)
Sun Jul 7 13:29:54 2013 UTC (6 years, 2 months ago) by cchiw
File size: 4024 byte(s)
 
(* mid-contract.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Contraction phase for MidIL.
 *)

structure MidContract : sig

    val transform : MidIL.program -> MidIL.program

  end = struct

    structure IL = MidIL
    structure Op = MidOps
    structure Ty = MidILTypes
    structure V = IL.Var
    structure ST = Stats

  (********** Counters for statistics **********)
    val cntAddNeg               = ST.newCounter "mid-opt:add-neg"
    val cntSubNeg               = ST.newCounter "mid-opt:sub-neg"
    val cntSubSame              = ST.newCounter "mid-opt:sub-same"
    val cntNegNeg               = ST.newCounter "mid-opt:neg-neg"
    val cntEigenVals            = ST.newCounter "mid-opt:eigen-vals"
    val cntUnused               = ST.newCounter "mid-opt:unused"
    val firstCounter            = cntAddNeg
    val lastCounter             = cntUnused

    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 getRHS x = (case V.binding x
           of IL.VB_RHS(IL.OP arg) => SOME arg
            | IL.VB_RHS(IL.VAR x') => getRHS x'
            | IL.VB_RHS(IL.EINAPP arg) => SOME arg
            | _ => NONE
          (* end case *))

(*replaces operators to Ioperator *)

    fun doAssign (lhs, IL.OP rhs) = (case rhs
           of (Op.IAdd ty, [a, b]) => (case getRHS b
                 of SOME(Op.INeg _, [c]) => (
                    (* rewrite to "a-c" *)
                      ST.tick cntAddNeg;
                      decUse b;
                      SOME[(lhs, IL.OP(Op.ISub ty, [a, use c]))])
                  | _ => NONE
                 (* end case *))
            | (Op.ISub ty, [a, b]) => if IL.Var.same(a, b)
                then ( (* rewrite to 0 *)
                  ST.tick cntSubSame;
                  decUse a; decUse b;
                  case ty
                   of Ty.IntTy => SOME[(lhs, IL.LIT(Literal.Int 0))]
                    | Ty.TensorTy[] => SOME[(lhs, IL.LIT(Literal.Float(FloatLit.zero false)))]
                    | _ => (incUse a; incUse b; NONE) (* FIXME: should handle arbitrary tensors *)
                  (* end case *))
                else (case getRHS b
                   of SOME(Op.INeg _, [c]) => (
                      (* rewrite to "a+c" *)
                        ST.tick cntSubNeg;
                        decUse b;
                        SOME[(lhs, IL.OP(Op.IAdd ty, [a, use c]))])
                    | _ => NONE
                   (* end case *))
            | (Op.INeg ty, [a]) => (case getRHS a
                 of SOME(Op.INeg _, [b]) => (
                    (* rewrite to "b" *)
                      ST.tick cntNegNeg;
                      decUse a;
                      SOME[(lhs, IL.VAR(use b))])
                  | _ => NONE
                 (* end case *))
            | _ => NONE
          (* end case *))
      | doAssign _ = NONE

    fun doMAssign (ys, rator, xs) = (case (ys, rator)
           of ([vals, vecs], Op.EigenVecs2x2) =>
                if (useCount vecs = 0)
                  then (
                    ST.tick cntEigenVals;
                    SOME[(vals, IL.OP(Op.EigenVals2x2, xs))])
                  else NONE
            | ([vals, vecs], Op.EigenVecs3x3) =>
                if (useCount vecs = 0)
                  then (
                    ST.tick cntEigenVals;
                    SOME[(vals, IL.OP(Op.EigenVals3x3, xs))])
                  else NONE
            | _ => NONE
          (* end case *))

    structure Rewrite = RewriteFn (
      struct
        structure IL = IL
        val doAssign = doAssign
        val doMAssign = doMAssign
        val elimUnusedVars = UnusedElim.reduce
      end)

    val transform = Rewrite.transform

  end

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