Debian Codegraph @

rpython/jit/codewriter/test/test_regalloc.py

raw symbols SCIP
1 import py, sys
2 from rpython.jit.codewriter import support
3 from rpython.jit.codewriter.regalloc import perform_register_allocation
4 from rpython.jit.codewriter.flatten import flatten_graph, ListOfKind
5 from rpython.jit.codewriter.format import assert_format
6 from rpython.jit.metainterp.history import AbstractDescr
7 from rpython.flowspace.model import Variable, Constant, SpaceOperation
8 from rpython.flowspace.model import FunctionGraph, Block, Link
9 from rpython.flowspace.model import c_last_exception
10 from rpython.rtyper.lltypesystem import lltype, llmemory
11 from rpython.rtyper import rclass
12 from rpython.rlib.rarithmetic import ovfcheck
13
14
15 class TestRegAlloc:
16
17 def make_graphs(self, func, values):
18 self.rtyper = support.annotate(func, values)
19 return self.rtyper.annotator.translator.graphs
20
21 def check_assembler(self, graph, expected, transform=False,
22 callcontrol=None):
23 # 'transform' can be False only for simple graphs. More complex
24 # graphs must first be transformed by jtransform.py before they can be
25 # subjected to register allocation and flattening.
26 if transform:
27 from rpython.jit.codewriter.jtransform import transform_graph
28 transform_graph(graph, callcontrol=callcontrol)
29 regalloc = perform_register_allocation(graph, 'int')
30 regalloc2 = perform_register_allocation(graph, 'ref')
31 ssarepr = flatten_graph(graph, {'int': regalloc,
32 'ref': regalloc2})
33 assert_format(ssarepr, expected)
34
35 def test_regalloc_simple(self):
36 def f(a, b):
37 return a + b
38 graph = self.make_graphs(f, [5, 6])[0]
39 regalloc = perform_register_allocation(graph, 'int')
40 va, vb = graph.startblock.inputargs
41 vc = graph.startblock.operations[0].result
42 assert regalloc.getcolor(va) == 0
43 assert regalloc.getcolor(vb) == 1
44 assert regalloc.getcolor(vc) == 0
45
46 def test_regalloc_void(self):
47 def f(a, b):
48 while a > 0:
49 b += a
50 a -= 1
51 return b
52 graph = self.make_graphs(f, [5, 6])[0]
53 regalloc = perform_register_allocation(graph, 'float')
54 # assert did not crash
55
56 def test_regalloc_loop(self):
57 def f(a, b):
58 while a > 0:
59 b += a
60 a -= 1
61 return b
62 graph = self.make_graphs(f, [5, 6])[0]
63 self.check_assembler(graph, """
64 L1:
65 int_gt %i0, $0 -> %i2
66 -live-
67 goto_if_not %i2, L2
68 int_add %i1, %i0 -> %i1
69 int_sub %i0, $1 -> %i0
70 goto L1
71 ---
72 L2:
73 int_return %i1
74 """)
75
76 def test_regalloc_loop_swap(self):
77 def f(a, b):
78 while a > 0:
79 a, b = b, a
80 return b
81 graph = self.make_graphs(f, [5, 6])[0]
82 self.check_assembler(graph, """
83 L1:
84 int_gt %i0, $0 -> %i2
85 -live-
86 goto_if_not %i2, L2
87 int_push %i1
88 int_copy %i0 -> %i1
89 int_pop -> %i0
90 goto L1
91 ---
92 L2:
93 int_return %i1
94 """)
95
96 def test_regalloc_loop_constant(self):
97 def f(a, b):
98 while a > 0:
99 a, b = b, 2
100 return b
101 graph = self.make_graphs(f, [5, 6])[0]
102 self.check_assembler(graph, """
103 L1:
104 int_gt %i0, $0 -> %i0
105 -live-
106 goto_if_not %i0, L2
107 int_copy %i1 -> %i0
108 int_copy $2 -> %i1
109 goto L1
110 ---
111 L2:
112 int_return %i1
113 """)
114
115 def test_regalloc_cycle(self):
116 def f(a, b, c):
117 while a > 0:
118 a, b, c = b, c, a
119 return b
120 graph = self.make_graphs(f, [5, 6, 7])[0]
121 self.check_assembler(graph, """
122 L1:
123 int_gt %i0, $0 -> %i3
124 -live-
125 goto_if_not %i3, L2
126 int_push %i1
127 int_copy %i2 -> %i1
128 int_copy %i0 -> %i2
129 int_pop -> %i0
130 goto L1
131 ---
132 L2:
133 int_return %i1
134 """)
135
136 def test_regalloc_same_as_var(self):
137 def f(a, b, c):
138 while a > 0:
139 b = c
140 return b
141 graph = self.make_graphs(f, [5, 6, 7])[0]
142 self.check_assembler(graph, """
143 L1:
144 int_gt %i0, $0 -> %i3
145 -live-
146 goto_if_not %i3, L2
147 int_copy %i2 -> %i1
148 goto L1
149 ---
150 L2:
151 int_return %i1
152 """)
153
154 def test_regalloc_call(self):
155 v1 = Variable(); v1.concretetype = lltype.Signed
156 v2 = Variable(); v2.concretetype = lltype.Signed
157 v3 = Variable(); v3.concretetype = lltype.Signed
158 v4 = Variable(); v4.concretetype = lltype.Signed
159 block = Block([v1])
160 block.operations = [
161 SpaceOperation('int_add', [v1, Constant(1, lltype.Signed)], v2),
162 SpaceOperation('rescall', [ListOfKind('int', [v1, v2])], v3),
163 ]
164 graph = FunctionGraph('f', block, v4)
165 block.closeblock(Link([v3], graph.returnblock))
166 #
167 self.check_assembler(graph, """
168 int_add %i0, $1 -> %i1
169 rescall I[%i0, %i1] -> %i0
170 int_return %i0
171 """)
172
173 def test_regalloc_exitswitch_2(self):
174 v1 = Variable(); v1.concretetype = rclass.CLASSTYPE
175 v2 = Variable(); v2.concretetype = rclass.CLASSTYPE
176 v3 = Variable(); v3.concretetype = rclass.CLASSTYPE
177 v4 = Variable(); v4.concretetype = rclass.CLASSTYPE
178 block = Block([])
179 block.operations = [
180 SpaceOperation('res_call', [], v1),
181 SpaceOperation('-live-', [], None),
182 ]
183 graph = FunctionGraph('f', block, v4)
184 exclink = Link([v2], graph.returnblock)
185 exclink.llexitcase = 123 # normally an exception class
186 exclink.last_exception = v2
187 exclink.last_exc_value = "unused"
188 block.exitswitch = c_last_exception
189 block.closeblock(Link([v1], graph.returnblock),
190 exclink)
191 #
192 self.check_assembler(graph, """
193 res_call -> %i0
194 -live-
195 catch_exception L1
196 int_return %i0
197 ---
198 L1:
199 goto_if_exception_mismatch $123, L2
200 last_exception -> %i0
201 int_return %i0
202 ---
203 L2:
204 reraise
205 """)
206
207 def test_regalloc_lists(self):
208 v1 = Variable(); v1.concretetype = lltype.Signed
209 v2 = Variable(); v2.concretetype = lltype.Signed
210 v3 = Variable(); v3.concretetype = lltype.Signed
211 v4 = Variable(); v4.concretetype = lltype.Signed
212 v5 = Variable(); v5.concretetype = lltype.Signed
213 block = Block([v1])
214 block.operations = [
215 SpaceOperation('int_add', [v1, Constant(1, lltype.Signed)], v2),
216 SpaceOperation('rescall', [ListOfKind('int', [v1, v2])], v5),
217 SpaceOperation('rescall', [ListOfKind('int', [v1, v2])], v3),
218 ]
219 graph = FunctionGraph('f', block, v4)
220 block.closeblock(Link([v3], graph.returnblock))
221 #
222 self.check_assembler(graph, """
223 int_add %i0, $1 -> %i1
224 rescall I[%i0, %i1] -> %i2
225 rescall I[%i0, %i1] -> %i0
226 int_return %i0
227 """)
228
229 def test_regalloc_bug_1(self):
230 def _ll_2_int_lshift_ovf(x, y):
231 result = x << y
232 if (result >> y) != x:
233 raise OverflowError
234 return result
235 graph = self.make_graphs(_ll_2_int_lshift_ovf, [5, 6])[0]
236 self.check_assembler(graph, """
237 int_lshift %i0, %i1 -> %i2
238 int_rshift %i2, %i1 -> %i1
239 -live-
240 goto_if_not_int_ne %i1, %i0, L1
241 raise $<* struct object>
242 ---
243 L1:
244 int_return %i2
245 """, transform=True)
246
247 def test_regalloc_bug_2(self):
248 class FakeDescr(AbstractDescr):
249 def __repr__(self):
250 return '<Descr>'
251 class FakeCallControl:
252 def guess_call_kind(self, op):
253 return 'residual'
254 def getcalldescr(self, op, **kwds):
255 return FakeDescr()
256 def calldescr_canraise(self, calldescr):
257 return True
258 class FooError(Exception):
259 def __init__(self, num):
260 self.num = num
261 def g(n):
262 if n > 100:
263 raise FooError(n)
264 return lltype.nullptr(llmemory.GCREF.TO)
265 def foo(e):
266 print "hello"
267 return e
268 def bar(e):
269 print "world"
270 return e
271 def f(n, kref):
272 kref2 = bar(kref)
273 try:
274 return g(n)
275 except FooError as e:
276 if foo(e):
277 return kref
278 else:
279 return kref2
280 graph = self.make_graphs(f, [5, lltype.nullptr(llmemory.GCREF.TO)])[0]
281 # this used to produce bogus code, containing these two
282 # lines in the following broken order:
283 # last_exc_value -> %r0
284 # ref_copy %r0 -> %r1 -- but expect to read the old value of %r0!
285 self.check_assembler(graph, """
286 residual_call_r_r $<* fn bar>, R[%r0], <Descr> -> %r1
287 -live-
288 residual_call_ir_r $<* fn g>, I[%i0], R[], <Descr> -> %r1
289 -live-
290 catch_exception L1
291 ref_return %r1
292 ---
293 L1:
294 goto_if_exception_mismatch $<* struct object_vtable>, L2
295 ref_copy %r0 -> %r1
296 last_exc_value -> %r0
297 residual_call_r_r $<* fn foo>, R[%r0], <Descr> -> %r0
298 -live-
299 ref_return %r1
300 ---
301 L2:
302 reraise
303 """, transform=True, callcontrol=FakeCallControl())