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

SCM Repository

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

View of /branches/vis12/src/compiler/translate/translate-basis.sml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2998 - (download) (annotate)
Sat Mar 7 15:46:15 2015 UTC (5 years, 6 months ago) by jhr
File size: 17339 byte(s)
(* translate-basis.sml
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.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 * SimpleTypes.meta_arg list * HighIL.var list)
          -> HighIL.assignment list

  end = struct

    structure BV = BasisVars
    structure IL = HighIL
    structure DstTy = HighILTypes
    structure Op = HighOps
    structure Ty = SimpleTypes
    structure VTbl = Var.Tbl

    fun trType (Ty.TY ty) = TranslateTy.tr ty
      | trType _ = raise Fail "expected type"
    fun dimVarToInt (Ty.DIM d) = d
      | dimVarToInt _ = raise Fail "expected dim"
    fun dimVarToTensor dv = DstTy.tensorTy[dimVarToInt dv]
    fun dimVarToMatrix dv = let
          val d = dimVarToInt dv
            DstTy.tensorTy[d, d]        (* square matrix type *)
    fun shapeVarToTensor (Ty.SHAPE shp) = DstTy.tensorTy shp
      | shapeVarToTensor _ = raise Fail "expected shape"

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

    fun basisFn name (y, [], xs) = [IL.ASSGN(y, IL.APPLY(name, 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, 0), [])

  (* utility functions for synthesizing eigenvector/eigenvalue code *)
    fun eigenVec (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.realTy, dim)
            fn (y, _, [m]) => let
                val v = IL.Var.new("evals", ty)
                  [IL.MASSGN([v, y], rator, [m])]
    fun eigenVal (rator, dim) = let
          val ty = DstTy.SeqTy(DstTy.vecTy dim, dim)
            fn (y, _, [m]) => let
                val v = IL.Var.new("evecs", ty)
                  [IL.MASSGN([y, v], rator, [m])]

  (* build a table that maps Basis variables to their translation functions *)
    val tbl : ((IL.var * Ty.meta_arg list * IL.var list) -> IL.assignment list) VTbl.hash_table = let
          val tbl = VTbl.mkTable (128, Fail "Translate table")
          val insert = VTbl.insert tbl
            List.app insert [
                (BV.at_Td,              fn (y, [tv], args) => assign(y, Op.Prepend(trType tv), args)),
                (BV.at_dT,              fn (y, [tv], args) => assign(y, Op.Append(trType tv), args)),
                (BV.at_dd,              fn (y, [tv], args) => assign(y, Op.Concat(trType tv), args)),
                (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.add_ii,             simpleOp(Op.Add DstTy.IntTy)),
                (BV.add_tt,             tensorOp Op.Add),
                (BV.add_ff,             fn (y, _, [f, g]) => assign(y, Op.AddField, [f, g])),
                (BV.add_fr,             fn (y, _, [f, s]) => assign(y, Op.OffsetField, [f, s])),
                (BV.add_rf,             fn (y, _, [s, f]) => assign(y, Op.OffsetField, [f, s])),
                (BV.sub_ii,             simpleOp(Op.Sub DstTy.IntTy)),
                (BV.sub_tt,             tensorOp Op.Sub),
                (BV.sub_ff,             fn (y, _, [f, g]) => assign(y, Op.SubField, [f, g])),
                (BV.sub_fr,             fn (y, _, [f, s]) => let
                                          val s' = IL.Var.copy s
                                          in [
                                            IL.ASSGN(s', IL.OP(Op.Neg DstTy.realTy, [s])),
                                            IL.ASSGN(y, IL.OP(Op.OffsetField, [f, s']))
                                          ] end),
                (BV.sub_rf,             fn (y, _, [s, f]) => let
                                          val f' = IL.Var.copy f
                                          in [
                                            IL.ASSGN(f', IL.OP(Op.NegField, [f])),
                                            IL.ASSGN(y, IL.OP(Op.OffsetField, [f', s]))
                                          ] end),
                (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.mul_rf,             fn (y, _, [s, f]) => assign(y, Op.ScaleField, [s, f])),
                (BV.mul_fr,             fn (y, _, [f, s]) => assign(y, Op.ScaleField, [s, f])),
                (BV.div_ii,             simpleOp(Op.Div DstTy.IntTy)),
                (BV.div_rr,             simpleOp(Op.Div DstTy.realTy)),
                (BV.div_tr,             fn (y, [sv], [x, s]) => let
                                          val one = IL.Var.new("one", DstTy.realTy)
                                          val s' = IL.Var.new("sInv", DstTy.realTy)
                                          in [
                                            IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
                                            IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
                                            IL.ASSGN(y, IL.OP(Op.Scale(shapeVarToTensor sv), [s', x]))
                                          ] end),
                (BV.div_fr,             fn (y, _, [f, s]) => let
                                          val one = IL.Var.new("one", DstTy.realTy)
                                          val s' = IL.Var.new("sInv", DstTy.realTy)
                                          in [
                                            IL.ASSGN(one, IL.LIT(Literal.Float(FloatLit.one))),
                                            IL.ASSGN(s', IL.OP(Op.Div DstTy.realTy, [one, s])),
                                            IL.ASSGN(y, IL.OP(Op.ScaleField, [s', f]))
                                          ] end),
                (BV.exp_ri,             simpleOp(Op.Power)),
                (BV.exp_rr,             basisFn MathFuns.pow),
                (BV.curl2D,             fn (y, _, xs) => assign(y, Op.CurlField 2, xs)),
                (BV.curl3D,             fn (y, _, xs) => assign(y, Op.CurlField 3, xs)),
                (BV.convolve_vk,        fn (y, [_, Ty.DIM d, Ty.SHAPE dd], xs) =>
                                          assign(y, Op.Field(DstTy.TensorTy[d], DstTy.TensorTy dd), xs)),
                (BV.convolve_kv,        fn (y, [_, Ty.DIM d, Ty.SHAPE dd], [k, v]) =>
                                          assign(y, Op.Field(DstTy.TensorTy[d], DstTy.TensorTy dd), [v, k])),
                (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_probe,           fn (y, [_, dv, sv], xs) =>
                                          assign(y, Op.Probe(dimVarToTensor dv, shapeVarToTensor sv), xs)),
                (BV.op_D,               fn (y, _, xs) => assign(y, Op.DiffField, xs)),
                (BV.op_Dotimes,         fn (y, _, xs) => assign(y, Op.DiffField, xs)),
                (BV.op_norm,            fn (y, [sv], xs) => (case shapeVarToTensor sv
                                           of DstTy.TensorTy[] => assign(y, Op.Abs DstTy.realTy, xs)
                                            | ty => assign(y, Op.Norm ty, xs)
                                          (* end case *))),
                (BV.op_not,             simpleOp Op.Not),
                (BV.op_cross,           simpleOp Op.Cross),
                (BV.op_outer,           fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
                                          assign (y, Op.Outer(DstTy.tensorTy[d1, d2]), xs)),
                (BV.op_inner,           fn (y, [Ty.SHAPE dd1, Ty.SHAPE dd2, _], xs) => let
                                          val ty1 = DstTy.TensorTy dd1
                                          val ty2 = DstTy.TensorTy dd2
                                          val rator = (case (dd1, dd2)
                                                 of ([d], [d']) => Op.Dot ty1
                                                  | ([d1], [d1', d2]) => Op.MulVecMat ty2
                                                  | ([d1, d2], [d2']) => Op.MulMatVec ty1
                                                  | ([d1, d2], [d2', d3]) => Op.MulMatMat(ty1, ty2)
                                                  | ([d1], [d1', d2, d3]) => Op.MulVecTen3 ty2
                                                  | ([d1, d2, d3], [d3']) => Op.MulTen3Vec ty1
                                                  | _ => raise Fail(concat[
                                                        "unsupported inner-product: ",
                                                        DstTy.toString ty1, " * ", DstTy.toString ty2
                                                (* end case *))
                                            assign (y, rator, xs)
                (BV.op_colon,           fn (y, [sh1, sh2, _], xs) => let
                                          val ty1 = shapeVarToTensor sh1
                                          val ty2 = shapeVarToTensor sh2
                                            assign (y, Op.ColonMul(ty1, ty2), xs)
                (BV.fn_inside,          fn (y, [_, Ty.DIM d, _], xs) =>
                                          assign(y, Op.Inside d, xs)),
                (BV.clamp_rrr,          simpleOp (Op.Clamp DstTy.realTy)),
                (BV.clamp_vvv,          vectorOp Op.Clamp),
                (BV.lerp3,              tensorOp Op.Lerp),
                (BV.lerp5,              fn (y, [sv], [a, b, x0, x, x1]) => let
                                          val t1 = IL.Var.new("t1", DstTy.realTy)
                                          val t2 = IL.Var.new("t2", DstTy.realTy)
                                          val t3 = IL.Var.new("t3", DstTy.realTy)
                                          in [
                                            IL.ASSGN(t1, IL.OP(Op.Sub DstTy.realTy, [x, x0])),
                                            IL.ASSGN(t2, IL.OP(Op.Sub DstTy.realTy, [x1, x0])),
                                            IL.ASSGN(t3, IL.OP(Op.Div DstTy.realTy, [t1, t2])),
                                            IL.ASSGN(y,  IL.OP(Op.Lerp(shapeVarToTensor sv), [a, b, t3]))
                                          ] end),
                (BV.evals2x2,           eigenVal (Op.Eigen2x2, 2)),
                (BV.evals3x3,           eigenVal (Op.Eigen3x3, 3)),
                (BV.evecs2x2,           eigenVec (Op.Eigen2x2, 2)),
                (BV.evecs3x3,           eigenVec (Op.Eigen3x3, 3)),
                (BV.fn_length,          fn (y, [tv], [s]) => assign(y, Op.Length(trType tv), [s])),
                (BV.fn_max,             simpleOp Op.Max),
                (BV.fn_min,             simpleOp Op.Min),
                (BV.fn_modulate,        vectorOp Op.Mul),
                (BV.fn_normalize,       vectorOp Op.Normalize),
                (BV.fn_principleEvec,   vectorOp Op.PrincipleEvec),
                (BV.fn_trace,           fn (y, [dv], xs) => assign(y, Op.Trace(dimVarToMatrix dv), xs)),
                (BV.fn_transpose,       fn (y, [Ty.DIM d1, Ty.DIM d2], xs) =>
                                          assign(y, Op.Transpose(d1, d2), xs)),
                (BV.kn_bspln3,          kernel Kernel.bspln3),
                (BV.kn_bspln5,          kernel Kernel.bspln5),
                (BV.kn_ctmr,            kernel Kernel.ctmr),
                (BV.kn_c2ctmr,          kernel Kernel.ctmr),
                (BV.kn_c4hexic,         kernel Kernel.c4hexic),
                (BV.kn_tent,            kernel Kernel.tent),
                (BV.kn_c1tent,          kernel Kernel.tent),
                (BV.i2r,                simpleOp Op.IntToReal),
                (BV.identity,           fn (y, [Ty.DIM d], []) =>
                                          assign(y, Op.Identity d, [])),
                (BV.zero,               fn (y, [sv], []) =>
                                          assign(y, Op.Zero(shapeVarToTensor sv), [])),
                (BV.nan,                fn (y, [sv], []) => raise Fail "FIXME"),
                (BV.subscript,          fn (y, [tv, Ty.DIM d], args) =>
                                            Op.SeqSub(DstTy.SeqTy(trType tv, d)),
                (BV.dynSubscript,       fn (y, [tv], args) =>
                                          assign(y, Op.SeqSub(DstTy.DynSeqTy(trType tv)), args)),
              (* image operations *)
                (BV.fn_size,            fn (y, [Ty.DIM d, _], [img]) => let
                                          val DstTy.ImageTy info = IL.Var.ty img
                                        (* we extract each dimension separately and then build the sequence value *)
                                          val dims = List.tabulate(d, fn i => IL.Var.new("i"^Int.toString i, DstTy.IntTy))
                                          fun mkStms ([], _, stms) = stms (* in reverse order! *)
                                            | mkStms (d::dr, i, stms) = mkStms (dr, i+1,
                                                IL.ASSGN(d, IL.OP(Op.ImageDim(info, i), [img])) :: stms)
                                            List.revAppend (mkStms (dims, 0, []), [
                                                IL.ASSGN(y, IL.CONS(DstTy.SeqTy(DstTy.intTy, d), dims))
                (BV.image_border,       fn (y, _, args as [img, _]) => let
                                          val DstTy.ImageTy info = IL.Var.ty img
                                            assign(y, Op.BorderCtlDefault info, args)
                (BV.image_clamp,        fn (y, _, args as [img]) => let
                                          val DstTy.ImageTy info = IL.Var.ty img
                                            assign(y, Op.BorderCtlClamp info, args)
                (BV.image_mirror,       fn (y, _, args as [img]) => let
                                          val DstTy.ImageTy info = IL.Var.ty img
                                            assign(y, Op.BorderCtlMirror info, args)
                (BV.image_wrap,         fn (y, _, args as [img]) => let
                                          val DstTy.ImageTy info = IL.Var.ty img
                                            assign(y, Op.BorderCtlWrap info, args)
          (* add C math functions *)
            List.app (fn (n, x) => insert(x, basisFn n)) BV.mathFns;

    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 *))
handle ex => (print(concat["translate (", IL.Var.toString y, ", ",
Var.uniqueNameOf f, ", ...)\n"]); raise ex)


ViewVC Help
Powered by ViewVC 1.0.0