Source

pypy / pypy / jit / metainterp / test / test_send.py

Full commit
  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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import py
from pypy.rlib.jit import JitDriver, promote, elidable
from pypy.jit.codewriter.policy import StopAtXPolicy
from pypy.jit.metainterp.test.support import LLJitMixin, OOJitMixin

class SendTests(object):
    
    def test_green_send(self):
        myjitdriver = JitDriver(greens = ['i'], reds = ['counter'])
        lst = ["123", "45"]
        def f(i):
            counter = 20
            c = 0
            while counter > 0:
                myjitdriver.can_enter_jit(counter=counter, i=i)
                myjitdriver.jit_merge_point(counter=counter, i=i)
                s = lst[i]
                c = len(s)
                counter -= 1
            return c
        res = self.meta_interp(f, [1])
        assert res == 2
        self.check_loops({'jump': 1,
                          'int_sub': 1, 'int_gt' : 1,
                          'guard_true': 1})    # all folded away

    def test_red_builtin_send(self):
        myjitdriver = JitDriver(greens = [], reds = ['i', 'counter'])
        lst = [{1:1, 2:2, 3:3}, {4:4, 5:5}]
        def externfn(i):
            return lst[i]
        def f(i):
            counter = 20
            res = 0
            while counter > 0:
                myjitdriver.can_enter_jit(counter=counter, i=i)
                myjitdriver.jit_merge_point(counter=counter, i=i)
                dct = externfn(i)
                res = len(dct)
                counter -= 1
            return res
        res = self.meta_interp(f, [1], policy=StopAtXPolicy(externfn))
        assert res == 2
        if self.type_system == 'ootype':
            self.check_loops(call=1, oosend=1) # 'len' remains
        else:
            # 'len' becomes a getfield('num_items') for now in lltype,
            # which is itself encoded as a 'getfield_gc'
            self.check_loops(call=1, getfield_gc=1)

    def test_send_to_single_target_method(self):
        myjitdriver = JitDriver(greens = [], reds = ['i', 'counter'])
        class Foo:
            def meth(self, y):
                return self.x + y
        def externfn(i):
            foo = Foo()
            foo.x = i * 42
            return foo
        def f(i):
            counter = 20
            res = 0
            while counter > 0:
                myjitdriver.can_enter_jit(counter=counter, i=i)
                myjitdriver.jit_merge_point(counter=counter, i=i)
                foo = externfn(i)
                res = foo.meth(i)
                counter -= 1
            return res
        res = self.meta_interp(f, [1], policy=StopAtXPolicy(externfn),
                               backendopt=True)
        assert res == 43
        self.check_loops({'call': 1, 'guard_no_exception': 1,
                          'getfield_gc': 1,
                          'int_add': 1,
                          'jump': 1, 'int_gt' : 1, 'guard_true' : 1,
                          'int_sub' : 1})

    def test_red_send_to_green_receiver(self):
        myjitdriver = JitDriver(greens = ['i'], reds = ['counter', 'j'])
        class Foo(object):
            def meth(self, i):
                return 42 + i
        class Foobar(Foo):
            def meth(self, i):
                return 146 + i
        lst = [Foo(), Foo(), Foobar(), Foo(), Foobar(), Foo()]
        def f(i, j):
            counter = 20
            res = 0
            while counter > 0:
                myjitdriver.can_enter_jit(counter=counter, i=i, j=j)
                myjitdriver.jit_merge_point(counter=counter, i=i, j=j)
                foo = lst[i]
                res = foo.meth(j)
                counter -= 1
            return res
        res = self.meta_interp(f, [4, -1])
        assert res == 145
        self.check_loops(int_add = 1, everywhere=True)

    def test_oosend_base(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'w'])
        class Base:
            pass
        class W1(Base):
            def __init__(self, x):
                self.x = x
            def incr(self):
                return W1(self.x + 1)
            def getvalue(self):
                return self.x
        class W2(Base):
            def __init__(self, y):
                self.y = y
            def incr(self):
                return W2(self.y + 100)
            def getvalue(self):
                return self.y
        def f(x, y):
            if x & 1:
                w = W1(x)
            else:
                w = W2(x)
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, w=w)
                myjitdriver.jit_merge_point(x=x, y=y, w=w)
                w = w.incr()
                y -= 1
            return w.getvalue()
        res = self.meta_interp(f, [3, 14])
        assert res == 17
        res = self.meta_interp(f, [4, 14])
        assert res == 1404
        self.check_loops(guard_class=0, new_with_vtable=0, new=0)

    def test_three_receivers(self):
        myjitdriver = JitDriver(greens = [], reds = ['y'])
        class Base:
            pass
        class W1(Base):
            def foo(self):
                return 1
        class W2(Base):
            def foo(self):
                return 2
        class W3(Base):
            def foo(self):
                return 3
        def externfn(y):
            lst = [W1, W1, W2, W2, W3, W3, W2, W1, W3]
            W = lst[y % len(lst)]
            return W()
        def f(y):
            while y > 0:
                myjitdriver.can_enter_jit(y=y)
                myjitdriver.jit_merge_point(y=y)                
                w = externfn(y)
                w.foo()
                y -= 1
            return 42
        policy = StopAtXPolicy(externfn)

        for j in range(69, 75):
            res = self.meta_interp(f, [j], policy=policy)
            assert res == 42
            self.check_enter_count_at_most(5)
            self.check_loop_count_at_most(5)

    def test_oosend_guard_failure(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'w'])
        class Base:
            pass
        class W1(Base):
            def __init__(self, x):
                self.x = x
            def incr(self):
                return W2(self.x + 1)
            def getvalue(self):
                return self.x
        class W2(Base):
            def __init__(self, y):
                self.y = y
            def incr(self):
                return W1(self.y + 100)
            def getvalue(self):
                return self.y
        def f(x, y):
            if x & 1:
                w = W1(x)
            else:
                w = W2(x)
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, w=w)
                myjitdriver.jit_merge_point(x=x, y=y, w=w)
                w = w.incr()
                y -= 1
            return w.getvalue()
        res = self.meta_interp(f, [3, 28])
        assert res == f(3, 28)
        res = self.meta_interp(f, [4, 28])
        assert res == f(4, 28)
        # This checks that the loop was originally aborted due to an
        # InvalidLoop condition, and was then unrolled, giving two copies
        # of the body in a single bigger loop with no failing guard except
        # the final one.
        self.check_loop_count(1)
        self.check_loops(guard_class=0,
                                int_add=2, int_sub=2)
        self.check_jumps(14)

    def test_oosend_guard_failure_2(self):
        # same as above, but using prebuilt objects 'w1' and 'w2'
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'w'])
        class Base:
            pass
        class W1(Base):
            def __init__(self, x):
                self.x = x
            def incr(self):
                return W2(self.x + 1)
            def getvalue(self):
                return self.x
        class W2(Base):
            def __init__(self, y):
                self.y = y
            def incr(self):
                return W1(self.y + 100)
            def getvalue(self):
                return self.y
        w1 = W1(10)
        w2 = W2(20)

        def f(x, y):
            if x & 1:
                w = w1
            else:
                w = w2
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, w=w)
                myjitdriver.jit_merge_point(x=x, y=y, w=w)
                w = w.incr()
                y -= 1
            return w.getvalue()
        res = self.meta_interp(f, [3, 28])
        assert res == f(3, 28)
        res = self.meta_interp(f, [4, 28])
        assert res == f(4, 28)
        self.check_loop_count(1)
        self.check_loops(guard_class=0,
                                int_add=2, int_sub=2)
        self.check_jumps(14)

    def test_oosend_different_initial_class(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'w'])
        class Base:
            pass
        class W1(Base):
            def __init__(self, x):
                self.x = x
            def incr(self):
                return W2(self.x + 1)
            def getvalue(self):
                return self.x
        class W2(Base):
            def __init__(self, y):
                self.y = y
            def incr(self):
                return W2(self.y * 2)
            def getvalue(self):
                return self.y
        def f(x, y):
            w = W1(x)
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, w=w)
                myjitdriver.jit_merge_point(x=x, y=y, w=w)
                w = w.incr()
                y -= 1
            return w.getvalue()
        res = self.meta_interp(f, [3, 28])
        assert res == f(3, 28)
        # The effect of the ClassGuard generated by the oosend to incr()
        # should be to unroll the first iteration of the loop.  Indeed,
        # looking only at the loop, we deduce that the class of 'w' is 'W2'.
        # However, this doesn't match the initial value of 'w'.
        # XXX This not completely easy to check...
        self.check_loop_count(1)
        self.check_loops(int_add=0, int_lshift=1, guard_class=0,
                         new_with_vtable=0, new=0)

    def test_indirect_call_unknown_object_1(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y'])
        def getvalue2():
            return 2
        def getvalue25():
            return 25
        def getvalue1001():
            return -1001
        def externfn(n):
            if n % 5:
                return getvalue2
            elif n % 7:
                return getvalue25
            else:
                return getvalue1001
        def f(y):
            x = 0
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y)
                myjitdriver.jit_merge_point(x=x, y=y)
                x += externfn(y)()
                y -= 1
            return x
        res = self.meta_interp(f, [198], policy=StopAtXPolicy(externfn))
        assert res == f(198)
        self.check_loop_count(4)

    def test_indirect_call_unknown_object_2(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'state'])
        def getvalue2():
            return 2
        def getvalue25():
            return 25
        def getvalue1001():
            return -1001

        class State:
            count = 0
            def externfn(self, n):
                assert n == 198 - self.count
                self.count += 1
                if n % 5:
                    return getvalue2
                elif n % 7:
                    return getvalue25
                else:
                    return getvalue1001
        def f(y):
            state = State()
            x = 0
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, state=state)
                myjitdriver.jit_merge_point(x=x, y=y, state=state)
                x += state.externfn(y)()
                y -= 1
            return x
        res = self.meta_interp(f, [198],
                               policy=StopAtXPolicy(State.externfn.im_func))
        assert res == f(198)
        # we get two TreeLoops: an initial one, and one entering from
        # the interpreter
        self.check_tree_loop_count(2)

    def test_indirect_call_unknown_object_3(self):
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'z', 'state'])
        def getvalue2():
            return 2
        def getvalue25():
            return 25
        def getvalue1001():
            return -1001

        class State:
            count = 0
            def externfn(self, n):
                assert n == 198 - self.count
                self.count += 1
                if n % 5:
                    return getvalue2
                elif n % 7:
                    return getvalue25
                else:
                    return getvalue1001
        def f(y):
            state = State()
            x = z = 0
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y, z=z, state=state)
                myjitdriver.jit_merge_point(x=x, y=y, z=z, state=state)
                x += z
                z = state.externfn(y)()
                y -= 1
            return x
        res = self.meta_interp(f, [198],
                               policy=StopAtXPolicy(State.externfn.im_func))
        assert res == f(198)
        # we get two TreeLoops: an initial one, and one entering from
        # the interpreter
        self.check_tree_loop_count(2)

    def test_two_behaviors(self):
        py.test.skip("XXX fix me!!!!!!! problem in optimize.py")
        myjitdriver = JitDriver(greens = [], reds = ['x', 'y'])
        class Int:
            def __init__(self, value):
                self.value = value
        cases = [True]*100 + [False, True]*10 + [False]*20
        def f(y):
            x = Int(0)
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y)
                myjitdriver.jit_merge_point(x=x, y=y)
                y -= 1
                if cases[y]:
                    x = Int(x.value + 1)
            return x.value
        res = self.meta_interp(f, [len(cases)])
        assert res == 110
        # The generated loops don't contain a new_with_vtable at all.  This
        # is true if we replace "if cases[y]" above with "if not cases[y]"
        # -- so there is no good reason that it fails.
        self.check_loops(new_with_vtable=0)
        self.check_loop_count(2)

    def test_behavior_change_after_a_while(self):
        myjitdriver = JitDriver(greens = [], reds = ['y', 'x'])
        class Base:
            def __init__(self, value):
                self.value = value
        class Int1(Base):
            pass
        class Int2(Base):
            pass
        cases = [False, True, True, True, True]*40
        def f(y):
            x = Int1(0)
            while y > 0:
                myjitdriver.can_enter_jit(x=x, y=y)
                myjitdriver.jit_merge_point(x=x, y=y)
                y -= 1
                value = x.value + 1
                if cases[y]:
                    x = Int1(value)
                else:
                    x = Int2(value)
            return x.value
        res = self.meta_interp(f, [len(cases)])
        assert res == 200
        # we expect 1 loop, 1 entry bridge, and 1 bridge going from the
        # loop back to the start of the entry bridge
        self.check_loop_count(2)        # 1 loop + 1 bridge
        self.check_tree_loop_count(2)   # 1 loop + 1 entry bridge  (argh)
        self.check_aborted_count(0)

    def test_three_cases(self):
        class Node:
            def __init__(self, x):
                self.x = x
        myjitdriver = JitDriver(greens = [], reds = ['node'])
        def f(n):
            node = Node(n)
            while node.x > 0:
                myjitdriver.can_enter_jit(node=node)
                myjitdriver.jit_merge_point(node=node)
                if node.x < 40:
                    if node.x < 20:
                        node = Node(node.x - 1)
                    node = Node(node.x - 1)
                node = Node(node.x - 1)
            return node.x
        res = self.meta_interp(f, [55])
        assert res == f(55)
        self.check_tree_loop_count(4)

    def test_three_classes(self):
        class Base:
            pass
        class A(Base):
            def f(self):
                return 1
        class B(Base):
            def f(self):
                return 2
        class C(Base):
            def f(self):
                return 3
        myjitdriver = JitDriver(greens = [], reds = ['n'])
        def extern(n):
            if n > 40:
                return A()
            elif n > 20:
                return B()
            else:
                return C()
        def f(n):
            while n > 0:
                myjitdriver.can_enter_jit(n=n)
                myjitdriver.jit_merge_point(n=n)
                n -= extern(n).f()
            return n
        res = self.meta_interp(f, [55], policy=StopAtXPolicy(extern))
        assert res == f(55)
        self.check_tree_loop_count(2)

    def test_bug1(self):
        myjitdriver = JitDriver(greens = [], reds = ['n', 'node'])
        class Base:
            pass
        class A(Base):
            def decr(self, n):
                return n - 2
        class B(Base):
            def __init__(self, n):
                self.n = n
            def decr(self, n):
                assert n == self.n
                return self.n - 1
        def extern(n):
            if n <= 21:
                return B(n)
            else:
                return A()
        def f(n):
            node = A()
            while n >= 0:
                myjitdriver.can_enter_jit(node=node, n=n)
                myjitdriver.jit_merge_point(node=node, n=n)
                n = node.decr(n)
                node = extern(n)
            return n
        res = self.meta_interp(f, [60], policy=StopAtXPolicy(extern))
        assert res == f(60)

    def test_recursive_call_to_portal_from_blackhole(self):
        from pypy.rpython.annlowlevel import hlstr
        
        myjitdriver = JitDriver(greens = ['k'], reds = ['n'])
        def f(n, k):
            while n >= 0:
                myjitdriver.can_enter_jit(n=n, k=k)
                myjitdriver.jit_merge_point(n=n, k=k)
                if n == 3 and k == 0:
                    return f(10, 1)
                n -= 1
            if k == 1:
                return "string"
            return "xyz"

        res = self.meta_interp(f, [20, 0])
        assert hlstr(res) == "string"

    def test_residual_oosend(self):
        myjitdriver = JitDriver(greens=[], reds = ['i', 'obj'])
        class A:
            def foo(self):
                return 41
        class B(A):
            def foo(self):
                return 42
        def new(n):
            if n:
                return A()
            else:
                return B()
        def fn(n, i):
            res = 0
            obj = new(n)
            while i > 0:
                myjitdriver.can_enter_jit(i=i, obj=obj)
                myjitdriver.jit_merge_point(i=i, obj=obj)
                res = obj.foo()
                i-=1
            return res

        policy = StopAtXPolicy(new, A.foo.im_func, B.foo.im_func)
        res = self.meta_interp(fn, [0, 20], policy=policy)
        assert res == 42
        if self.type_system == 'ootype':
            self.check_loops(oosend=1)
        else:
            self.check_loops(call=1)


    def test_residual_oosend_with_void(self):
        myjitdriver = JitDriver(greens=[], reds = ['i', 'obj'])
        class A:
            def foo(self, other):
                return 41
            def _freeze_(self):
                return True
        def new(n):
            if n:
                return A()
            else:
                return None
        pbc = A()
        def fn(n, i):
            res = 0
            obj = new(n)
            while i > 0:
                myjitdriver.can_enter_jit(i=i, obj=obj)
                myjitdriver.jit_merge_point(i=i, obj=obj)
                res = obj.foo(pbc)
                i-=1
            return res
        policy = StopAtXPolicy(new, A.foo.im_func)
        res = self.meta_interp(fn, [1, 20], policy=policy)
        assert res == 41
        if self.type_system == 'ootype':
            self.check_loops(oosend=1)
        else:
            self.check_loops(call=1)

    def test_constfold_pure_oosend(self):
        myjitdriver = JitDriver(greens=[], reds = ['i', 'obj'])
        class A:
            @elidable
            def foo(self):
                return 42
        def fn(n, i):
            res = 0
            obj = A()
            while i > 0:
                myjitdriver.can_enter_jit(i=i, obj=obj)
                myjitdriver.jit_merge_point(i=i, obj=obj)
                promote(obj)
                res = obj.foo()
                i-=1
            return res
        policy = StopAtXPolicy(A.foo.im_func)
        res = self.meta_interp(fn, [1, 20], policy=policy)
        assert res == 42
        if self.type_system == 'ootype':
            self.check_loops(oosend=0)
        else:
            self.check_loops(call=0)

    def test_generalize_loop(self):
        myjitdriver = JitDriver(greens=[], reds = ['i', 'obj'])
        class A:
            def __init__(self, n):
                self.n = n
        def extern(obj):
            pass
        def fn(i):
            obj = A(1)
            while i > 0:
                myjitdriver.can_enter_jit(i=i, obj=obj)
                myjitdriver.jit_merge_point(i=i, obj=obj)
                obj = A(obj.n + 1)
                if i < 10:
                    extern(obj)
                i -= 1
            return obj.n
        res = self.meta_interp(fn, [20], policy=StopAtXPolicy(extern))
        assert res == 21


class TestOOtype(SendTests, OOJitMixin):
    pass

class TestLLtype(SendTests, LLJitMixin):
    pass