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

SCM Repository

[diderot] View of /trunk/src/compiler/translate/translate-basis.sml
ViewVC logotype

View of /trunk/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 426 - (download) (annotate)
Mon Oct 18 18:49:55 2010 UTC (11 years, 8 months ago) by jhr
File size: 4490 byte(s)
  Filter out dimensions of size one in tensor types
(* translate-basis.sml
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 * Translation for basis operations in Simple AST to HighIL code

structure TranslateBasis : sig

  (* translate(lhs, f, mvs, args) translates the application of f (specialized
   * to the instantiated meta variables mvs) to a list of SSA assignments in
   * reverse order.
    val translate : (HighIL.var * Var.var * Types.meta_var list * HighIL.var list)
	  -> HighIL.assign list

  end = struct

    structure BV = BasisVars
    structure IL = HighIL
    structure DstTy = HighILTypes
    structure Op = HighOps
    structure Ty = Types
    structure TU = TypeUtil
    structure MV = MetaVar
    structure VTbl = Var.Tbl

    fun pruneDim d = (case TU.pruneDim d
	   of (Ty.DimConst n) => n
	    | d => raise Fail("unresolved dimension " ^ TU.dimToString d)
	  (* end case *))

    fun pruneShape sv = (case TU.pruneShape(MV.toShape sv)
	   of Ty.Shape dd => DstTy.tensorTy(List.map pruneDim dd)
	    | shp => raise Fail("unresolved shape " ^ TU.shapeToString shp)
	  (* end case *))

    fun dimVarToTensor dv = DstTy.tensorTy[pruneDim(MV.toDim dv)]
    fun shapeVarToTensor sv = pruneShape sv

    fun assign (y, rator, xs) = [(y, IL.OP(rator, xs))]

    fun simpleOp rator (y, [], xs) = assign (y, rator, xs)

    fun tensorOp rator (y, [sv], xs) = assign (y, rator(shapeVarToTensor sv), xs)

    fun vectorOp rator (y, [dv], xs) = assign (y, rator(dimVarToTensor dv), xs)

    fun kernel h (y, [], []) = assign(y, Op.Kernel h, [])

  (* build a table that maps Basis variables to their translation functions *)
    val tbl : ((IL.var * Ty.meta_var list * IL.var list) -> IL.assign list) VTbl.hash_table = let
	  val tbl = VTbl.mkTable (128, Fail "Translate table")
	    List.app (VTbl.insert tbl) [
		(BV.add_ii,		simpleOp(Op.Add DstTy.IntTy)),
		(BV.add_tt,		tensorOp Op.Add),
		(BV.sub_ii,		simpleOp(Op.Sub DstTy.IntTy)),
		(BV.sub_tt,		tensorOp Op.Sub),
		(BV.mul_ii,		simpleOp(Op.Mul DstTy.IntTy)),
		(BV.mul_rr,		simpleOp(Op.Mul(DstTy.realTy))),
		(BV.mul_rt,		tensorOp Op.Scale),
		(BV.mul_tr,		fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])),
		(BV.div_ii,		simpleOp(Op.Div DstTy.IntTy)),
		(BV.div_rr,		simpleOp(Op.Div(DstTy.realTy))),
		(BV.div_tr,		tensorOp Op.InvScale),
		(BV.lt_ii,		simpleOp(Op.LT DstTy.IntTy)),
		(BV.lt_rr,		simpleOp(Op.LT(DstTy.realTy))),
		(BV.lte_ii,		simpleOp(Op.LTE DstTy.IntTy)),
		(BV.lte_rr,		simpleOp(Op.LTE(DstTy.realTy))),
		(BV.gte_ii,		simpleOp(Op.GTE DstTy.IntTy)),
		(BV.gte_rr,		simpleOp(Op.GTE(DstTy.realTy))),
		(BV.gt_ii,		simpleOp(Op.GT DstTy.IntTy)),
		(BV.gt_rr,		simpleOp(Op.GT(DstTy.realTy))),
		(BV.equ_bb,		simpleOp(Op.EQ DstTy.BoolTy)),
		(BV.equ_ii,		simpleOp(Op.EQ DstTy.IntTy)),
		(BV.equ_ss,		simpleOp(Op.EQ DstTy.StringTy)),
		(BV.equ_rr,		simpleOp(Op.EQ(DstTy.realTy))),
		(BV.neq_bb,		simpleOp(Op.NEQ DstTy.BoolTy)),
		(BV.neq_ii,		simpleOp(Op.NEQ DstTy.IntTy)),
		(BV.neq_ss,		simpleOp(Op.NEQ DstTy.StringTy)),
		(BV.neq_rr,		simpleOp(Op.NEQ(DstTy.realTy))),
		(BV.neg_i,		simpleOp(Op.Neg DstTy.IntTy)),
		(BV.neg_t,		tensorOp Op.Neg),
		(BV.neg_f,		fn (y, _, xs) => assign(y, Op.NegField, xs)),
		(BV.op_at,		fn (y, [_, dv, sv], xs) =>
					  assign(y, Op.Probe(shapeVarToTensor sv, dimVarToTensor dv), xs)),
		(BV.op_convolve,	fn (y, _, xs) => assign(y, Op.Convolve, xs)),
		(BV.op_D,		fn (y, _, xs) => assign(y, Op.DiffField, xs)),
		(BV.op_norm,		tensorOp Op.Norm),
		(BV.op_not,		simpleOp Op.Not),
		(BV.fn_CL,		fn (y, _, xs) => assign(y, Op.CL, xs)),
		(BV.fn_convolve,	fn (y, _, [h, img]) => assign(y, Op.Convolve, [img, h])),
		(BV.fn_cos,		simpleOp Op.Cos),
		(BV.fn_dot,		vectorOp Op.Dot),
		(BV.fn_inside,		fn (y, [_, dv, _], xs) =>
					  assign(y, Op.Inside(dimVarToTensor dv), xs)),
		(BV.fn_max,		simpleOp Op.Max),
		(BV.fn_min,		simpleOp Op.Min),
		(BV.fn_modulate,	vectorOp Op.Mul),
		(BV.fn_pow,		simpleOp Op.Pow),
		(BV.fn_principleEvec,	vectorOp Op.PrincipleEvec),
		(BV.fn_sin,		simpleOp Op.Sin),
		(BV.kn_bspln3,		kernel Kernel.bspln3),
		(BV.kn_bspln5,		kernel Kernel.bspln5),
		(BV.kn_ctmr,		kernel Kernel.ctmr),
		(BV.kn_tent,		kernel Kernel.tent),
		(BV.i2r,		simpleOp Op.IntToReal)

    fun translate (y, f, mvs, xs) = (case VTbl.find tbl f
	   of SOME transFn => transFn(y, mvs, xs)
	    | NONE => raise Fail("TranslateBasis.translate: unknown basis function " ^ Var.uniqueNameOf f)
	  (* end case *))


ViewVC Help
Powered by ViewVC 1.0.0