| 1 |
from __future__ import with_statement |
| 2 |
import sys, os |
| 3 |
import types |
| 4 |
import subprocess |
| 5 |
import py, pytest |
| 6 |
from rpython.tool.udir import udir |
| 7 |
from rpython.tool import logparser |
| 8 |
from rpython.jit.tool.jitoutput import parse_prof |
| 9 |
from rpython.tool.jitlogparser import storage |
| 10 |
from pypy.module.pypyjit.test_pypy_c.model import \ |
| 11 |
Log, find_id_linenos, OpMatcher, InvalidMatch |
| 12 |
|
| 13 |
my_file = __file__ |
| 14 |
if my_file.endswith('pyc'): |
| 15 |
my_file = my_file[:-1] |
| 16 |
|
| 17 |
|
| 18 |
class BaseTestPyPyC(object): |
| 19 |
log_string = 'jit-log-opt,jit-log-noopt,jit-log-virtualstate,jit-summary' |
| 20 |
|
| 21 |
def setup_class(cls): |
| 22 |
pypy_c = pytest.config.option.pypy_c or None |
| 23 |
if pypy_c is not None: |
| 24 |
pypy_c = os.path.expanduser(pypy_c) |
| 25 |
assert os.path.exists(pypy_c), ( |
| 26 |
"--pypy specifies %r, which does not exist" % (pypy_c,)) |
| 27 |
out = subprocess.check_output([pypy_c, '-c', |
| 28 |
"import sys; print('__pypy__' in sys.builtin_module_names)"]) |
| 29 |
assert 'True' in out, "%r is not a pypy executable" % (pypy_c,) |
| 30 |
out = subprocess.check_output([pypy_c, '-c', |
| 31 |
"import sys; print(sys.pypy_translation_info['translation.jit'])"]) |
| 32 |
assert 'True' in out, "%r is a not a JIT-enabled pypy" % (pypy_c,) |
| 33 |
out = subprocess.check_output([pypy_c, '-c', |
| 34 |
"import sys; print(sys.version)"]) |
| 35 |
assert out.startswith('3'), "%r is a not a pypy 3" % (pypy_c,) |
| 36 |
cls.pypy_c = pypy_c |
| 37 |
cls.tmpdir = udir.join('test-pypy-jit') |
| 38 |
cls.tmpdir.ensure(dir=True) |
| 39 |
|
| 40 |
def setup_method(self, meth): |
| 41 |
self.filepath = self.tmpdir.join(meth.im_func.func_name + '.py') |
| 42 |
|
| 43 |
def run(self, func_or_src, args=[], import_site=False, |
| 44 |
discard_stdout_before_last_line=False, **jitopts): |
| 45 |
jitopts.setdefault('threshold', 200) |
| 46 |
jitopts.setdefault('disable_unrolling', 9999) |
| 47 |
if self.pypy_c is None: |
| 48 |
py.test.skip("run with --pypy=PATH") |
| 49 |
src = py.code.Source(func_or_src) |
| 50 |
if isinstance(func_or_src, types.FunctionType): |
| 51 |
funcname = func_or_src.func_name |
| 52 |
else: |
| 53 |
funcname = 'main' |
| 54 |
|
| 55 |
arglist = ', '.join(map(repr, args)) |
| 56 |
with self.filepath.open("w") as f: |
| 57 |
|
| 58 |
|
| 59 |
f.write("import sys\n") |
| 60 |
f.write("sys.setcheckinterval(10000000)\n") |
| 61 |
f.write(str(src) + "\n") |
| 62 |
f.write("print(%s(%s))\n" % (funcname, arglist)) |
| 63 |
|
| 64 |
|
| 65 |
logfile = self.filepath.new(ext='.log') |
| 66 |
|
| 67 |
cmdline = [self.pypy_c] |
| 68 |
if not import_site: |
| 69 |
cmdline.append('-S') |
| 70 |
if jitopts: |
| 71 |
jitcmdline = ['%s=%s' % (key, value) |
| 72 |
for key, value in jitopts.items()] |
| 73 |
cmdline += ['--jit', ','.join(jitcmdline)] |
| 74 |
cmdline.append(str(self.filepath)) |
| 75 |
|
| 76 |
env = os.environ.copy() |
| 77 |
|
| 78 |
env['PYPYLOG'] = self.log_string + ':' + str(logfile) |
| 79 |
jitlogfile = str(logfile) + '.jlog' |
| 80 |
env['JITLOG'] = str(jitlogfile) |
| 81 |
pipe = subprocess.Popen(cmdline, |
| 82 |
env=env, |
| 83 |
stdout=subprocess.PIPE, |
| 84 |
stderr=subprocess.PIPE) |
| 85 |
stdout, stderr = pipe.communicate() |
| 86 |
if pipe.wait() < 0: |
| 87 |
raise IOError("subprocess was killed by signal %d" % ( |
| 88 |
pipe.returncode,)) |
| 89 |
if stderr.startswith('SKIP:'): |
| 90 |
py.test.skip(stderr) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if not stdout: |
| 95 |
raise Exception("no stdout produced; stderr='''\n%s'''" |
| 96 |
% (stderr,)) |
| 97 |
if stderr: |
| 98 |
print '*** stderr of the subprocess: ***' |
| 99 |
print stderr |
| 100 |
|
| 101 |
if discard_stdout_before_last_line: |
| 102 |
stdout = stdout.splitlines(True)[-1] |
| 103 |
|
| 104 |
|
| 105 |
rawlog = logparser.parse_log_file(str(logfile), verbose=False) |
| 106 |
rawtraces = logparser.extract_category(rawlog, 'jit-log-opt-') |
| 107 |
log = Log(rawtraces) |
| 108 |
log.result = eval(stdout) |
| 109 |
log.logfile = str(logfile) |
| 110 |
log.jitlogfile = jitlogfile |
| 111 |
|
| 112 |
summaries = logparser.extract_category(rawlog, 'jit-summary') |
| 113 |
if len(summaries) > 0: |
| 114 |
log.jit_summary = parse_prof(summaries[-1]) |
| 115 |
else: |
| 116 |
log.jit_summary = None |
| 117 |
|
| 118 |
return log |
| 119 |
|
| 120 |
def run_and_check(self, src, args=[], **jitopts): |
| 121 |
log1 = self.run(src, args, threshold=-1) |
| 122 |
log2 = self.run(src, args, **jitopts) |
| 123 |
assert log1.result == log2.result |
| 124 |
|
| 125 |
assert len(log2.loops_by_filename(self.filepath)) > 0 |
| 126 |
|
| 127 |
|
| 128 |
class TestLog(object): |
| 129 |
def test_find_id_linenos(self): |
| 130 |
def f(): |
| 131 |
a = 0 |
| 132 |
return a |
| 133 |
|
| 134 |
start_lineno = f.func_code.co_firstlineno |
| 135 |
code = storage.GenericCode(my_file, start_lineno, 'f') |
| 136 |
ids = find_id_linenos(code) |
| 137 |
myline = ids['myline'] |
| 138 |
assert myline == start_lineno + 1 |
| 139 |
|
| 140 |
|
| 141 |
class TestOpMatcher_(object): |
| 142 |
def match(self, src1, src2, **kwds): |
| 143 |
from rpython.tool.jitlogparser.parser import SimpleParser |
| 144 |
loop = SimpleParser.parse_from_input(src1) |
| 145 |
matcher = OpMatcher(loop.operations) |
| 146 |
try: |
| 147 |
res = matcher.match(src2, **kwds) |
| 148 |
assert res is True |
| 149 |
return True |
| 150 |
except InvalidMatch: |
| 151 |
return False |
| 152 |
|
| 153 |
def test_match_var(self): |
| 154 |
match_var = OpMatcher([]).match_var |
| 155 |
assert match_var('v0', 'V0') |
| 156 |
assert not match_var('v0', 'V1') |
| 157 |
assert match_var('v0', 'V0') |
| 158 |
|
| 159 |
|
| 160 |
assert match_var('ConstPtr(ptr0)', 'PTR0') |
| 161 |
assert not match_var('ConstPtr(ptr0)', 'PTR1') |
| 162 |
assert match_var('ConstPtr(ptr0)', 'PTR0') |
| 163 |
|
| 164 |
|
| 165 |
assert match_var('ConstClass(foo)', 'ConstClass(foo)') |
| 166 |
assert not match_var('ConstClass(bar)', 'v1') |
| 167 |
assert not match_var('v2', 'ConstClass(baz)') |
| 168 |
|
| 169 |
|
| 170 |
assert match_var('v0', '_') |
| 171 |
assert match_var('v0', 'V0') |
| 172 |
assert match_var('ConstPtr(ptr0)', '_') |
| 173 |
py.test.raises(AssertionError, "match_var('_', 'v0')") |
| 174 |
|
| 175 |
|
| 176 |
assert match_var('1234', '1234') |
| 177 |
assert not match_var('1234', '1235') |
| 178 |
assert not match_var('v0', '1234') |
| 179 |
assert not match_var('1234', 'v0') |
| 180 |
assert match_var('1234', '#') |
| 181 |
assert not match_var('v0', '#') |
| 182 |
assert match_var('1234', '_') |
| 183 |
|
| 184 |
|
| 185 |
assert match_var('0.000000', '0.0') |
| 186 |
assert not match_var('0.000000', '0') |
| 187 |
assert not match_var('0', '0.0') |
| 188 |
assert not match_var('v0', '0.0') |
| 189 |
assert not match_var('0.0', 'v0') |
| 190 |
assert match_var('0.0', '#') |
| 191 |
assert match_var('0.0', '_') |
| 192 |
|
| 193 |
def test_parse_op(self): |
| 194 |
res = OpMatcher.parse_op(" a = int_add( b, 3 ) # foo") |
| 195 |
assert res == ("int_add", "a", ["b", "3"], None, True) |
| 196 |
res = OpMatcher.parse_op("guard_true(a)") |
| 197 |
assert res == ("guard_true", None, ["a"], None, True) |
| 198 |
res = OpMatcher.parse_op("setfield_gc(p0, i0, descr=<foobar>)") |
| 199 |
assert res == ("setfield_gc", None, ["p0", "i0"], "<foobar>", True) |
| 200 |
res = OpMatcher.parse_op("i1 = getfield_gc(p0, descr=<foobar>)") |
| 201 |
assert res == ("getfield_gc", "i1", ["p0"], "<foobar>", True) |
| 202 |
res = OpMatcher.parse_op("p0 = force_token()") |
| 203 |
assert res == ("force_token", "p0", [], None, True) |
| 204 |
res = OpMatcher.parse_op("guard_not_invalidated?") |
| 205 |
assert res == ("guard_not_invalidated", None, [], '...', False) |
| 206 |
|
| 207 |
def test_exact_match(self): |
| 208 |
loop = """ |
| 209 |
[i0] |
| 210 |
i2 = int_add(i0, 1) |
| 211 |
jump(i2) |
| 212 |
""" |
| 213 |
expected = """ |
| 214 |
i5 = int_add(i2, 1) |
| 215 |
jump(i5, descr=...) |
| 216 |
""" |
| 217 |
assert self.match(loop, expected) |
| 218 |
|
| 219 |
expected = """ |
| 220 |
i5 = int_sub(i2, 1) |
| 221 |
jump(i5, descr=...) |
| 222 |
""" |
| 223 |
assert not self.match(loop, expected) |
| 224 |
|
| 225 |
expected = """ |
| 226 |
i5 = int_add(i2, 1) |
| 227 |
jump(i5, descr=...) |
| 228 |
extra_stuff(i5) |
| 229 |
""" |
| 230 |
assert not self.match(loop, expected) |
| 231 |
|
| 232 |
expected = """ |
| 233 |
i5 = int_add(i2, 1) |
| 234 |
# missing op at the end |
| 235 |
""" |
| 236 |
assert not self.match(loop, expected) |
| 237 |
|
| 238 |
expected = """ |
| 239 |
i5 = int_add(i2, 2) |
| 240 |
jump(i5, descr=...) |
| 241 |
""" |
| 242 |
assert not self.match(loop, expected) |
| 243 |
|
| 244 |
def test_dotdotdot_in_operation(self): |
| 245 |
loop = """ |
| 246 |
[i0, i1] |
| 247 |
jit_debug(i0, 1, ConstClass(myclass), i1) |
| 248 |
""" |
| 249 |
assert self.match(loop, "jit_debug(...)") |
| 250 |
assert self.match(loop, "jit_debug(i0, ...)") |
| 251 |
assert self.match(loop, "jit_debug(i0, 1, ...)") |
| 252 |
assert self.match(loop, "jit_debug(i0, 1, _, ...)") |
| 253 |
assert self.match(loop, "jit_debug(i0, 1, _, i1, ...)") |
| 254 |
py.test.raises(AssertionError, self.match, |
| 255 |
loop, "jit_debug(i0, 1, ..., i1)") |
| 256 |
|
| 257 |
def test_match_descr(self): |
| 258 |
loop = """ |
| 259 |
[p0] |
| 260 |
setfield_gc(p0, 1, descr=<foobar>) |
| 261 |
""" |
| 262 |
assert self.match(loop, "setfield_gc(p0, 1, descr=<foobar>)") |
| 263 |
assert self.match(loop, "setfield_gc(p0, 1, descr=...)") |
| 264 |
assert self.match(loop, "setfield_gc(p0, 1, descr=<.*bar>)") |
| 265 |
assert not self.match(loop, "setfield_gc(p0, 1)") |
| 266 |
assert not self.match(loop, "setfield_gc(p0, 1, descr=<zzz>)") |
| 267 |
|
| 268 |
|
| 269 |
def test_partial_match(self): |
| 270 |
loop = """ |
| 271 |
[i0] |
| 272 |
i1 = int_add(i0, 1) |
| 273 |
i2 = int_sub(i1, 10) |
| 274 |
i3 = int_xor(i2, 100) |
| 275 |
i4 = int_mul(i1, 1000) |
| 276 |
jump(i4) |
| 277 |
""" |
| 278 |
expected = """ |
| 279 |
i1 = int_add(i0, 1) |
| 280 |
... |
| 281 |
i4 = int_mul(i1, 1000) |
| 282 |
jump(i4, descr=...) |
| 283 |
""" |
| 284 |
assert self.match(loop, expected) |
| 285 |
|
| 286 |
def test_partial_match_is_non_greedy(self): |
| 287 |
loop = """ |
| 288 |
[i0] |
| 289 |
i1 = int_add(i0, 1) |
| 290 |
i2 = int_sub(i1, 10) |
| 291 |
i3 = int_mul(i2, 1000) |
| 292 |
i4 = int_mul(i1, 1000) |
| 293 |
jump(i4, descr=...) |
| 294 |
""" |
| 295 |
expected = """ |
| 296 |
i1 = int_add(i0, 1) |
| 297 |
... |
| 298 |
_ = int_mul(_, 1000) |
| 299 |
jump(i4, descr=...) |
| 300 |
""" |
| 301 |
|
| 302 |
|
| 303 |
assert not self.match(loop, expected) |
| 304 |
|
| 305 |
def test_partial_match_at_the_end(self): |
| 306 |
loop = """ |
| 307 |
[i0] |
| 308 |
i1 = int_add(i0, 1) |
| 309 |
i2 = int_sub(i1, 10) |
| 310 |
i3 = int_xor(i2, 100) |
| 311 |
i4 = int_mul(i1, 1000) |
| 312 |
jump(i4) |
| 313 |
""" |
| 314 |
expected = """ |
| 315 |
i1 = int_add(i0, 1) |
| 316 |
... |
| 317 |
""" |
| 318 |
assert self.match(loop, expected) |
| 319 |
|
| 320 |
def test_ignore_opcodes(self): |
| 321 |
loop = """ |
| 322 |
[i0] |
| 323 |
i1 = int_add(i0, 1) |
| 324 |
i4 = force_token() |
| 325 |
i2 = int_sub(i1, 10) |
| 326 |
jump(i4) |
| 327 |
""" |
| 328 |
expected = """ |
| 329 |
i1 = int_add(i0, 1) |
| 330 |
i2 = int_sub(i1, 10) |
| 331 |
jump(i4, descr=...) |
| 332 |
""" |
| 333 |
assert self.match(loop, expected, ignore_ops=['force_token']) |
| 334 |
|
| 335 |
loop = """ |
| 336 |
[i0] |
| 337 |
i1 = int_add(i0, 1) |
| 338 |
i4 = force_token() |
| 339 |
""" |
| 340 |
expected = """ |
| 341 |
i1 = int_add(i0, 1) |
| 342 |
""" |
| 343 |
assert self.match(loop, expected, ignore_ops=['force_token']) |
| 344 |
|
| 345 |
def test_match_dots_in_arguments(self): |
| 346 |
loop = """ |
| 347 |
[i0] |
| 348 |
i1 = int_add(0, 1) |
| 349 |
jump(i4, descr=...) |
| 350 |
""" |
| 351 |
expected = """ |
| 352 |
i1 = int_add(...) |
| 353 |
jump(i4, descr=...) |
| 354 |
""" |
| 355 |
assert self.match(loop, expected) |
| 356 |
|
| 357 |
def test_match_any_order(self): |
| 358 |
loop = """ |
| 359 |
[i0, i1] |
| 360 |
i2 = int_add(i0, 1) |
| 361 |
i3 = int_add(i1, 2) |
| 362 |
jump(i2, i3, descr=...) |
| 363 |
""" |
| 364 |
expected = """ |
| 365 |
{{{ |
| 366 |
i2 = int_add(i0, 1) |
| 367 |
i3 = int_add(i1, 2) |
| 368 |
}}} |
| 369 |
jump(i2, i3, descr=...) |
| 370 |
""" |
| 371 |
assert self.match(loop, expected) |
| 372 |
|
| 373 |
expected = """ |
| 374 |
{{{ |
| 375 |
i3 = int_add(i1, 2) |
| 376 |
i2 = int_add(i0, 1) |
| 377 |
}}} |
| 378 |
jump(i2, i3, descr=...) |
| 379 |
""" |
| 380 |
assert self.match(loop, expected) |
| 381 |
|
| 382 |
expected = """ |
| 383 |
{{{ |
| 384 |
i2 = int_add(i0, 1) |
| 385 |
i3 = int_add(i1, 2) |
| 386 |
i4 = int_add(i1, 3) |
| 387 |
}}} |
| 388 |
jump(i2, i3, descr=...) |
| 389 |
""" |
| 390 |
assert not self.match(loop, expected) |
| 391 |
|
| 392 |
expected = """ |
| 393 |
{{{ |
| 394 |
i2 = int_add(i0, 1) |
| 395 |
}}} |
| 396 |
jump(i2, i3, descr=...) |
| 397 |
""" |
| 398 |
assert not self.match(loop, expected) |
| 399 |
|
| 400 |
def test_match_optional_op(self): |
| 401 |
loop = """ |
| 402 |
i1 = int_add(i0, 1) |
| 403 |
""" |
| 404 |
expected = """ |
| 405 |
guard_not_invalidated? |
| 406 |
i1 = int_add(i0, 1) |
| 407 |
""" |
| 408 |
assert self.match(loop, expected) |
| 409 |
|
| 410 |
loop = """ |
| 411 |
i1 = int_add(i0, 1) |
| 412 |
""" |
| 413 |
expected = """ |
| 414 |
i1 = int_add(i0, 1) |
| 415 |
guard_not_invalidated? |
| 416 |
""" |
| 417 |
assert self.match(loop, expected) |
| 418 |
|
| 419 |
|
| 420 |
class TestRunPyPyC(BaseTestPyPyC): |
| 421 |
def test_run_function(self): |
| 422 |
def f(a, b): |
| 423 |
return a+b |
| 424 |
log = self.run(f, [30, 12]) |
| 425 |
assert log.result == 42 |
| 426 |
|
| 427 |
def test_run_src(self): |
| 428 |
src = """ |
| 429 |
def f(a, b): |
| 430 |
return a+b |
| 431 |
def main(a, b): |
| 432 |
return f(a, b) |
| 433 |
""" |
| 434 |
log = self.run(src, [30, 12]) |
| 435 |
assert log.result == 42 |
| 436 |
|
| 437 |
def test_skip(self): |
| 438 |
import pytest |
| 439 |
def f(): |
| 440 |
import sys |
| 441 |
sys.stderr.write('SKIP: foobar\n') |
| 442 |
|
| 443 |
pytest.raises(pytest.skip.Exception, "self.run(f, [])") |
| 444 |
|
| 445 |
def test_parse_jitlog(self): |
| 446 |
def f(): |
| 447 |
i = 0 |
| 448 |
while i < 1003: |
| 449 |
i += 1 |
| 450 |
return i |
| 451 |
|
| 452 |
log = self.run(f) |
| 453 |
assert log.result == 1003 |
| 454 |
loops = log.loops_by_filename(self.filepath) |
| 455 |
assert len(loops) == 1 |
| 456 |
assert loops[0].filename == self.filepath |
| 457 |
assert len([op for op in loops[0].allops() if op.name == 'label']) == 0 |
| 458 |
assert len([op for op in loops[0].allops() if op.name == 'guard_nonnull_class']) == 0 |
| 459 |
|
| 460 |
loops = log.loops_by_filename(self.filepath, is_entry_bridge=True) |
| 461 |
assert len(loops) == 1 |
| 462 |
assert len([op for op in loops[0].allops() if op.name == 'label']) == 0 |
| 463 |
assert len([op for op in loops[0].allops() if op.name == 'guard_nonnull_class']) > 0 |
| 464 |
|
| 465 |
loops = log.loops_by_filename(self.filepath, is_entry_bridge='*') |
| 466 |
assert len(loops) == 1 |
| 467 |
assert len([op for op in loops[0].allops() if op.name == 'label']) == 2 |
| 468 |
|
| 469 |
def test_loops_by_id(self): |
| 470 |
def f(): |
| 471 |
i = 0 |
| 472 |
while i < 1003: |
| 473 |
i += 1 |
| 474 |
return i |
| 475 |
|
| 476 |
log = self.run(f) |
| 477 |
loop, = log.loops_by_id('increment') |
| 478 |
assert loop.filename == self.filepath |
| 479 |
assert loop.code.name == 'f' |
| 480 |
|
| 481 |
ops = loop.allops() |
| 482 |
assert log.opnames(ops) == [ |
| 483 |
|
| 484 |
'int_lt', 'guard_true', 'int_add', |
| 485 |
|
| 486 |
'guard_not_invalidated', 'getfield_raw_i', 'int_lt', 'guard_false', |
| 487 |
'jump' |
| 488 |
] |
| 489 |
|
| 490 |
def test_ops_by_id(self): |
| 491 |
def f(): |
| 492 |
i = 0 |
| 493 |
while i < 1003: |
| 494 |
i += 1 |
| 495 |
a = 0 |
| 496 |
return i |
| 497 |
|
| 498 |
log = self.run(f) |
| 499 |
loop, = log.loops_by_id('increment') |
| 500 |
|
| 501 |
ops = loop.ops_by_id('increment') |
| 502 |
assert log.opnames(ops) == ['int_add'] |
| 503 |
|
| 504 |
ops = loop.ops_by_id('cond') |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
assert log.opnames(ops) == ['int_lt', 'guard_true', 'jump'] |
| 509 |
|
| 510 |
def test_ops_by_id_and_opcode(self): |
| 511 |
def f(): |
| 512 |
i = 0 |
| 513 |
j = 0 |
| 514 |
while i < 1003: |
| 515 |
i += 1; j -= 1 |
| 516 |
a = 0 |
| 517 |
return i |
| 518 |
|
| 519 |
log = self.run(f) |
| 520 |
loop, = log.loops_by_id('foo') |
| 521 |
|
| 522 |
ops = loop.ops_by_id('foo', opcode='INPLACE_ADD') |
| 523 |
assert log.opnames(ops) == ['int_add'] |
| 524 |
|
| 525 |
ops = loop.ops_by_id('foo', opcode='INPLACE_SUBTRACT') |
| 526 |
assert log.opnames(ops) == ['int_sub_ovf', 'guard_no_overflow'] |
| 527 |
|
| 528 |
def test_inlined_function(self): |
| 529 |
def f(): |
| 530 |
def g(x): |
| 531 |
return x+1 |
| 532 |
i = 0 |
| 533 |
while i < 1003: |
| 534 |
i = g(i) |
| 535 |
a = 0 |
| 536 |
return i |
| 537 |
|
| 538 |
log = self.run(f) |
| 539 |
loop, = log.loops_by_filename(self.filepath) |
| 540 |
call_ops = log.opnames(loop.ops_by_id('call')) |
| 541 |
assert call_ops == ['guard_not_invalidated', 'force_token'] |
| 542 |
|
| 543 |
add_ops = log.opnames(loop.ops_by_id('add')) |
| 544 |
assert add_ops == ['int_add'] |
| 545 |
|
| 546 |
ops = log.opnames(loop.allops()) |
| 547 |
assert ops == [ |
| 548 |
|
| 549 |
'int_lt', 'guard_true', |
| 550 |
'guard_not_invalidated', 'force_token', 'int_add', |
| 551 |
|
| 552 |
'getfield_raw_i', 'int_lt', 'guard_false', |
| 553 |
'jump' |
| 554 |
] |
| 555 |
|
| 556 |
def test_loop_match(self): |
| 557 |
def f(): |
| 558 |
i = 0 |
| 559 |
while i < 1003: |
| 560 |
i += 1 |
| 561 |
return i |
| 562 |
|
| 563 |
log = self.run(f) |
| 564 |
loop, = log.loops_by_id('increment') |
| 565 |
assert loop.match(""" |
| 566 |
i6 = int_lt(i4, 1003) |
| 567 |
guard_true(i6, descr=...) |
| 568 |
i8 = int_add(i4, 1) |
| 569 |
# signal checking stuff |
| 570 |
guard_not_invalidated(descr=...) |
| 571 |
i10 = getfield_raw_i(..., descr=<.* pypysig_long_struct_inner.c_value .*>) |
| 572 |
i14 = int_lt(i10, 0) |
| 573 |
guard_false(i14, descr=...) |
| 574 |
jump(..., descr=...) |
| 575 |
""") |
| 576 |
|
| 577 |
assert loop.match(""" |
| 578 |
i6 = int_lt(i4, 1003) |
| 579 |
guard_true(i6, descr=...) |
| 580 |
i8 = int_add(i4, 1) |
| 581 |
--TICK-- |
| 582 |
jump(..., descr=...) |
| 583 |
""") |
| 584 |
|
| 585 |
py.test.raises(InvalidMatch, loop.match, """ |
| 586 |
i6 = int_lt(i4, 1003) |
| 587 |
guard_true(i6) |
| 588 |
i8 = int_add(i5, 1) # variable mismatch |
| 589 |
--TICK-- |
| 590 |
jump(..., descr=...) |
| 591 |
""") |
| 592 |
|
| 593 |
def test_match_by_id(self): |
| 594 |
def f(): |
| 595 |
i = 0 |
| 596 |
j = 2000 |
| 597 |
while i < 1003: |
| 598 |
i += 1 |
| 599 |
j -= 1 |
| 600 |
a = 0 |
| 601 |
return i |
| 602 |
|
| 603 |
log = self.run(f) |
| 604 |
loop, = log.loops_by_id('increment') |
| 605 |
assert loop.match_by_id('increment', """ |
| 606 |
i1 = int_add(i0, 1) |
| 607 |
""") |
| 608 |
assert loop.match_by_id('product', """ |
| 609 |
i4 = int_sub_ovf(i3, 1) |
| 610 |
guard_no_overflow(descr=...) |
| 611 |
""") |
| 612 |
|
| 613 |
def test_match_constants(self): |
| 614 |
def f(): |
| 615 |
from socket import ntohs |
| 616 |
i = 0 |
| 617 |
while i < 1003: |
| 618 |
i += 1 |
| 619 |
j = ntohs(1) |
| 620 |
a = 0 |
| 621 |
return i |
| 622 |
log = self.run(f, import_site=True) |
| 623 |
loop, = log.loops_by_id('ntohs') |
| 624 |
assert loop.match_by_id('ntohs', """ |
| 625 |
i12 = call_i(ConstClass(ntohs), 1, descr=...) |
| 626 |
guard_no_exception(descr=...) |
| 627 |
""", |
| 628 |
include_guard_not_invalidated=False) |
| 629 |
|
| 630 |
py.test.raises(InvalidMatch, loop.match_by_id, 'ntohs', """ |
| 631 |
guard_not_invalidated(descr=...) |
| 632 |
i12 = call_i(ConstClass(foobar), 1, descr=...) |
| 633 |
guard_no_exception(descr=...) |
| 634 |
""") |