Debian Codegraph @

rpython/translator/backendopt/test/test_removenoops.py

raw symbols SCIP
1 from rpython.translator.backendopt.removenoops import remove_same_as, \
2 remove_unaryops, remove_duplicate_casts
3 from rpython.translator.backendopt.inline import simple_inline_function
4 from rpython.translator.translator import TranslationContext, graphof
5 from rpython.memory.gctransform.test.test_transform import getops
6 from rpython.translator.test.snippet import simple_method
7 from rpython.translator.backendopt.all import backend_optimizations
8 from rpython.translator.backendopt.all import INLINE_THRESHOLD_FOR_TEST
9 from rpython.flowspace.model import checkgraph, Block
10 from rpython.rtyper.lltypesystem import lltype
11 from rpython.rtyper.lltypesystem.lloperation import llop
12 from rpython.rtyper.llinterp import LLInterpreter
13 from rpython.conftest import option
14
15
16 def get_graph(fn, signature, all_opts=True):
17 t = TranslationContext()
18 t.buildannotator().build_types(fn, signature)
19 t.buildrtyper().specialize()
20 if all_opts:
21 backend_optimizations(t, inline_threshold=INLINE_THRESHOLD_FOR_TEST,
22 constfold=False)
23 graph = graphof(t, fn)
24 if option.view:
25 t.view()
26 return graph, t
27
28 def check_graph(graph, args, expected_result, t):
29 interp = LLInterpreter(t.rtyper)
30 res = interp.eval_graph(graph, args)
31 assert res == expected_result
32
33 def check_get_graph(fn, signature, args, expected_result):
34 graph, t = get_graph(fn, signature)
35 check_graph(graph, args, expected_result, t)
36 return graph
37
38
39 def test_remove_same_as():
40 def nothing(x):
41 return x
42 def f():
43 nothing(False)
44 if nothing(True):
45 return 42
46 else:
47 return 666
48 t = TranslationContext()
49 t.buildannotator().build_types(f, [])
50 t.buildrtyper().specialize()
51 # now we make the 'if True' appear
52 f_graph = graphof(t, f)
53 simple_inline_function(t, nothing, f_graph)
54 # here, the graph looks like v21=same_as(True); exitswitch: v21
55 remove_same_as(f_graph)
56 t.checkgraphs()
57 # only one path should be left
58 for block in f_graph.iterblocks():
59 assert len(block.exits) <= 1
60
61 interp = LLInterpreter(t.rtyper)
62 result = interp.eval_graph(f_graph, [])
63 assert result == 42
64
65
66 def test_remove_same_as_nonconst():
67 from rpython.rlib.nonconst import NonConstant
68 from rpython.rtyper.lltypesystem.lloperation import llop
69 from rpython.rtyper.lltypesystem import lltype
70
71 def f():
72 if NonConstant(False):
73 x = llop.same_as(lltype.Signed, 666)
74 return 42
75
76 t = TranslationContext()
77 t.buildannotator().build_types(f, [])
78 t.buildrtyper().specialize()
79 f_graph = graphof(t, f)
80 #simple_inline_function(t, nothing, f_graph)
81 # here, the graph looks like v21=same_as(True); exitswitch: v21
82 remove_same_as(f_graph)
83 t.checkgraphs()
84 # only one path should be left
85 for block in f_graph.iterblocks():
86 assert len(block.exits) <= 1
87
88 for block in t.annotator.annotated:
89 assert None not in block.operations
90
91 interp = LLInterpreter(t.rtyper)
92 result = interp.eval_graph(f_graph, [])
93 assert result == 42
94
95
96 def test_remove_unaryops():
97 # We really want to use remove_unaryops for more complex operations, but
98 # it's easier to test it with operations on ints here.
99 def f(x):
100 i = llop.int_invert(lltype.Signed, x)
101 i = llop.int_add(lltype.Signed, x, 1)
102 return llop.int_neg(lltype.Signed, i)
103 t = TranslationContext()
104 t.buildannotator().build_types(f, [int])
105 t.buildrtyper().specialize()
106 f_graph = graphof(t, f)
107 remove_unaryops(f_graph, ["int_neg", "int_invert"])
108 t.checkgraphs()
109
110 interp = LLInterpreter(t.rtyper)
111 result = interp.eval_graph(f_graph, [-2])
112 assert result == -1
113
114 def test_remove_duplicate_casts():
115 class A(object):
116 def __init__(self, x, y):
117 self.x = x
118 self.y = y
119 def getsum(self):
120 return self.x + self.y
121 class B(A):
122 def __init__(self, x, y, z):
123 A.__init__(self, x, y)
124 self.z = z
125 def getsum(self):
126 return self.x + self.y + self.z
127 def f(x, switch):
128 a = A(x, x + 1)
129 b = B(x, x + 1, x + 2)
130 if switch:
131 c = A(x, x + 1)
132 else:
133 c = B(x, x + 1, x + 2)
134 return a.x + a.y + b.x + b.y + b.z + c.getsum()
135 assert f(10, True) == 75
136 graph, t = get_graph(f, [int, bool], all_opts=False)
137 num_cast_pointer = len(getops(graph)['cast_pointer'])
138 changed = remove_duplicate_casts(graph, t)
139 assert changed
140 ops = getops(graph)
141 assert len(ops['cast_pointer']) < num_cast_pointer
142 print len(ops['cast_pointer']), num_cast_pointer
143 graph_getsum = graphof(t, B.getsum.im_func)
144 num_cast_pointer = len(getops(graph_getsum)['cast_pointer'])
145 changed = remove_duplicate_casts(graph_getsum, t)
146 assert changed
147 if option.view:
148 t.view()
149 check_graph(graph, [10, True], 75, t)
150 ops = getops(graph_getsum)
151 assert len(ops['cast_pointer']) < num_cast_pointer
152 print len(ops['cast_pointer']), num_cast_pointer
153