Debian Codegraph @

rpython/jit/metainterp/optimizeopt/test/test_guard.py

raw symbols SCIP
1 from rpython.jit.metainterp import compile
2 from rpython.jit.metainterp.history import Const
3 from rpython.jit.metainterp.optimizeopt.dependency import (
4 DependencyGraph, IndexVar)
5 from rpython.jit.metainterp.optimizeopt.guard import (GuardStrengthenOpt,
6 Guard)
7 from rpython.jit.metainterp.optimizeopt.test.test_schedule import SchedulerBaseTest
8 from rpython.jit.metainterp.optimizeopt.test.test_vecopt import FakeLoopInfo
9 from rpython.jit.metainterp.resoperation import (rop,
10 ResOperation, InputArgInt)
11
12 class FakeMemoryRef(object):
13 def __init__(self, array, iv):
14 self.index_var = iv
15 self.array = array
16
17 def is_adjacent_to(self, other):
18 if self.array is not other.array:
19 return False
20 iv = self.index_var
21 ov = other.index_var
22 val = (int(str(ov.var)[1:]) - int(str(iv.var)[1:]))
23 # i0 and i1 are adjacent
24 # i1 and i0 ...
25 # but not i0, i2
26 # ...
27 return abs(val) == 1
28
29 class FakeOp(object):
30 def __init__(self, cmpop):
31 self.boolinverse = ResOperation(cmpop, [box(0), box(0)], None).boolinverse
32 self.cmpop = cmpop
33
34 def getopnum(self):
35 return self.cmpop
36
37 def getarg(self, index):
38 if index == 0:
39 return 'lhs'
40 elif index == 1:
41 return 'rhs'
42 else:
43 assert 0
44
45 class FakeResOp(object):
46 def __init__(self, opnum):
47 self.opnum = opnum
48
49 def getopnum(self):
50 return self.opnum
51
52 def box(value):
53 return InputArgInt(value)
54
55 def const(value):
56 return Const._new(value)
57
58 def iv(value, coeff=(1,1,0)):
59 var = IndexVar(value)
60 var.coefficient_mul = coeff[0]
61 var.coefficient_div = coeff[1]
62 var.constant = coeff[2]
63 return var
64
65 def guard(opnum):
66 def guard_impl(cmpop, lhs, rhs):
67 guard = Guard(0, FakeResOp(opnum), FakeOp(cmpop), {'lhs': lhs, 'rhs': rhs})
68 return guard
69 return guard_impl
70 guard_true = guard(rop.GUARD_TRUE)
71 guard_false = guard(rop.GUARD_FALSE)
72 del guard
73
74 class TestGuard(SchedulerBaseTest):
75 def optguards(self, loop, user_code=False):
76 info = FakeLoopInfo(loop)
77 info.snapshot(loop)
78 for op in loop.operations:
79 if op.is_guard():
80 op.setdescr(compile.CompileLoopVersionDescr())
81 dep = DependencyGraph(loop)
82 opt = GuardStrengthenOpt(dep.index_vars)
83 opt.propagate_all_forward(info, loop, user_code)
84 return opt
85
86 def assert_guard_count(self, loop, count):
87 guard = 0
88 for op in loop.operations + loop.prefix:
89 if op.is_guard():
90 guard += 1
91 if guard != count:
92 self.debug_print_operations(loop)
93 assert guard == count
94
95 def assert_contains_sequence(self, loop, instr):
96 class Glob(object):
97 next = None
98 prev = None
99 def __repr__(self):
100 return '*'
101 from rpython.jit.tool.oparser import OpParser, default_fail_descr
102 parser = OpParser(instr, self.cpu, self.namespace, None, default_fail_descr, True, None)
103 parser.vars = { arg.repr_short(arg._repr_memo) : arg for arg in loop.inputargs}
104 operations = []
105 last_glob = None
106 prev_op = None
107 for line in instr.splitlines():
108 line = line.strip()
109 if line.startswith("#") or \
110 line == "":
111 continue
112 if line.startswith("..."):
113 last_glob = Glob()
114 last_glob.prev = prev_op
115 operations.append(last_glob)
116 continue
117 op = parser.parse_next_op(line)
118 if last_glob is not None:
119 last_glob.next = op
120 last_glob = None
121 operations.append(op)
122 def check(op, candidate, rename):
123 m = 0
124 if isinstance(candidate, Glob):
125 if candidate.next is None:
126 return 0 # consumes the rest
127 if op.getopnum() != candidate.next.getopnum():
128 return 0
129 m = 1
130 candidate = candidate.next
131 if op.getopnum() == candidate.getopnum():
132 for i,arg in enumerate(op.getarglist()):
133 oarg = candidate.getarg(i)
134 if arg in rename:
135 assert rename[arg].same_box(oarg)
136 else:
137 rename[arg] = oarg
138
139 if not op.returns_void():
140 rename[op] = candidate
141 m += 1
142 return m
143 return 0
144 j = 0
145 rename = {}
146 ops = loop.finaloplist()
147 for i, op in enumerate(ops):
148 candidate = operations[j]
149 j += check(op, candidate, rename)
150 if isinstance(operations[-1], Glob):
151 assert j == len(operations)-1, self.debug_print_operations(loop)
152 else:
153 assert j == len(operations), self.debug_print_operations(loop)
154
155 def test_basic(self):
156 loop1 = self.parse_trace("""
157 i10 = int_lt(i1, 42)
158 guard_true(i10) []
159 i101 = int_add(i1, 1)
160 i102 = int_lt(i101, 42)
161 guard_true(i102) []
162 """)
163 opt = self.optguards(loop1)
164 self.assert_guard_count(loop1, 1)
165 self.assert_contains_sequence(loop1, """
166 ...
167 i101 = int_add(i1, 1)
168 i12 = int_lt(i101, 42)
169 guard_true(i12) []
170 ...
171 """)
172
173 def test_basic_sub(self):
174 loop1 = self.parse_trace("""
175 i10 = int_gt(i1, 42)
176 guard_true(i10) []
177 i101 = int_sub(i1, 1)
178 i12 = int_gt(i101, 42)
179 guard_true(i12) []
180 """)
181 opt = self.optguards(loop1)
182 self.assert_guard_count(loop1, 1)
183 self.assert_contains_sequence(loop1, """
184 ...
185 i101 = int_sub(i1, 1)
186 i12 = int_gt(i101, 42)
187 guard_true(i12) []
188 ...
189 """)
190
191 def test_basic_mul(self):
192 loop1 = self.parse_trace("""
193 i10 = int_mul(i1, 4)
194 i20 = int_lt(i10, 42)
195 guard_true(i20) []
196 i12 = int_add(i10, 1)
197 i13 = int_lt(i12, 42)
198 guard_true(i13) []
199 """)
200 opt = self.optguards(loop1)
201 self.assert_guard_count(loop1, 1)
202 self.assert_contains_sequence(loop1, """
203 ...
204 i101 = int_mul(i1, 4)
205 i12 = int_add(i101, 1)
206 i13 = int_lt(i12, 42)
207 guard_true(i13) []
208 ...
209 """)
210
211 def test_compare(self):
212 key = box(1)
213 incomparable = (False, 0)
214 # const const
215 assert iv(const(42)).compare(iv(const(42))) == (True, 0)
216 assert iv(const(-400)).compare(iv(const(-200))) == (True, -200)
217 assert iv(const(0)).compare(iv(const(-1))) == (True, 1)
218 # var const
219 assert iv(key, coeff=(1,1,0)).compare(iv(const(42))) == incomparable
220 assert iv(key, coeff=(5,70,500)).compare(iv(const(500))) == incomparable
221 # var var
222 assert iv(key, coeff=(1,1,0)).compare(iv(key,coeff=(1,1,0))) == (True, 0)
223 assert iv(key, coeff=(1,7,0)).compare(iv(key,coeff=(1,7,0))) == (True, 0)
224 assert iv(key, coeff=(4,7,0)).compare(iv(key,coeff=(3,7,0))) == incomparable
225 assert iv(key, coeff=(14,7,0)).compare(iv(key,coeff=(2,1,0))) == (True, 0)
226 assert iv(key, coeff=(14,7,33)).compare(iv(key,coeff=(2,1,0))) == (True, 33)
227 assert iv(key, coeff=(15,5,33)).compare(iv(key,coeff=(3,1,33))) == (True, 0)
228
229
230 def test_imply_basic(self):
231 key = box(1)
232 # if x < 42 <=> x < 42
233 g1 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,0)), iv(const(42)))
234 g2 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,0)), iv(const(42)))
235 assert g1.implies(g2)
236 assert g2.implies(g1)
237 # if x+1 < 42 => x < 42
238 g1 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,1)), iv(const(42)))
239 g2 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,0)), iv(const(42)))
240 assert g1.implies(g2)
241 assert not g2.implies(g1)
242 # if x+2 < 42 => x < 39
243 # counter: 39+2 < 42 => 39 < 39
244 g1 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,2)), iv(const(42)))
245 g2 = guard_true(rop.INT_LT, iv(key, coeff=(1,1,0)), iv(const(39)))
246 assert not g1.implies(g2)
247 assert not g2.implies(g1)
248 # if x+2 <= 42 => x <= 43
249 g1 = guard_true(rop.INT_LE, iv(key, coeff=(1,1,2)), iv(const(42)))
250 g2 = guard_true(rop.INT_LE, iv(key, coeff=(1,1,0)), iv(const(43)))
251 assert g1.implies(g2)
252 assert not g2.implies(g1)
253 # if x*13/3+1 <= 0 => x*13/3 <= -1
254 # is true, but the implies method is not smart enough
255 g1 = guard_true(rop.INT_LE, iv(key, coeff=(13,3,1)), iv(const(0)))
256 g2 = guard_true(rop.INT_LE, iv(key, coeff=(13,3,0)), iv(const(-1)))
257 assert not g1.implies(g2)
258 assert not g2.implies(g1)
259 # > or >=
260 # if x > -55 => x*2 > -44
261 # counter: -44 > -55 (True) => -88 > -44 (False)
262 g1 = guard_true(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(const(-55)))
263 g2 = guard_true(rop.INT_GT, iv(key, coeff=(2,1,0)), iv(const(-44)))
264 assert not g1.implies(g2)
265 assert not g2.implies(g1)
266 # if x*2/2 > -44 => x*2/2 > -55
267 g1 = guard_true(rop.INT_GE, iv(key, coeff=(2,2,0)), iv(const(-44)))
268 g2 = guard_true(rop.INT_GE, iv(key, coeff=(2,2,0)), iv(const(-55)))
269 assert g1.implies(g2)
270 assert not g2.implies(g1)
271
272 def test_imply_coeff(self):
273 key = box(1)
274 key2 = box(2)
275 # if x > y * 9/3 => x > y
276 # counter: x = -2, y = -1, -2 > -3 => -2 > -1, True => False
277 g1 = guard_true(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(box(1),coeff=(9,3,0)))
278 g2 = guard_true(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(box(1),coeff=(1,1,0)))
279 assert not g1.implies(g2)
280 assert not g2.implies(g1)
281 # if x > y * 15/5 <=> x > y * 3
282 g1 = guard_true(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(key2,coeff=(15,5,0)))
283 g2 = guard_true(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(key2,coeff=(3,1,0)))
284 assert g1.implies(g2)
285 assert g2.implies(g1)
286 # x >= y => x*3-5 >= y
287 # counter: 1 >= 0 => 1*3-5 >= 0 == -2 >= 0, True => False
288 g1 = guard_true(rop.INT_GE, iv(key, coeff=(1,1,0)), iv(key2))
289 g2 = guard_true(rop.INT_GE, iv(key, coeff=(3,1,-5)), iv(key2))
290 assert not g1.implies(g2)
291 assert not g2.implies(g1)
292 # guard false inverst >= to <
293 # x < y => x*3-5 < y
294 # counter: 3 < 4 => 3*3-5 < 4 == 4 < 4, True => False
295 g1 = guard_false(rop.INT_GE, iv(key, coeff=(1,1,0)), iv(key2))
296 g2 = guard_false(rop.INT_GE, iv(key, coeff=(3,1,-5)), iv(key2))
297 assert not g1.implies(g2)
298 assert not g2.implies(g1)
299 # x <= y => x*3-5 > y
300 # counter: 3 < 4 => 3*3-5 < 4 == 4 < 4, True => False
301 g1 = guard_false(rop.INT_GT, iv(key, coeff=(1,1,0)), iv(key2))
302 g2 = guard_true(rop.INT_GT, iv(key, coeff=(3,1,-5)), iv(key2))
303 assert not g1.implies(g2)
304 assert not g2.implies(g1)
305
306 def test_collapse(self):
307 loop1 = self.parse_trace("""
308 i10 = int_gt(i1, 42)
309 guard_true(i10) []
310 i11 = int_add(i1, 1)
311 i12 = int_gt(i11, i2)
312 guard_true(i12) []
313 """)
314 opt = self.optguards(loop1, True)
315 self.assert_guard_count(loop1, 2)
316 self.assert_contains_sequence(loop1, """
317 ...
318 i100 = int_ge(42, i2)
319 guard_true(i100) []
320 ...
321 i40 = int_gt(i1, 42)
322 guard_true(i40) []
323 ...
324 """)