Debian Codegraph @

rpython/jit/codewriter/test/test_liveness.py

raw symbols SCIP
1 from rpython.jit.codewriter.liveness import compute_liveness, OFFSET_SIZE
2 from rpython.jit.codewriter.liveness import encode_offset, decode_offset
3 from rpython.jit.codewriter.liveness import encode_liveness, LivenessIterator
4 from rpython.jit.codewriter.format import unformat_assembler, assert_format
5
6 from hypothesis import strategies, given, example
7
8 class TestFlatten:
9
10 def liveness_test(self, input, output):
11 ssarepr = unformat_assembler(input)
12 compute_liveness(ssarepr)
13 assert_format(ssarepr, output)
14
15 def test_simple_no_live(self):
16 self.liveness_test("""
17 -live-
18 int_add %i0, $10 -> %i1
19 -live-
20 """, """
21 -live- %i0
22 int_add %i0, $10 -> %i1
23 -live-
24 """)
25
26 def test_simple(self):
27 self.liveness_test("""
28 -live-
29 int_add %i0, $10 -> %i1
30 -live-
31 int_add %i0, $3 -> %i2
32 -live-
33 int_mul %i1, %i2 -> %i3
34 -live-
35 int_add %i0, $6 -> %i4
36 -live-
37 int_mul %i3, %i4 -> %i5
38 -live-
39 int_return %i5
40 """, """
41 -live- %i0
42 int_add %i0, $10 -> %i1
43 -live- %i0, %i1
44 int_add %i0, $3 -> %i2
45 -live- %i0, %i1, %i2
46 int_mul %i1, %i2 -> %i3
47 -live- %i0, %i3
48 int_add %i0, $6 -> %i4
49 -live- %i3, %i4
50 int_mul %i3, %i4 -> %i5
51 -live- %i5
52 int_return %i5
53 """)
54
55 def test_one_path(self):
56 self.liveness_test("""
57 int_add %i0, $5 -> %i2
58 -live-
59 int_is_true %i2 -> %i3
60 goto_if_not %i3, L1
61 int_copy %i0 -> %i4
62 int_add %i4, $1 -> %i5
63 -live-
64 int_return %i5
65 ---
66 L1:
67 int_copy %i1 -> %i6
68 int_add %i6, $2 -> %i7
69 -live-
70 int_return %i7
71 """, """
72 int_add %i0, $5 -> %i2
73 -live- %i0, %i1, %i2
74 int_is_true %i2 -> %i3
75 goto_if_not %i3, L1
76 int_copy %i0 -> %i4
77 int_add %i4, $1 -> %i5
78 -live- %i5
79 int_return %i5
80 ---
81 L1:
82 int_copy %i1 -> %i6
83 int_add %i6, $2 -> %i7
84 -live- %i7
85 int_return %i7
86 """)
87
88 def test_other_path(self):
89 self.liveness_test("""
90 int_add %i0, $5 -> %i2
91 -live- %i2
92 int_is_true %i2 -> %i3
93 goto_if_not %i3, L1
94 int_copy %i0 -> %i4
95 int_copy %i1 -> %i5
96 int_add %i4, %i5 -> %i6
97 -live- %i6
98 int_return %i6
99 ---
100 L1:
101 int_copy %i0 -> %i7
102 int_add %i7, $2 -> %i8
103 -live- %i8
104 int_return %i8
105 """, """
106 int_add %i0, $5 -> %i2
107 -live- %i0, %i1, %i2
108 int_is_true %i2 -> %i3
109 goto_if_not %i3, L1
110 int_copy %i0 -> %i4
111 int_copy %i1 -> %i5
112 int_add %i4, %i5 -> %i6
113 -live- %i6
114 int_return %i6
115 ---
116 L1:
117 int_copy %i0 -> %i7
118 int_add %i7, $2 -> %i8
119 -live- %i8
120 int_return %i8
121 """)
122
123 def test_no_path(self):
124 self.liveness_test("""
125 int_add %i0, %i1 -> %i2
126 -live- %i2
127 int_is_true %i2 -> %i3
128 goto_if_not %i3, L1
129 int_copy %i0 -> %i4
130 int_add %i4, $5 -> %i5
131 -live- %i5
132 int_return %i5
133 ---
134 L1:
135 int_copy %i0 -> %i6
136 int_add %i6, $2 -> %i7
137 -live- %i7
138 int_return %i7
139 """, """
140 int_add %i0, %i1 -> %i2
141 -live- %i0, %i2
142 int_is_true %i2 -> %i3
143 goto_if_not %i3, L1
144 int_copy %i0 -> %i4
145 int_add %i4, $5 -> %i5
146 -live- %i5
147 int_return %i5
148 ---
149 L1:
150 int_copy %i0 -> %i6
151 int_add %i6, $2 -> %i7
152 -live- %i7
153 int_return %i7
154 """)
155
156 def test_list_of_kind(self):
157 self.liveness_test("""
158 -live-
159 foobar I[$25, %i0]
160 """, """
161 -live- %i0
162 foobar I[$25, %i0]
163 """)
164
165 def test_switch(self):
166 self.liveness_test("""
167 goto_maybe L1
168 -live-
169 fooswitch <SwitchDictDescr 4:L2, 5:L3>
170 ---
171 L3:
172 int_return %i7
173 ---
174 L1:
175 int_return %i4
176 ---
177 L2:
178 int_return %i3
179 """, """
180 goto_maybe L1
181 -live- %i3, %i7
182 fooswitch <SwitchDictDescr 4:L2, 5:L3>
183 ---
184 L3:
185 int_return %i7
186 ---
187 L1:
188 int_return %i4
189 ---
190 L2:
191 int_return %i3
192 """)
193
194 def test_already_some(self):
195 self.liveness_test("""
196 foo %i0, %i1, %i2
197 -live- %i0, $52, %i2, %i0
198 bar %i3, %i4, %i5
199 """, """
200 foo %i0, %i1, %i2
201 -live- %i0, %i2, %i3, %i4, %i5
202 bar %i3, %i4, %i5
203 """)
204
205 def test_keepalive(self):
206 self.liveness_test("""
207 -live-
208 build $1 -> %i6
209 -live-
210 foo %i0, %i1 -> %i2
211 -live-
212 bar %i3, %i2 -> %i5
213 -live- %i6
214 """, """
215 -live- %i0, %i1, %i3
216 build $1 -> %i6
217 -live- %i0, %i1, %i3, %i6
218 foo %i0, %i1 -> %i2
219 -live- %i2, %i3, %i6
220 bar %i3, %i2 -> %i5
221 -live- %i6
222 """)
223
224 def test_live_with_label(self):
225 self.liveness_test("""
226 -live- L1
227 foo %i0
228 ---
229 L1:
230 bar %i1
231 """, """
232 -live- %i0, %i1, L1
233 foo %i0
234 ---
235 L1:
236 bar %i1
237 """)
238
239 def test_live_duplicate(self):
240 self.liveness_test("""
241 -live- L1
242 -live- %i12
243 foo %i0
244 ---
245 L1:
246 bar %i1
247 """, """
248 -live- %i0, %i1, %i12, L1
249 foo %i0
250 ---
251 L1:
252 bar %i1
253 """)
254 self.liveness_test("""
255 goto_if_not %i3, L1
256 -live-
257 L2:
258 -live-
259 L3:
260 -live- %i19
261 int_return %i0
262 ---
263 L1:
264 int_add %i0, $1 -> %i0
265 goto L2
266 """, """
267 goto_if_not %i3, L1
268 L2:
269 -live- %i0, %i19
270 int_return %i0
271 ---
272 L1:
273 int_add %i0, $1 -> %i0
274 goto L2
275 """)
276
277 class TestEncodeDecode(object):
278 @given(strategies.integers(min_value=0, max_value=2**(8 * OFFSET_SIZE)-1), strategies.binary(), strategies.binary())
279 def test_encode_decode_offset(self, x, prefix, postfix):
280 l = []
281 encode_offset(x, l)
282 data = prefix + "".join(l) + postfix
283 assert decode_offset(data, len(prefix)) == x
284
285 def test_liveness_encoding(self):
286 res = encode_liveness({})
287 assert res == ''
288 res = encode_liveness({'\x00'})
289 assert res == '\x01'
290 res = encode_liveness({'\x01'})
291 assert res == '\x02'
292 res = encode_liveness({'\x02'})
293 assert res == '\x04'
294 res = encode_liveness({'\x03'})
295 assert res == '\x08'
296 res = encode_liveness({'\x01', '\x02'})
297 assert res == '\x06'
298 res = encode_liveness({'\x08'})
299 assert res == '\x00\x01'
300 res = encode_liveness({'\x01', '\x09'})
301 assert res == '\x02\x02'
302
303 def test_liveness_iterator(self):
304 liveness = '\x04\x01'
305 l = list(LivenessIterator(0, 2, liveness))
306 assert l == [2, 8]
307
308 @example({0, 9})
309 @given(strategies.sets(strategies.integers(min_value=0, max_value=255), min_size=1))
310 def test_encode_decode_liveness(self, live):
311 live_chars = [chr(i) for i in live]
312 res = encode_liveness(live_chars)
313 l = list(LivenessIterator(0, len(live), res))
314 s = set(l)
315 assert len(l) == len(s)
316 assert s == live
317
318 @given(strategies.sets(strategies.integers(min_value=0, max_value=255)),
319 strategies.sets(strategies.integers(min_value=0, max_value=255)),
320 strategies.sets(strategies.integers(min_value=0, max_value=255)))
321 def test_encode_decode_liveness_3(self, live_i, live_r, live_f):
322 live_i_chars = [chr(i) for i in live_i]
323 live_r_chars = [chr(i) for i in live_r]
324 live_f_chars = [chr(i) for i in live_f]
325 all_liveness = encode_liveness(live_i_chars) + encode_liveness(live_r_chars) + encode_liveness(live_f_chars)
326 offset = 0
327 length_i = len(live_i)
328 length_r = len(live_r)
329 length_f = len(live_f)
330 if length_i:
331 it = LivenessIterator(offset, length_i, all_liveness)
332 s = set(it)
333 assert s == live_i
334 offset = it.offset
335 if length_r:
336 it = LivenessIterator(offset, length_r, all_liveness)
337 s = set(it)
338 assert s == live_r
339 offset = it.offset
340 if length_f:
341 it = LivenessIterator(offset, length_f, all_liveness)
342 s = set(it)
343 assert s == live_f
344 offset = it.offset
345