[pypy-svn] r20563 - pypy/dist/pypy/translator/backendopt/test

pedronis at codespeak.net pedronis at codespeak.net
Fri Dec 2 02:23:46 CET 2005


Author: pedronis
Date: Fri Dec  2 02:23:45 2005
New Revision: 20563

Modified:
   pypy/dist/pypy/translator/backendopt/test/test_all.py
   pypy/dist/pypy/translator/backendopt/test/test_inline.py
   pypy/dist/pypy/translator/backendopt/test/test_malloc.py
   pypy/dist/pypy/translator/backendopt/test/test_propagate.py
   pypy/dist/pypy/translator/backendopt/test/test_removenoops.py
   pypy/dist/pypy/translator/backendopt/test/test_tailrecursion.py
Log:
mostly last round of Translator -> TranslationContext



Modified: pypy/dist/pypy/translator/backendopt/test/test_all.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_all.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_all.py	Fri Dec  2 02:23:45 2005
@@ -1,11 +1,17 @@
 import py
 from pypy.translator.backendopt.all import backend_optimizations
 from pypy.translator.backendopt.test.test_malloc import check_malloc_removed
-from pypy.translator.translator import Translator, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.objspace.flow.model import Constant
 from pypy.annotation import model as annmodel
 from pypy.rpython.llinterp import LLInterpreter
 
+def translateopt(func, sig, **optflags):
+    t = TranslationContext()
+    t.buildannotator().build_types(func, sig)
+    t.buildrtyper().specialize()
+    backend_optimizations(t, **optflags)
+    return t
 
 class A:
     def __init__(self, x, y):
@@ -40,10 +46,7 @@
 def test_big():
     assert big() == 83
 
-    t = Translator(big)
-    t.annotate([])
-    t.specialize()
-    backend_optimizations(t, inline_threshold=100, mallocs=True)
+    t = translateopt(big, [], inline_threshold=100, mallocs=True) 
 
     big_graph = graphof(t, big)
     check_malloc_removed(big_graph)
@@ -60,10 +63,8 @@
         for i in range(n):
             total += i
         return total
-    t = Translator(f)
-    t.annotate([int])
-    t.specialize()
-    t.backend_optimizations(inline_threshold=1, mallocs=True)
+
+    t  = translateopt(f, [int], inline_threshold=1, mallocs=True)
     # this also checks that the BASE_INLINE_THRESHOLD is enough for 'for' loops
 
     f_graph = graph = graphof(t, f)
@@ -78,10 +79,8 @@
     def f(n1, n2):
         c = [i for i in range(n2)]
         return 33
-    t = Translator(f)
-    t.annotate([int, int])
-    t.specialize()
-    t.backend_optimizations(inline_threshold=10, mallocs=True)
+
+    t  = translateopt(f, [int, int], inline_threshold=10, mallocs=True)
 
     f_graph = graphof(t, f)
     check_malloc_removed(f_graph)
@@ -110,10 +109,7 @@
             #debug(" lowered -> " + a)
         return 0
 
-    t = Translator(entry_point)
-    t.annotate(inputtypes)
-    t.specialize()
-    t.backend_optimizations(inline_threshold=1, mallocs=True)
+    t  = translateopt(entry_point, inputtypes, inline_threshold=1, mallocs=True)
 
     entry_point_graph = graphof(t, entry_point)
 

Modified: pypy/dist/pypy/translator/backendopt/test/test_inline.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_inline.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_inline.py	Fri Dec  2 02:23:45 2005
@@ -7,7 +7,7 @@
 from pypy.translator.backendopt.inline import auto_inlining
 from pypy.translator.backendopt.inline import collect_called_functions
 from pypy.translator.backendopt.inline import measure_median_execution_cost
-from pypy.translator.translator import Translator, TranslationContext, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.rpython.rarithmetic import ovfcheck
 from pypy.translator.test.snippet import is_perfect_number
@@ -36,22 +36,39 @@
         checkgraph(graph)
         traverse(no_missing_concretetype, graph)
 
-def check_inline(func, in_func, sig):
-    t = Translator(in_func)
-    a = t.annotate(sig)
-    a.simplify()
-    t.specialize()
+def translate(func, argtypes):
+    t = TranslationContext()
+    t.buildannotator().build_types(func, argtypes)
+    t.buildrtyper().specialize()
+    return t
+
+def check_inline(func, in_func, sig, entry=None):
+    if entry is None:
+        entry = in_func
+    t = translate(entry, sig)
     # inline!
     sanity_check(t)    # also check before inlining (so we don't blame it)
-    in_func_graph = graphof(t, in_func)
-    func_graph = graphof(t, func)
-    inline_function(t, func, in_func_graph)
+    inline_function(t, func, graphof(t, in_func))
     sanity_check(t)
     interp = LLInterpreter(t.rtyper)
     def eval_func(args):
-        return interp.eval_graph(in_func_graph, args)
+        return interp.eval_graph(graphof(t, entry), args)
     return eval_func
 
+def check_auto_inlining(func, sig, threshold=None):
+    t = translate(func, sig)
+    # inline!
+    sanity_check(t)    # also check before inlining (so we don't blame it)
+    if threshold is None:
+        auto_inlining(t)
+    else:
+        auto_inlining(t, threshold=threshold)
+    sanity_check(t)
+    interp = LLInterpreter(t.rtyper)
+    def eval_func(args):
+        return interp.eval_graph(graphof(t, func), args)
+    return eval_func, t
+
 
 def test_inline_simple():
     def f(x, y):
@@ -95,20 +112,12 @@
         except KeyError:
             return 2
         return x
-    t = Translator(h)
-    a = t.annotate([int])
-    a.simplify()
-    t.specialize()
-    sanity_check(t)    # also check before inlining (so we don't blame it)
-    inline_function(t, f, graphof(t, g))
-    sanity_check(t)
-    interp = LLInterpreter(t.rtyper)
-    h_graph = graphof(t, h)
-    result = interp.eval_graph(h_graph, [0])
+    eval_func = check_inline(f,g, [int], entry=h)
+    result = eval_func([0])
     assert result == 0
-    result = interp.eval_graph(h_graph, [1])
+    result = eval_func([1])
     assert result == 1
-    result = interp.eval_graph(h_graph, [2])
+    result = eval_func([2])
     assert result == 2    
 
 def test_inline_several_times():
@@ -165,20 +174,13 @@
         except KeyError:
             return 3
         return 1
-    t = Translator(g)
-    a = t.annotate([int])
-    a.simplify()
-    t.specialize()
-    auto_inlining(t, threshold=10)
-    for graph in t.graphs:
-        traverse(no_missing_concretetype, graph)
-    interp = LLInterpreter(t.rtyper)
-    g_graph = graphof(t, g)
-    result = interp.eval_graph(g_graph, [0])
+
+    eval_func, _ = check_auto_inlining(g, [int], threshold=10)
+    result = eval_func([0])
     assert result == 2
-    result = interp.eval_graph(g_graph, [1])
+    result = eval_func([1])
     assert result == 3
-    result = interp.eval_graph(g_graph, [42])
+    result = eval_func([42])
     assert result == 1
 
 def test_inline_nonraising_into_catching():
@@ -208,17 +210,10 @@
             return f(x)+3
         except KeyError:
             return -1
-    t = Translator(g)
-    a = t.annotate([int])
-    a.simplify()
-    t.specialize()
-    sanity_check(t)    # also check before inlining (so we don't blame it)
-    inline_function(t, f, t.flowgraphs[g])
-    sanity_check(t)
-    interp = LLInterpreter(t.flowgraphs, t.rtyper)
-    result = interp.eval_function(g, [100])
+    eval_func = check_inline(f, g, [int])
+    result = eval_func([100])
     assert result == 106
-    result = interp.eval_function(g, [-100])
+    result = eval_func(g, [-100])
     assert result == -1
 
 def test_for_loop():
@@ -227,16 +222,13 @@
         for i in range(0, x):
             result += i
         return result
-    t = Translator(f)
-    a = t.annotate([int])
-    a.simplify()
-    t.specialize()
+    t = translate(f, [int])
+    sanity_check(t)    # also check before inlining (so we don't blame it)
     for graph in t.graphs:
         if graph.name.startswith('ll_rangenext'):
             break
     else:
         assert 0, "cannot find ll_rangenext_*() function"
-    sanity_check(t)    # also check before inlining (so we don't blame it)
     inline_function(t, graph, graphof(t, f))
     sanity_check(t)
     interp = LLInterpreter(t.rtyper)
@@ -283,17 +275,13 @@
             return g(n)
         except OverflowError:
             return -1
-    t = Translator(f)
-    a = t.annotate([int])
-    a.simplify()
-    t.specialize()
-    auto_inlining(t, threshold=10)
+    eval_func, t = check_auto_inlining(f, [int], threshold=10)
     f_graph = graphof(t, f)
     assert len(collect_called_functions(f_graph)) == 0
-    interp = LLInterpreter(t.rtyper)
-    result = interp.eval_graph(f_graph, [10])
+
+    result = eval_func([10])
     assert result == 45
-    result = interp.eval_graph(f_graph, [15])
+    result = eval_func([15])
     assert result == -1
 
 def test_inline_exception_catching():
@@ -332,13 +320,8 @@
     directory = "./."
     def f():
         return os.path.isdir(directory)
-    t = Translator(f)
-    a = t.annotate([])
-    a.simplify()
-    t.specialize()
-    auto_inlining(t)
-    interp = LLInterpreter(t.rtyper)
-    result = interp.eval_graph(graphof(t, f), [])
+    eval_func, _ = check_auto_inlining(f, [])
+    result = eval_func([])
     assert result is True
 
 def test_inline_raiseonly():

Modified: pypy/dist/pypy/translator/backendopt/test/test_malloc.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_malloc.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_malloc.py	Fri Dec  2 02:23:45 2005
@@ -1,6 +1,6 @@
 from pypy.translator.backendopt.malloc import remove_simple_mallocs
 from pypy.translator.backendopt.inline import inline_function
-from pypy.translator.translator import Translator, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.objspace.flow.model import checkgraph, flatten, Block
 from pypy.rpython.llinterp import LLInterpreter
 
@@ -18,9 +18,9 @@
     assert count2 == 0   # number of direct_calls left
 
 def check(fn, signature, args, expected_result, must_be_removed=True):
-    t = Translator(fn)
-    t.annotate(signature)
-    t.specialize()
+    t = TranslationContext()
+    t.buildannotator().build_types(fn, signature)
+    t.buildrtyper().specialize()
     graph = graphof(t, fn)
     remove_simple_mallocs(graph)
     if must_be_removed:

Modified: pypy/dist/pypy/translator/backendopt/test/test_propagate.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_propagate.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_propagate.py	Fri Dec  2 02:23:45 2005
@@ -1,13 +1,14 @@
-from pypy.translator.translator import Translator, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.translator.backendopt.propagate import *
+from pypy.translator.backendopt.all import backend_optimizations
 from pypy.rpython.llinterp import LLInterpreter
 
 
 def get_graph(fn, signature):
-    t = Translator(fn)
-    t.annotate(signature)
-    t.specialize()
-    t.backend_optimizations(ssa_form=False, propagate=False) 
+    t = TranslationContext()
+    t.buildannotator().build_types(fn, signature)
+    t.buildrtyper().specialize()
+    backend_optimizations(t, ssa_form=False, propagate=False) 
     graph = graphof(t, fn)
     return graph, t
 
@@ -115,6 +116,6 @@
     
 def test_call_list_default_argument():
     graph, t = get_graph(call_list_default_argument, [int])
-    t.backend_optimizations(propagate=True, ssa_form=False) 
+    backend_optimizations(t, propagate=True, ssa_form=False) 
     for i in range(10):
         check_graph(graph, [i], call_list_default_argument(i), t)

Modified: pypy/dist/pypy/translator/backendopt/test/test_removenoops.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_removenoops.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_removenoops.py	Fri Dec  2 02:23:45 2005
@@ -1,6 +1,6 @@
 from pypy.translator.backendopt.removenoops import remove_void, remove_same_as
 from pypy.translator.backendopt.inline import inline_function
-from pypy.translator.translator import Translator, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.translator.test.snippet import simple_method
 from pypy.objspace.flow.model import checkgraph, flatten, Block
 from pypy.rpython.lltypesystem.lltype import Void
@@ -11,9 +11,9 @@
 
 
 def annotate_and_remove_void(f, annotate):
-    t = Translator(f)
-    a = t.annotate(annotate)
-    t.specialize()
+    t = TranslationContext()
+    t.buildannotator().build_types(f, annotate)
+    t.buildrtyper().specialize()
     remove_void(t)
     return t
 
@@ -51,9 +51,9 @@
             return 42
         else:
             return 666
-    t = Translator(f)
-    a = t.annotate([])
-    t.specialize()
+    t = TranslationContext()
+    t.buildannotator().build_types(f, [])
+    t.buildrtyper().specialize()
     # now we make the 'if True' appear
     f_graph = graphof(t, f)
     inline_function(t, nothing, f_graph)

Modified: pypy/dist/pypy/translator/backendopt/test/test_tailrecursion.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_tailrecursion.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_tailrecursion.py	Fri Dec  2 02:23:45 2005
@@ -1,6 +1,6 @@
 from pypy.objspace.flow.model import traverse, Block, Link, Variable, Constant
 from pypy.translator.backendopt.tailrecursion import remove_tail_calls_to_self
-from pypy.translator.translator import Translator, graphof
+from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.translator.test.snippet import is_perfect_number
 
@@ -11,9 +11,9 @@
         if a > b:
             return gcd(b, a)
         return gcd(b % a, a)
-    t = Translator(gcd)
-    a = t.annotate([int, int])
-    t.specialize()
+    t = TranslationContext()
+    t.buildannotator().build_types(gcd, [int, int])
+    t.buildrtyper().specialize()
     gcd_graph = graphof(t, gcd)
     remove_tail_calls_to_self(t, gcd_graph )
     lli = LLInterpreter(t.rtyper)



More information about the Pypy-commit mailing list