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

SCM Repository

[diderot] Annotation of /branches/femprime/src/compiler/high-opt/normalize.sml
ViewVC logotype

Annotation of /branches/femprime/src/compiler/high-opt/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4088 - (view) (download)
Original Path: branches/vis15/src/compiler/high-opt/normalize.sml

1 : jhr 3513 (* normalize.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Normalize : sig
10 :    
11 :     val transform : HighIR.program -> HighIR.program
12 :    
13 :     end = struct
14 :    
15 :     structure IR = HighIR
16 :     structure Op = HighOps
17 :     structure V = IR.Var
18 : jhr 3517 structure ST = Stats
19 : jhr 3513
20 : jhr 3517 (********** Counters for statistics **********)
21 :     val cntUnused = ST.newCounter "high-opt:unused"
22 :    
23 : jhr 3513 structure UnusedElim = UnusedElimFn (
24 :     structure IR = IR
25 :     val cntUnused = cntUnused)
26 :    
27 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
28 :    
29 :     (* adjust a variable's use count *)
30 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
31 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
32 :     fun use x = (incUse x; x)
33 :    
34 : jhr 3587 fun getEinApp x = (case V.getDef x
35 :     of IR.EINAPP(e, arg) => SOME(e, arg)
36 : jhr 3513 | _ => NONE
37 :     (* end case *))
38 :    
39 : jhr 3520 (* doNormalize : EIN -> EIN
40 : jhr 3513 * Orders EIN, normalizes it, then cleans the summation
41 :     *)
42 : cchiw 3978 fun doNormalize e' = let
43 :     val ordered = Reorder.transform e'
44 : jhr 3513 in
45 : jhr 3520 case NormalizeEin.transform ordered
46 :     of NONE => ordered
47 : jhr 3992 | SOME e => EinSums.clean e
48 : jhr 3520 (* end case *)
49 : jhr 3513 end
50 :    
51 :     fun nameCnt e = String.concat[V.toString e, "(", Int.toString(useCount e), ")"]
52 :     fun nameCnts e = String.concatWithMap "" nameCnt e
53 :    
54 :     (* rewriteEin : EIN.Params*int*int*EIN*HighIR Vars* rhs list* EIN *HighIR Var
55 :     * -> int*EIN*int*rhs list
56 : jhr 4080 * Orders EIN, normalizes it, then cleans the summation orig-original EIN
57 : jhr 3513 *)
58 : jhr 3535 fun rewriteEin (params, place, changed, newE, newArgs, done, newEinApp, orig, lhs) = (
59 :     case List.nth(params, place)
60 : jhr 4088 of Ein.TEN(false, _) => (
61 :     incUse lhs; incUse newEinApp;
62 : cchiw 4087 (changed, orig, place+1, done@[newEinApp]))
63 : jhr 3513 | _ => let
64 :     val rtnArgs = done @ newArgs
65 : jhr 3521 val (c, subst) = Apply.apply(orig, place, newE)
66 : jhr 3513 in
67 : jhr 3521 if c
68 : jhr 3517 then (true, subst, place + length newArgs, rtnArgs)
69 : jhr 3513 else (
70 : jhr 3517 incUse lhs; List.app incUse newArgs; decUse newEinApp;
71 : jhr 3513 (true, subst, place + length newArgs, rtnArgs))
72 :     end
73 :     (* end case *))
74 :    
75 :     (* doRHS: HighIR.var * rhs -> (var * rhs) list option
76 :     * Looks at each argument to the original EINAPP.
77 : jhr 3535 * If it is another EIN APP calls rewriteEin to do application
78 : jhr 3513 * "place"-The Param-id for the EIN operator.
79 :     * Keeps track of the place of the argument in substitution.
80 :     *)
81 :     fun doRHS (lhs, IR.EINAPP(ein, args)) = let
82 :     fun rewrite (false, _, _, [], _) = NONE
83 :     | rewrite (true, orig, _, [], args') =
84 : jhr 3517 SOME[(lhs, IR.EINAPP(doNormalize orig, args'))]
85 : jhr 3587 | rewrite (changed, orig, place, e::es, args') = (case getEinApp e
86 : jhr 3513 of NONE => rewrite(changed, orig, place+1, es, args'@[e])
87 : jhr 3587 | SOME(newE, newA) => let
88 : jhr 3513 val Ein.EIN{params, index, body} = orig
89 :     val (changed, e', place', done') =
90 :     rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
91 :     in
92 :     rewrite(changed, e', place', es, done')
93 :     end
94 :     (* end case *))
95 :     in
96 :     rewrite (false, ein, 0, args, [])
97 :     end
98 :     | doRHS _ = NONE
99 :    
100 :     structure Rewrite = RewriteFn (
101 :     struct
102 :     structure IR = IR
103 :     val doAssign = doRHS
104 :    
105 :     fun doMAssign _ = NONE
106 :     val elimUnusedVars = UnusedElim.reduce
107 :     end)
108 :    
109 : jhr 3615 structure Promote = PromoteFn (IR)
110 : jhr 3513
111 : jhr 3615 val transform = Promote.transform o Rewrite.transform
112 :    
113 : jhr 4084 (*DEBUG*
114 : jhr 4032 fun transform prog = let
115 :     val prog = Rewrite.transform prog
116 :     val _ = HighPP.output(Log.logFile(), "AFTER REWRITE", prog)
117 :     val prog = Promote.transform prog
118 :     val _ = HighPP.output(Log.logFile(), "AFTER PROMOTE", prog)
119 :     in
120 :     prog
121 :     end
122 : jhr 4084 *DEBUG*)
123 : jhr 4032
124 : jhr 3513 end

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