[pypy-svn] r73905 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 20 14:36:06 CEST 2010


Author: arigo
Date: Tue Apr 20 14:36:04 2010
New Revision: 73905

Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/codewriter.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/regalloc.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py
Log:
Perform three regallocs on each graph: for ints, for refs and for floats.


Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py	Tue Apr 20 14:36:04 2010
@@ -35,7 +35,6 @@
         #
         all_funcs = []
         for key in self._insns:
-            assert key is not None, "hole!"
             assert key.count('/') == 1, "bad key: %r" % (key,)
             name, argcodes = key.split('/')
             all_funcs.append(self._get_method(name, argcodes))

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/codewriter.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/codewriter.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/codewriter.py	Tue Apr 20 14:36:04 2010
@@ -3,6 +3,8 @@
 from pypy.jit.codewriter.flatten import flatten_graph
 from pypy.jit.codewriter.assembler import Assembler
 
+KINDS = ['int', 'ref', 'float']
+
 
 class CodeWriter(object):
 
@@ -16,7 +18,9 @@
         return self.transform_graph_to_jitcode(graph)
 
     def transform_graph_to_jitcode(self, graph):
-        regalloc = perform_register_allocation(graph)
-        ssarepr = flatten_graph(graph, regalloc)
+        regallocs = {}
+        for kind in KINDS:
+            regallocs[kind] = perform_register_allocation(graph, kind)
+        ssarepr = flatten_graph(graph, regallocs)
         jitcode = self.assembler.assemble(ssarepr)
         return jitcode

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	Tue Apr 20 14:36:04 2010
@@ -1,4 +1,5 @@
 from pypy.objspace.flow.model import Variable
+from pypy.jit.metainterp.history import getkind
 
 
 class SSARepr(object):
@@ -28,10 +29,10 @@
 
 # ____________________________________________________________
 
-def flatten_graph(graph, regalloc):
+def flatten_graph(graph, regallocs):
     """Flatten the graph into an SSARepr, with already-computed register
-    allocations."""
-    flattener = GraphFlattener(graph, regalloc)
+    allocations.  'regallocs' in a dict {kind: RegAlloc}."""
+    flattener = GraphFlattener(graph, regallocs)
     flattener.enforce_input_args()
     flattener.generate_ssa_form()
     return flattener.assembler
@@ -39,20 +40,24 @@
 
 class GraphFlattener(object):
 
-    def __init__(self, graph, regalloc):
+    def __init__(self, graph, regallocs):
         self.graph = graph
-        self.regalloc = regalloc
+        self.regallocs = regallocs
         self.registers = {}
 
     def enforce_input_args(self):
         inputargs = self.graph.startblock.inputargs
-        for i in range(len(inputargs)):
-            col = self.regalloc.getcolor(inputargs[i])
-            if col != i:
-                assert col > i
-                self.regalloc.swapcolors(i, col)
-        for i in range(len(inputargs)):
-            assert self.regalloc.getcolor(inputargs[i]) == i
+        numkinds = {}
+        for v in inputargs:
+            kind = getkind(v.concretetype)
+            if kind == 'void':
+                continue
+            curcol = self.regallocs[kind].getcolor(v)
+            realcol = numkinds.get(kind, 0)
+            numkinds[kind] = realcol + 1
+            if curcol != realcol:
+                assert curcol > realcol
+                self.regallocs[kind].swapcolors(realcol, curcol)
 
     def generate_ssa_form(self):
         self.assembler = SSARepr(self.graph.name)
@@ -162,7 +167,8 @@
         self.emitline(op.opname, *args)
 
     def getcolor(self, v):
-        col = self.regalloc.getcolor(v)
+        kind = getkind(v.concretetype)
+        col = self.regallocs[kind].getcolor(v)    # if kind=='void', fix caller
         try:
             r = self.registers[col]
         except KeyError:

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/regalloc.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/regalloc.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/regalloc.py	Tue Apr 20 14:36:04 2010
@@ -2,10 +2,12 @@
 from pypy.objspace.flow.model import Variable
 from pypy.tool.algo.color import DependencyGraph
 from pypy.tool.algo.unionfind import UnionFind
+from pypy.jit.metainterp.history import getkind
 
-
-def perform_register_allocation(graph):
-    regalloc = RegAllocator(graph)
+def perform_register_allocation(graph, kind):
+    """Perform register allocation for the Variables of the given 'kind'
+    in the 'graph'."""
+    regalloc = RegAllocator(graph, kind)
     regalloc.make_dependencies()
     regalloc.coalesce_variables()
     regalloc.find_node_coloring()
@@ -15,8 +17,9 @@
 class RegAllocator(object):
     DEBUG_REGALLOC = False
 
-    def __init__(self, graph):
+    def __init__(self, graph, kind):
         self.graph = graph
+        self.kind = kind
 
     def make_dependencies(self):
         dg = DependencyGraph()
@@ -38,20 +41,23 @@
                 dg.add_node(v)
                 for j in range(i):
                     dg.add_edge(block.inputargs[j], v)
-            livevars = set(block.inputargs)
             die_at = [(value, key) for (key, value) in die_at.items()]
             die_at.sort()
             die_at.append((sys.maxint,))
+            # Done.  XXX the code above this line runs 3 times
+            # (for kind in KINDS) to produce the same result...
+            livevars = set(block.inputargs)
             die_index = 0
             for i, op in enumerate(block.operations):
                 while die_at[die_index][0] == i:
                     livevars.remove(die_at[die_index][1])
                     die_index += 1
-                if op.result is not None:
+                if getkind(op.result.concretetype) == self.kind:
                     livevars.add(op.result)
                     dg.add_node(op.result)
                     for v in livevars:
-                        dg.add_edge(v, op.result)
+                        if getkind(v.concretetype) == self.kind:
+                            dg.add_edge(v, op.result)
         self._depgraph = dg
 
     def coalesce_variables(self):
@@ -68,7 +74,8 @@
             # middle during blackholing.
             for link in block.exits:
                 for i, v in enumerate(link.args):
-                    if isinstance(v, Variable):
+                    if (isinstance(v, Variable) and
+                        getkind(v.concretetype) == self.kind):
                         w = link.target.inputargs[i]
                         v0 = uf.find_rep(v)
                         w0 = uf.find_rep(w)

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	Tue Apr 20 14:36:04 2010
@@ -24,7 +24,7 @@
 
     def encoding_test(self, func, args, expected, optimize=True):
         graphs = self.make_graphs(func, args)
-        ssarepr = flatten_graph(graphs[0], FakeRegAlloc())
+        ssarepr = flatten_graph(graphs[0], {'int': FakeRegAlloc()})
         asm = format_assembler(ssarepr)
         expected = str(py.code.Source(expected)).strip() + '\n'
         assert asm == expected

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py	Tue Apr 20 14:36:04 2010
@@ -12,8 +12,11 @@
         return self.rtyper.annotator.translator.graphs
 
     def check_assembler(self, graph, expected):
-        regalloc = perform_register_allocation(graph)
-        ssarepr = flatten_graph(graph, regalloc)
+        """Only for simple graphs.  More complex graphs must first be
+        transformed by jitter.py before they can be subjected to
+        register allocation and flattening."""
+        regalloc = perform_register_allocation(graph, 'int')
+        ssarepr = flatten_graph(graph, {'int': regalloc})
         asm = format_assembler(ssarepr)
         assert asm == str(py.code.Source(expected).strip()) + '\n'
 
@@ -21,13 +24,23 @@
         def f(a, b):
             return a + b
         graph = self.make_graphs(f, [5, 6])[0]
-        regalloc = perform_register_allocation(graph)
+        regalloc = perform_register_allocation(graph, 'int')
         va, vb = graph.startblock.inputargs
         vc = graph.startblock.operations[0].result
         assert regalloc.getcolor(va) == 0
         assert regalloc.getcolor(vb) == 1
         assert regalloc.getcolor(vc) == 0
 
+    def test_regalloc_void(self):
+        def f(a, b):
+            while a > 0:
+                b += a
+                a -= 1
+            return b
+        graph = self.make_graphs(f, [5, 6])[0]
+        regalloc = perform_register_allocation(graph, 'float')
+        # assert did not crash
+
     def test_regalloc_loop(self):
         def f(a, b):
             while a > 0:



More information about the Pypy-commit mailing list