Author: Carl Friedrich Bolz <cfb...@gmx.de>
Branch: optinfo-into-bridges
Changeset: r87811:5c26cc66adc6
Date: 2016-10-15 13:01 +0200
http://bitbucket.org/pypy/pypy/changeset/5c26cc66adc6/

Log:    a general resume improvement: move the rd_count into the resumecode

diff --git a/rpython/jit/metainterp/compile.py 
b/rpython/jit/metainterp/compile.py
--- a/rpython/jit/metainterp/compile.py
+++ b/rpython/jit/metainterp/compile.py
@@ -863,10 +863,9 @@
 
 
 class ResumeGuardDescr(AbstractResumeGuardDescr):
-    _attrs_ = ('rd_numb', 'rd_count', 'rd_consts', 'rd_virtuals',
+    _attrs_ = ('rd_numb', 'rd_consts', 'rd_virtuals',
                'rd_pendingfields', 'status')
     rd_numb = lltype.nullptr(NUMBERING)
-    rd_count = 0
     rd_consts = None
     rd_virtuals = None
     rd_pendingfields = lltype.nullptr(PENDINGFIELDSP.TO)
@@ -875,7 +874,6 @@
         if isinstance(other, ResumeGuardCopiedDescr):
             other = other.prev
         assert isinstance(other, ResumeGuardDescr)
-        self.rd_count = other.rd_count
         self.rd_consts = other.rd_consts
         self.rd_pendingfields = other.rd_pendingfields
         self.rd_virtuals = other.rd_virtuals
@@ -888,7 +886,6 @@
 
     def store_final_boxes(self, guard_op, boxes, metainterp_sd):
         guard_op.setfailargs(boxes)
-        self.rd_count = len(boxes)
         self.store_hash(metainterp_sd)
 
     def clone(self):
diff --git a/rpython/jit/metainterp/resume.py b/rpython/jit/metainterp/resume.py
--- a/rpython/jit/metainterp/resume.py
+++ b/rpython/jit/metainterp/resume.py
@@ -253,7 +253,8 @@
     def number(self, optimizer, position, trace):
         snapshot_iter = trace.get_snapshot_iter(position)
         numb_state = NumberingState(snapshot_iter.size)
-        numb_state.append_int(0) # patch later
+        numb_state.append_int(0) # patch later: size of resume section
+        numb_state.append_int(0) # patch later: number of failargs
 
         arr = snapshot_iter.vable_array
 
@@ -458,6 +459,8 @@
         self._number_virtuals(liveboxes, optimizer, num_virtuals)
         self._add_pending_fields(optimizer, pending_setfields)
 
+        numb_state.patch(1, len(liveboxes))
+
         self._add_optimizer_sections(numb_state, liveboxes, liveboxes_from_env)
         storage.rd_numb = numb_state.create_numbering()
         storage.rd_consts = self.memo.consts
@@ -928,7 +931,7 @@
         self.resumecodereader = resumecode.Reader(storage.rd_numb)
         count = self.resumecodereader.next_item()
         self.items_resume_section = count
-        self.count = storage.rd_count
+        self.count = self.resumecodereader.next_item()
         self.consts = storage.rd_consts
 
     def _prepare(self, storage):
@@ -1072,7 +1075,7 @@
         self._init(metainterp.cpu, storage)
         self.deadframe = deadframe
         self.metainterp = metainterp
-        self.liveboxes = [None] * storage.rd_count
+        self.liveboxes = [None] * self.count
         self._prepare(storage)
 
     def consume_boxes(self, info, boxes_i, boxes_r, boxes_f):
@@ -1263,9 +1266,6 @@
         assert box.type == kind
         return box
 
-    def next_box_of_type(self, kind):
-        return self.decode_box(self.resumecodereader.next_item(), kind)
-
     def load_box_from_cpu(self, num, kind):
         if num < 0:
             num += len(self.liveboxes)
@@ -1294,7 +1294,7 @@
             kind = FLOAT
         else:
             raise AssertionError(kind)
-        return self.decode_box(tagged, kind)
+        return self.decode_box(self.resumecodereader.next_item(), kind)
     next_box_of_type._annspecialcase_ = 'specialize:arg(1)'
 
     def write_an_int(self, index, box):
@@ -1405,7 +1405,6 @@
         # just reset the token, we'll force it later
         vinfo.reset_token_gcref(virtualizable)
         vinfo.write_from_resume_data_partial(virtualizable, self)
-        return index
 
     def load_next_value_of_type(self, TYPE):
         from rpython.jit.metainterp.warmstate import specialize_value
diff --git a/rpython/jit/metainterp/resumecode.py 
b/rpython/jit/metainterp/resumecode.py
--- a/rpython/jit/metainterp/resumecode.py
+++ b/rpython/jit/metainterp/resumecode.py
@@ -2,6 +2,7 @@
 
   # ----- resume section
   [total size of resume section, unencoded]
+  [number of failargs]
   [<length> <virtualizable object> <numb> <numb> <numb>]    if vinfo is not 
None
    -OR-
   [1 <ginfo object>]                                        if ginfo is not 
None
@@ -96,29 +97,29 @@
 
 class Writer(object):
     def __init__(self, size=0):
-        self.current = objectmodel.newlist_hint(3 * size)
-        self.grow(size)
-        self.items = 0
+        self.current = objectmodel.newlist_hint(size)
 
     def append_int(self, item):
-        self.items += 1
-        append_numbering(self.current, item)
+        """ append an item. return the position of the item """
+        if not integer_fits(item):
+            raise TagOverflow
+        self.current.append(item)
+        return len(self.current) - 1
 
     def create_numbering(self):
-        numb = lltype.malloc(NUMBERING, len(self.current))
-        for i, elt in enumerate(self.current):
+        l = []
+        for item in self.current:
+            append_numbering(l, item)
+        numb = lltype.malloc(NUMBERING, len(l))
+        for i, elt in enumerate(l):
             numb.code[i] = elt
         return numb
 
-    def grow(self, size):
-        pass
+    def patch_current_size(self, index):
+        self.patch(index, len(self.current))
 
-    def patch_current_size(self, index):
-        # mess :-(
-        assert self.current[index] == 0
-        l = []
-        append_numbering(l, self.items)
-        self.current = l + self.current[1:]
+    def patch(self, index, item):
+        self.current[index] = item
 
 
 def create_numbering(l):
diff --git a/rpython/jit/metainterp/test/test_resume.py 
b/rpython/jit/metainterp/test/test_resume.py
--- a/rpython/jit/metainterp/test/test_resume.py
+++ b/rpython/jit/metainterp/test/test_resume.py
@@ -36,7 +36,6 @@
     rd_consts = []
     rd_virtuals = None
     rd_pendingfields = None
-    rd_count = 0
 
 
 class FakeOptimizer(object):
@@ -116,10 +115,10 @@
     assert tag((1<<13)-1, 3) == ((1<<15)-1)|3
     assert tag(-1<<13, 3) ==(-1<<15)|3
     py.test.raises(AssertionError, tag, 3, 5)
-    py.test.raises(TagOverflow, tag, 1<<13, 0)
-    py.test.raises(TagOverflow, tag, (1<<13)+1, 0)
-    py.test.raises(TagOverflow, tag, (-1<<13)-1, 0)
-    py.test.raises(TagOverflow, tag, (-1<<13)-5, 0)
+    py.test.raises(TagOverflow, tag, 1<<22, 0)
+    py.test.raises(TagOverflow, tag, (1<<22)+1, 0)
+    py.test.raises(TagOverflow, tag, (-1<<22)-1, 0)
+    py.test.raises(TagOverflow, tag, (-1<<22)-5, 0)
 
 def test_untag():
     assert untag(tag(3, 1)) == (3, 1)
@@ -302,12 +301,11 @@
     c1, c2, c3 = [ConstInt(111), ConstInt(222), ConstInt(333)]
     storage = Storage()
     storage.rd_consts = [c1, c2, c3]
-    numb = Numbering([tag(0, TAGBOX), tagconst(0),
+    numb = Numbering([3, tag(0, TAGBOX), tagconst(0),
                        NULLREF, tag(0, TAGBOX), tag(1, TAGBOX)] +
-                       [tagconst(1), tagconst(2)] + 
+                       [tagconst(1), tagconst(2)] +
                        [tag(0, TAGBOX), tag(1, TAGBOX), tag(2, TAGBOX)])
     storage.rd_numb = numb
-    storage.rd_count = 3
     #
     cpu = MyCPU([42, gcref1, -66])
     metainterp = MyMetaInterp(cpu)
@@ -348,7 +346,7 @@
 def test_simple_read_tagged_ints():
     storage = Storage()
     storage.rd_consts = []
-    numb = Numbering([tag(100, TAGINT)])
+    numb = Numbering([1, tag(100, TAGINT)])
     storage.rd_numb = numb
     #
     cpu = MyCPU([])
@@ -365,10 +363,9 @@
             return s
     class FakeStorage(object):
         rd_virtuals = [FakeVinfo(), None]
-        rd_numb = Numbering([])
+        rd_numb = Numbering([1])
         rd_consts = []
         rd_pendingfields = None
-        rd_count = 0
     class FakeMetainterp(object):
         _already_allocated_resume_virtuals = None
         cpu = None
@@ -776,12 +773,12 @@
     assert untag(tagged) == (44, TAGINT)
     tagged = memo.getconst(ConstInt(-3))
     assert untag(tagged) == (-3, TAGINT)
-    const = ConstInt(50000)
+    const = ConstInt(5000000)
     tagged = memo.getconst(const)
     index, tagbits = untag(tagged)
     assert tagbits == TAGCONST
     assert memo.consts[index - TAG_CONST_OFFSET] is const
-    tagged = memo.getconst(ConstInt(50000))
+    tagged = memo.getconst(ConstInt(5000000))
     index2, tagbits = untag(tagged)
     assert tagbits == TAGCONST
     assert index2 == index
@@ -861,7 +858,7 @@
     base = [0, 0, tag(0, TAGBOX), tag(1, TAGINT),
             tag(1, TAGBOX), tag(0, TAGBOX), tag(2, TAGINT)]
 
-    assert unpack_numbering(numb) == [16, 0, 0] + base + [0, 2, tag(3, 
TAGINT), tag(2, TAGBOX),
+    assert unpack_numbering(numb) == [17, 0, 0, 0] + base + [0, 2, tag(3, 
TAGINT), tag(2, TAGBOX),
                                       tag(0, TAGBOX), tag(1, TAGINT)]
     t.append(0)
     snap2 = t.create_top_snapshot(FakeJitCode("jitcode", 0), 2, Frame(env2),
@@ -875,7 +872,7 @@
     assert numb_state2.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX),
                                      b3: tag(2, TAGBOX)}
     assert numb_state2.liveboxes is not numb_state.liveboxes
-    assert unpack_numbering(numb2) == [16, 0, 0] + base + [0, 2, tag(3, 
TAGINT), tag(2, TAGBOX),
+    assert unpack_numbering(numb2) == [17, 0, 0, 0] + base + [0, 2, tag(3, 
TAGINT), tag(2, TAGBOX),
                                        tag(0, TAGBOX), tag(3, TAGINT)]
 
     t.append(0)
@@ -897,7 +894,7 @@
     assert numb_state3.num_virtuals == 0
     
     assert numb_state3.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX)}
-    assert unpack_numbering(numb3) == ([16, 0, 2, tag(3, TAGINT), tag(4, 
TAGINT),
+    assert unpack_numbering(numb3) == ([17, 0, 0, 2, tag(3, TAGINT), tag(4, 
TAGINT),
                                        tag(0, TAGBOX), tag(3, TAGINT)] +
                                        base + [0, 2])
 
@@ -914,7 +911,7 @@
     
     assert numb_state4.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX),
                                      b4: tag(0, TAGVIRTUAL)}
-    assert unpack_numbering(numb4) == [16, 0, 2, tag(3, TAGINT), tag(0, 
TAGVIRTUAL),
+    assert unpack_numbering(numb4) == [17, 0, 0, 2, tag(3, TAGINT), tag(0, 
TAGVIRTUAL),
                                        tag(0, TAGBOX), tag(3, TAGINT)] + base 
+ [0, 2]
 
     t.append(0)
@@ -933,7 +930,7 @@
 
     assert numb_state5.liveboxes == {b1: tag(0, TAGBOX), b2: tag(1, TAGBOX),
                                      b4: tag(0, TAGVIRTUAL), b5: tag(1, 
TAGVIRTUAL)}
-    assert unpack_numbering(numb5) == [21,
+    assert unpack_numbering(numb5) == [22, 0,
         3, tag(0, TAGBOX), tag(0, TAGVIRTUAL), tag(1, TAGVIRTUAL),
         0] + base + [
         2, 1, tag(3, TAGINT), tag(0, TAGVIRTUAL), tag(0, TAGBOX), tag(3, 
TAGINT)
@@ -954,14 +951,15 @@
     l = unpack_numbering(numb)
     assert l[0] == len(l)
     assert l[1] == 0
+    assert l[1] == 0
     assert l[2] == 0
     assert l[3] == 0
     assert l[4] == 0
     mapping = dict(zip(inpargs, i.inputargs))
     for i, item in enumerate(lst):
-        v, tag = untag(l[i + 5])
+        v, tag = untag(l[i + 6])
         if tag == TAGBOX:
-            assert l[i + 5] == numb_state.liveboxes[mapping[item]]
+            assert l[i + 6] == numb_state.liveboxes[mapping[item]]
         elif tag == TAGCONST:
             assert memo.consts[v].getint() == item.getint()
         elif tag == TAGINT:
@@ -1081,7 +1079,7 @@
     _next_section(reader, sys.maxint, 2**16, -65)
 
 def test_virtual_adder_memo_const_sharing():
-    b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**16), ConstInt(-65)]
+    b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**23), ConstInt(-65)]
     storage, t = make_storage(b1s, b2s, b3s)
     metainterp_sd = FakeMetaInterpStaticData()
     memo = ResumeDataLoopMemo(metainterp_sd)
@@ -1091,7 +1089,7 @@
     assert len(memo.consts) == 2
     assert storage.rd_consts is memo.consts
 
-    b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**17), ConstInt(-65)]
+    b1s, b2s, b3s = [ConstInt(sys.maxint), ConstInt(2**24), ConstInt(-65)]
     storage2, t = make_storage(b1s, b2s, b3s)
     i = t.get_iter()
     modifier2 = ResumeDataVirtualAdder(FakeOptimizer(i), storage2, storage2,
@@ -1234,7 +1232,7 @@
     liveboxes = []
     modifier._number_virtuals(liveboxes, FakeOptimizer(), 0)
     storage.rd_consts = memo.consts[:]
-    storage.rd_numb = Numbering([])
+    storage.rd_numb = Numbering([0])
     # resume
     b3t, b5t = [IntFrontendOp(0), RefFrontendOp(0)]
     b5t.setref_base(demo55o)
@@ -1305,7 +1303,7 @@
     modifier._number_virtuals(liveboxes, FakeOptimizer(), 0)
     dump_storage(storage, liveboxes)
     storage.rd_consts = memo.consts[:]
-    storage.rd_numb = Numbering([])
+    storage.rd_numb = Numbering([0])
     # resume
     b1t, b3t, b4t = [IntFrontendOp(0), IntFrontendOp(0), IntFrontendOp(0)]
     b1t.setint(11)
@@ -1358,7 +1356,7 @@
     modifier._number_virtuals(liveboxes, FakeOptimizer(), 0)
     dump_storage(storage, liveboxes)
     storage.rd_consts = memo.consts[:]
-    storage.rd_numb = Numbering([])
+    storage.rd_numb = Numbering([0])
     b4t = RefFrontendOp(0)
     newboxes = _resume_remap(liveboxes, [#b2s -- virtual
                                          b4s], b4t)
@@ -1404,7 +1402,7 @@
     modifier._add_pending_fields(FakeOptimizer(), [
         ResOperation(rop.SETFIELD_GC, [b2s, b4s], 
descr=LLtypeMixin.nextdescr)])
     storage.rd_consts = memo.consts[:]
-    storage.rd_numb = Numbering([])
+    storage.rd_numb = Numbering([0])
     # resume
     demo55.next = lltype.nullptr(LLtypeMixin.NODE)
     b2t = RefFrontendOp(0)
diff --git a/rpython/jit/metainterp/test/test_resumecode.py 
b/rpython/jit/metainterp/test/test_resumecode.py
--- a/rpython/jit/metainterp/test/test_resumecode.py
+++ b/rpython/jit/metainterp/test/test_resumecode.py
@@ -21,8 +21,10 @@
     [13000, 12000, 10000, 256, 255, 254, 257, -3, -1000]
 ]
 
+codelists = strategies.lists(strategies.integers(-2**21, 2**21-1), min_size=1)
+
 def hypothesis_and_examples(func):
-    func = given(strategies.lists(strategies.integers(-2**21, 2**21-1)))(func)
+    func = given(codelists)(func)
     for ex in examples:
         func = example(ex)(func)
     return func
@@ -56,7 +58,7 @@
         assert unpack_numbering(n) == l
 
 @hypothesis_and_examples
-def test_patch(l):
+def test_patch_current_size(l):
     for middle in range(len(l)):
         l1 = l[:middle]
         l2 = l[middle:]
@@ -71,4 +73,18 @@
         assert unpack_numbering(n)[1:] == l
         assert unpack_numbering(n)[0] == middle + 1
 
+@hypothesis_and_examples
+def test_patch(l):
+    item = l[0]
+    l = l[1:]
+    for middle in range(len(l)):
+        output = l[:]
+        output[middle] = item
+        w = Writer(len(l))
+        for i, num in enumerate(l):
+            index = w.append_int(num)
+            assert index == i
+        w.patch(middle, item)
+        n = w.create_numbering()
+        assert unpack_numbering(n) == output
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to