Debian Codegraph @

rpython/jit/backend/llsupport/test/zrpy_gc_test.py

raw symbols SCIP
1 """
2 This is a test that translates a complete JIT together with a GC and runs it.
3 It is testing that the GC-dependent aspects basically work, mostly the mallocs
4 and the various cases of write barrier.
5 """
6 from __future__ import print_function
7
8 import weakref
9 import os, py
10 from rpython.rlib import rgc
11 from rpython.rtyper.lltypesystem import lltype
12 from rpython.rlib.jit import JitDriver, dont_look_inside
13 from rpython.rlib.jit import elidable, unroll_safe
14 from rpython.rlib.jit import promote
15 from rpython.jit.backend.llsupport.gc import GcLLDescr_framework
16 from rpython.tool.udir import udir
17 from rpython.config.translationoption import DEFL_GC
18 from rpython.config.config import ConfigError
19
20
21 class X(object):
22 def __init__(self, x=0):
23 self.x = x
24
25 next = None
26
27 class Y(object):
28 # for pinning tests we need an object without references to other
29 # objects
30 def __init__(self, x=0):
31 self.x = x
32
33 class CheckError(Exception):
34 pass
35
36 def check(flag):
37 if not flag:
38 raise CheckError
39
40 def get_entry(g):
41
42 def entrypoint(args):
43 name = ''
44 n = 2000
45 argc = len(args)
46 if argc > 1:
47 name = args[1]
48 if argc > 2:
49 n = int(args[2])
50 r_list = []
51 for i in range(20):
52 r = g(name, n)
53 r_list.append(r)
54 rgc.collect()
55 rgc.collect(); rgc.collect()
56 freed = 0
57 for r in r_list:
58 if r() is None:
59 freed += 1
60 print(freed)
61 return 0
62
63 return entrypoint
64
65 def get_functions_to_patch():
66 from rpython.jit.backend.llsupport import gc
67 #
68 can_use_nursery_malloc1 = gc.GcLLDescr_framework.can_use_nursery_malloc
69 def can_use_nursery_malloc2(*args):
70 try:
71 if os.environ['PYPY_NO_INLINE_MALLOC']:
72 return False
73 except KeyError:
74 pass
75 return can_use_nursery_malloc1(*args)
76 #
77 return {(gc.GcLLDescr_framework, 'can_use_nursery_malloc'):
78 can_use_nursery_malloc2}
79
80 def compile(f, gc, **kwds):
81 from rpython.annotator.listdef import s_list_of_strings
82 from rpython.translator.translator import TranslationContext
83 from rpython.jit.metainterp.warmspot import apply_jit
84 from rpython.translator.c import genc
85 from rpython.translator.backendopt.all import backend_optimizations
86 #
87 t = TranslationContext()
88 t.config.translation.gc = gc
89 if gc != 'boehm':
90 t.config.translation.gcremovetypeptr = True
91 for name, value in kwds.items():
92 setattr(t.config.translation, name, value)
93 ann = t.buildannotator()
94 ann.build_types(f, [s_list_of_strings], main_entry_point=True)
95 t.buildrtyper().specialize()
96
97 if kwds['jit']:
98 patch = get_functions_to_patch()
99 old_value = {}
100 try:
101 for (obj, attr), value in patch.items():
102 old_value[obj, attr] = getattr(obj, attr)
103 setattr(obj, attr, value)
104 #
105 apply_jit(t)
106 #
107 finally:
108 for (obj, attr), oldvalue in old_value.items():
109 setattr(obj, attr, oldvalue)
110
111 backend_optimizations(t,
112 graphs=t.graphs,
113 merge_if_blocks=True,
114 constfold=True,
115 remove_asserts=True,
116 really_remove_asserts=True,
117 replace_we_are_jitted=False)
118 cbuilder = genc.CStandaloneBuilder(t, f, t.config)
119 cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
120 cbuilder.compile()
121 return cbuilder
122
123 def run(cbuilder, args=''):
124 #
125 pypylog = udir.join('test_zrpy_gc.log')
126 env = os.environ.copy()
127 env['PYPYLOG'] = ':%s' % pypylog
128 data = cbuilder.cmdexec(args, env=env)
129 return data.strip()
130
131 # ______________________________________________________________________
132
133
134 class BaseFrameworkTests(object):
135 gc = DEFL_GC
136
137 def setup_class(cls):
138 funcs = []
139 name_to_func = {}
140 for fullname in dir(cls):
141 if not fullname.startswith('define'):
142 continue
143 definefunc = getattr(cls, fullname)
144 _, name = fullname.split('_', 1)
145 beforefunc, loopfunc, afterfunc = definefunc.im_func(cls)
146 if beforefunc is None:
147 def beforefunc(n, x):
148 return n, x, None, None, None, None, None, None, None, None, None, ''
149 if afterfunc is None:
150 def afterfunc(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
151 pass
152 beforefunc.__name__ = 'before_'+name
153 loopfunc.__name__ = 'loop_'+name
154 afterfunc.__name__ = 'after_'+name
155 funcs.append((beforefunc, loopfunc, afterfunc))
156 assert name not in name_to_func
157 name_to_func[name] = len(name_to_func)
158 print(name_to_func)
159 def allfuncs(name, n):
160 x = X()
161 x.foo = 2
162 main_allfuncs(name, n, x)
163 x.foo = 5
164 return weakref.ref(x)
165 def main_allfuncs(name, n, x):
166 num = name_to_func[name]
167 n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = funcs[num][0](n, x)
168 while n > 0:
169 myjitdriver.can_enter_jit(num=num, n=n, x=x, x0=x0, x1=x1,
170 x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s)
171 myjitdriver.jit_merge_point(num=num, n=n, x=x, x0=x0, x1=x1,
172 x2=x2, x3=x3, x4=x4, x5=x5, x6=x6, x7=x7, l=l, s=s)
173
174 n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s = funcs[num][1](
175 n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s)
176 funcs[num][2](n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s)
177 myjitdriver = JitDriver(greens = ['num'],
178 reds = ['n', 'x', 'x0', 'x1', 'x2', 'x3', 'x4',
179 'x5', 'x6', 'x7', 'l', 's'], is_recursive=True)
180 cls.main_allfuncs = staticmethod(main_allfuncs)
181 cls.name_to_func = name_to_func
182 OLD_DEBUG = GcLLDescr_framework.DEBUG
183 try:
184 GcLLDescr_framework.DEBUG = True
185 cls.cbuilder = compile(get_entry(allfuncs), cls.gc,
186 gcrootfinder=cls.gcrootfinder, jit=True,
187 thread=True)
188 finally:
189 GcLLDescr_framework.DEBUG = OLD_DEBUG
190
191 def _run(self, name, n, env):
192 res = self.cbuilder.cmdexec("%s %d" %(name, n), env=env)
193 assert int(res) == 20
194
195 def run(self, name, n=2000):
196 pypylog = udir.join('TestCompileFramework.log')
197 env = os.environ.copy()
198 env['PYPYLOG'] = ':%s' % pypylog
199 env['PYPY_NO_INLINE_MALLOC'] = '1'
200 self._run(name, n, env)
201 env['PYPY_NO_INLINE_MALLOC'] = ''
202 self._run(name, n, env)
203
204 def run_orig(self, name, n, x):
205 self.main_allfuncs(name, n, x)
206
207
208 class CompileFrameworkTests(BaseFrameworkTests):
209 # Test suite using (so far) the minimark GC.
210
211 ## def define_libffi_workaround(cls):
212 ## # XXX: this is a workaround for a bug in database.py. It seems that
213 ## # the problem is triggered by optimizeopt/fficall.py, and in
214 ## # particular by the ``cast_base_ptr_to_instance(Func, llfunc)``: in
215 ## # these tests, that line is the only place where libffi.Func is
216 ## # referenced.
217 ## #
218 ## # The problem occurs because the gctransformer tries to annotate a
219 ## # low-level helper to call the __del__ of libffi.Func when it's too
220 ## # late.
221 ## #
222 ## # This workaround works by forcing the annotator (and all the rest of
223 ## # the toolchain) to see libffi.Func in a "proper" context, not just as
224 ## # the target of cast_base_ptr_to_instance. Note that the function
225 ## # below is *never* called by any actual test, it's just annotated.
226 ## #
227 ## from rpython.rlib.libffi import get_libc_name, CDLL, types, ArgChain
228 ## libc_name = get_libc_name()
229 ## def f(n, x, *args):
230 ## libc = CDLL(libc_name)
231 ## ptr = libc.getpointer('labs', [types.slong], types.slong)
232 ## chain = ArgChain()
233 ## chain.arg(n)
234 ## n = ptr.call(chain, lltype.Signed)
235 ## return (n, x) + args
236 ## return None, f, None
237
238 def define_compile_framework_1(cls):
239 # a moving GC. Simple test, works
240 # without write_barriers and root stack enumeration.
241 def f(n, x, *args):
242 y = X()
243 y.foo = x.foo
244 n -= y.foo
245 return (n, x) + args
246 return None, f, None
247
248 def test_compile_framework_1(self):
249 self.run('compile_framework_1')
250
251 def define_compile_framework_2(cls):
252 # More complex test, requires root stack enumeration but
253 # not write_barriers.
254 def f(n, x, *args):
255 prev = x
256 for j in range(101): # f() runs 20'000 times, thus allocates
257 y = X() # a total of 2'020'000 objects
258 y.foo = prev.foo
259 prev = y
260 n -= prev.foo
261 return (n, x) + args
262 return None, f, None
263
264 def test_compile_framework_2(self):
265 self.run('compile_framework_2')
266
267 def define_compile_framework_3(cls):
268 # Third version of the test. Really requires write_barriers.
269 def f(n, x, *args):
270 x.next = None
271 for j in range(101): # f() runs 20'000 times, thus allocates
272 y = X() # a total of 2'020'000 objects
273 y.foo = j+1
274 y.next = x.next
275 x.next = y
276 check(x.next.foo == 101)
277 total = 0
278 y = x
279 for j in range(101):
280 y = y.next
281 total += y.foo
282 check(not y.next)
283 check(total == 101*102/2)
284 n -= x.foo
285 return (n, x) + args
286 return None, f, None
287
288
289
290 def test_compile_framework_3(self):
291 x_test = X()
292 x_test.foo = 5
293 self.run_orig('compile_framework_3', 6, x_test) # check that it does not raise CheckError
294 self.run('compile_framework_3')
295
296 def define_compile_framework_3_extra(cls):
297 # Extra version of the test, with tons of live vars around the residual
298 # call that all contain a GC pointer.
299 @dont_look_inside
300 def residual(n=26):
301 x = X()
302 x.next = X()
303 x.next.foo = n
304 return x
305 #
306 def before(n, x):
307 residual(5)
308 x0 = residual()
309 x1 = residual()
310 x2 = residual()
311 x3 = residual()
312 x4 = residual()
313 x5 = residual()
314 x6 = residual()
315 x7 = residual()
316 n *= 19
317 return n, None, x0, x1, x2, x3, x4, x5, x6, x7, None, None
318 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
319 x8 = residual()
320 x9 = residual()
321 check(x0.next.foo == 26)
322 check(x1.next.foo == 26)
323 check(x2.next.foo == 26)
324 check(x3.next.foo == 26)
325 check(x4.next.foo == 26)
326 check(x5.next.foo == 26)
327 check(x6.next.foo == 26)
328 check(x7.next.foo == 26)
329 check(x8.next.foo == 26)
330 check(x9.next.foo == 26)
331 x0, x1, x2, x3, x4, x5, x6, x7 = x7, x4, x6, x5, x3, x2, x9, x8
332 n -= 1
333 return n, None, x0, x1, x2, x3, x4, x5, x6, x7, None, None
334 return before, f, None
335
336 def test_compile_framework_3_extra(self):
337 self.run_orig('compile_framework_3_extra', 6, None) # check that it does not raise CheckError
338 self.run('compile_framework_3_extra')
339
340 def define_compile_framework_4(cls):
341 # Fourth version of the test, with __del__.
342 from rpython.rlib.debug import debug_print
343 class Counter:
344 cnt = 0
345 counter = Counter()
346 class Z:
347 def __del__(self):
348 counter.cnt -= 1
349 def before(n, x):
350 debug_print('counter.cnt =', counter.cnt)
351 check(counter.cnt < 5)
352 counter.cnt = n // x.foo
353 return n, x, None, None, None, None, None, None, None, None, None, None
354 def f(n, x, *args):
355 Z()
356 n -= x.foo
357 return (n, x) + args
358 return before, f, None
359
360 def test_compile_framework_4(self):
361 self.run('compile_framework_4')
362
363 def define_compile_framework_5(cls):
364 # Test string manipulation.
365 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
366 n -= x.foo
367 s += str(n)
368 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
369 def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
370 check(len(s) == 1*5 + 2*45 + 3*450 + 4*500)
371 return None, f, after
372
373 def test_compile_framework_5(self):
374 self.run('compile_framework_5')
375
376 def define_compile_framework_7(cls):
377 # Array of pointers (test the write barrier for setarrayitem_gc)
378 def before(n, x):
379 return n, x, None, None, None, None, None, None, None, None, [X(123)], None
380 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
381 if n < 1900:
382 check(l[0].x == 123)
383 l = [None] * 16
384 l[0] = X(123)
385 l[1] = X(n)
386 l[2] = X(n+10)
387 l[3] = X(n+20)
388 l[4] = X(n+30)
389 l[5] = X(n+40)
390 l[6] = X(n+50)
391 l[7] = X(n+60)
392 l[8] = X(n+70)
393 l[9] = X(n+80)
394 l[10] = X(n+90)
395 l[11] = X(n+100)
396 l[12] = X(n+110)
397 l[13] = X(n+120)
398 l[14] = X(n+130)
399 l[15] = X(n+140)
400 if n < 1800:
401 check(len(l) == 16)
402 check(l[0].x == 123)
403 check(l[1].x == n)
404 check(l[2].x == n+10)
405 check(l[3].x == n+20)
406 check(l[4].x == n+30)
407 check(l[5].x == n+40)
408 check(l[6].x == n+50)
409 check(l[7].x == n+60)
410 check(l[8].x == n+70)
411 check(l[9].x == n+80)
412 check(l[10].x == n+90)
413 check(l[11].x == n+100)
414 check(l[12].x == n+110)
415 check(l[13].x == n+120)
416 check(l[14].x == n+130)
417 check(l[15].x == n+140)
418 n -= x.foo
419 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
420 def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
421 check(len(l) == 16)
422 check(l[0].x == 123)
423 check(l[1].x == 2)
424 check(l[2].x == 12)
425 check(l[3].x == 22)
426 check(l[4].x == 32)
427 check(l[5].x == 42)
428 check(l[6].x == 52)
429 check(l[7].x == 62)
430 check(l[8].x == 72)
431 check(l[9].x == 82)
432 check(l[10].x == 92)
433 check(l[11].x == 102)
434 check(l[12].x == 112)
435 check(l[13].x == 122)
436 check(l[14].x == 132)
437 check(l[15].x == 142)
438 return before, f, after
439
440 def test_compile_framework_7(self):
441 self.run('compile_framework_7')
442
443 def define_compile_framework_7_interior(cls):
444 # Array of structs containing pointers (test the write barrier
445 # for setinteriorfield_gc)
446 S = lltype.GcStruct('S', ('i', lltype.Signed))
447 A = lltype.GcArray(lltype.Struct('entry', ('x', lltype.Ptr(S)),
448 ('y', lltype.Ptr(S)),
449 ('z', lltype.Ptr(S))))
450 class Glob:
451 a = lltype.nullptr(A)
452 glob = Glob()
453 #
454 def make_s(i):
455 s = lltype.malloc(S)
456 s.i = i
457 return s
458 #
459 @unroll_safe
460 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
461 a = glob.a
462 if not a:
463 a = glob.a = lltype.malloc(A, 10)
464 i = 0
465 while i < 10:
466 a[i].x = make_s(n + i * 100 + 1)
467 a[i].y = make_s(n + i * 100 + 2)
468 a[i].z = make_s(n + i * 100 + 3)
469 i += 1
470 i = 0
471 while i < 10:
472 check(a[i].x.i == n + i * 100 + 1)
473 check(a[i].y.i == n + i * 100 + 2)
474 check(a[i].z.i == n + i * 100 + 3)
475 i += 1
476 n -= x.foo
477 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
478 return None, f, None
479
480 def test_compile_framework_7_interior(self):
481 self.run('compile_framework_7_interior')
482
483 def define_compile_framework_8(cls):
484 # Array of pointers, of unknown length (test write_barrier_from_array)
485 def before(n, x):
486 return n, x, None, None, None, None, None, None, None, None, [X(123)], None
487 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
488 if n < 1900:
489 check(l[0].x == 123)
490 l = [None] * (16 + (n & 7))
491 l[0] = X(123)
492 l[1] = X(n)
493 l[2] = X(n+10)
494 l[3] = X(n+20)
495 l[4] = X(n+30)
496 l[5] = X(n+40)
497 l[6] = X(n+50)
498 l[7] = X(n+60)
499 l[8] = X(n+70)
500 l[9] = X(n+80)
501 l[10] = X(n+90)
502 l[11] = X(n+100)
503 l[12] = X(n+110)
504 l[13] = X(n+120)
505 l[14] = X(n+130)
506 l[15] = X(n+140)
507 if n < 1800:
508 check(len(l) == 16 + (n & 7))
509 check(l[0].x == 123)
510 check(l[1].x == n)
511 check(l[2].x == n+10)
512 check(l[3].x == n+20)
513 check(l[4].x == n+30)
514 check(l[5].x == n+40)
515 check(l[6].x == n+50)
516 check(l[7].x == n+60)
517 check(l[8].x == n+70)
518 check(l[9].x == n+80)
519 check(l[10].x == n+90)
520 check(l[11].x == n+100)
521 check(l[12].x == n+110)
522 check(l[13].x == n+120)
523 check(l[14].x == n+130)
524 check(l[15].x == n+140)
525 n -= x.foo
526 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
527 def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
528 check(len(l) >= 16)
529 check(l[0].x == 123)
530 check(l[1].x == 2)
531 check(l[2].x == 12)
532 check(l[3].x == 22)
533 check(l[4].x == 32)
534 check(l[5].x == 42)
535 check(l[6].x == 52)
536 check(l[7].x == 62)
537 check(l[8].x == 72)
538 check(l[9].x == 82)
539 check(l[10].x == 92)
540 check(l[11].x == 102)
541 check(l[12].x == 112)
542 check(l[13].x == 122)
543 check(l[14].x == 132)
544 check(l[15].x == 142)
545 return before, f, after
546
547 def test_compile_framework_8(self):
548 self.run('compile_framework_8')
549
550 def define_compile_framework_9(cls):
551 # Like compile_framework_8, but with variable indexes and large
552 # arrays, testing the card_marking case
553 def before(n, x):
554 return n, x, None, None, None, None, None, None, None, None, [X(123)], None
555 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
556 if n < 1900:
557 check(l[0].x == 123)
558 num = 512 + (n & 7)
559 l = [None] * num
560 l[0] = X(123)
561 l[1] = X(n)
562 l[2] = X(n+10)
563 l[3] = X(n+20)
564 l[4] = X(n+30)
565 l[5] = X(n+40)
566 l[6] = X(n+50)
567 l[7] = X(n+60)
568 l[num-8] = X(n+70)
569 l[num-9] = X(n+80)
570 l[num-10] = X(n+90)
571 l[num-11] = X(n+100)
572 l[-12] = X(n+110)
573 l[-13] = X(n+120)
574 l[-14] = X(n+130)
575 l[-15] = X(n+140)
576 if n < 1800:
577 num = 512 + (n & 7)
578 check(len(l) == num)
579 check(l[0].x == 123)
580 check(l[1].x == n)
581 check(l[2].x == n+10)
582 check(l[3].x == n+20)
583 check(l[4].x == n+30)
584 check(l[5].x == n+40)
585 check(l[6].x == n+50)
586 check(l[7].x == n+60)
587 check(l[num-8].x == n+70)
588 check(l[num-9].x == n+80)
589 check(l[num-10].x == n+90)
590 check(l[num-11].x == n+100)
591 check(l[-12].x == n+110)
592 check(l[-13].x == n+120)
593 check(l[-14].x == n+130)
594 check(l[-15].x == n+140)
595 n -= x.foo
596 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
597 def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
598 check(len(l) >= 512)
599 check(l[0].x == 123)
600 check(l[1].x == 2)
601 check(l[2].x == 12)
602 check(l[3].x == 22)
603 check(l[4].x == 32)
604 check(l[5].x == 42)
605 check(l[6].x == 52)
606 check(l[7].x == 62)
607 check(l[-8].x == 72)
608 check(l[-9].x == 82)
609 check(l[-10].x == 92)
610 check(l[-11].x == 102)
611 check(l[-12].x == 112)
612 check(l[-13].x == 122)
613 check(l[-14].x == 132)
614 check(l[-15].x == 142)
615 return before, f, after
616
617 def test_compile_framework_9(self):
618 self.run('compile_framework_9')
619
620 def define_compile_framework_external_exception_handling(cls):
621 def before(n, x):
622 x = X(0)
623 return n, x, None, None, None, None, None, None, None, None, None, None
624
625 @dont_look_inside
626 def g(x):
627 if x > 200:
628 return 2
629 raise ValueError
630 @dont_look_inside
631 def h(x):
632 if x > 150:
633 raise ValueError
634 return 2
635
636 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
637 try:
638 x.x += g(n)
639 except ValueError:
640 x.x += 1
641 try:
642 x.x += h(n)
643 except ValueError:
644 x.x -= 1
645 n -= 1
646 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
647
648 def after(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
649 check(x.x == 1800 * 2 + 150 * 2 + 200 - 1850)
650
651 return before, f, after
652
653 def test_compile_framework_external_exception_handling(self):
654 self.run('compile_framework_external_exception_handling')
655
656 def define_compile_framework_bug1(self):
657 @elidable
658 def nonmoving():
659 x = X(1)
660 for i in range(7):
661 rgc.collect()
662 return x
663
664 @dont_look_inside
665 def do_more_stuff():
666 x = X(5)
667 for i in range(7):
668 rgc.collect()
669 return x
670
671 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
672 x0 = do_more_stuff()
673 check(nonmoving().x == 1)
674 n -= 1
675 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
676
677 return None, f, None
678
679 def test_compile_framework_bug1(self):
680 self.run('compile_framework_bug1', 200)
681
682 def define_compile_framework_vref(self):
683 from rpython.rlib.jit import virtual_ref, virtual_ref_finish
684 class A:
685 pass
686 glob = A()
687 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
688 a = A()
689 glob.v = vref = virtual_ref(a)
690 virtual_ref_finish(vref, a)
691 n -= 1
692 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
693 return None, f, None
694
695 def test_compile_framework_vref(self):
696 self.run('compile_framework_vref', 200)
697
698 def define_compile_framework_float(self):
699 # test for a bug: the fastpath_malloc does not save and restore
700 # xmm registers around the actual call to the slow path
701 class A:
702 x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = 0
703 @dont_look_inside
704 def escape1(a):
705 a.x0 += 0
706 a.x1 += 6
707 a.x2 += 12
708 a.x3 += 18
709 a.x4 += 24
710 a.x5 += 30
711 a.x6 += 36
712 a.x7 += 42
713 @dont_look_inside
714 def escape2(n, f0, f1, f2, f3, f4, f5, f6, f7):
715 check(f0 == n + 0.0)
716 check(f1 == n + 0.125)
717 check(f2 == n + 0.25)
718 check(f3 == n + 0.375)
719 check(f4 == n + 0.5)
720 check(f5 == n + 0.625)
721 check(f6 == n + 0.75)
722 check(f7 == n + 0.875)
723 @unroll_safe
724 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
725 i = 0
726 while i < 42:
727 m = n + i
728 f0 = m + 0.0
729 f1 = m + 0.125
730 f2 = m + 0.25
731 f3 = m + 0.375
732 f4 = m + 0.5
733 f5 = m + 0.625
734 f6 = m + 0.75
735 f7 = m + 0.875
736 a1 = A()
737 # at this point, all or most f's are still in xmm registers
738 escape1(a1)
739 escape2(m, f0, f1, f2, f3, f4, f5, f6, f7)
740 i += 1
741 n -= 1
742 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
743 return None, f, None
744
745 def test_compile_framework_float(self):
746 self.run('compile_framework_float')
747
748 def define_compile_framework_minimal_size_in_nursery(self):
749 S = lltype.GcStruct('S') # no fields!
750 T = lltype.GcStruct('T', ('i', lltype.Signed))
751 @unroll_safe
752 def f42(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s):
753 lst1 = []
754 lst2 = []
755 i = 0
756 while i < 42:
757 s1 = lltype.malloc(S)
758 t1 = lltype.malloc(T)
759 t1.i = 10000 + i + n
760 lst1.append(s1)
761 lst2.append(t1)
762 i += 1
763 i = 0
764 while i < 42:
765 check(lst2[i].i == 10000 + i + n)
766 i += 1
767 n -= 1
768 return n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s
769 return None, f42, None
770
771 def test_compile_framework_minimal_size_in_nursery(self):
772 self.run('compile_framework_minimal_size_in_nursery')
773
774 def define_compile_framework_call_assembler(self):
775 S = lltype.GcForwardReference()
776 S.become(lltype.GcStruct('S', ('s', lltype.Ptr(S))))
777 driver = JitDriver(greens = [], reds = 'auto', is_recursive=True)
778
779 def f(n, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s0):
780 driver.jit_merge_point()
781 i = 0
782 prev_s = lltype.nullptr(S)
783 while i < 100:
784 s = lltype.malloc(S)
785 s.s = prev_s
786 prev_s = s
787 i += 1
788 return n - 1, x, x0, x1, x2, x3, x4, x5, x6, x7, l, s0
789
790 return None, f, None
791
792 def test_compile_framework_call_assembler(self):
793 self.run('compile_framework_call_assembler')
794
795 def define_pinned_simple(cls):
796 class H:
797 inst = None
798 helper = H()
799
800 @dont_look_inside
801 def get_y():
802 if not helper.inst:
803 helper.inst = Y()
804 helper.inst.x = 101
805 check(rgc.pin(helper.inst))
806 else:
807 check(rgc._is_pinned(helper.inst))
808 return helper.inst
809
810 def fn(n, x, *args):
811 t = get_y()
812 promote(t)
813 t.x += 11
814 n -= 1
815 return (n, x) + args
816
817 return None, fn, None
818
819 def test_pinned_simple(self):
820 self.run('pinned_simple')
821
822 def define_pinned_unpin(cls):
823 class H:
824 inst = None
825 pinned = False
826 count_pinned = 0
827 count_unpinned = 0
828 helper = H()
829
830 @dont_look_inside
831 def get_y(n):
832 if not helper.inst:
833 helper.inst = Y()
834 helper.inst.x = 101
835 helper.pinned = True
836 check(rgc.pin(helper.inst))
837 elif n < 100 and helper.pinned:
838 rgc.unpin(helper.inst)
839 helper.pinned = False
840 #
841 if helper.pinned:
842 check(rgc._is_pinned(helper.inst))
843 helper.count_pinned += 1
844 else:
845 check(not rgc._is_pinned(helper.inst))
846 helper.count_unpinned += 1
847 return helper.inst
848
849 def fn(n, x, *args):
850 t = get_y(n)
851 promote(t)
852 check(t.x == 101)
853 n -= 1
854 return (n, x) + args
855
856 def after(n, x, *args):
857 check(helper.count_pinned > 0)
858 check(helper.count_unpinned > 0)
859 check(not helper.pinned)
860
861 return None, fn, after
862
863 def test_pinned_unpin(self):
864 self.run('pinned_unpin')
865
866 def define_multiple_pinned(cls):
867 class H:
868 inst1 = None
869 inst2 = None
870 inst3 = None
871 initialised = False
872 helper = H()
873
874 @dont_look_inside
875 def get_instances():
876 if not helper.initialised:
877 helper.inst1 = Y()
878 helper.inst1.x = 101
879 check(rgc.pin(helper.inst1))
880 #
881 helper.inst2 = Y()
882 helper.inst2.x = 102
883 #
884 helper.inst3 = Y()
885 helper.inst3.x = 103
886 check(rgc.pin(helper.inst3))
887 #
888 helper.initialised = True
889 #
890 check(rgc._is_pinned(helper.inst1))
891 check(not rgc._is_pinned(helper.inst2))
892 check(rgc._is_pinned(helper.inst3))
893 return (helper.inst1, helper.inst2, helper.inst3)
894
895 def fn(n, x, *args):
896 inst1, inst2, inst3 = get_instances()
897 promote(inst1)
898 promote(inst2)
899 promote(inst3)
900 #
901 check(inst1.x == 101)
902 check(inst2.x == 102)
903 check(inst3.x == 103)
904 #
905 n -= 1
906 return (n, x) + args
907
908 return None, fn, None
909
910 def test_multiple_pinned(self):
911 self.run('multiple_pinned')