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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/low-opt/low-contract.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/low-opt/low-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3891 - (view) (download)

1 : jhr 3714 (* low-contract.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 :     * Contraction phase for LowIR.
9 :     *)
10 :    
11 :     structure LowContract : sig
12 :    
13 :     val transform : LowIR.program -> LowIR.program
14 :    
15 :     end = struct
16 :    
17 :     structure IR = LowIR
18 :     structure Op = LowOps
19 :     structure Ty = LowTypes
20 :     structure V = IR.Var
21 :     structure ST = Stats
22 :    
23 :     (********** Counters for statistics **********)
24 : jhr 3799 val cntAddNeg = ST.newCounter "low-contract:add-neg"
25 :     val cntSubNeg = ST.newCounter "low-contract:sub-neg"
26 :     val cntSubSame = ST.newCounter "low-contract:sub-same"
27 :     val cntNegNeg = ST.newCounter "low-contract:neg-neg"
28 :     val cntIntToReal = ST.newCounter "low-contract:int-to-real"
29 :     val cntTensorIndex = ST.newCounter "low-contract:tensor-index"
30 :     val cntProjectLast = ST.newCounter "low-contract:project-last"
31 : jhr 3856 val cntSubscript = ST.newCounter "low-contract:subscript"
32 : jhr 3799 val cntUnused = ST.newCounter "low-contract:unused"
33 : jhr 3714 val firstCounter = cntAddNeg
34 :     val lastCounter = cntUnused
35 :    
36 :     structure UnusedElim = UnusedElimFn (
37 :     structure IR = IR
38 :     val cntUnused = cntUnused)
39 :    
40 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
41 :    
42 :     (* adjust a variable's use count *)
43 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
44 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
45 :     fun use x = (incUse x; x)
46 :    
47 :     fun getRHSOpt x = (case V.getDef x
48 :     of IR.OP arg => SOME arg
49 :     | _ => NONE
50 :     (* end case *))
51 :    
52 : jhr 3814 (* get the local definition of a variable. Unlike getDef, this function does
53 :     * not chase through global definitions, which means that we do not have to
54 :     * worry about lifting local variables in the globalInit to global scope.
55 :     *)
56 :     fun getLocalDef (x as IR.V{bind, ...}) = (case !bind
57 :     of IR.VB_RHS rhs => (case rhs
58 :     of IR.VAR x => getLocalDef x
59 :     | _ => rhs
60 :     (* end case *))
61 :     | _ => IR.VAR x
62 :     (* end case *))
63 :    
64 : jhr 3714 (* TODO: tensor selection operations *)
65 :     fun doAssign (lhs, IR.OP rhs) = (case rhs
66 :     of (Op.IAdd, [a, b]) => (case getRHSOpt b
67 :     of SOME(Op.INeg, [c]) => (
68 :     (* rewrite to "a-c" *)
69 :     ST.tick cntAddNeg;
70 :     decUse b;
71 :     SOME[(lhs, IR.OP(Op.ISub, [a, use c]))])
72 :     | _ => NONE
73 :     (* end case *))
74 :     | (Op.ISub, [a, b]) => if IR.Var.same(a, b)
75 :     then ( (* rewrite to 0 *)
76 :     ST.tick cntSubSame;
77 :     decUse a; decUse b;
78 :     SOME[(lhs, IR.LIT(Literal.Int 0))])
79 :     else (case getRHSOpt b
80 :     of SOME(Op.INeg, [c]) => (
81 :     (* rewrite to "a+c" *)
82 :     ST.tick cntSubNeg;
83 :     decUse b;
84 :     SOME[(lhs, IR.OP(Op.IAdd, [a, use c]))])
85 :     | _ => NONE
86 :     (* end case *))
87 :     | (Op.INeg, [a]) => (case getRHSOpt a
88 :     of SOME(Op.INeg, [b]) => (
89 :     (* rewrite to "b" *)
90 :     ST.tick cntNegNeg;
91 :     decUse a;
92 :     SOME[(lhs, IR.VAR(use b))])
93 :     | _ => NONE
94 :     (* end case *))
95 :     | (Op.IntToReal, [a]) => (case V.getDef a
96 :     of IR.LIT(Literal.Int n) => (
97 :     (* rerite to a real literal *)
98 :     ST.tick cntIntToReal;
99 :     decUse a;
100 :     SOME[(lhs, IR.LIT(Literal.Real(RealLit.fromInt n)))])
101 :     | _ => NONE
102 :     (* end case *))
103 : jhr 3799 | (Op.TensorIndex(Ty.TensorTy dims, idxs), [t]) => let
104 :     fun get ([], [], x) = (
105 :     SOME[(lhs, IR.VAR(use x))])
106 : jhr 3814 | get (ix::ixs, d::ds, x) = (case getLocalDef x
107 : jhr 3799 of IR.CONS(ys, _) => get(ixs, ds, List.nth(ys, ix))
108 : jhr 3891 | _ => let
109 :     val rator = if List.null ds
110 :     then Op.VIndex(d, ix)
111 :     else Op.TensorIndex(Ty.tensorTy(d::ds), ix::ixs)
112 :     in
113 :     SOME[(lhs, IR.OP(rator, [use x]))]
114 :     end
115 : jhr 3799 (* end case *))
116 :     | get _ = raise Fail "malformed TensorIndex"
117 :     in
118 : jhr 3814 case getLocalDef t
119 : jhr 3799 of IR.CONS _ => (ST.tick cntTensorIndex; decUse t; get(idxs, dims, t))
120 :     | _ => NONE
121 :     (* end case *)
122 :     end
123 :     | (Op.ProjectLast(Ty.TensorTy dims, idxs), [t]) => let
124 :     fun get ([], [_], x) = (
125 :     SOME[(lhs, IR.VAR(use x))])
126 : jhr 3814 | get (ix::ixs, d::ds, x) = (case getLocalDef x
127 : jhr 3799 of IR.CONS(ys, _) => get(ixs, ds, List.nth(ys, ix))
128 : jhr 3814 | _ => SOME[
129 :     (lhs, IR.OP(Op.ProjectLast(Ty.tensorTy(d::ds), ix::ixs), [use x]))
130 :     ]
131 : jhr 3799 (* end case *))
132 :     | get _ = raise Fail "malformed ProjectLast"
133 :     in
134 : jhr 3814 case getLocalDef t
135 : jhr 3799 of IR.CONS _ => (ST.tick cntProjectLast; decUse t; get(idxs, dims, t))
136 :     | _ => NONE
137 :     (* end case *)
138 :     end
139 : jhr 3856 | (Op.Subscript ty, [seq, idx]) => (case (getLocalDef seq, V.getDef idx)
140 :     of (IR.SEQ(vs, _), IR.LIT(Literal.Int i)) => (
141 :     ST.tick cntSubscript; decUse seq; decUse idx;
142 :     SOME[(lhs, IR.VAR(use (List.nth(vs, Int.fromLarge i))))])
143 :     | _ => NONE
144 :     (* end case *))
145 : jhr 3714 | _ => NONE
146 :     (* end case *))
147 :     | doAssign _ = NONE
148 :    
149 : jhr 3747 fun doMAssign _ = NONE
150 : jhr 3714
151 :     structure Rewrite = RewriteFn (
152 :     struct
153 :     structure IR = IR
154 :     val doAssign = doAssign
155 :     val doMAssign = doMAssign
156 :     val elimUnusedVars = UnusedElim.reduce
157 :     end)
158 :    
159 :     val transform = Rewrite.transform
160 :    
161 :     end

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