Debian Codegraph @

rpython/jit/metainterp/test/test_heapcache.py

raw symbols SCIP
1 import py
2 from rpython.jit.metainterp.heapcache import HeapCache
3 from rpython.jit.metainterp.resoperation import rop, InputArgInt
4 from rpython.jit.metainterp.history import ConstInt, ConstPtr, BasicFailDescr
5 from rpython.jit.metainterp.history import IntFrontendOp, RefFrontendOp as OrigRefFrontendOp
6 from rpython.rtyper.lltypesystem import llmemory, rffi
7
8 def RefFrontendOp(pos):
9 return OrigRefFrontendOp(pos, OrigRefFrontendOp._resref)
10
11 descr1 = object()
12 descr2 = object()
13 descr3 = object()
14
15 index1 = ConstInt(0)
16 index2 = ConstInt(1)
17
18
19 class FakeEffectinfo(object):
20 EF_ELIDABLE_CANNOT_RAISE = 0 #elidable function (and cannot raise)
21 EF_LOOPINVARIANT = 1 #special: call it only once per loop
22 EF_CANNOT_RAISE = 2 #a function which cannot raise
23 EF_ELIDABLE_OR_MEMORYERROR = 3
24 EF_ELIDABLE_CAN_RAISE = 4 #elidable function (but can raise)
25 EF_CAN_RAISE = 5 #normal function (can raise)
26 EF_FORCES_VIRTUAL_OR_VIRTUALIZABLE = 6 #can raise and force virtualizables
27 EF_RANDOM_EFFECTS = 7 #can do whatever
28
29 OS_ARRAYCOPY = 0
30 OS_ARRAYMOVE = 9
31
32 def __init__(self, extraeffect, oopspecindex, write_descrs_fields, write_descrs_arrays):
33 self.extraeffect = extraeffect
34 self.oopspecindex = oopspecindex
35 self._write_descrs_fields = write_descrs_fields
36 self._write_descrs_arrays = write_descrs_arrays
37 if len(write_descrs_arrays) == 1:
38 [self.single_write_descr_array] = write_descrs_arrays
39 else:
40 self.single_write_descr_array = None
41
42 def has_random_effects(self):
43 return self.extraeffect == self.EF_RANDOM_EFFECTS
44
45 class FakeCallDescr(object):
46 def __init__(self, extraeffect, oopspecindex=None, write_descrs_fields=[], write_descrs_arrays=[]):
47 self.extraeffect = extraeffect
48 self.oopspecindex = oopspecindex
49 self.__write_descrs_fields = write_descrs_fields
50 self.__write_descrs_arrays = write_descrs_arrays
51
52 def get_extra_info(self):
53 return FakeEffectinfo(
54 self.extraeffect, self.oopspecindex,
55 write_descrs_fields=self.__write_descrs_fields,
56 write_descrs_arrays=self.__write_descrs_arrays,
57 )
58
59 arraycopydescr1 = FakeCallDescr(FakeEffectinfo.EF_CANNOT_RAISE, FakeEffectinfo.OS_ARRAYCOPY, write_descrs_arrays=[descr1])
60
61
62 class TestHeapCache(object):
63 def test_known_class_box(self):
64 h = HeapCache()
65 box1 = RefFrontendOp(1)
66 box2 = RefFrontendOp(2)
67 assert not h.is_class_known(box1)
68 assert not h.is_class_known(box2)
69 h.class_now_known(box1)
70 assert h.is_class_known(box1)
71 assert not h.is_class_known(box2)
72
73 h.reset()
74 assert not h.is_class_known(box1)
75 assert not h.is_class_known(box2)
76
77 def test_known_nullity(self):
78 h = HeapCache()
79 box1 = RefFrontendOp(1)
80 box2 = RefFrontendOp(2)
81 assert not h.is_nullity_known(box1)
82 assert not h.is_nullity_known(box2)
83 h.nullity_now_known(box1)
84 assert h.is_nullity_known(box1)
85 assert not h.is_nullity_known(box2)
86
87 h.reset()
88 assert not h.is_nullity_known(box1)
89 assert not h.is_nullity_known(box2)
90
91 def test_known_nullity_more_cases(self):
92 h = HeapCache()
93 box1 = RefFrontendOp(1)
94 box2 = RefFrontendOp(2)
95 h.class_now_known(box1)
96 assert h.is_nullity_known(box1)
97
98 h.new(box2)
99 assert h.is_nullity_known(box2)
100
101 h.reset()
102 assert not h.is_nullity_known(box1)
103 assert not h.is_nullity_known(box2)
104
105 def test_nonstandard_virtualizable(self):
106 h = HeapCache()
107 box1 = RefFrontendOp(1)
108 box2 = RefFrontendOp(2)
109 assert not h.is_known_nonstandard_virtualizable(box1)
110 assert not h.is_known_nonstandard_virtualizable(box2)
111 h.nonstandard_virtualizables_now_known(box1)
112 assert h.is_known_nonstandard_virtualizable(box1)
113 assert not h.is_known_nonstandard_virtualizable(box2)
114
115 h.reset()
116 assert not h.is_known_nonstandard_virtualizable(box1)
117 assert not h.is_known_nonstandard_virtualizable(box2)
118
119 def test_nonstandard_virtualizable_const(self):
120 h = HeapCache()
121 # rare but not impossible situation for some interpreters: we have a
122 # *constant* nonstandard virtualizable
123 c_box = ConstPtr(ConstPtr.value)
124 h.nonstandard_virtualizables_now_known(c_box) # should not crash
125 assert not h.is_known_nonstandard_virtualizable(c_box)
126
127 def test_nonstandard_virtualizable_allocation(self):
128 h = HeapCache()
129 box1 = RefFrontendOp(1)
130 h.new(box1)
131 # we've seen the allocation, so it's not the virtualizable
132 assert h.is_known_nonstandard_virtualizable(box1)
133
134 h.reset()
135 assert not h.is_known_nonstandard_virtualizable(box1)
136
137 def test_heapcache_fields(self):
138 h = HeapCache()
139 box1 = RefFrontendOp(1)
140 box2 = RefFrontendOp(2)
141 box3 = RefFrontendOp(3)
142 assert h.getfield(box1, descr1) is None
143 assert h.getfield(box1, descr2) is None
144 h.setfield(box1, box2, descr1)
145 assert h.getfield(box1, descr1) is box2
146 assert h.getfield(box1, descr2) is None
147 h.setfield(box1, box3, descr2)
148 assert h.getfield(box1, descr1) is box2
149 assert h.getfield(box1, descr2) is box3
150 h.setfield(box1, box3, descr1)
151 assert h.getfield(box1, descr1) is box3
152 assert h.getfield(box1, descr2) is box3
153 h.setfield(box3, box1, descr1)
154 assert h.getfield(box3, descr1) is box1
155 assert h.getfield(box1, descr1) is None
156 assert h.getfield(box1, descr2) is box3
157
158 h.reset()
159 assert h.getfield(box1, descr1) is None
160 assert h.getfield(box1, descr2) is None
161 assert h.getfield(box3, descr1) is None
162
163 def test_heapcache_read_fields_multiple(self):
164 h = HeapCache()
165 box1 = RefFrontendOp(1)
166 box2 = RefFrontendOp(2)
167 box3 = RefFrontendOp(3)
168 box4 = RefFrontendOp(4)
169 h.getfield_now_known(box1, descr1, box2)
170 h.getfield_now_known(box3, descr1, box4)
171 assert h.getfield(box1, descr1) is box2
172 assert h.getfield(box1, descr2) is None
173 assert h.getfield(box3, descr1) is box4
174 assert h.getfield(box3, descr2) is None
175
176 h.reset()
177 assert h.getfield(box1, descr1) is None
178 assert h.getfield(box1, descr2) is None
179 assert h.getfield(box3, descr1) is None
180 assert h.getfield(box3, descr2) is None
181
182 def test_heapcache_write_fields_multiple(self):
183 h = HeapCache()
184 box1 = RefFrontendOp(1)
185 box2 = RefFrontendOp(2)
186 box3 = RefFrontendOp(3)
187 box4 = RefFrontendOp(4)
188 h.setfield(box1, box2, descr1)
189 assert h.getfield(box1, descr1) is box2
190 h.setfield(box3, box4, descr1)
191 assert h.getfield(box3, descr1) is box4
192 assert h.getfield(box1, descr1) is None # box1 and box3 can alias
193
194 h = HeapCache()
195 box1 = RefFrontendOp(1)
196 box2 = RefFrontendOp(2)
197 box3 = RefFrontendOp(3)
198 box4 = RefFrontendOp(4)
199 h.new(box1)
200 h.setfield(box1, box2, descr1)
201 assert h.getfield(box1, descr1) is box2
202 h.setfield(box3, box4, descr1)
203 assert h.getfield(box3, descr1) is box4
204 assert h.getfield(box1, descr1) is None # box1 and box3 can alias
205
206 h = HeapCache()
207 box1 = RefFrontendOp(1)
208 box2 = RefFrontendOp(2)
209 box3 = RefFrontendOp(3)
210 box4 = RefFrontendOp(4)
211 h.new(box1)
212 h.new(box3)
213 h.setfield(box1, box2, descr1)
214 assert h.getfield(box1, descr1) is box2
215 h.setfield(box3, box4, descr1)
216 assert h.getfield(box3, descr1) is box4
217 assert h.getfield(box1, descr1) is box2 # box1 and box3 cannot alias
218 h.setfield(box1, box3, descr1)
219 assert h.getfield(box1, descr1) is box3
220
221
222 def test_heapcache_arrays(self):
223 h = HeapCache()
224 box1 = RefFrontendOp(1)
225 box2 = RefFrontendOp(2)
226 box3 = RefFrontendOp(3)
227 box4 = RefFrontendOp(4)
228 assert h.getarrayitem(box1, index1, descr1) is None
229 assert h.getarrayitem(box1, index1, descr2) is None
230 assert h.getarrayitem(box1, index2, descr1) is None
231 assert h.getarrayitem(box1, index2, descr2) is None
232
233 h.setarrayitem(box1, index1, box2, descr1)
234 assert h.getarrayitem(box1, index1, descr1) is box2
235 assert h.getarrayitem(box1, index1, descr2) is None
236 assert h.getarrayitem(box1, index2, descr1) is None
237 assert h.getarrayitem(box1, index2, descr2) is None
238 h.setarrayitem(box1, index2, box4, descr1)
239 assert h.getarrayitem(box1, index1, descr1) is box2
240 assert h.getarrayitem(box1, index1, descr2) is None
241 assert h.getarrayitem(box1, index2, descr1) is box4
242 assert h.getarrayitem(box1, index2, descr2) is None
243
244 h.setarrayitem(box1, index1, box3, descr2)
245 assert h.getarrayitem(box1, index1, descr1) is box2
246 assert h.getarrayitem(box1, index1, descr2) is box3
247 assert h.getarrayitem(box1, index2, descr1) is box4
248 assert h.getarrayitem(box1, index2, descr2) is None
249
250 h.setarrayitem(box1, index1, box3, descr1)
251 assert h.getarrayitem(box1, index1, descr1) is box3
252 assert h.getarrayitem(box1, index1, descr2) is box3
253 assert h.getarrayitem(box1, index2, descr1) is box4
254 assert h.getarrayitem(box1, index2, descr2) is None
255
256 h.setarrayitem(box3, index1, box1, descr1)
257 assert h.getarrayitem(box3, index1, descr1) is box1
258 assert h.getarrayitem(box1, index1, descr1) is None
259 assert h.getarrayitem(box1, index1, descr2) is box3
260 assert h.getarrayitem(box1, index2, descr1) is box4
261 assert h.getarrayitem(box1, index2, descr2) is None
262
263 h.reset()
264 assert h.getarrayitem(box1, index1, descr1) is None
265 assert h.getarrayitem(box1, index1, descr2) is None
266 assert h.getarrayitem(box3, index1, descr1) is None
267
268 def test_heapcache_array_nonconst_index(self):
269 h = HeapCache()
270 box1 = RefFrontendOp(1)
271 box2 = RefFrontendOp(2)
272 box3 = RefFrontendOp(3)
273 box4 = RefFrontendOp(4)
274 h.setarrayitem(box1, index1, box2, descr1)
275 h.setarrayitem(box1, index2, box4, descr1)
276 assert h.getarrayitem(box1, index1, descr1) is box2
277 assert h.getarrayitem(box1, index2, descr1) is box4
278 h.setarrayitem(box1, box2, box3, descr1)
279 assert h.getarrayitem(box1, index1, descr1) is None
280 assert h.getarrayitem(box1, index2, descr1) is None
281
282 def test_heapcache_read_fields_multiple_array(self):
283 h = HeapCache()
284 box1 = RefFrontendOp(1)
285 box2 = RefFrontendOp(2)
286 box3 = RefFrontendOp(3)
287 box4 = RefFrontendOp(4)
288 h.getarrayitem_now_known(box1, index1, box2, descr1)
289 h.getarrayitem_now_known(box3, index1, box4, descr1)
290 assert h.getarrayitem(box1, index1, descr1) is box2
291 assert h.getarrayitem(box1, index1, descr2) is None
292 assert h.getarrayitem(box3, index1, descr1) is box4
293 assert h.getarrayitem(box3, index1, descr2) is None
294
295 h.reset()
296 assert h.getarrayitem(box1, index1, descr1) is None
297 assert h.getarrayitem(box1, index1, descr2) is None
298 assert h.getarrayitem(box3, index1, descr1) is None
299 assert h.getarrayitem(box3, index1, descr2) is None
300
301 def test_heapcache_write_fields_multiple_array(self):
302 h = HeapCache()
303 box1 = RefFrontendOp(1)
304 box2 = RefFrontendOp(2)
305 box3 = RefFrontendOp(3)
306 box4 = RefFrontendOp(4)
307 h.setarrayitem(box1, index1, box2, descr1)
308 assert h.getarrayitem(box1, index1, descr1) is box2
309 h.setarrayitem(box3, index1, box4, descr1)
310 assert h.getarrayitem(box3, index1, descr1) is box4
311 assert h.getarrayitem(box1, index1, descr1) is None # box1 and box3 can alias
312
313 h = HeapCache()
314 box1 = RefFrontendOp(1)
315 box2 = RefFrontendOp(2)
316 box3 = RefFrontendOp(3)
317 box4 = RefFrontendOp(4)
318 h.new(box1)
319 h.setarrayitem(box1, index1, box2, descr1)
320 assert h.getarrayitem(box1, index1, descr1) is box2
321 h.setarrayitem(box3, index1, box4, descr1)
322 assert h.getarrayitem(box3, index1, descr1) is box4
323 assert h.getarrayitem(box1, index1, descr1) is None # box1 and box3 can alias
324
325 h = HeapCache()
326 box1 = RefFrontendOp(1)
327 box2 = RefFrontendOp(2)
328 box3 = RefFrontendOp(3)
329 box4 = RefFrontendOp(4)
330 h.new(box1)
331 h.new(box3)
332 h.setarrayitem(box1, index1, box2, descr1)
333 assert h.getarrayitem(box1, index1, descr1) is box2
334 h.setarrayitem(box3, index1, box4, descr1)
335 assert h.getarrayitem(box3, index1, descr1) is box4
336 assert h.getarrayitem(box1, index1, descr1) is box2 # box1 and box3 cannot alias
337 h.setarrayitem(box1, index1, box3, descr1)
338 assert h.getarrayitem(box3, index1, descr1) is box4
339 assert h.getarrayitem(box1, index1, descr1) is box3 # box1 and box3 cannot alias
340
341 def test_length_cache(self):
342 h = HeapCache()
343 box1 = RefFrontendOp(1)
344 box2 = RefFrontendOp(2)
345 lengthbox1 = IntFrontendOp(11, 11)
346 lengthbox2 = IntFrontendOp(12, 11)
347 h.new_array(box1, lengthbox1)
348 assert h.arraylen(box1) is lengthbox1
349
350 assert h.arraylen(box2) is None
351 h.arraylen_now_known(box2, lengthbox2)
352 assert h.arraylen(box2) is lengthbox2
353
354
355 def test_invalidate_cache(self):
356 h = HeapCache()
357 box1 = RefFrontendOp(1)
358 box2 = RefFrontendOp(2)
359 box4 = RefFrontendOp(4)
360 h.setfield(box1, box2, descr1)
361 h.setarrayitem(box1, index1, box2, descr1)
362 h.setarrayitem(box1, index2, box4, descr1)
363 h.invalidate_caches(rop.INT_ADD, None)
364 h.invalidate_caches(rop.INT_ADD_OVF, None)
365 h.invalidate_caches(rop.SETFIELD_RAW, None)
366 h.invalidate_caches(rop.SETARRAYITEM_RAW, None)
367 h.invalidate_caches(rop.RECORD_EXACT_CLASS, None)
368 h.invalidate_caches(rop.RECORD_EXACT_VALUE_I, None)
369 h.invalidate_caches(rop.RECORD_EXACT_VALUE_R, None)
370 assert h.getfield(box1, descr1) is box2
371 assert h.getarrayitem(box1, index1, descr1) is box2
372 assert h.getarrayitem(box1, index2, descr1) is box4
373
374 h.invalidate_caches_varargs(
375 rop.CALL_N, FakeCallDescr(FakeEffectinfo.EF_ELIDABLE_CANNOT_RAISE), [])
376 assert h.getfield(box1, descr1) is box2
377 assert h.getarrayitem(box1, index1, descr1) is box2
378 assert h.getarrayitem(box1, index2, descr1) is box4
379
380 h.invalidate_caches(rop.GUARD_TRUE, None)
381 assert h.getfield(box1, descr1) is box2
382 assert h.getarrayitem(box1, index1, descr1) is box2
383 assert h.getarrayitem(box1, index2, descr1) is box4
384
385 h.invalidate_caches_varargs(
386 rop.CALL_LOOPINVARIANT_N, FakeCallDescr(FakeEffectinfo.EF_LOOPINVARIANT), [])
387
388 h.invalidate_caches_varargs(
389 rop.CALL_N, FakeCallDescr(FakeEffectinfo.EF_RANDOM_EFFECTS), [])
390 assert h.getfield(box1, descr1) is None
391 assert h.getarrayitem(box1, index1, descr1) is None
392 assert h.getarrayitem(box1, index2, descr1) is None
393
394 def test_replace_box_with_box(self):
395 py.test.skip("replacing a box with another box: not supported any more")
396 h = HeapCache()
397 box1 = RefFrontendOp(1)
398 box2 = RefFrontendOp(2)
399 box3 = RefFrontendOp(3)
400 box4 = RefFrontendOp(4)
401 h.setfield(box1, box2, descr1)
402 h.setfield(box1, box3, descr2)
403 h.setfield(box2, box3, descr3)
404 h.replace_box(box1, box4)
405 assert h.getfield(box4, descr1) is box2
406 assert h.getfield(box4, descr2) is box3
407 assert h.getfield(box2, descr3) is box3
408 h.setfield(box4, box3, descr1)
409 assert h.getfield(box4, descr1) is box3
410
411 def test_replace_box_with_const(self):
412 h = HeapCache()
413 box1 = RefFrontendOp(1)
414 box2 = RefFrontendOp(2)
415 box3 = RefFrontendOp(3)
416 c_box3 = ConstPtr(ConstPtr.value)
417 h.setfield(box1, box2, descr1)
418 h.setfield(box1, box3, descr2)
419 h.setfield(box2, box3, descr3)
420 h.replace_box(box3, c_box3)
421 assert h.getfield(box1, descr1) is box2
422 assert c_box3.same_constant(h.getfield(box1, descr2))
423 assert c_box3.same_constant(h.getfield(box2, descr3))
424
425 def test_replace_box_twice(self):
426 py.test.skip("replacing a box with another box: not supported any more")
427 h = HeapCache()
428 h.setfield(box1, box2, descr1)
429 h.setfield(box1, box3, descr2)
430 h.setfield(box2, box3, descr3)
431 h.replace_box(box1, box4)
432 h.replace_box(box4, box5)
433 assert h.getfield(box5, descr1) is box2
434 assert h.getfield(box5, descr2) is box3
435 assert h.getfield(box2, descr3) is box3
436 h.setfield(box5, box3, descr1)
437 assert h.getfield(box4, descr1) is box3
438
439 h = HeapCache()
440 h.setfield(box1, box2, descr1)
441 h.setfield(box1, box3, descr2)
442 h.setfield(box2, box3, descr3)
443 h.replace_box(box3, box4)
444 h.replace_box(box4, box5)
445 assert h.getfield(box1, descr1) is box2
446 assert h.getfield(box1, descr2) is box5
447 assert h.getfield(box2, descr3) is box5
448
449 def test_replace_box_array(self):
450 py.test.skip("replacing a box with another box: not supported any more")
451 h = HeapCache()
452 h.setarrayitem(box1, index1, box2, descr1)
453 h.setarrayitem(box1, index1, box3, descr2)
454 h.arraylen_now_known(box1, lengthbox1)
455 h.setarrayitem(box2, index2, box1, descr1)
456 h.setarrayitem(box3, index2, box1, descr2)
457 h.setarrayitem(box2, index2, box3, descr3)
458 h.replace_box(box1, box4)
459 assert h.arraylen(box4) is lengthbox1
460 assert h.getarrayitem(box4, index1, descr1) is box2
461 assert h.getarrayitem(box4, index1, descr2) is box3
462 assert h.getarrayitem(box2, index2, descr1) is box4
463 assert h.getarrayitem(box3, index2, descr2) is box4
464 assert h.getarrayitem(box2, index2, descr3) is box3
465
466 h.replace_box(lengthbox1, lengthbox2)
467 assert h.arraylen(box4) is lengthbox2
468
469 def test_replace_box_array_twice(self):
470 py.test.skip("replacing a box with another box: not supported any more")
471 h = HeapCache()
472 h.setarrayitem(box1, index1, box2, descr1)
473 h.setarrayitem(box1, index1, box3, descr2)
474 h.arraylen_now_known(box1, lengthbox1)
475 h.setarrayitem(box2, index2, box1, descr1)
476 h.setarrayitem(box3, index2, box1, descr2)
477 h.setarrayitem(box2, index2, box3, descr3)
478 h.replace_box(box1, box4)
479 h.replace_box(box4, box5)
480 assert h.arraylen(box4) is lengthbox1
481 assert h.getarrayitem(box5, index1, descr1) is box2
482 assert h.getarrayitem(box5, index1, descr2) is box3
483 assert h.getarrayitem(box2, index2, descr1) is box5
484 assert h.getarrayitem(box3, index2, descr2) is box5
485 assert h.getarrayitem(box2, index2, descr3) is box3
486
487 h.replace_box(lengthbox1, lengthbox2)
488 h.replace_box(lengthbox2, lengthbox3)
489 assert h.arraylen(box4) is lengthbox3
490
491 def test_replace_box_with_const_in_array(self):
492 h = HeapCache()
493 box1 = RefFrontendOp(1)
494 lengthbox2 = IntFrontendOp(2, 10)
495 h.arraylen_now_known(box1, lengthbox2)
496 assert h.arraylen(box1) is lengthbox2
497 c10 = ConstInt(10)
498 h.replace_box(lengthbox2, c10)
499 assert c10.same_constant(h.arraylen(box1))
500
501 box2 = IntFrontendOp(2, 12)
502 h.setarrayitem(box1, index2, box2, descr1)
503 assert h.getarrayitem(box1, index2, descr1) is box2
504 c12 = ConstInt(12)
505 h.replace_box(box2, c12)
506 assert c12.same_constant(h.getarrayitem(box1, index2, descr1))
507
508 def test_ll_arraycopy(self):
509 h = HeapCache()
510 box1 = RefFrontendOp(1)
511 box2 = RefFrontendOp(2)
512 box3 = RefFrontendOp(3)
513 box4 = RefFrontendOp(4)
514 box5 = RefFrontendOp(5)
515 lengthbox1 = IntFrontendOp(11, 12)
516 lengthbox2 = IntFrontendOp(12, 178)
517 h.new_array(box1, lengthbox1)
518 h.setarrayitem(box1, index1, box2, descr1)
519 h.new_array(box2, lengthbox1)
520 # Just need the destination box for this call
521 h.invalidate_caches_varargs(
522 rop.CALL_N,
523 arraycopydescr1,
524 [None, box5, box2, index1, index1, index1]
525 )
526 assert h.getarrayitem(box1, index1, descr1) is box2
527 h.invalidate_caches_varargs(
528 rop.CALL_N,
529 arraycopydescr1,
530 [None, box5, box3, index1, index1, index1]
531 )
532 assert h.getarrayitem(box1, index1, descr1) is box2
533
534 h.setarrayitem(box4, index1, box2, descr1)
535 assert h.getarrayitem(box4, index1, descr1) is box2
536 h.invalidate_caches_varargs(
537 rop.CALL_N,
538 arraycopydescr1,
539 [None, box3, box5, index1, index1, index2]
540 )
541 assert h.getarrayitem(box4, index1, descr1) is None
542
543 def test_ll_arraycopy_differing_descrs(self):
544 h = HeapCache()
545 box1 = RefFrontendOp(1)
546 box2 = RefFrontendOp(2)
547 box3 = RefFrontendOp(3)
548 lengthbox2 = IntFrontendOp(12, 12)
549 h.setarrayitem(box1, index1, box2, descr2)
550 assert h.getarrayitem(box1, index1, descr2) is box2
551 h.new_array(box2, lengthbox2)
552 h.invalidate_caches_varargs(
553 rop.CALL_N,
554 arraycopydescr1,
555 [None, box3, box2, index1, index1, index2]
556 )
557 assert h.getarrayitem(box1, index1, descr2) is box2
558
559 def test_ll_arraycopy_differing_descrs_nonconst_index(self):
560 h = HeapCache()
561 box1 = RefFrontendOp(1)
562 box2 = RefFrontendOp(2)
563 box3 = RefFrontendOp(3)
564 h.setarrayitem(box1, index1, box2, descr2)
565 assert h.getarrayitem(box1, index1, descr2) is box2
566 h.invalidate_caches_varargs(
567 rop.CALL_N,
568 arraycopydescr1,
569 [None, box3, box2, index1, index1, InputArgInt()]
570 )
571 assert h.getarrayitem(box1, index1, descr2) is box2
572
573 def test_ll_arraycopy_result_propogated(self):
574 h = HeapCache()
575 box1 = RefFrontendOp(1)
576 box2 = RefFrontendOp(2)
577 box3 = RefFrontendOp(3)
578 h.setarrayitem(box1, index1, box2, descr1)
579 h.invalidate_caches_varargs(
580 rop.CALL_N,
581 arraycopydescr1,
582 [None, box1, box3, index1, index1, index2]
583 )
584 assert h.getarrayitem(box3, index1, descr1) is box2
585
586 def test_ll_arraycopy_dest_new(self):
587 h = HeapCache()
588 box1 = RefFrontendOp(1)
589 box2 = RefFrontendOp(2)
590 box3 = RefFrontendOp(3)
591 box4 = RefFrontendOp(4)
592 lengthbox1 = IntFrontendOp(11, 435)
593 h.new_array(box1, lengthbox1)
594 h.setarrayitem(box3, index1, box4, descr1)
595 h.invalidate_caches_varargs(
596 rop.CALL_N,
597 arraycopydescr1,
598 [None, box2, box1, index1, index1, index2]
599 )
600
601 def test_ll_arraycopy_doesnt_escape_arrays(self):
602 h = HeapCache()
603 box1 = RefFrontendOp(1)
604 box2 = RefFrontendOp(2)
605 lengthbox1 = IntFrontendOp(11, 1)
606 lengthbox2 = IntFrontendOp(12, 3)
607 h.new_array(box1, index2)
608 h.new_array(box2, index2)
609 assert h.is_unescaped(box1)
610 assert h.is_unescaped(box2)
611 h.invalidate_caches_varargs(
612 rop.CALL_N,
613 arraycopydescr1,
614 [None, box2, box1, index1, index1, index2]
615 )
616 assert h.is_unescaped(box1)
617 assert h.is_unescaped(box2)
618 h.invalidate_caches_varargs(
619 rop.CALL_N,
620 arraycopydescr1,
621 [None, box2, box1, index1, index1, InputArgInt()]
622 )
623 assert not h.is_unescaped(box1)
624 assert not h.is_unescaped(box2)
625
626 def test_unescaped(self):
627 h = HeapCache()
628 box1 = RefFrontendOp(1)
629 box2 = RefFrontendOp(2)
630 assert not h.is_unescaped(box1)
631 h.new(box2)
632 assert h.is_unescaped(box2)
633 h.invalidate_caches(rop.SETFIELD_GC, None, box2, box1)
634 assert h.is_unescaped(box2)
635 h.invalidate_caches(rop.SETFIELD_GC, None, box1, box2)
636 assert not h.is_unescaped(box2)
637
638 def test_unescaped_testing(self):
639 h = HeapCache()
640 box1 = RefFrontendOp(1)
641 box2 = RefFrontendOp(2)
642 box3 = RefFrontendOp(3)
643 h.new(box1)
644 h.new(box2)
645 assert h.is_unescaped(box1)
646 assert h.is_unescaped(box2)
647 # Putting a virtual inside of another virtual doesn't escape it.
648 h.invalidate_caches(rop.SETFIELD_GC, None, box1, box2)
649 assert h.is_unescaped(box2)
650 # Reading a field from a virtual doesn't escape it.
651 h.invalidate_caches(rop.GETFIELD_GC_I, None, box1)
652 assert h.is_unescaped(box1)
653 # Escaping a virtual transitively escapes anything inside of it.
654 assert not h.is_unescaped(box3)
655 h.invalidate_caches(rop.SETFIELD_GC, None, box3, box1)
656 assert not h.is_unescaped(box1)
657 assert not h.is_unescaped(box2)
658
659 def test_ops_dont_escape(self):
660 h = HeapCache()
661 box1 = RefFrontendOp(1)
662 box2 = RefFrontendOp(2)
663 h.new(box1)
664 h.new(box2)
665 assert h.is_unescaped(box1)
666 assert h.is_unescaped(box2)
667 h.invalidate_caches(rop.INSTANCE_PTR_EQ, None, box1, box2)
668 assert h.is_unescaped(box1)
669 assert h.is_unescaped(box2)
670 h.invalidate_caches(rop.INSTANCE_PTR_NE, None, box1, box2)
671 assert h.is_unescaped(box1)
672 assert h.is_unescaped(box2)
673
674 def test_circular_virtuals(self):
675 h = HeapCache()
676 box1 = RefFrontendOp(1)
677 box2 = RefFrontendOp(2)
678 box3 = RefFrontendOp(3)
679 h.new(box1)
680 h.new(box2)
681 h.invalidate_caches(rop.SETFIELD_GC, None, box1, box2)
682 h.invalidate_caches(rop.SETFIELD_GC, None, box2, box1)
683 h.invalidate_caches(rop.SETFIELD_GC, None, box3, box1) # does not crash
684
685 def test_unescaped_array(self):
686 h = HeapCache()
687 box1 = RefFrontendOp(1)
688 box2 = RefFrontendOp(2)
689 lengthbox1 = IntFrontendOp(11, 3)
690 lengthbox2 = IntFrontendOp(12, 3)
691 h.new_array(box1, index2)
692 assert h.is_unescaped(box1)
693 h.invalidate_caches(rop.SETARRAYITEM_GC, None, box1, index1, box2)
694 assert h.is_unescaped(box1)
695 h.invalidate_caches(rop.SETARRAYITEM_GC, None, box2, index1, box1)
696 assert not h.is_unescaped(box1)
697
698 h = HeapCache()
699 h.new_array(box1, index2)
700 h.new(box2)
701 assert h.is_unescaped(box1)
702 assert h.is_unescaped(box2)
703 h.invalidate_caches(rop.SETARRAYITEM_GC, None, box1, lengthbox2, box2)
704 assert h.is_unescaped(box1)
705 assert h.is_unescaped(box2)
706 h.invalidate_caches_varargs(
707 rop.CALL_N, FakeCallDescr(FakeEffectinfo.EF_RANDOM_EFFECTS), [box1]
708 )
709 assert not h.is_unescaped(box1)
710 assert not h.is_unescaped(box2)
711
712 def test_call_doesnt_invalidate_unescaped_boxes(self):
713 h = HeapCache()
714 box1 = RefFrontendOp(1)
715 box2 = RefFrontendOp(2)
716 h.new(box1)
717 assert h.is_unescaped(box1)
718 h.setfield(box1, box2, descr1)
719 h.invalidate_caches_varargs(rop.CALL_N,
720 FakeCallDescr(FakeEffectinfo.EF_CAN_RAISE),
721 []
722 )
723 assert h.getfield(box1, descr1) is box2
724
725 def test_call_doesnt_invalidate_unescaped_array_boxes(self):
726 h = HeapCache()
727 box1 = RefFrontendOp(1)
728 box3 = RefFrontendOp(3)
729 lengthbox1 = IntFrontendOp(11, 3)
730 h.new_array(box1, index2)
731 assert h.is_unescaped(box1)
732 h.setarrayitem(box1, index1, box3, descr1)
733 h.invalidate_caches_varargs(rop.CALL_N,
734 FakeCallDescr(FakeEffectinfo.EF_CAN_RAISE),
735 []
736 )
737 assert h.getarrayitem(box1, index1, descr1) is box3
738
739 def test_bug_missing_ignored_operations(self):
740 h = HeapCache()
741 box1 = RefFrontendOp(1)
742 box2 = RefFrontendOp(2)
743 h.new(box1)
744 h.new(box2)
745 h.setfield(box1, box2, descr1)
746 assert h.getfield(box1, descr1) is box2
747 h.invalidate_caches(rop.STRSETITEM, None)
748 h.invalidate_caches(rop.UNICODESETITEM, None)
749 h.invalidate_caches(rop.SETFIELD_RAW, None)
750 h.invalidate_caches(rop.SETARRAYITEM_RAW, None)
751 h.invalidate_caches(rop.SETINTERIORFIELD_RAW, None)
752 h.invalidate_caches(rop.RAW_STORE, None)
753 assert h.is_unescaped(box1)
754 assert h.is_unescaped(box2)
755 assert h.getfield(box1, descr1) is box2
756
757 def test_bug_heap_cache_is_cleared_but_not_is_unescaped_1(self):
758 # bug if only the getfield() link is cleared (heap_cache) but not
759 # the is_unescaped() flags: we can do later a GETFIELD(box1) which
760 # will give us a fresh box3, which is actually equal to box2. This
761 # box3 is escaped, but box2 is still unescaped. Bug shown e.g. by
762 # calling some residual code that changes the values on box3: then
763 # the content of box2 is still cached at the old value.
764 h = HeapCache()
765 box1 = RefFrontendOp(1)
766 box2 = RefFrontendOp(2)
767 h.new(box1)
768 h.new(box2)
769 h.setfield(box1, box2, descr1)
770 h.invalidate_caches(rop.SETFIELD_GC, None, box1, box2)
771 assert h.getfield(box1, descr1) is box2
772 h.invalidate_caches_varargs(rop.CALL_MAY_FORCE_N, FakeCallDescr(FakeEffectinfo.EF_RANDOM_EFFECTS), [])
773 assert not h.is_unescaped(box1)
774 assert not h.is_unescaped(box2)
775 assert h.getfield(box1, descr1) is None
776
777 def test_bug_heap_cache_is_cleared_but_not_is_unescaped_2(self):
778 h = HeapCache()
779 box1 = RefFrontendOp(1)
780 box2 = RefFrontendOp(2)
781 h.new(box1)
782 h.new(box2)
783 h.setfield(box1, box2, descr1)
784 h.invalidate_caches(rop.SETFIELD_GC, None, box1, box2)
785 assert h.getfield(box1, descr1) is box2
786 descr = BasicFailDescr()
787 class XTra:
788 oopspecindex = 0
789 OS_ARRAYCOPY = 42
790 OS_ARRAYMOVE = 49
791 extraeffect = 5
792 EF_LOOPINVARIANT = 1
793 EF_ELIDABLE_CANNOT_RAISE = 2
794 EF_ELIDABLE_OR_MEMORYERROR = 3
795 EF_ELIDABLE_CAN_RAISE = 4
796 descr.get_extra_info = XTra
797 h.invalidate_caches_varargs(rop.CALL_N, descr, [])
798 assert h.is_unescaped(box1)
799 assert h.is_unescaped(box2)
800 assert h.getfield(box1, descr1) is box2
801
802 def test_is_likely_virtual(self):
803 h = HeapCache()
804 box1 = RefFrontendOp(1)
805 h.new(box1)
806 assert h.is_unescaped(box1)
807 assert h.is_likely_virtual(box1)
808 h.reset_keep_likely_virtuals()
809 assert not h.is_unescaped(box1)
810 assert h.is_likely_virtual(box1)
811 h._escape_box(box1)
812 assert not h.is_unescaped(box1)
813 assert not h.is_likely_virtual(box1)
814
815 def test_is_likely_virtual_2(self):
816 h = HeapCache()
817 box1 = RefFrontendOp(1)
818 h.new(box1)
819 assert h.is_unescaped(box1)
820 assert h.is_likely_virtual(box1)
821 h.reset_keep_likely_virtuals()
822 assert not h.is_unescaped(box1)
823 assert h.is_likely_virtual(box1)
824 h.reset() # reset everything
825 assert not h.is_unescaped(box1)
826 assert not h.is_likely_virtual(box1)
827
828 def test_is_likely_virtual_3(self):
829 h = HeapCache()
830 box1 = RefFrontendOp(1)
831 h.new(box1)
832 assert h.is_unescaped(box1)
833 assert h.is_likely_virtual(box1)
834 h.reset_keep_likely_virtuals()
835 assert not h.is_unescaped(box1)
836 assert h.is_likely_virtual(box1)
837 h.class_now_known(box1) # interaction of the two families of flags
838 assert not h.is_unescaped(box1)
839 assert h.is_likely_virtual(box1)
840
841 def test_is_likely_virtual_array(self):
842 h = HeapCache()
843 box1 = RefFrontendOp(1)
844 h.new_array(box1, index2)
845 assert h.is_likely_virtual(box1)
846 box2 = RefFrontendOp(2)
847 lengthbox = IntFrontendOp(11, 3)
848 # arrays are only virtual if the length is constant
849 h.new_array(box2, lengthbox)
850 assert not h.is_likely_virtual(box2)
851
852 def test_quasiimmut_seen(self):
853 h = HeapCache()
854 box1 = RefFrontendOp(1)
855 box2 = RefFrontendOp(2)
856 box3 = RefFrontendOp(3)
857 box4 = RefFrontendOp(4)
858 assert not h.is_quasi_immut_known(descr1, box1)
859 assert not h.is_quasi_immut_known(descr1, box2)
860 assert not h.is_quasi_immut_known(descr2, box3)
861 assert not h.is_quasi_immut_known(descr2, box4)
862 h.quasi_immut_now_known(descr1, box1)
863 assert h.is_quasi_immut_known(descr1, box1)
864 assert not h.is_quasi_immut_known(descr1, box2)
865 assert not h.is_quasi_immut_known(descr2, box3)
866 assert not h.is_quasi_immut_known(descr2, box4)
867 h.quasi_immut_now_known(descr1, box2)
868 assert h.is_quasi_immut_known(descr1, box1)
869 assert h.is_quasi_immut_known(descr1, box2)
870 assert not h.is_quasi_immut_known(descr2, box3)
871 assert not h.is_quasi_immut_known(descr2, box4)
872 h.quasi_immut_now_known(descr2, box3)
873 assert h.is_quasi_immut_known(descr1, box1)
874 assert h.is_quasi_immut_known(descr1, box2)
875 assert h.is_quasi_immut_known(descr2, box3)
876 assert not h.is_quasi_immut_known(descr2, box4)
877 h.quasi_immut_now_known(descr2, box4)
878 assert h.is_quasi_immut_known(descr1, box1)
879 assert h.is_quasi_immut_known(descr1, box2)
880 assert h.is_quasi_immut_known(descr2, box3)
881 assert h.is_quasi_immut_known(descr2, box4)
882
883 # invalidate the descr1 cache
884
885 h.setfield(box1, box3, descr1)
886 assert not h.is_quasi_immut_known(descr1, box1)
887 assert not h.is_quasi_immut_known(descr1, box2)
888
889 # a call invalidates everything
890 h.invalidate_caches_varargs(
891 rop.CALL_N, FakeCallDescr(FakeEffectinfo.EF_CAN_RAISE), [])
892 assert not h.is_quasi_immut_known(descr2, box3)
893 assert not h.is_quasi_immut_known(descr2, box4)
894
895
896 def test_quasiimmut_seen_consts(self):
897 h = HeapCache()
898 box1 = ConstPtr(rffi.cast(llmemory.GCREF, 1))
899 box2 = ConstPtr(rffi.cast(llmemory.GCREF, 1))
900 box3 = ConstPtr(rffi.cast(llmemory.GCREF, 1))
901 box4 = ConstPtr(rffi.cast(llmemory.GCREF, 1))
902 assert not h.is_quasi_immut_known(descr1, box1)
903 assert not h.is_quasi_immut_known(descr1, box2)
904 assert not h.is_quasi_immut_known(descr2, box3)
905 assert not h.is_quasi_immut_known(descr2, box4)
906 h.quasi_immut_now_known(descr1, box1)
907 assert h.is_quasi_immut_known(descr1, box1)
908 assert h.is_quasi_immut_known(descr1, box2)
909 assert not h.is_quasi_immut_known(descr2, box3)
910 assert not h.is_quasi_immut_known(descr2, box4)
911 h.quasi_immut_now_known(descr2, box3)
912 assert h.is_quasi_immut_known(descr1, box1)
913 assert h.is_quasi_immut_known(descr1, box2)
914 assert h.is_quasi_immut_known(descr2, box3)
915 assert h.is_quasi_immut_known(descr2, box4)
916
917 # invalidate the descr1 cache
918
919 vbox1 = RefFrontendOp(1)
920 vbox2 = RefFrontendOp(2)
921 h.setfield(vbox1, vbox2, descr1)
922 assert not h.is_quasi_immut_known(descr1, box1)
923 assert not h.is_quasi_immut_known(descr1, box2)
924
925 # a call invalidates everything
926 h.invalidate_caches_varargs(
927 rop.CALL_N, FakeCallDescr(FakeEffectinfo.EF_CAN_RAISE), [])
928 assert not h.is_quasi_immut_known(descr2, box3)
929 assert not h.is_quasi_immut_known(descr2, box4)
930
931 def test_heapcache_on_const(self):
932 h = HeapCache()
933 # two different boxes of the same value
934 box1 = ConstPtr(ConstPtr.value)
935 box2 = ConstPtr(ConstPtr.value)
936 box3 = RefFrontendOp(3)
937 box4 = RefFrontendOp(4)
938
939 # the cache unifies a bit
940 assert h.getfield(box1, descr1) is None
941 assert h.getfield(box2, descr1) is None
942 h.getfield_now_known(box1, descr1, box3)
943 assert h.heap_cache[descr1].last_const_box is box1
944 assert h.heap_cache[descr1]._unique_const_heuristic(box2) is box1
945 assert h.getfield(box1, descr1) is box3
946 assert h.getfield(box2, descr1) is box3
947
948 h.reset()
949 h.setfield(box2, box4, descr1)
950 assert h.getfield(box1, descr1) is box4
951 assert h.getfield(box2, descr1) is box4