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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/low-to-tree/env.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/low-to-tree/env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5215 - (view) (download)

1 : jhr 3834 (* env.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Env : sig
10 :    
11 :     (* the translated bindings of LowIR variables. If the variable has a vector type,
12 : jhr 3948 * then it is bound to a vector of expression trees, otherwise it is bound to
13 :     * either a RHS (if the expression is not inlinable) or TREE (if the expression can
14 :     * be a subexpression).
15 : jhr 3834 *)
16 :     datatype binding
17 : jhr 4317 = RHS of TreeTypes.t * TreeIR.exp (* non-inlinable tree expression *)
18 :     | TREE of TreeIR.exp (* inlinable tree expression *)
19 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list (* composite of vector expressions *)
20 : jhr 3834
21 : jhr 4592 val bindingToString : binding -> string
22 :    
23 : jhr 3834 type t
24 :    
25 :     (* create a new environment *)
26 : jhr 3861 val new : TreeIR.target_info -> t
27 : jhr 3834
28 : jhr 4380 (* create a new environment that includes function definitions for map-reduce statments *)
29 :     val newWithFuncs : TreeIR.target_info * LowIR.func_def list -> t
30 :    
31 : jhr 3843 (* return the layout of a Low IR vector of a given width as a Tree IR composite vector *)
32 :     val layoutVec : t -> int -> TreeTypes.vec_layout
33 :    
34 : jhr 3852 val isInlineOp : t -> LowOps.rator -> bool
35 : jhr 3851
36 : jhr 3834 (* get the binding for a variable *)
37 :     val useVar : t -> LowIR.var -> binding
38 :    
39 :     (* add a binding to the environment *)
40 : jhr 3851 val bindVar : t * LowIR.var * binding -> unit
41 : jhr 3834
42 : jhr 3852 (* set the definition of a variable, where the RHS is a simple expression that can be
43 :     * replicated without code-size or performance hit (e.g., a literal or variable).
44 : jhr 4039 * If the variable has tensor type, then we add a TensorRef wrapper. The expression will
45 :     * be wrapped in a TREE constructor.
46 : jhr 3852 *)
47 : jhr 3948 val bindSimple : t * LowIR.var * TreeIR.exp -> unit
48 : jhr 3836
49 :     (* at the end of a block, we need to assign any pending expressions to locals. The
50 :     * blkStms list and the resulting statement list are in reverse order.
51 :     *)
52 : jhr 3851 val flushPending : t * TreeIR.stm list -> TreeIR.stm list
53 : jhr 3836
54 : jhr 4380 (* lookup a function definition *)
55 :     val lookupFunc : t * LowIR.func -> LowIR.func_def
56 :    
57 : jhr 3834 end = struct
58 :    
59 :     structure VT = LowIR.Var.Tbl
60 :    
61 :     datatype binding
62 : jhr 4317 = RHS of TreeTypes.t * TreeIR.exp (* non-inlinable tree expression *)
63 :     | TREE of TreeIR.exp (* inlinable tree expression *)
64 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list (* composite of vector expressions *)
65 : jhr 3834
66 : jhr 3948 (*DEBUG*)
67 :     fun bindingToString (RHS(ty, e)) =
68 : jhr 4317 concat["RHS(", TreeTypes.toString ty, ", ", TreePP.expToString e, ")"]
69 : jhr 4039 | bindingToString (TREE e) = concat["TREE(", TreePP.expToString e, ")"]
70 : jhr 4151 | bindingToString (VEC(layout, _)) = concat["VEC(", VectorLayout.toString layout, ", _)"]
71 : jhr 3948 (*DEBUG*)
72 :    
73 : jhr 3834 datatype t = E of {
74 : jhr 4317 tbl : (bool * binding) VT.hash_table,
75 : jhr 4380 info : TreeIR.target_info,
76 : jhr 5215 funcs : LowIR.func_def list
77 : jhr 3834 }
78 :    
79 :     fun decCount (LowIR.V{useCnt, ...}) = let
80 : jhr 4317 val n = !useCnt - 1
81 :     in
82 :     useCnt := n; (n <= 0)
83 :     end
84 : jhr 3834
85 : jhr 3851 fun new info = E{
86 : jhr 4317 tbl = VT.mkTable (256, Fail "tbl"),
87 : jhr 4380 info = info,
88 : jhr 5215 funcs = []
89 : jhr 4317 }
90 : jhr 3834
91 : jhr 4380 fun newWithFuncs (info, fdefs) = E{
92 :     tbl = VT.mkTable (256, Fail "tbl"),
93 :     info = info,
94 : jhr 5215 funcs = List.filter (fn (LowIR.Func{name, ...}) => Flatten.isMapFunc name) fdefs
95 : jhr 4380 }
96 :    
97 : jhr 3851 fun layoutVec (E{info={layout, ...}, ...}) wid = layout wid
98 : jhr 3843
99 : jhr 3851 fun isInlineOp (E{info={isInline, ...}, ...}) rator = isInline rator
100 :    
101 : jhr 3856 (* use a variable. If this is its last use, we remove it from the table.
102 :     * NOTE: we assume that this function is _not_ called on variables that are
103 :     * in an equivalence class; those are handled in low-to-tree.sml.
104 :     *)
105 : jhr 3834 fun useVar (env as E{tbl, ...}) = let
106 : jhr 4317 val find = VT.find tbl
107 :     val remove = VT.remove tbl
108 :     fun use x = let
109 : jhr 4386 fun removeUnused () = if (decCount x) then ignore(remove x) else ()
110 : jhr 4317 in
111 :     case find x
112 :     of SOME(true, binding) => (removeUnused(); binding)
113 :     | SOME(false, binding) => (removeUnused(); binding)
114 :     | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
115 :     (* end case *)
116 :     end
117 :     in
118 :     use
119 :     end
120 : jhr 3834
121 : jhr 3851 fun bindVar (E{tbl, ...}, x, b) = VT.insert tbl (x, (false, b))
122 : jhr 4387 (* DEBUG
123 :     val bindVar = fn (env, x, b) => (
124 :     print(concat["bindVar(_, ", LowIR.Var.toString x, ", ", bindingToString b, ")\n"]);
125 :     bindVar(env, x, b))
126 :     *)
127 : jhr 3834
128 : jhr 4039 fun bindSimple (E{tbl, ...}, x, b) = (case TreeTypeOf.exp b
129 : jhr 4317 of TreeTypes.TensorTy(shp as _::_) =>
130 :     VT.insert tbl (x, (true, TREE(TreeIR.E_Op(TreeOps.TensorRef shp, [b]))))
131 :     | _ => VT.insert tbl (x, (true, TREE b))
132 :     (* end case *))
133 : jhr 4387 (* DEBUG
134 :     val bindSimple = fn (env, x, b) => (
135 :     print(concat["bindSimple(_, ", LowIR.Var.toString x, ", ", TreePP.expToString b, ")\n"]);
136 :     bindSimple(env, x, b))
137 :     *)
138 : jhr 3836
139 : jhr 3851 fun flushPending (E{tbl, ...}, blkStms) = let
140 : jhr 3848 fun doVar (x, (false, TREE e), stms) = let
141 : jhr 3842 val t = Util.newLocalVar x
142 : jhr 3836 in
143 :     VT.insert tbl (x, (true, TREE(TreeIR.E_Var t)));
144 : jhr 3857 TreeIR.S_Assign(true, t, e)::stms
145 : jhr 3836 end
146 : jhr 4317 | doVar (x, (false, VEC(layout, es)), stms) = let
147 :     val xs = Util.newVectorVars layout
148 :     val stms = ListPair.foldlEq
149 :     (fn (x, e, stms) => TreeIR.S_Assign(true, x, e)::stms)
150 :     stms (xs, es)
151 :     in
152 :     VT.insert tbl (x, (true, VEC(layout, List.map TreeIR.E_Var xs)));
153 :     stms
154 :     end
155 : jhr 4387 | doVar (_, _, stms) = stms
156 : jhr 3848 val stms = VT.foldi doVar blkStms tbl
157 : jhr 3836 in
158 : jhr 3851 stms
159 : jhr 3836 end
160 :    
161 : jhr 4380 fun lookupFunc (E{funcs, ...}, f) = (
162 : jhr 5215 case List.find (fn (LowIR.Func{name, ...}) => LowIR.Func.same(f, name)) funcs
163 :     of SOME fdef => fdef
164 :     | NONE => raise Fail(concat["lookupFunc(", LowIR.Func.toString f, ")"])
165 :     (* end case *))
166 : jhr 4380
167 : jhr 3834 end

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