-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregister_allocation.py
More file actions
328 lines (249 loc) · 9.77 KB
/
register_allocation.py
File metadata and controls
328 lines (249 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import copy
from random import choice
from typing import List, Set, Collection, Dict, Optional, Tuple
import matplotlib.pyplot as plt
import networkx as nx
class Dec:
def __init__(self, reg: str, dead: bool):
self.reg = reg
self.dead = dead
class Use:
def __init__(self, reg: str, dead: bool):
self.reg = reg
self.dead = dead
class Instruction:
def __init__(self, opcode: str, dec: List[Dec], use: List[Use], frequency=1):
self.opcode = opcode
self.dec = dec
self.use = use
self.frequency = frequency
class IntermediateLanguage:
"""
The intermediate language. Maintains an ordered sequence of instructions.
"""
def __init__(self, instructions: List[Instruction]):
self.instructions = instructions
def overwrite_il(self, new_instructions: List[Instruction]):
self.instructions = new_instructions
def rewrite_il(self, f: Dict) -> None:
self.instructions = [Instruction(
instruction.opcode,
[Dec(f.get(dec.reg, dec.reg), dec.dead) for dec in instruction.dec],
[Use(f.get(use.reg, use.reg), use.dead) for use in instruction.use]
) for instruction in self.instructions]
def registers(self) -> Set[str]:
reg = set()
for instruction in self.instructions:
for dec in instruction.dec:
reg.add(dec.reg)
for use in instruction.use:
reg.add(use.reg)
return reg
class Graph:
"""
The register interference graph.
"""
def __init__(self):
self._adjacency_list = {}
def __copy__(self):
cls = self.__class__
new_graph = self.__new__(cls)
new_graph._adjacency_list = copy.deepcopy(self._adjacency_list)
return new_graph
def add_edge(self, x, y):
"""
Add an edge to the graph.
The interference graph is undirected so add_edge('a', 'b') and add_edge('b', 'a') have the same effect.
"""
# Add y to x
x_list = self._adjacency_list.get(x, [])
if y not in x_list:
x_list.append(y)
self._adjacency_list[x] = x_list
# Add x to y
y_list = self._adjacency_list.get(y, [])
if x not in y_list:
y_list.append(x)
self._adjacency_list[y] = y_list
def contains_edge(self, x, y):
return y in self._adjacency_list.get(x, [])
def remove_node(self, node):
if node in self._adjacency_list:
self._adjacency_list.pop(node)
for key in self._adjacency_list.keys():
if node in self._adjacency_list.get(key):
self._adjacency_list.get(key).remove(node)
def rename_node(self, from_label, to_label):
from_list = self._adjacency_list.pop(from_label, [])
to_list = self._adjacency_list.get(to_label, [])
self._adjacency_list[to_label] = list(set(from_list + to_list))
for key in self._adjacency_list.keys():
self._adjacency_list[key] = list(set(
[to_label if value == from_label else value for value in self._adjacency_list[key]]
))
def neighbors(self, x):
return self._adjacency_list.get(x, [])
def plot(self, coloring, title):
G = nx.Graph()
# Sorting to get repeatable graphs
nodes = sorted(self._adjacency_list.keys())
ordered_coloring = [coloring.get(node, 'grey') for node in nodes]
G.add_nodes_from(nodes)
for key in self._adjacency_list.keys():
for value in self._adjacency_list[key]:
G.add_edge(key, value)
plt.title(title)
nx.draw(G, pos=nx.circular_layout(G), node_color=ordered_coloring, with_labels=True, font_weight='bold')
plt.show()
def run(il: IntermediateLanguage, colors: List[str]) -> Tuple[Optional[Graph], Optional[Dict[str, str]]]:
graph, coloring = color_il(il, colors)
if coloring is None:
graph.plot({}, 'Initial')
cost = estimate_spill_costs(il)
spilled = decide_spills(il, graph, colors, cost)
insert_spill_code(il, spilled)
graph, coloring = color_il(il, colors)
graph.plot({}, 'After Spilling')
graph.plot(coloring, 'Colored')
return graph, coloring
def color_il(il: IntermediateLanguage, colors: List[str]) -> Tuple[Optional[Graph], Optional[Dict[str, str]]]:
graph = build_graph(il)
graph.plot({}, 'Initial')
coalesce_nodes(il, graph)
# graph.plot({}, 'After Coalescing')
coloring = color_graph(graph, il.registers(), colors)
if coloring is None:
return graph, None
# graph.plot(coloring, 'Colored')
return graph, coloring
def build_graph(il: IntermediateLanguage) -> Graph:
graph = Graph()
liveness = None
for instruction in il.instructions:
if instruction.opcode == 'bb':
liveness = {}
for dec in [dec for dec in instruction.dec if not dec.dead]:
liveness[dec.reg] = liveness.get(dec.reg, 0) + 1
else:
for use in [use for use in instruction.use if use.dead]:
liveness[use.reg] -= 1
if liveness[use.reg] == 0:
liveness.pop(use.reg)
for dec in instruction.dec:
for key, value in liveness.items():
if key != dec.reg:
graph.add_edge(dec.reg, key)
if not dec.dead:
liveness[dec.reg] = liveness.get(dec.reg, 0) + 1
return graph
def is_unnecessary_copy(instruction: Instruction, graph: Graph) -> bool:
if len(instruction.dec) == 0 or len(instruction.use) == 0:
return False
source = instruction.dec[0].reg
target = instruction.use[0].reg
return (instruction.opcode == 'copy' and
source != target and
not graph.contains_edge(source, target))
def coalesce_nodes(il: IntermediateLanguage, graph: Graph) -> None:
modified = True
while modified:
found = next((instruction for instruction in il.instructions if is_unnecessary_copy(instruction, graph)), None)
if found is not None:
source = found.dec[0].reg
target = found.use[0].reg
f = {source: target}
graph.rename_node(source, target)
il.rewrite_il(f)
else:
modified = False
def color_graph(g: Graph, n: Collection[str], colors: List[str]) -> Optional[Dict[str, str]]:
if len(n) == 0:
return {}
node = next((node for node in n if len(g.neighbors(node)) < len(colors)), None)
if node is None:
return None
g_copy = copy.copy(g)
g_copy.remove_node(node)
coloring = color_graph(g_copy, [n for n in n if n != node], colors)
if coloring is None:
return None
neighbor_colors = [coloring[neighbor] for neighbor in g.neighbors(node)]
coloring[node] = choice([color for color in colors if color not in neighbor_colors])
return coloring
def estimate_spill_costs(il: IntermediateLanguage) -> Dict[str, float]:
"""
:param il: The intermediate language to compute spill costs on.
:return: The estimated cost of spilling each symbolic register
"""
cost = {}
frequency = None
for instruction in il.instructions:
if instruction.opcode == 'bb':
frequency = instruction.frequency
else:
registers = set()
for dec in instruction.dec:
registers.add(dec.reg)
for use in instruction.use:
registers.add(use.reg)
for reg in registers:
cost[reg] = cost.get(reg, 0) + frequency
return cost
def decide_spills(il: IntermediateLanguage, graph: Graph, colors: List[str], cost: Dict[str, float]) -> Set[str]:
"""
Determines which symbolic registers to spill.
:param il: The intermediate language
:param graph: The interference graph
:param colors: Possible colors
:param cost: Estimated cost of spilling each symbolic register
:return: The set of spilled symbolic registers
"""
spilled = set()
g = copy.copy(graph)
n = il.registers()
while len(n) != 0:
node = next((node for node in n if len(g.neighbors(node)) < len(colors)), None)
if node is None:
node = next(x for x in n if cost[x] == min([cost[y] for y in n]))
spilled.add(node)
g.remove_node(node)
n.remove(node)
return spilled
def insert_spill_code(il: IntermediateLanguage, spilled: Set[str]) -> None:
new_il = []
for instruction in il.instructions:
if instruction.opcode == 'bb':
new_il.append(
Instruction(
'bb',
[dec for dec in instruction.dec if dec.reg not in spilled],
instruction.use.copy()
)
)
else:
before = []
after = []
newdef = []
newuse = []
for use in instruction.use:
if use.reg in spilled:
newuse.append(Use(use.reg, True))
before.append(Instruction(
'reload',
[Dec(use.reg, False)],
[]
))
else:
newuse.append(Use(use.reg, use.dead))
for dec in instruction.dec:
if dec.reg in spilled:
newdef.append(Dec(dec.reg, False))
after.append(Instruction(
'spill',
[],
[Use(dec.reg, True)]
))
else:
newdef.append(Dec(dec.reg, dec.dead))
new_il.extend(before + [Instruction(instruction.opcode, newdef, newuse)] + after)
il.overwrite_il(new_il)