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

SCM Repository

[diderot] View of /trunk/src/compiler/high-il/check-high-il.sml
ViewVC logotype

View of /trunk/src/compiler/high-il/check-high-il.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2636 - (download) (annotate)
Tue May 27 16:18:36 2014 UTC (5 years, 1 month ago) by jhr
File size: 6150 byte(s)
  Merging changes from vis12 branch (via staging).  The main change is the new
  syntax for inputs (especially image inputs).
(* check-high-il.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure CheckOps : OPERATOR_TY = struct

    structure Op = HighOps
    structure Ty = HighILTypes

    type rator = Op.rator
    type ty = Ty.ty

  (* utility function for synthesizing eigenvector/eigenvalue signature *)
    fun eigenSig dim = let
          val tplTy = Ty.TupleTy[
                  Ty.SeqTy(Ty.realTy, dim),
                  Ty.SeqTy(Ty.vecTy dim, dim)
                ]
          in
            (tplTy, [Ty.TensorTy[dim, dim]])
          end

  (* Return the signature of a HighIL operator. *)
    fun sigOf rator = (case rator
           of Op.Add ty => (ty, [ty, ty])
            | Op.Sub ty => (ty, [ty, ty])
            | Op.Mul ty => (ty, [ty, ty])
            | Op.Div ty => (ty, [ty, ty])
            | Op.Neg ty => (ty, [ty])
            | Op.Abs ty => (ty, [ty])
            | Op.LT ty => (Ty.BoolTy, [ty, ty])
            | Op.LTE ty => (Ty.BoolTy, [ty, ty])
            | Op.EQ ty => (Ty.BoolTy, [ty, ty])
            | Op.NEQ ty => (Ty.BoolTy, [ty, ty])
            | Op.GT ty => (Ty.BoolTy, [ty, ty])
            | Op.GTE ty => (Ty.BoolTy, [ty, ty])
            | Op.Power => (Ty.realTy, [Ty.realTy, Ty.IntTy])
            | Op.Not => (Ty.BoolTy, [Ty.BoolTy])
            | Op.Max => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.Min => (Ty.realTy, [Ty.realTy, Ty.realTy])
            | Op.Clamp ty => (ty, [ty, ty, ty])
            | Op.Lerp ty => (ty, [ty, ty, Ty.realTy])
            | Op.Dot ty => (Ty.realTy, [ty, ty])
            | Op.MulVecMat(ty as Ty.TensorTy[d1, d2]) => (Ty.TensorTy[d2], [Ty.TensorTy[d1], ty])
            | Op.MulMatVec(ty as Ty.TensorTy[d1, d2]) => (Ty.TensorTy[d1], [ty, Ty.TensorTy[d2]])
            | Op.MulMatMat(ty1 as Ty.TensorTy[d1, d2], ty2 as Ty.TensorTy[d2', d3]) =>
                (Ty.TensorTy[d1, d3], [ty1, ty2])
            | Op.MulVecTen3(ty as Ty.TensorTy[d1, d2, d3]) =>
                (Ty.TensorTy[d2, d3], [Ty.TensorTy[d1], ty])
            | Op.MulTen3Vec(ty as Ty.TensorTy[d1, d2, d3]) =>
                (Ty.TensorTy[d1, d2], [ty, Ty.TensorTy[d3]])
            | Op.ColonMul(ty1 as Ty.TensorTy dd1, ty2 as Ty.TensorTy(d21::d22::dd2)) => let
                fun last2 ([d1, d2], prefix) = (prefix, d1, d2)
                  | last2 (d::dd, prefix) = last2(dd, d::prefix)
                  | last2 _ = raise Fail("sigOf: invalid operator " ^ Op.toString rator)
                val (prefix, d11, d12) = last2 (dd1, [])
                in
                  if (d11 <> d21) orelse (d12 <> d22)
                    then raise Fail("sigOf: invalid operator " ^ Op.toString rator)
                    else (Ty.TensorTy(List.revAppend(prefix, dd2)), [ty1, ty2])
                end
            | Op.Cross => (Ty.vec3Ty, [Ty.vec3Ty, Ty.vec3Ty])
            | Op.Outer(ty as Ty.TensorTy[d1, d2]) => (ty, [Ty.TensorTy[d1], Ty.TensorTy[d2]])
            | Op.Norm ty => (Ty.realTy, [ty])
            | Op.Normalize ty => (ty, [ty])
            | Op.Scale ty => (ty, [Ty.realTy, ty])
            | Op.PrincipleEvec(resTy as Ty.TensorTy[d]) => (resTy, [Ty.TensorTy[d,d]])
            | Op.Eigen2x2 => eigenSig 2
            | Op.Eigen3x3 => eigenSig 3
            | Op.Identity d => (Ty.TensorTy[d,d], [])
            | Op.Zero ty => (ty, [])
            | Op.Trace ty => (Ty.realTy, [ty])
	    | Op.Transpose(d1, d2) => (Ty.TensorTy[d2, d1], [Ty.TensorTy[d1, d2]])
            | Op.Slice(ty as Ty.TensorTy dd, m) => let
                val (resDims, idxDims) = let
                      fun f (d, false, (rds, ids)) = (d::rds, ids)
                        | f (_, true, (rds, ids)) = (rds, Ty.IntTy::ids)
                      in
                        ListPair.foldr f ([], []) (dd, m)
                      end
                in
                  (Ty.TensorTy resDims, ty::idxDims)
                end
            | Op.TensorSub(ty as Ty.TensorTy dd) =>
                (Ty.realTy, ty :: List.map (fn _ => Ty.IntTy) dd)
            | Op.Select(ty as Ty.TupleTy tys, i) =>
                if (1 <= i) andalso (i <= length tys)
                  then (List.nth(tys, i-1), [ty])
                  else raise Fail("sigOf: invalid operator " ^ Op.toString rator)
            | Op.SeqSub(ty as Ty.SeqTy(elemTy, _)) => (elemTy, [ty, Ty.IntTy])
            | Op.IntToReal => (Ty.realTy, [Ty.IntTy])
            | Op.TruncToInt => (Ty.IntTy, [Ty.realTy])
            | Op.RoundToInt => (Ty.IntTy, [Ty.realTy])
            | Op.CeilToInt => (Ty.IntTy, [Ty.realTy])
            | Op.FloorToInt => (Ty.IntTy, [Ty.realTy])
            | Op.Kernel _ => (Ty.KernelTy, [])
            | Op.Field dim => (Ty.FieldTy, [Ty.ImageTy dim, Ty.KernelTy])
            | Op.Inside dim => (Ty.BoolTy, [Ty.TensorTy[dim], Ty.FieldTy])
            | Op.AddField => (Ty.FieldTy, [Ty.FieldTy, Ty.FieldTy])
            | Op.SubField => (Ty.FieldTy, [Ty.FieldTy, Ty.FieldTy])
            | Op.ScaleField => (Ty.FieldTy, [Ty.realTy, Ty.FieldTy])
            | Op.OffsetField => (Ty.FieldTy, [Ty.FieldTy, Ty.realTy])
            | Op.NegField => (Ty.FieldTy, [Ty.FieldTy])
            | Op.DiffField => (Ty.FieldTy, [Ty.FieldTy])
            | Op.CurlField 2 => (Ty.FieldTy, [Ty.FieldTy])
            | Op.CurlField 3 => (Ty.FieldTy, [Ty.FieldTy])
            | Op.Probe(domTy, rngTy) => (rngTy, [Ty.FieldTy, domTy])
            | Op.LoadImage(ty, _, _) => (ty, [])
            | Op.Input(Inputs.INP{ty, ...}) => (ty, [])
            | Op.Print tys => (Ty.TupleTy[], tys)
            | _ => raise Fail("sigOf: invalid operator " ^ Op.toString rator)
          (* end case *))

    fun typeOfCons (Ty.TensorTy dd', (ty1 as Ty.TensorTy dd)::r) =
          if List.all (fn ty => Ty.same(ty1, ty)) r
            then (dd' = (List.length r + 1)::dd)
            else false
      | typeOfCons (Ty.SeqTy(ty, n), tys) =
          List.all (fn ty' => Ty.same(ty, ty')) tys andalso (List.length tys = n)
      | typeOfCons _ = false

  end

structure CheckHighIL = CheckILFn (
    structure IL = HighIL
    structure OpTy = CheckOps)

structure HighPP = SSAPPFn (HighIL)


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