Debian Codegraph @

lib/mlrisc-lib/MLRISC/scheduling/regionBuilder.sml

raw
1 (*
2 * Partition the IR into regions according to partition criteria
3 * and frequencies. Then feed the regions into the instruction
4 * scheduler.
5 *
6 * The partitional criteria can be:
7 * 1. The maximum number of blocks
8 * 2. The maximum number of instructions
9 *)
10
11 functor RegionBuilder(IR : MLRISC_IR) : REGION_BUILDER =
12 struct
13 structure IR = IR
14 structure CFG = IR.CFG
15 structure Util = IR.Util
16 structure G = Graph
17 structure A = Array
18 structure PQ = PriorityQueue
19 structure DA = DynArray
20
21 fun error msg = MLRiscErrorMsg.error("RegionBuilder",msg)
22
23 val view_IR = MLRiscControl.getFlag "view-IR"
24
25 val i2s = Int.toString
26
27 fun regionBuilder {maxBlocks, maxInstrs, sideEntries, minFreqRatio,
28 traceOnly, internalBackEdges, insertDummyBlocks
29 } (IR as G.GRAPH cfg) schedule =
30 let val N = #capacity cfg ()
31 val G.GRAPH loop = IR.loop IR
32
33 (* Note: tables must be dynamic because the cfg may be changed
34 * while scheduling is being performed
35 *)
36 val processed = DA.array(N, false)
37 fun isProcessed i = DA.sub(processed, i)
38 fun markAsProcessed i = DA.update(processed, i, true)
39 val blockIdTbl = DA.array(N, 0)
40
41 (* A queue of all the blocks ranked by priority
42 * Give loop headers extra priority
43 *)
44 fun freqOf(CFG.BLOCK{freq,...}) = !freq
45 fun highestFreqFirst((i,i'),(j,j')) =
46 let val f_i = freqOf i'
47 val f_j = freqOf j'
48 in if f_i = f_j then #has_node loop i
49 else f_i > f_j
50 end
51 val seeds = PQ.fromList highestFreqFirst (#nodes cfg ())
52
53 (* Initialization *)
54 fun initialization() =
55 (app markAsProcessed (#entries cfg ());
56 app markAsProcessed (#exits cfg ())
57 )
58
59 (* Locate an unprocessed seed block; raises exception if everything
60 * is done.
61 *)
62 fun newSeed() =
63 let val (i,i') = PQ.deleteMin seeds
64 in if isProcessed i then newSeed()
65 else if freqOf i' = 0 then raise PQ.EmptyPriorityQueue
66 else (i,i')
67 end
68
69 (* Grow a region according to the various parameters *)
70 fun grow(seed as (s,s')) =
71 let val freq = real(freqOf s')
72 val minFreq = freq * minFreqRatio
73
74 (* Remove non candidates *)
75 fun prune(j,j') = isProcessed j orelse real(freqOf j') < minFreq
76
77 fun pruneEdge(w) = real(!w) < minFreq
78
79 fun followSucc([], blocks) = blocks
80 | followSucc((_,j,CFG.EDGE{w,...})::es, blocks) =
81 let val j' = #node_info cfg j
82 in if pruneEdge w orelse prune(j,j') then followSucc(es, blocks)
83 else followSucc(es, (j,j')::blocks)
84 end
85
86 fun followPred([], blocks) = blocks
87 | followPred((j,_,CFG.EDGE{w,...})::es, blocks) =
88 let val j' = #node_info cfg j
89 in if pruneEdge w orelse prune(j,j') then followPred(es, blocks)
90 else followPred(es, (j,j')::blocks)
91 end
92
93
94 val queue = PQ.fromList highestFreqFirst [seed]
95 val enqueue = PQ.insert queue
96
97 fun chooseBest [] = []
98 | chooseBest ((j,j')::rest) =
99 let val w = freqOf j'
100 fun find([],j,j',w) = [(j,j')]
101 | find((k,k')::rest, j, j', w) =
102 let val w' = freqOf k'
103 in if w' > w then find(rest, k, k', w')
104 else find(rest, j, j', w)
105 end
106 in find(rest, j, j', w) end
107
108 fun add([], blocks, blockCount) = (blocks, blockCount)
109 | add((j,j')::rest, blocks, blockCount) =
110 if isProcessed j then add(rest, blocks, blockCount)
111 else (markAsProcessed j;
112 enqueue (j,j');
113 add(rest, j::blocks, blockCount+1)
114 )
115
116 (* Find the region using best first search *)
117 fun collect(front, back, blockCount) =
118 if PQ.isEmpty queue orelse blockCount >= maxBlocks then
119 front @ rev back
120 else
121 let val node as (j,j') = PQ.deleteMin queue
122 val succs = followSucc(#out_edges cfg j, [])
123 val succs = if traceOnly then chooseBest succs else succs
124 val (back, blockCount) = add(succs, back, blockCount)
125 (* val preds = followPred(#in_edges cfg j, [])
126 val preds = if traceOnly then chooseBest preds else preds
127 val (front, blockCount) = add(preds, front, blockCount) *)
128 in collect(front, back, blockCount)
129 end
130
131 val _ = markAsProcessed s (* mark the seed block as processed *)
132 val blocks = collect([s], [], 1)
133 (* The blocks collected are not in linear order *)
134 in blocks
135 end
136
137 (* Create a new subgraph from the blocks *)
138 fun makeSubgraph blocks =
139 if traceOnly then TraceView.trace_view blocks IR
140 else AcyclicSubgraphView.acyclic_view blocks IR
141
142 (*
143 * Perform tail duplication if no side entries are allowed
144 * BUG: make sure liveness information is kept up-to-date! XXX
145 *)
146 fun tailDuplication(root, subgraph) =
147 let val {nodes, edges} = Util.tailDuplicate IR
148 {subgraph=subgraph, root=root}
149 val ins = PQ.insert seeds
150 fun newNode (b,b') = (ins(b,b'); DA.update(blockIdTbl, b, 0))
151 in (* add new nodes created as a consequence of tail duplication
152 * onto the queue so that they will be properly processed later.
153 *)
154 app newNode nodes
155 end
156
157
158 (* Create a new region *)
159 fun createRegion() =
160 let val seed = newSeed()
161 val blocks = grow seed
162 val subgraph = makeSubgraph blocks;
163 in if sideEntries then () else tailDuplication(hd blocks, subgraph);
164 subgraph
165 end
166
167 (* Number of instructions *)
168 fun numberOfInstructions(G.GRAPH cfg) =
169 let val size = ref 0
170 in #forall_nodes cfg (fn (_,CFG.BLOCK{insns, ...}) =>
171 size := !size + length(!insns));
172 !size
173 end
174
175 fun sizeOf(G.GRAPH cfg) = #order cfg ()
176
177 (* Main loop *)
178 fun main() =
179 let val region as G.GRAPH R = createRegion()
180 val size = sizeOf region
181 in if size <= 1 then ()
182 else
183 let val numberOfInstructions = numberOfInstructions region
184 in if numberOfInstructions <= 2 then ()
185 else
186 let val _ =
187 (print("REGION["^i2s(#order R ())^"] ");
188 app (fn (X,_) => print(i2s X^" ")) (#nodes R ());
189 print "\n")
190
191 in if !view_IR then IR.viewSubgraph IR region else ();
192 schedule{ir=IR, region=region,
193 blockIdTbl=DA.baseArray blockIdTbl,
194 numberOfInstructions=numberOfInstructions};
195 if !view_IR then IR.viewSubgraph IR region else ()
196 end
197 end;
198 main()
199 end
200
201 in initialization();
202 main() handle PQ.EmptyPriorityQueue => ()
203 end
204
205 end