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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/tree-opt/tree-contract.sml
ViewVC logotype

View of /branches/vis15/src/compiler/tree-opt/tree-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3889 - (download) (annotate)
Sat May 21 14:32:02 2016 UTC (4 years, 2 months ago) by jhr
File size: 2662 byte(s)
working on merge
(* tree-contract.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.
 *)

structure TreeContract : sig

    val transform : TreeIR.program -> TreeIR.program

  end = struct

    structure IR = TreeIR
    structure V = TreeVar
    structure Ty = TreeTypes
    structure Op = TreeOps
    structure ST = Stats

  (********** Counters for statistics **********)
    val cntIndexLoad		= ST.newCounter "tree-contract:index-vload"
    val cntIndexCons		= ST.newCounter "tree-contract:index-cons"

(* rewrites:
	VIndex<_, _, i>(VLoad(_, e, j)) ==> TensorIndex
	Pack(Vec _, ..., Vec _) => CONS
	VLoad(_, CONS([..., xs, ...], _), _) => Vec(xs)
*)

  (* are there available rewrites for an expression? *)
    fun canReduce exp = (case exp
	   of IR.E_Global _ => false
	    | IR.E_State _ => false
	    | IR.E_Var _ => false
	    | IR.E_Lit _ => false
	    | IR.E_Op(Op.VIndex _, [IR.E_VLoad _]) => true
	    | IR.E_Op(_, es) => List.exists canReduce es
	    | IR.E_Vec(_, _, es) => List.exists canReduce es
	    | IR.E_Cons(es, _) => List.exists canReduce es
	    | IR.E_Seq(es, _) => List.exists canReduce es
	    | IR.E_Pack(layout, es) => let
		fun chk ([], allVecs) = allVecs
		  | chk ((e as IR.E_Vec _)::es, true) = canReduce e orelse chk (es, true)
		  | chk (e::es, _) = canReduce e orelse chk (es, false)
		in
		  chk (es, true)
		end
	    | IR.E_VLoad(_, e, _) => canReduce e
	  (* end case *))

    fun reduce exp = (case exp
	   of IR.E_Global _ => exp
	    | IR.E_State _ => exp
	    | IR.E_Var _ => exp
	    | IR.E_Lit _ => exp
	    | IR.E_Op(Op.VIndex(_, _, i), [IR.E_VLoad(layout, e, j)]) => let
		fun index (0, _, idx) = idx
		  | index (j, p::ps, idx) = index (j-1, ps, idx+p)
		val idx = index (j, #pieces layout, i)
		in
		  ST.tick cntIndexLoad;
		  case e
		   of IR.E_Cons(es, _) => (ST.tick cntIndexCons; reduce (List.nth(es, idx)))
		    | _ => IR.E_Op(Op.TensorIndex(Ty.TensorTy[#wid layout], [idx]), [reduce e])
		  (* end case *)
		end
	    | IR.E_Op(rator, es) => IR.E_Op(rator, List.map reduce es)
	    | IR.E_Vec(w, pw, es) => IR.E_Vec(w, pw, List.map reduce es)
	    | IR.E_Cons(es, ty) => IR.E_Cons(List.map reduce es, ty)
	    | IR.E_Seq(es, ty) => IR.E_Seq(List.map reduce es, ty)
(* TODO: check for all E_Vec args *)
	    | IR.E_Pack(layout, es) => IR.E_Pack(layout, List.map reduce es)
(* TODO: check for CONS arg *)
	    | IR.E_VLoad(layout, e, i) => IR.E_VLoad(layout, reduce e, i)
	  (* end case *))

(* TODO: unused variable elimination; tensor/vector conversion elimination *)
    fun transform prog = prog

  end

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