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

SCM Repository

[diderot] Annotation of /branches/lamont/src/compiler/mid-il/mid-contract.sml
ViewVC logotype

Annotation of /branches/lamont/src/compiler/mid-il/mid-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 1640 (* mid-contract.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 1640 * All rights reserved.
7 :     *
8 :     * Contraction phase for MidIL.
9 :     *)
10 :    
11 :     structure MidContract : sig
12 :    
13 :     val transform : MidIL.program -> MidIL.program
14 :    
15 :     end = struct
16 :    
17 :     structure IL = MidIL
18 :     structure Op = MidOps
19 : jhr 2298 structure Ty = MidILTypes
20 : jhr 1640 structure V = IL.Var
21 :     structure ST = Stats
22 :    
23 :     (********** Counters for statistics **********)
24 : jhr 2298 val cntAddNeg = ST.newCounter "mid-opt:add-neg"
25 :     val cntSubNeg = ST.newCounter "mid-opt:sub-neg"
26 :     val cntSubSame = ST.newCounter "mid-opt:sub-same"
27 :     val cntNegNeg = ST.newCounter "mid-opt:neg-neg"
28 :     val cntEigenVals = ST.newCounter "mid-opt:eigen-vals"
29 :     val cntUnused = ST.newCounter "mid-opt:unused"
30 :     val firstCounter = cntAddNeg
31 : jhr 1640 val lastCounter = cntUnused
32 :    
33 :     structure UnusedElim = UnusedElimFn (
34 : jhr 2298 structure IL = IL
35 :     val cntUnused = cntUnused)
36 : jhr 1640
37 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
38 :    
39 :     (* adjust a variable's use count *)
40 :     fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
41 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
42 : jhr 2298 fun use x = (incUse x; x)
43 : jhr 1640
44 : jhr 2298 fun getRHS x = (case V.binding x
45 :     of IL.VB_RHS(IL.OP arg) => SOME arg
46 :     | IL.VB_RHS(IL.VAR x') => getRHS x'
47 :     | _ => NONE
48 : jhr 1640 (* end case *))
49 :    
50 : jhr 2298 fun doAssign (lhs, IL.OP rhs) = (case rhs
51 :     of (Op.Add ty, [a, b]) => (case getRHS b
52 :     of SOME(Op.Neg _, [c]) => (
53 :     (* rewrite to "a-c" *)
54 :     ST.tick cntAddNeg;
55 :     decUse b;
56 :     SOME[(lhs, IL.OP(Op.Sub ty, [a, use c]))])
57 :     | _ => NONE
58 :     (* end case *))
59 :     | (Op.Sub ty, [a, b]) => if IL.Var.same(a, b)
60 :     then ( (* rewrite to 0 *)
61 :     ST.tick cntSubSame;
62 :     decUse a; decUse b;
63 :     case ty
64 :     of Ty.IntTy => SOME[(lhs, IL.LIT(Literal.Int 0))]
65 :     | Ty.TensorTy[] => SOME[(lhs, IL.LIT(Literal.Float(FloatLit.zero false)))]
66 :     | _ => (incUse a; incUse b; NONE) (* FIXME: should handle arbitrary tensors *)
67 :     (* end case *))
68 :     else (case getRHS b
69 :     of SOME(Op.Neg _, [c]) => (
70 :     (* rewrite to "a+c" *)
71 :     ST.tick cntSubNeg;
72 :     decUse b;
73 :     SOME[(lhs, IL.OP(Op.Add ty, [a, use c]))])
74 :     | _ => NONE
75 :     (* end case *))
76 :     | (Op.Neg ty, [a]) => (case getRHS a
77 :     of SOME(Op.Neg _, [b]) => (
78 :     (* rewrite to "b" *)
79 :     ST.tick cntNegNeg;
80 :     decUse a;
81 :     SOME[(lhs, IL.VAR(use b))])
82 :     | _ => NONE
83 :     (* end case *))
84 :     | _ => NONE
85 :     (* end case *))
86 :     | doAssign _ = NONE
87 : jhr 1640
88 : jhr 2298 fun doMAssign (ys, rator, xs) = (case (ys, rator)
89 :     of ([vals, vecs], Op.EigenVecs2x2) =>
90 :     if (useCount vecs = 0)
91 :     then (
92 :     ST.tick cntEigenVals;
93 :     SOME[(vals, IL.OP(Op.EigenVals2x2, xs))])
94 :     else NONE
95 :     | ([vals, vecs], Op.EigenVecs3x3) =>
96 :     if (useCount vecs = 0)
97 :     then (
98 :     ST.tick cntEigenVals;
99 :     SOME[(vals, IL.OP(Op.EigenVals3x3, xs))])
100 :     else NONE
101 :     | _ => NONE
102 :     (* end case *))
103 : jhr 1640
104 : jhr 2298 structure Rewrite = RewriteFn (
105 :     struct
106 :     structure IL = IL
107 :     val doAssign = doAssign
108 :     val doMAssign = doMAssign
109 :     val elimUnusedVars = UnusedElim.reduce
110 :     end)
111 :    
112 :     val transform = Rewrite.transform
113 :    
114 : jhr 1640 end

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