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 4151 - (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 3948 = 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 :     type t
22 :    
23 :     (* create a new environment *)
24 : jhr 3861 val new : TreeIR.target_info -> t
25 : jhr 3834
26 : jhr 3843 (* return the layout of a Low IR vector of a given width as a Tree IR composite vector *)
27 :     val layoutVec : t -> int -> TreeTypes.vec_layout
28 :    
29 : jhr 3852 val isInlineOp : t -> LowOps.rator -> bool
30 : jhr 3851
31 : jhr 3834 (* get the binding for a variable *)
32 :     val useVar : t -> LowIR.var -> binding
33 :    
34 :     (* add a binding to the environment *)
35 : jhr 3851 val bindVar : t * LowIR.var * binding -> unit
36 : jhr 3834
37 : jhr 3852 (* set the definition of a variable, where the RHS is a simple expression that can be
38 :     * replicated without code-size or performance hit (e.g., a literal or variable).
39 : jhr 4039 * If the variable has tensor type, then we add a TensorRef wrapper. The expression will
40 :     * be wrapped in a TREE constructor.
41 : jhr 3852 *)
42 : jhr 3948 val bindSimple : t * LowIR.var * TreeIR.exp -> unit
43 : jhr 3836
44 :     (* at the end of a block, we need to assign any pending expressions to locals. The
45 :     * blkStms list and the resulting statement list are in reverse order.
46 :     *)
47 : jhr 3851 val flushPending : t * TreeIR.stm list -> TreeIR.stm list
48 : jhr 3836
49 : jhr 3834 end = struct
50 :    
51 :     structure VT = LowIR.Var.Tbl
52 :    
53 :     datatype binding
54 : jhr 3948 = RHS of TreeTypes.t * TreeIR.exp (* non-inlinable tree expression *)
55 :     | TREE of TreeIR.exp (* inlinable tree expression *)
56 :     | VEC of TreeTypes.vec_layout * TreeIR.exp list (* composite of vector expressions *)
57 : jhr 3834
58 : jhr 3948 (*DEBUG*)
59 :     fun bindingToString (RHS(ty, e)) =
60 :     concat["RHS(", TreeTypes.toString ty, ", ", TreePP.expToString e, ")"]
61 : jhr 4039 | bindingToString (TREE e) = concat["TREE(", TreePP.expToString e, ")"]
62 : jhr 4151 | bindingToString (VEC(layout, _)) = concat["VEC(", VectorLayout.toString layout, ", _)"]
63 : jhr 3948 (*DEBUG*)
64 :    
65 : jhr 3834 datatype t = E of {
66 : jhr 3836 tbl : (bool * binding) VT.hash_table,
67 : jhr 3861 info : TreeIR.target_info
68 : jhr 3834 }
69 :    
70 :     fun decCount (LowIR.V{useCnt, ...}) = let
71 :     val n = !useCnt - 1
72 :     in
73 :     useCnt := n; (n <= 0)
74 :     end
75 :    
76 : jhr 3851 fun new info = E{
77 : jhr 3843 tbl = VT.mkTable (256, Fail "tbl"),
78 : jhr 3851 info = info
79 : jhr 3843 }
80 : jhr 3834
81 : jhr 3851 fun layoutVec (E{info={layout, ...}, ...}) wid = layout wid
82 : jhr 3843
83 : jhr 3851 fun isInlineOp (E{info={isInline, ...}, ...}) rator = isInline rator
84 :    
85 : jhr 3856 (* use a variable. If this is its last use, we remove it from the table.
86 :     * NOTE: we assume that this function is _not_ called on variables that are
87 :     * in an equivalence class; those are handled in low-to-tree.sml.
88 :     *)
89 : jhr 3834 fun useVar (env as E{tbl, ...}) = let
90 :     val find = VT.find tbl
91 :     val remove = VT.remove tbl
92 :     fun use x = let
93 :     fun removeUnused () = if (decCount x) then ignore(VT.remove tbl x) else ()
94 :     in
95 : jhr 3856 case find x
96 :     of SOME(true, binding) => (removeUnused(); binding)
97 :     | SOME(false, binding) => (removeUnused(); binding)
98 :     | NONE => raise Fail(concat ["useVar(", LowIR.Var.toString x, ")"])
99 : jhr 3834 (* end case *)
100 :     end
101 :     in
102 :     use
103 :     end
104 :    
105 : jhr 3851 fun bindVar (E{tbl, ...}, x, b) = VT.insert tbl (x, (false, b))
106 : jhr 3834
107 : jhr 4039 fun bindSimple (E{tbl, ...}, x, b) = (case TreeTypeOf.exp b
108 :     of TreeTypes.TensorTy(shp as _::_) =>
109 :     VT.insert tbl (x, (true, TREE(TreeIR.E_Op(TreeOps.TensorRef shp, [b]))))
110 :     | _ => VT.insert tbl (x, (true, TREE b))
111 :     (* end case *))
112 : jhr 3836
113 : jhr 3851 fun flushPending (E{tbl, ...}, blkStms) = let
114 : jhr 3848 fun doVar (x, (false, TREE e), stms) = let
115 : jhr 3842 val t = Util.newLocalVar x
116 : jhr 3836 in
117 :     VT.insert tbl (x, (true, TREE(TreeIR.E_Var t)));
118 : jhr 3857 TreeIR.S_Assign(true, t, e)::stms
119 : jhr 3836 end
120 : jhr 3890 | doVar (x, (false, VEC(layout, es)), stms) = let
121 :     val xs = Util.newVectorVars layout
122 :     val stms = ListPair.foldlEq
123 :     (fn (x, e, stms) => TreeIR.S_Assign(true, x, e)::stms)
124 :     stms (xs, es)
125 :     in
126 :     VT.insert tbl (x, (true, VEC(layout, List.map TreeIR.E_Var xs)));
127 :     stms
128 :     end
129 : jhr 3836 | doVar (_, _, acc) = acc
130 : jhr 3848 val stms = VT.foldi doVar blkStms tbl
131 : jhr 3836 in
132 : jhr 3851 stms
133 : jhr 3836 end
134 :    
135 : jhr 3834 end

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