| 1 |
import contextlib |
| 2 |
import itertools |
| 3 |
import sys |
| 4 |
import textwrap |
| 5 |
import unittest |
| 6 |
import gc |
| 7 |
import os |
| 8 |
|
| 9 |
import _opcode |
| 10 |
|
| 11 |
from test.support import (script_helper, requires_specialization, |
| 12 |
import_helper, Py_GIL_DISABLED, requires_jit_enabled, |
| 13 |
reset_code) |
| 14 |
|
| 15 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 16 |
|
| 17 |
from _testinternalcapi import TIER2_THRESHOLD |
| 18 |
|
| 19 |
|
| 20 |
@contextlib.contextmanager |
| 21 |
def clear_executors(func): |
| 22 |
# Clear executors in func before and after running a block |
| 23 |
reset_code(func) |
| 24 |
try: |
| 25 |
yield |
| 26 |
finally: |
| 27 |
reset_code(func) |
| 28 |
|
| 29 |
|
| 30 |
def get_first_executor(func): |
| 31 |
code = func.__code__ |
| 32 |
co_code = code.co_code |
| 33 |
for i in range(0, len(co_code), 2): |
| 34 |
try: |
| 35 |
return _opcode.get_executor(code, i) |
| 36 |
except ValueError: |
| 37 |
pass |
| 38 |
return None |
| 39 |
|
| 40 |
|
| 41 |
def iter_opnames(ex): |
| 42 |
for item in ex: |
| 43 |
yield item[0] |
| 44 |
|
| 45 |
|
| 46 |
def get_opnames(ex): |
| 47 |
return list(iter_opnames(ex)) |
| 48 |
|
| 49 |
|
| 50 |
@requires_specialization |
| 51 |
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds") |
| 52 |
@requires_jit_enabled |
| 53 |
class TestExecutorInvalidation(unittest.TestCase): |
| 54 |
|
| 55 |
def test_invalidate_object(self): |
| 56 |
# Generate a new set of functions at each call |
| 57 |
ns = {} |
| 58 |
func_src = "\n".join( |
| 59 |
f""" |
| 60 |
def f{n}(): |
| 61 |
for _ in range({TIER2_THRESHOLD}): |
| 62 |
pass |
| 63 |
""" for n in range(5) |
| 64 |
) |
| 65 |
exec(textwrap.dedent(func_src), ns, ns) |
| 66 |
funcs = [ ns[f'f{n}'] for n in range(5)] |
| 67 |
objects = [object() for _ in range(5)] |
| 68 |
|
| 69 |
for f in funcs: |
| 70 |
f() |
| 71 |
executors = [get_first_executor(f) for f in funcs] |
| 72 |
# Set things up so each executor depends on the objects |
| 73 |
# with an equal or lower index. |
| 74 |
for i, exe in enumerate(executors): |
| 75 |
self.assertTrue(exe.is_valid()) |
| 76 |
for obj in objects[:i+1]: |
| 77 |
_testinternalcapi.add_executor_dependency(exe, obj) |
| 78 |
self.assertTrue(exe.is_valid()) |
| 79 |
# Assert that the correct executors are invalidated |
| 80 |
# and check that nothing crashes when we invalidate |
| 81 |
# an executor multiple times. |
| 82 |
for i in (4,3,2,1,0): |
| 83 |
_testinternalcapi.invalidate_executors(objects[i]) |
| 84 |
for exe in executors[i:]: |
| 85 |
self.assertFalse(exe.is_valid()) |
| 86 |
for exe in executors[:i]: |
| 87 |
self.assertTrue(exe.is_valid()) |
| 88 |
|
| 89 |
def test_uop_optimizer_invalidation(self): |
| 90 |
# Generate a new function at each call |
| 91 |
ns = {} |
| 92 |
exec(textwrap.dedent(f""" |
| 93 |
def f(): |
| 94 |
for i in range({TIER2_THRESHOLD}): |
| 95 |
pass |
| 96 |
"""), ns, ns) |
| 97 |
f = ns['f'] |
| 98 |
f() |
| 99 |
exe = get_first_executor(f) |
| 100 |
self.assertIsNotNone(exe) |
| 101 |
self.assertTrue(exe.is_valid()) |
| 102 |
_testinternalcapi.invalidate_executors(f.__code__) |
| 103 |
self.assertFalse(exe.is_valid()) |
| 104 |
|
| 105 |
def test_sys__clear_internal_caches(self): |
| 106 |
def f(): |
| 107 |
for _ in range(TIER2_THRESHOLD): |
| 108 |
pass |
| 109 |
f() |
| 110 |
exe = get_first_executor(f) |
| 111 |
self.assertIsNotNone(exe) |
| 112 |
self.assertTrue(exe.is_valid()) |
| 113 |
sys._clear_internal_caches() |
| 114 |
self.assertFalse(exe.is_valid()) |
| 115 |
exe = get_first_executor(f) |
| 116 |
self.assertIsNone(exe) |
| 117 |
|
| 118 |
|
| 119 |
@requires_specialization |
| 120 |
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds") |
| 121 |
@requires_jit_enabled |
| 122 |
@unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.") |
| 123 |
class TestUops(unittest.TestCase): |
| 124 |
|
| 125 |
def test_basic_loop(self): |
| 126 |
def testfunc(x): |
| 127 |
i = 0 |
| 128 |
while i < x: |
| 129 |
i += 1 |
| 130 |
|
| 131 |
testfunc(TIER2_THRESHOLD) |
| 132 |
|
| 133 |
ex = get_first_executor(testfunc) |
| 134 |
self.assertIsNotNone(ex) |
| 135 |
uops = get_opnames(ex) |
| 136 |
self.assertIn("_JUMP_TO_TOP", uops) |
| 137 |
self.assertIn("_LOAD_FAST_BORROW_0", uops) |
| 138 |
|
| 139 |
def test_extended_arg(self): |
| 140 |
"Check EXTENDED_ARG handling in superblock creation" |
| 141 |
ns = {} |
| 142 |
exec(textwrap.dedent(f""" |
| 143 |
def many_vars(): |
| 144 |
# 260 vars, so z9 should have index 259 |
| 145 |
a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = 42 |
| 146 |
b0 = b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = 42 |
| 147 |
c0 = c1 = c2 = c3 = c4 = c5 = c6 = c7 = c8 = c9 = 42 |
| 148 |
d0 = d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 42 |
| 149 |
e0 = e1 = e2 = e3 = e4 = e5 = e6 = e7 = e8 = e9 = 42 |
| 150 |
f0 = f1 = f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = 42 |
| 151 |
g0 = g1 = g2 = g3 = g4 = g5 = g6 = g7 = g8 = g9 = 42 |
| 152 |
h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = h8 = h9 = 42 |
| 153 |
i0 = i1 = i2 = i3 = i4 = i5 = i6 = i7 = i8 = i9 = 42 |
| 154 |
j0 = j1 = j2 = j3 = j4 = j5 = j6 = j7 = j8 = j9 = 42 |
| 155 |
k0 = k1 = k2 = k3 = k4 = k5 = k6 = k7 = k8 = k9 = 42 |
| 156 |
l0 = l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = 42 |
| 157 |
m0 = m1 = m2 = m3 = m4 = m5 = m6 = m7 = m8 = m9 = 42 |
| 158 |
n0 = n1 = n2 = n3 = n4 = n5 = n6 = n7 = n8 = n9 = 42 |
| 159 |
o0 = o1 = o2 = o3 = o4 = o5 = o6 = o7 = o8 = o9 = 42 |
| 160 |
p0 = p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = 42 |
| 161 |
q0 = q1 = q2 = q3 = q4 = q5 = q6 = q7 = q8 = q9 = 42 |
| 162 |
r0 = r1 = r2 = r3 = r4 = r5 = r6 = r7 = r8 = r9 = 42 |
| 163 |
s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = 42 |
| 164 |
t0 = t1 = t2 = t3 = t4 = t5 = t6 = t7 = t8 = t9 = 42 |
| 165 |
u0 = u1 = u2 = u3 = u4 = u5 = u6 = u7 = u8 = u9 = 42 |
| 166 |
v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = v8 = v9 = 42 |
| 167 |
w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = w8 = w9 = 42 |
| 168 |
x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = 42 |
| 169 |
y0 = y1 = y2 = y3 = y4 = y5 = y6 = y7 = y8 = y9 = 42 |
| 170 |
z0 = z1 = z2 = z3 = z4 = z5 = z6 = z7 = z8 = z9 = {TIER2_THRESHOLD} |
| 171 |
while z9 > 0: |
| 172 |
z9 = z9 - 1 |
| 173 |
+z9 |
| 174 |
"""), ns, ns) |
| 175 |
many_vars = ns["many_vars"] |
| 176 |
|
| 177 |
ex = get_first_executor(many_vars) |
| 178 |
self.assertIsNone(ex) |
| 179 |
many_vars() |
| 180 |
|
| 181 |
ex = get_first_executor(many_vars) |
| 182 |
self.assertIsNotNone(ex) |
| 183 |
self.assertTrue(any((opcode, oparg, operand) == ("_LOAD_FAST_BORROW", 259, 0) |
| 184 |
for opcode, oparg, _, operand in list(ex))) |
| 185 |
|
| 186 |
def test_unspecialized_unpack(self): |
| 187 |
# An example of an unspecialized opcode |
| 188 |
def testfunc(x): |
| 189 |
i = 0 |
| 190 |
while i < x: |
| 191 |
i += 1 |
| 192 |
a, b = {1: 2, 3: 3} |
| 193 |
assert a == 1 and b == 3 |
| 194 |
i = 0 |
| 195 |
while i < x: |
| 196 |
i += 1 |
| 197 |
|
| 198 |
testfunc(TIER2_THRESHOLD) |
| 199 |
|
| 200 |
ex = get_first_executor(testfunc) |
| 201 |
self.assertIsNotNone(ex) |
| 202 |
uops = get_opnames(ex) |
| 203 |
self.assertIn("_UNPACK_SEQUENCE", uops) |
| 204 |
|
| 205 |
def test_pop_jump_if_false(self): |
| 206 |
def testfunc(n): |
| 207 |
i = 0 |
| 208 |
while i < n: |
| 209 |
i += 1 |
| 210 |
|
| 211 |
testfunc(TIER2_THRESHOLD) |
| 212 |
|
| 213 |
ex = get_first_executor(testfunc) |
| 214 |
self.assertIsNotNone(ex) |
| 215 |
uops = get_opnames(ex) |
| 216 |
self.assertIn("_GUARD_IS_TRUE_POP", uops) |
| 217 |
|
| 218 |
def test_pop_jump_if_none(self): |
| 219 |
def testfunc(a): |
| 220 |
for x in a: |
| 221 |
if x is None: |
| 222 |
x = 0 |
| 223 |
|
| 224 |
testfunc(range(TIER2_THRESHOLD)) |
| 225 |
|
| 226 |
ex = get_first_executor(testfunc) |
| 227 |
self.assertIsNotNone(ex) |
| 228 |
uops = get_opnames(ex) |
| 229 |
self.assertNotIn("_GUARD_IS_NONE_POP", uops) |
| 230 |
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops) |
| 231 |
|
| 232 |
def test_pop_jump_if_not_none(self): |
| 233 |
def testfunc(a): |
| 234 |
for x in a: |
| 235 |
x = None |
| 236 |
if x is not None: |
| 237 |
x = 0 |
| 238 |
|
| 239 |
testfunc(range(TIER2_THRESHOLD)) |
| 240 |
|
| 241 |
ex = get_first_executor(testfunc) |
| 242 |
self.assertIsNotNone(ex) |
| 243 |
uops = get_opnames(ex) |
| 244 |
self.assertNotIn("_GUARD_IS_NONE_POP", uops) |
| 245 |
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops) |
| 246 |
|
| 247 |
def test_pop_jump_if_true(self): |
| 248 |
def testfunc(n): |
| 249 |
i = 0 |
| 250 |
while not i >= n: |
| 251 |
i += 1 |
| 252 |
|
| 253 |
testfunc(TIER2_THRESHOLD) |
| 254 |
|
| 255 |
ex = get_first_executor(testfunc) |
| 256 |
self.assertIsNotNone(ex) |
| 257 |
uops = get_opnames(ex) |
| 258 |
self.assertIn("_GUARD_IS_FALSE_POP", uops) |
| 259 |
|
| 260 |
def test_jump_backward(self): |
| 261 |
def testfunc(n): |
| 262 |
i = 0 |
| 263 |
while i < n: |
| 264 |
i += 1 |
| 265 |
|
| 266 |
testfunc(TIER2_THRESHOLD) |
| 267 |
|
| 268 |
ex = get_first_executor(testfunc) |
| 269 |
self.assertIsNotNone(ex) |
| 270 |
uops = get_opnames(ex) |
| 271 |
self.assertIn("_JUMP_TO_TOP", uops) |
| 272 |
|
| 273 |
def test_jump_forward(self): |
| 274 |
def testfunc(n): |
| 275 |
a = 0 |
| 276 |
while a < n: |
| 277 |
if a < 0: |
| 278 |
a = -a |
| 279 |
else: |
| 280 |
a = +a |
| 281 |
a += 1 |
| 282 |
return a |
| 283 |
|
| 284 |
testfunc(TIER2_THRESHOLD) |
| 285 |
|
| 286 |
ex = get_first_executor(testfunc) |
| 287 |
self.assertIsNotNone(ex) |
| 288 |
uops = get_opnames(ex) |
| 289 |
# Since there is no JUMP_FORWARD instruction, |
| 290 |
# look for indirect evidence: the += operator |
| 291 |
self.assertIn("_BINARY_OP_ADD_INT", uops) |
| 292 |
|
| 293 |
def test_for_iter_range(self): |
| 294 |
def testfunc(n): |
| 295 |
total = 0 |
| 296 |
for i in range(n): |
| 297 |
total += i |
| 298 |
return total |
| 299 |
|
| 300 |
total = testfunc(TIER2_THRESHOLD) |
| 301 |
self.assertEqual(total, sum(range(TIER2_THRESHOLD))) |
| 302 |
|
| 303 |
ex = get_first_executor(testfunc) |
| 304 |
self.assertIsNotNone(ex) |
| 305 |
# for i, (opname, oparg) in enumerate(ex): |
| 306 |
# print(f"{i:4d}: {opname:<20s} {oparg:3d}") |
| 307 |
uops = get_opnames(ex) |
| 308 |
self.assertIn("_GUARD_NOT_EXHAUSTED_RANGE", uops) |
| 309 |
# Verification that the jump goes past END_FOR |
| 310 |
# is done by manual inspection of the output |
| 311 |
|
| 312 |
def test_for_iter_list(self): |
| 313 |
def testfunc(a): |
| 314 |
total = 0 |
| 315 |
for i in a: |
| 316 |
total += i |
| 317 |
return total |
| 318 |
|
| 319 |
a = list(range(TIER2_THRESHOLD)) |
| 320 |
total = testfunc(a) |
| 321 |
self.assertEqual(total, sum(a)) |
| 322 |
|
| 323 |
ex = get_first_executor(testfunc) |
| 324 |
self.assertIsNotNone(ex) |
| 325 |
# for i, (opname, oparg) in enumerate(ex): |
| 326 |
# print(f"{i:4d}: {opname:<20s} {oparg:3d}") |
| 327 |
uops = get_opnames(ex) |
| 328 |
self.assertIn("_GUARD_NOT_EXHAUSTED_LIST", uops) |
| 329 |
# Verification that the jump goes past END_FOR |
| 330 |
# is done by manual inspection of the output |
| 331 |
|
| 332 |
def test_for_iter_tuple(self): |
| 333 |
def testfunc(a): |
| 334 |
total = 0 |
| 335 |
for i in a: |
| 336 |
total += i |
| 337 |
return total |
| 338 |
|
| 339 |
a = tuple(range(TIER2_THRESHOLD)) |
| 340 |
total = testfunc(a) |
| 341 |
self.assertEqual(total, sum(a)) |
| 342 |
|
| 343 |
ex = get_first_executor(testfunc) |
| 344 |
self.assertIsNotNone(ex) |
| 345 |
# for i, (opname, oparg) in enumerate(ex): |
| 346 |
# print(f"{i:4d}: {opname:<20s} {oparg:3d}") |
| 347 |
uops = get_opnames(ex) |
| 348 |
self.assertIn("_GUARD_NOT_EXHAUSTED_TUPLE", uops) |
| 349 |
# Verification that the jump goes past END_FOR |
| 350 |
# is done by manual inspection of the output |
| 351 |
|
| 352 |
def test_list_edge_case(self): |
| 353 |
def testfunc(it): |
| 354 |
for x in it: |
| 355 |
pass |
| 356 |
|
| 357 |
a = [1, 2, 3] |
| 358 |
it = iter(a) |
| 359 |
testfunc(it) |
| 360 |
a.append(4) |
| 361 |
with self.assertRaises(StopIteration): |
| 362 |
next(it) |
| 363 |
|
| 364 |
def test_call_py_exact_args(self): |
| 365 |
def testfunc(n): |
| 366 |
def dummy(x): |
| 367 |
return x+1 |
| 368 |
for i in range(n): |
| 369 |
dummy(i) |
| 370 |
|
| 371 |
testfunc(TIER2_THRESHOLD) |
| 372 |
|
| 373 |
ex = get_first_executor(testfunc) |
| 374 |
self.assertIsNotNone(ex) |
| 375 |
uops = get_opnames(ex) |
| 376 |
self.assertIn("_PUSH_FRAME", uops) |
| 377 |
self.assertIn("_BINARY_OP_ADD_INT", uops) |
| 378 |
|
| 379 |
def test_branch_taken(self): |
| 380 |
def testfunc(n): |
| 381 |
for i in range(n): |
| 382 |
if i < 0: |
| 383 |
i = 0 |
| 384 |
else: |
| 385 |
i = 1 |
| 386 |
|
| 387 |
testfunc(TIER2_THRESHOLD) |
| 388 |
|
| 389 |
ex = get_first_executor(testfunc) |
| 390 |
self.assertIsNotNone(ex) |
| 391 |
uops = get_opnames(ex) |
| 392 |
self.assertIn("_GUARD_IS_FALSE_POP", uops) |
| 393 |
|
| 394 |
def test_for_iter_tier_two(self): |
| 395 |
class MyIter: |
| 396 |
def __init__(self, n): |
| 397 |
self.n = n |
| 398 |
def __iter__(self): |
| 399 |
return self |
| 400 |
def __next__(self): |
| 401 |
self.n -= 1 |
| 402 |
if self.n < 0: |
| 403 |
raise StopIteration |
| 404 |
return self.n |
| 405 |
|
| 406 |
def testfunc(n, m): |
| 407 |
x = 0 |
| 408 |
for i in range(m): |
| 409 |
for j in MyIter(n): |
| 410 |
x += 1000*i + j |
| 411 |
return x |
| 412 |
|
| 413 |
x = testfunc(TIER2_THRESHOLD, TIER2_THRESHOLD) |
| 414 |
|
| 415 |
self.assertEqual(x, sum(range(TIER2_THRESHOLD)) * TIER2_THRESHOLD * 1001) |
| 416 |
|
| 417 |
ex = get_first_executor(testfunc) |
| 418 |
self.assertIsNotNone(ex) |
| 419 |
uops = get_opnames(ex) |
| 420 |
self.assertIn("_FOR_ITER_TIER_TWO", uops) |
| 421 |
|
| 422 |
def test_confidence_score(self): |
| 423 |
def testfunc(n): |
| 424 |
bits = 0 |
| 425 |
for i in range(n): |
| 426 |
if i & 0x01: |
| 427 |
bits += 1 |
| 428 |
if i & 0x02: |
| 429 |
bits += 1 |
| 430 |
if i&0x04: |
| 431 |
bits += 1 |
| 432 |
if i&0x08: |
| 433 |
bits += 1 |
| 434 |
if i&0x10: |
| 435 |
bits += 1 |
| 436 |
return bits |
| 437 |
|
| 438 |
x = testfunc(TIER2_THRESHOLD * 2) |
| 439 |
|
| 440 |
self.assertEqual(x, TIER2_THRESHOLD * 5) |
| 441 |
ex = get_first_executor(testfunc) |
| 442 |
self.assertIsNotNone(ex) |
| 443 |
ops = list(iter_opnames(ex)) |
| 444 |
#Since branch is 50/50 the trace could go either way. |
| 445 |
count = ops.count("_GUARD_IS_TRUE_POP") + ops.count("_GUARD_IS_FALSE_POP") |
| 446 |
self.assertLessEqual(count, 2) |
| 447 |
|
| 448 |
|
| 449 |
@requires_specialization |
| 450 |
@unittest.skipIf(Py_GIL_DISABLED, "optimizer not yet supported in free-threaded builds") |
| 451 |
@requires_jit_enabled |
| 452 |
@unittest.skipIf(os.getenv("PYTHON_UOPS_OPTIMIZE") == "0", "Needs uop optimizer to run.") |
| 453 |
class TestUopsOptimization(unittest.TestCase): |
| 454 |
|
| 455 |
def _run_with_optimizer(self, testfunc, arg): |
| 456 |
res = testfunc(arg) |
| 457 |
|
| 458 |
ex = get_first_executor(testfunc) |
| 459 |
return res, ex |
| 460 |
|
| 461 |
|
| 462 |
def test_int_type_propagation(self): |
| 463 |
def testfunc(loops): |
| 464 |
num = 0 |
| 465 |
for i in range(loops): |
| 466 |
x = num + num |
| 467 |
a = x + 1 |
| 468 |
num += 1 |
| 469 |
return a |
| 470 |
|
| 471 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 472 |
self.assertIsNotNone(ex) |
| 473 |
self.assertEqual(res, (TIER2_THRESHOLD - 1) * 2 + 1) |
| 474 |
binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"] |
| 475 |
guard_tos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 476 |
guard_nos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"] |
| 477 |
self.assertGreaterEqual(len(binop_count), 3) |
| 478 |
self.assertLessEqual(len(guard_tos_int_count), 1) |
| 479 |
self.assertLessEqual(len(guard_nos_int_count), 1) |
| 480 |
|
| 481 |
def test_int_type_propagation_through_frame(self): |
| 482 |
def double(x): |
| 483 |
return x + x |
| 484 |
def testfunc(loops): |
| 485 |
num = 0 |
| 486 |
for i in range(loops): |
| 487 |
x = num + num |
| 488 |
a = double(x) |
| 489 |
num += 1 |
| 490 |
return a |
| 491 |
|
| 492 |
res = testfunc(TIER2_THRESHOLD) |
| 493 |
|
| 494 |
ex = get_first_executor(testfunc) |
| 495 |
self.assertIsNotNone(ex) |
| 496 |
self.assertEqual(res, (TIER2_THRESHOLD - 1) * 4) |
| 497 |
binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"] |
| 498 |
guard_tos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 499 |
guard_nos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"] |
| 500 |
self.assertGreaterEqual(len(binop_count), 3) |
| 501 |
self.assertLessEqual(len(guard_tos_int_count), 1) |
| 502 |
self.assertLessEqual(len(guard_nos_int_count), 1) |
| 503 |
|
| 504 |
def test_int_type_propagation_from_frame(self): |
| 505 |
def double(x): |
| 506 |
return x + x |
| 507 |
def testfunc(loops): |
| 508 |
num = 0 |
| 509 |
for i in range(loops): |
| 510 |
a = double(num) |
| 511 |
x = a + a |
| 512 |
num += 1 |
| 513 |
return x |
| 514 |
|
| 515 |
res = testfunc(TIER2_THRESHOLD) |
| 516 |
|
| 517 |
ex = get_first_executor(testfunc) |
| 518 |
self.assertIsNotNone(ex) |
| 519 |
self.assertEqual(res, (TIER2_THRESHOLD - 1) * 4) |
| 520 |
binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"] |
| 521 |
guard_tos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 522 |
guard_nos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"] |
| 523 |
self.assertGreaterEqual(len(binop_count), 3) |
| 524 |
self.assertLessEqual(len(guard_tos_int_count), 1) |
| 525 |
self.assertLessEqual(len(guard_nos_int_count), 1) |
| 526 |
|
| 527 |
def test_int_impure_region(self): |
| 528 |
def testfunc(loops): |
| 529 |
num = 0 |
| 530 |
while num < loops: |
| 531 |
x = num + num |
| 532 |
y = 1 |
| 533 |
x // 2 |
| 534 |
a = x + y |
| 535 |
num += 1 |
| 536 |
return a |
| 537 |
|
| 538 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 539 |
self.assertIsNotNone(ex) |
| 540 |
binop_count = [opname for opname in iter_opnames(ex) if opname == "_BINARY_OP_ADD_INT"] |
| 541 |
self.assertGreaterEqual(len(binop_count), 3) |
| 542 |
|
| 543 |
def test_call_py_exact_args(self): |
| 544 |
def testfunc(n): |
| 545 |
def dummy(x): |
| 546 |
return x+1 |
| 547 |
for i in range(n): |
| 548 |
dummy(i) |
| 549 |
|
| 550 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 551 |
self.assertIsNotNone(ex) |
| 552 |
uops = get_opnames(ex) |
| 553 |
self.assertIn("_PUSH_FRAME", uops) |
| 554 |
self.assertIn("_BINARY_OP_ADD_INT", uops) |
| 555 |
self.assertNotIn("_CHECK_PEP_523", uops) |
| 556 |
|
| 557 |
def test_int_type_propagate_through_range(self): |
| 558 |
def testfunc(n): |
| 559 |
|
| 560 |
for i in range(n): |
| 561 |
x = i + i |
| 562 |
return x |
| 563 |
|
| 564 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 565 |
self.assertEqual(res, (TIER2_THRESHOLD - 1) * 2) |
| 566 |
self.assertIsNotNone(ex) |
| 567 |
uops = get_opnames(ex) |
| 568 |
self.assertNotIn("_GUARD_TOS_INT", uops) |
| 569 |
self.assertNotIn("_GUARD_NOS_INT", uops) |
| 570 |
|
| 571 |
def test_int_value_numbering(self): |
| 572 |
def testfunc(n): |
| 573 |
|
| 574 |
y = 1 |
| 575 |
for i in range(n): |
| 576 |
x = y |
| 577 |
z = x |
| 578 |
a = z |
| 579 |
b = a |
| 580 |
res = x + z + a + b |
| 581 |
return res |
| 582 |
|
| 583 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 584 |
self.assertEqual(res, 4) |
| 585 |
self.assertIsNotNone(ex) |
| 586 |
uops = get_opnames(ex) |
| 587 |
self.assertIn("_GUARD_TOS_INT", uops) |
| 588 |
self.assertNotIn("_GUARD_NOS_INT", uops) |
| 589 |
guard_tos_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 590 |
self.assertEqual(len(guard_tos_count), 1) |
| 591 |
|
| 592 |
def test_comprehension(self): |
| 593 |
def testfunc(n): |
| 594 |
for _ in range(n): |
| 595 |
return [i for i in range(n)] |
| 596 |
|
| 597 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 598 |
self.assertEqual(res, list(range(TIER2_THRESHOLD))) |
| 599 |
self.assertIsNotNone(ex) |
| 600 |
uops = get_opnames(ex) |
| 601 |
self.assertNotIn("_BINARY_OP_ADD_INT", uops) |
| 602 |
|
| 603 |
def test_call_py_exact_args_disappearing(self): |
| 604 |
def dummy(x): |
| 605 |
return x+1 |
| 606 |
|
| 607 |
def testfunc(n): |
| 608 |
for i in range(n): |
| 609 |
dummy(i) |
| 610 |
|
| 611 |
# Trigger specialization |
| 612 |
testfunc(8) |
| 613 |
del dummy |
| 614 |
gc.collect() |
| 615 |
|
| 616 |
def dummy(x): |
| 617 |
return x + 2 |
| 618 |
testfunc(32) |
| 619 |
|
| 620 |
ex = get_first_executor(testfunc) |
| 621 |
# Honestly as long as it doesn't crash it's fine. |
| 622 |
# Whether we get an executor or not is non-deterministic, |
| 623 |
# because it's decided by when the function is freed. |
| 624 |
# This test is a little implementation specific. |
| 625 |
|
| 626 |
def test_promote_globals_to_constants(self): |
| 627 |
|
| 628 |
result = script_helper.run_python_until_end('-c', textwrap.dedent(""" |
| 629 |
import _testinternalcapi |
| 630 |
import opcode |
| 631 |
import _opcode |
| 632 |
|
| 633 |
def get_first_executor(func): |
| 634 |
code = func.__code__ |
| 635 |
co_code = code.co_code |
| 636 |
for i in range(0, len(co_code), 2): |
| 637 |
try: |
| 638 |
return _opcode.get_executor(code, i) |
| 639 |
except ValueError: |
| 640 |
pass |
| 641 |
return None |
| 642 |
|
| 643 |
def get_opnames(ex): |
| 644 |
return {item[0] for item in ex} |
| 645 |
|
| 646 |
def testfunc(n): |
| 647 |
for i in range(n): |
| 648 |
x = range(i) |
| 649 |
return x |
| 650 |
|
| 651 |
testfunc(_testinternalcapi.TIER2_THRESHOLD) |
| 652 |
|
| 653 |
ex = get_first_executor(testfunc) |
| 654 |
assert ex is not None |
| 655 |
uops = get_opnames(ex) |
| 656 |
assert "_LOAD_GLOBAL_BUILTINS" not in uops |
| 657 |
assert "_LOAD_CONST_INLINE_BORROW" in uops |
| 658 |
"""), PYTHON_JIT="1") |
| 659 |
self.assertEqual(result[0].rc, 0, result) |
| 660 |
|
| 661 |
def test_float_add_constant_propagation(self): |
| 662 |
def testfunc(n): |
| 663 |
a = 1.0 |
| 664 |
for _ in range(n): |
| 665 |
a = a + 0.25 |
| 666 |
a = a + 0.25 |
| 667 |
a = a + 0.25 |
| 668 |
a = a + 0.25 |
| 669 |
return a |
| 670 |
|
| 671 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 672 |
self.assertAlmostEqual(res, TIER2_THRESHOLD + 1) |
| 673 |
self.assertIsNotNone(ex) |
| 674 |
uops = get_opnames(ex) |
| 675 |
guard_tos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_FLOAT"] |
| 676 |
guard_nos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"] |
| 677 |
self.assertLessEqual(len(guard_tos_float_count), 1) |
| 678 |
self.assertLessEqual(len(guard_nos_float_count), 1) |
| 679 |
# TODO gh-115506: this assertion may change after propagating constants. |
| 680 |
# We'll also need to verify that propagation actually occurs. |
| 681 |
self.assertIn("_BINARY_OP_ADD_FLOAT", uops) |
| 682 |
|
| 683 |
def test_float_subtract_constant_propagation(self): |
| 684 |
def testfunc(n): |
| 685 |
a = 1.0 |
| 686 |
for _ in range(n): |
| 687 |
a = a - 0.25 |
| 688 |
a = a - 0.25 |
| 689 |
a = a - 0.25 |
| 690 |
a = a - 0.25 |
| 691 |
return a |
| 692 |
|
| 693 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 694 |
self.assertAlmostEqual(res, -TIER2_THRESHOLD + 1) |
| 695 |
self.assertIsNotNone(ex) |
| 696 |
uops = get_opnames(ex) |
| 697 |
guard_tos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_FLOAT"] |
| 698 |
guard_nos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"] |
| 699 |
self.assertLessEqual(len(guard_tos_float_count), 1) |
| 700 |
self.assertLessEqual(len(guard_nos_float_count), 1) |
| 701 |
# TODO gh-115506: this assertion may change after propagating constants. |
| 702 |
# We'll also need to verify that propagation actually occurs. |
| 703 |
self.assertIn("_BINARY_OP_SUBTRACT_FLOAT", uops) |
| 704 |
|
| 705 |
def test_float_multiply_constant_propagation(self): |
| 706 |
def testfunc(n): |
| 707 |
a = 1.0 |
| 708 |
for _ in range(n): |
| 709 |
a = a * 1.0 |
| 710 |
a = a * 1.0 |
| 711 |
a = a * 1.0 |
| 712 |
a = a * 1.0 |
| 713 |
return a |
| 714 |
|
| 715 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 716 |
self.assertAlmostEqual(res, 1.0) |
| 717 |
self.assertIsNotNone(ex) |
| 718 |
uops = get_opnames(ex) |
| 719 |
guard_tos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_FLOAT"] |
| 720 |
guard_nos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"] |
| 721 |
self.assertLessEqual(len(guard_tos_float_count), 1) |
| 722 |
self.assertLessEqual(len(guard_nos_float_count), 1) |
| 723 |
# TODO gh-115506: this assertion may change after propagating constants. |
| 724 |
# We'll also need to verify that propagation actually occurs. |
| 725 |
self.assertIn("_BINARY_OP_MULTIPLY_FLOAT", uops) |
| 726 |
|
| 727 |
def test_add_unicode_propagation(self): |
| 728 |
def testfunc(n): |
| 729 |
a = "" |
| 730 |
for _ in range(n): |
| 731 |
a + a |
| 732 |
a + a |
| 733 |
a + a |
| 734 |
a + a |
| 735 |
return a |
| 736 |
|
| 737 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 738 |
self.assertEqual(res, "") |
| 739 |
self.assertIsNotNone(ex) |
| 740 |
uops = get_opnames(ex) |
| 741 |
guard_tos_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_UNICODE"] |
| 742 |
guard_nos_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_UNICODE"] |
| 743 |
self.assertLessEqual(len(guard_tos_unicode_count), 1) |
| 744 |
self.assertLessEqual(len(guard_nos_unicode_count), 1) |
| 745 |
self.assertIn("_BINARY_OP_ADD_UNICODE", uops) |
| 746 |
|
| 747 |
def test_compare_op_type_propagation_float(self): |
| 748 |
def testfunc(n): |
| 749 |
a = 1.0 |
| 750 |
for _ in range(n): |
| 751 |
x = a == a |
| 752 |
x = a == a |
| 753 |
x = a == a |
| 754 |
x = a == a |
| 755 |
return x |
| 756 |
|
| 757 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 758 |
self.assertTrue(res) |
| 759 |
self.assertIsNotNone(ex) |
| 760 |
uops = get_opnames(ex) |
| 761 |
guard_tos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_FLOAT"] |
| 762 |
guard_nos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"] |
| 763 |
self.assertLessEqual(len(guard_tos_float_count), 1) |
| 764 |
self.assertLessEqual(len(guard_nos_float_count), 1) |
| 765 |
self.assertIn("_COMPARE_OP_FLOAT", uops) |
| 766 |
|
| 767 |
def test_compare_op_type_propagation_int(self): |
| 768 |
def testfunc(n): |
| 769 |
a = 1 |
| 770 |
for _ in range(n): |
| 771 |
x = a == a |
| 772 |
x = a == a |
| 773 |
x = a == a |
| 774 |
x = a == a |
| 775 |
return x |
| 776 |
|
| 777 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 778 |
self.assertTrue(res) |
| 779 |
self.assertIsNotNone(ex) |
| 780 |
uops = get_opnames(ex) |
| 781 |
guard_tos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 782 |
guard_nos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"] |
| 783 |
self.assertLessEqual(len(guard_tos_int_count), 1) |
| 784 |
self.assertLessEqual(len(guard_nos_int_count), 1) |
| 785 |
self.assertIn("_COMPARE_OP_INT", uops) |
| 786 |
|
| 787 |
def test_compare_op_type_propagation_int_partial(self): |
| 788 |
def testfunc(n): |
| 789 |
a = 1 |
| 790 |
for _ in range(n): |
| 791 |
if a > 2: |
| 792 |
x = 0 |
| 793 |
if a < 2: |
| 794 |
x = 1 |
| 795 |
return x |
| 796 |
|
| 797 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 798 |
self.assertEqual(res, 1) |
| 799 |
self.assertIsNotNone(ex) |
| 800 |
uops = get_opnames(ex) |
| 801 |
guard_nos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"] |
| 802 |
guard_tos_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_INT"] |
| 803 |
self.assertLessEqual(len(guard_nos_int_count), 1) |
| 804 |
self.assertEqual(len(guard_tos_int_count), 0) |
| 805 |
self.assertIn("_COMPARE_OP_INT", uops) |
| 806 |
|
| 807 |
def test_compare_op_type_propagation_float_partial(self): |
| 808 |
def testfunc(n): |
| 809 |
a = 1.0 |
| 810 |
for _ in range(n): |
| 811 |
if a > 2.0: |
| 812 |
x = 0 |
| 813 |
if a < 2.0: |
| 814 |
x = 1 |
| 815 |
return x |
| 816 |
|
| 817 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 818 |
self.assertEqual(res, 1) |
| 819 |
self.assertIsNotNone(ex) |
| 820 |
uops = get_opnames(ex) |
| 821 |
guard_nos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"] |
| 822 |
guard_tos_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_FLOAT"] |
| 823 |
self.assertLessEqual(len(guard_nos_float_count), 1) |
| 824 |
self.assertEqual(len(guard_tos_float_count), 0) |
| 825 |
self.assertIn("_COMPARE_OP_FLOAT", uops) |
| 826 |
|
| 827 |
def test_compare_op_type_propagation_unicode(self): |
| 828 |
def testfunc(n): |
| 829 |
a = "" |
| 830 |
for _ in range(n): |
| 831 |
x = a == a |
| 832 |
x = a == a |
| 833 |
x = a == a |
| 834 |
x = a == a |
| 835 |
return x |
| 836 |
|
| 837 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 838 |
self.assertTrue(res) |
| 839 |
self.assertIsNotNone(ex) |
| 840 |
uops = get_opnames(ex) |
| 841 |
guard_tos_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_TOS_UNICODE"] |
| 842 |
guard_nos_unicode_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_UNICODE"] |
| 843 |
self.assertLessEqual(len(guard_tos_unicode_count), 1) |
| 844 |
self.assertLessEqual(len(guard_nos_unicode_count), 1) |
| 845 |
self.assertIn("_COMPARE_OP_STR", uops) |
| 846 |
|
| 847 |
def test_type_inconsistency(self): |
| 848 |
ns = {} |
| 849 |
src = textwrap.dedent(""" |
| 850 |
def testfunc(n): |
| 851 |
for i in range(n): |
| 852 |
x = _test_global + _test_global |
| 853 |
""") |
| 854 |
exec(src, ns, ns) |
| 855 |
testfunc = ns['testfunc'] |
| 856 |
ns['_test_global'] = 0 |
| 857 |
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD - 1) |
| 858 |
self.assertIsNone(ex) |
| 859 |
ns['_test_global'] = 1 |
| 860 |
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD - 1) |
| 861 |
self.assertIsNotNone(ex) |
| 862 |
uops = get_opnames(ex) |
| 863 |
self.assertNotIn("_GUARD_TOS_INT", uops) |
| 864 |
self.assertNotIn("_GUARD_NOS_INT", uops) |
| 865 |
self.assertIn("_BINARY_OP_ADD_INT", uops) |
| 866 |
# Try again, but between the runs, set the global to a float. |
| 867 |
# This should result in no executor the second time. |
| 868 |
ns = {} |
| 869 |
exec(src, ns, ns) |
| 870 |
testfunc = ns['testfunc'] |
| 871 |
ns['_test_global'] = 0 |
| 872 |
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD - 1) |
| 873 |
self.assertIsNone(ex) |
| 874 |
ns['_test_global'] = 3.14 |
| 875 |
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD - 1) |
| 876 |
self.assertIsNone(ex) |
| 877 |
|
| 878 |
def test_combine_stack_space_checks_sequential(self): |
| 879 |
def dummy12(x): |
| 880 |
return x - 1 |
| 881 |
def dummy13(y): |
| 882 |
z = y + 2 |
| 883 |
return y, z |
| 884 |
def testfunc(n): |
| 885 |
a = 0 |
| 886 |
for _ in range(n): |
| 887 |
b = dummy12(7) |
| 888 |
c, d = dummy13(9) |
| 889 |
a += b + c + d |
| 890 |
return a |
| 891 |
|
| 892 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 893 |
self.assertEqual(res, TIER2_THRESHOLD * 26) |
| 894 |
self.assertIsNotNone(ex) |
| 895 |
|
| 896 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 897 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 898 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2) |
| 899 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 2) |
| 900 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0) |
| 901 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 902 |
# sequential calls: max(12, 13) == 13 |
| 903 |
largest_stack = _testinternalcapi.get_co_framesize(dummy13.__code__) |
| 904 |
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands) |
| 905 |
|
| 906 |
def test_combine_stack_space_checks_nested(self): |
| 907 |
def dummy12(x): |
| 908 |
return x + 3 |
| 909 |
def dummy15(y): |
| 910 |
z = dummy12(y) |
| 911 |
return y, z |
| 912 |
def testfunc(n): |
| 913 |
a = 0 |
| 914 |
for _ in range(n): |
| 915 |
b, c = dummy15(2) |
| 916 |
a += b + c |
| 917 |
return a |
| 918 |
|
| 919 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 920 |
self.assertEqual(res, TIER2_THRESHOLD * 7) |
| 921 |
self.assertIsNotNone(ex) |
| 922 |
|
| 923 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 924 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 925 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2) |
| 926 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 2) |
| 927 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0) |
| 928 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 929 |
# nested calls: 15 + 12 == 27 |
| 930 |
largest_stack = ( |
| 931 |
_testinternalcapi.get_co_framesize(dummy15.__code__) + |
| 932 |
_testinternalcapi.get_co_framesize(dummy12.__code__) |
| 933 |
) |
| 934 |
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands) |
| 935 |
|
| 936 |
def test_combine_stack_space_checks_several_calls(self): |
| 937 |
def dummy12(x): |
| 938 |
return x + 3 |
| 939 |
def dummy13(y): |
| 940 |
z = y + 2 |
| 941 |
return y, z |
| 942 |
def dummy18(y): |
| 943 |
z = dummy12(y) |
| 944 |
x, w = dummy13(z) |
| 945 |
return z, x, w |
| 946 |
def testfunc(n): |
| 947 |
a = 0 |
| 948 |
for _ in range(n): |
| 949 |
b = dummy12(5) |
| 950 |
c, d, e = dummy18(2) |
| 951 |
a += b + c + d + e |
| 952 |
return a |
| 953 |
|
| 954 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 955 |
self.assertEqual(res, TIER2_THRESHOLD * 25) |
| 956 |
self.assertIsNotNone(ex) |
| 957 |
|
| 958 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 959 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 960 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 4) |
| 961 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 4) |
| 962 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0) |
| 963 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 964 |
# max(12, 18 + max(12, 13)) == 31 |
| 965 |
largest_stack = ( |
| 966 |
_testinternalcapi.get_co_framesize(dummy18.__code__) + |
| 967 |
_testinternalcapi.get_co_framesize(dummy13.__code__) |
| 968 |
) |
| 969 |
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands) |
| 970 |
|
| 971 |
def test_combine_stack_space_checks_several_calls_different_order(self): |
| 972 |
# same as `several_calls` but with top-level calls reversed |
| 973 |
def dummy12(x): |
| 974 |
return x + 3 |
| 975 |
def dummy13(y): |
| 976 |
z = y + 2 |
| 977 |
return y, z |
| 978 |
def dummy18(y): |
| 979 |
z = dummy12(y) |
| 980 |
x, w = dummy13(z) |
| 981 |
return z, x, w |
| 982 |
def testfunc(n): |
| 983 |
a = 0 |
| 984 |
for _ in range(n): |
| 985 |
c, d, e = dummy18(2) |
| 986 |
b = dummy12(5) |
| 987 |
a += b + c + d + e |
| 988 |
return a |
| 989 |
|
| 990 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 991 |
self.assertEqual(res, TIER2_THRESHOLD * 25) |
| 992 |
self.assertIsNotNone(ex) |
| 993 |
|
| 994 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 995 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 996 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 4) |
| 997 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 4) |
| 998 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0) |
| 999 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 1000 |
# max(18 + max(12, 13), 12) == 31 |
| 1001 |
largest_stack = ( |
| 1002 |
_testinternalcapi.get_co_framesize(dummy18.__code__) + |
| 1003 |
_testinternalcapi.get_co_framesize(dummy13.__code__) |
| 1004 |
) |
| 1005 |
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands) |
| 1006 |
|
| 1007 |
def test_combine_stack_space_complex(self): |
| 1008 |
def dummy0(x): |
| 1009 |
return x |
| 1010 |
def dummy1(x): |
| 1011 |
return dummy0(x) |
| 1012 |
def dummy2(x): |
| 1013 |
return dummy1(x) |
| 1014 |
def dummy3(x): |
| 1015 |
return dummy0(x) |
| 1016 |
def dummy4(x): |
| 1017 |
y = dummy0(x) |
| 1018 |
return dummy3(y) |
| 1019 |
def dummy5(x): |
| 1020 |
return dummy2(x) |
| 1021 |
def dummy6(x): |
| 1022 |
y = dummy5(x) |
| 1023 |
z = dummy0(y) |
| 1024 |
return dummy4(z) |
| 1025 |
def testfunc(n): |
| 1026 |
a = 0 |
| 1027 |
for _ in range(n): |
| 1028 |
b = dummy5(1) |
| 1029 |
c = dummy0(1) |
| 1030 |
d = dummy6(1) |
| 1031 |
a += b + c + d |
| 1032 |
return a |
| 1033 |
|
| 1034 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1035 |
self.assertEqual(res, TIER2_THRESHOLD * 3) |
| 1036 |
self.assertIsNotNone(ex) |
| 1037 |
|
| 1038 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 1039 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 1040 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 15) |
| 1041 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 15) |
| 1042 |
|
| 1043 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 0) |
| 1044 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 1045 |
largest_stack = ( |
| 1046 |
_testinternalcapi.get_co_framesize(dummy6.__code__) + |
| 1047 |
_testinternalcapi.get_co_framesize(dummy5.__code__) + |
| 1048 |
_testinternalcapi.get_co_framesize(dummy2.__code__) + |
| 1049 |
_testinternalcapi.get_co_framesize(dummy1.__code__) + |
| 1050 |
_testinternalcapi.get_co_framesize(dummy0.__code__) |
| 1051 |
) |
| 1052 |
self.assertIn( |
| 1053 |
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands |
| 1054 |
) |
| 1055 |
|
| 1056 |
def test_combine_stack_space_checks_large_framesize(self): |
| 1057 |
# Create a function with a large framesize. This ensures _CHECK_STACK_SPACE is |
| 1058 |
# actually doing its job. Note that the resulting trace hits |
| 1059 |
# UOP_MAX_TRACE_LENGTH, but since all _CHECK_STACK_SPACEs happen early, this |
| 1060 |
# test is still meaningful. |
| 1061 |
repetitions = 10000 |
| 1062 |
ns = {} |
| 1063 |
header = """ |
| 1064 |
def dummy_large(a0): |
| 1065 |
""" |
| 1066 |
body = "".join([f""" |
| 1067 |
a{n+1} = a{n} + 1 |
| 1068 |
""" for n in range(repetitions)]) |
| 1069 |
return_ = f""" |
| 1070 |
return a{repetitions-1} |
| 1071 |
""" |
| 1072 |
exec(textwrap.dedent(header + body + return_), ns, ns) |
| 1073 |
dummy_large = ns['dummy_large'] |
| 1074 |
|
| 1075 |
# this is something like: |
| 1076 |
# |
| 1077 |
# def dummy_large(a0): |
| 1078 |
# a1 = a0 + 1 |
| 1079 |
# a2 = a1 + 1 |
| 1080 |
# .... |
| 1081 |
# a9999 = a9998 + 1 |
| 1082 |
# return a9999 |
| 1083 |
|
| 1084 |
def dummy15(z): |
| 1085 |
y = dummy_large(z) |
| 1086 |
return y + 3 |
| 1087 |
|
| 1088 |
def testfunc(n): |
| 1089 |
b = 0 |
| 1090 |
for _ in range(n): |
| 1091 |
b += dummy15(7) |
| 1092 |
return b |
| 1093 |
|
| 1094 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1095 |
self.assertEqual(res, TIER2_THRESHOLD * (repetitions + 9)) |
| 1096 |
self.assertIsNotNone(ex) |
| 1097 |
|
| 1098 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 1099 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 1100 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2) |
| 1101 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 1102 |
|
| 1103 |
# this hits a different case during trace projection in refcount test runs only, |
| 1104 |
# so we need to account for both possibilities |
| 1105 |
self.assertIn(uop_names.count("_CHECK_STACK_SPACE"), [0, 1]) |
| 1106 |
if uop_names.count("_CHECK_STACK_SPACE") == 0: |
| 1107 |
largest_stack = ( |
| 1108 |
_testinternalcapi.get_co_framesize(dummy15.__code__) + |
| 1109 |
_testinternalcapi.get_co_framesize(dummy_large.__code__) |
| 1110 |
) |
| 1111 |
else: |
| 1112 |
largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__) |
| 1113 |
self.assertIn( |
| 1114 |
("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands |
| 1115 |
) |
| 1116 |
|
| 1117 |
def test_combine_stack_space_checks_recursion(self): |
| 1118 |
def dummy15(x): |
| 1119 |
while x > 0: |
| 1120 |
return dummy15(x - 1) |
| 1121 |
return 42 |
| 1122 |
def testfunc(n): |
| 1123 |
a = 0 |
| 1124 |
for _ in range(n): |
| 1125 |
a += dummy15(n) |
| 1126 |
return a |
| 1127 |
|
| 1128 |
recursion_limit = sys.getrecursionlimit() |
| 1129 |
try: |
| 1130 |
sys.setrecursionlimit(TIER2_THRESHOLD + recursion_limit) |
| 1131 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1132 |
finally: |
| 1133 |
sys.setrecursionlimit(recursion_limit) |
| 1134 |
self.assertEqual(res, TIER2_THRESHOLD * 42) |
| 1135 |
self.assertIsNotNone(ex) |
| 1136 |
|
| 1137 |
uops_and_operands = [(opcode, operand) for opcode, _, _, operand in ex] |
| 1138 |
uop_names = [uop[0] for uop in uops_and_operands] |
| 1139 |
self.assertEqual(uop_names.count("_PUSH_FRAME"), 2) |
| 1140 |
self.assertEqual(uop_names.count("_RETURN_VALUE"), 0) |
| 1141 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE"), 1) |
| 1142 |
self.assertEqual(uop_names.count("_CHECK_STACK_SPACE_OPERAND"), 1) |
| 1143 |
largest_stack = _testinternalcapi.get_co_framesize(dummy15.__code__) |
| 1144 |
self.assertIn(("_CHECK_STACK_SPACE_OPERAND", largest_stack), uops_and_operands) |
| 1145 |
|
| 1146 |
def test_many_nested(self): |
| 1147 |
# overflow the trace_stack |
| 1148 |
def dummy_a(x): |
| 1149 |
return x |
| 1150 |
def dummy_b(x): |
| 1151 |
return dummy_a(x) |
| 1152 |
def dummy_c(x): |
| 1153 |
return dummy_b(x) |
| 1154 |
def dummy_d(x): |
| 1155 |
return dummy_c(x) |
| 1156 |
def dummy_e(x): |
| 1157 |
return dummy_d(x) |
| 1158 |
def dummy_f(x): |
| 1159 |
return dummy_e(x) |
| 1160 |
def dummy_g(x): |
| 1161 |
return dummy_f(x) |
| 1162 |
def dummy_h(x): |
| 1163 |
return dummy_g(x) |
| 1164 |
def testfunc(n): |
| 1165 |
a = 0 |
| 1166 |
for _ in range(n): |
| 1167 |
a += dummy_h(n) |
| 1168 |
return a |
| 1169 |
|
| 1170 |
res, ex = self._run_with_optimizer(testfunc, 32) |
| 1171 |
self.assertEqual(res, 32 * 32) |
| 1172 |
self.assertIsNone(ex) |
| 1173 |
|
| 1174 |
def test_return_generator(self): |
| 1175 |
def gen(): |
| 1176 |
yield None |
| 1177 |
def testfunc(n): |
| 1178 |
for i in range(n): |
| 1179 |
gen() |
| 1180 |
return i |
| 1181 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1182 |
self.assertEqual(res, TIER2_THRESHOLD - 1) |
| 1183 |
self.assertIsNotNone(ex) |
| 1184 |
self.assertIn("_RETURN_GENERATOR", get_opnames(ex)) |
| 1185 |
|
| 1186 |
@unittest.skip("Tracing into generators currently isn't supported.") |
| 1187 |
def test_for_iter_gen(self): |
| 1188 |
def gen(n): |
| 1189 |
for i in range(n): |
| 1190 |
yield i |
| 1191 |
def testfunc(n): |
| 1192 |
g = gen(n) |
| 1193 |
s = 0 |
| 1194 |
for i in g: |
| 1195 |
s += i |
| 1196 |
return s |
| 1197 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1198 |
self.assertEqual(res, sum(range(TIER2_THRESHOLD))) |
| 1199 |
self.assertIsNotNone(ex) |
| 1200 |
self.assertIn("_FOR_ITER_GEN_FRAME", get_opnames(ex)) |
| 1201 |
|
| 1202 |
def test_modified_local_is_seen_by_optimized_code(self): |
| 1203 |
l = sys._getframe().f_locals |
| 1204 |
a = 1 |
| 1205 |
s = 0 |
| 1206 |
for j in range(1 << 10): |
| 1207 |
a + a |
| 1208 |
l["xa"[j >> 9]] = 1.0 |
| 1209 |
s += a |
| 1210 |
self.assertIs(type(a), float) |
| 1211 |
self.assertIs(type(s), float) |
| 1212 |
self.assertEqual(s, 1024.0) |
| 1213 |
|
| 1214 |
def test_guard_type_version_removed(self): |
| 1215 |
def thing(a): |
| 1216 |
x = 0 |
| 1217 |
for _ in range(TIER2_THRESHOLD): |
| 1218 |
x += a.attr |
| 1219 |
x += a.attr |
| 1220 |
return x |
| 1221 |
|
| 1222 |
class Foo: |
| 1223 |
attr = 1 |
| 1224 |
|
| 1225 |
res, ex = self._run_with_optimizer(thing, Foo()) |
| 1226 |
opnames = list(iter_opnames(ex)) |
| 1227 |
self.assertIsNotNone(ex) |
| 1228 |
self.assertEqual(res, TIER2_THRESHOLD * 2) |
| 1229 |
guard_type_version_count = opnames.count("_GUARD_TYPE_VERSION") |
| 1230 |
self.assertEqual(guard_type_version_count, 1) |
| 1231 |
|
| 1232 |
def test_guard_type_version_removed_inlined(self): |
| 1233 |
""" |
| 1234 |
Verify that the guard type version if we have an inlined function |
| 1235 |
""" |
| 1236 |
|
| 1237 |
def fn(): |
| 1238 |
pass |
| 1239 |
|
| 1240 |
def thing(a): |
| 1241 |
x = 0 |
| 1242 |
for _ in range(TIER2_THRESHOLD): |
| 1243 |
x += a.attr |
| 1244 |
fn() |
| 1245 |
x += a.attr |
| 1246 |
return x |
| 1247 |
|
| 1248 |
class Foo: |
| 1249 |
attr = 1 |
| 1250 |
|
| 1251 |
res, ex = self._run_with_optimizer(thing, Foo()) |
| 1252 |
opnames = list(iter_opnames(ex)) |
| 1253 |
self.assertIsNotNone(ex) |
| 1254 |
self.assertEqual(res, TIER2_THRESHOLD * 2) |
| 1255 |
guard_type_version_count = opnames.count("_GUARD_TYPE_VERSION") |
| 1256 |
self.assertEqual(guard_type_version_count, 1) |
| 1257 |
|
| 1258 |
def test_guard_type_version_removed_invalidation(self): |
| 1259 |
|
| 1260 |
def thing(a): |
| 1261 |
x = 0 |
| 1262 |
for i in range(TIER2_THRESHOLD * 2 + 1): |
| 1263 |
x += a.attr |
| 1264 |
# The first TIER2_THRESHOLD iterations we set the attribute on |
| 1265 |
# this dummy class, which shouldn't trigger the type watcher. |
| 1266 |
# Note that the code needs to be in this weird form so it's |
| 1267 |
# optimized inline without any control flow: |
| 1268 |
setattr((Bar, Foo)[i == TIER2_THRESHOLD + 1], "attr", 2) |
| 1269 |
x += a.attr |
| 1270 |
return x |
| 1271 |
|
| 1272 |
class Foo: |
| 1273 |
attr = 1 |
| 1274 |
|
| 1275 |
class Bar: |
| 1276 |
pass |
| 1277 |
|
| 1278 |
res, ex = self._run_with_optimizer(thing, Foo()) |
| 1279 |
opnames = list(iter_opnames(ex)) |
| 1280 |
self.assertIsNotNone(ex) |
| 1281 |
self.assertEqual(res, TIER2_THRESHOLD * 6 + 1) |
| 1282 |
call = opnames.index("_CALL_BUILTIN_FAST") |
| 1283 |
load_attr_top = opnames.index("_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES", 0, call) |
| 1284 |
load_attr_bottom = opnames.index("_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES", call) |
| 1285 |
self.assertEqual(opnames[:load_attr_top].count("_GUARD_TYPE_VERSION"), 1) |
| 1286 |
self.assertEqual(opnames[call:load_attr_bottom].count("_CHECK_VALIDITY"), 2) |
| 1287 |
|
| 1288 |
def test_guard_type_version_removed_escaping(self): |
| 1289 |
|
| 1290 |
def thing(a): |
| 1291 |
x = 0 |
| 1292 |
for i in range(TIER2_THRESHOLD): |
| 1293 |
x += a.attr |
| 1294 |
# eval should be escaping |
| 1295 |
eval("None") |
| 1296 |
x += a.attr |
| 1297 |
return x |
| 1298 |
|
| 1299 |
class Foo: |
| 1300 |
attr = 1 |
| 1301 |
res, ex = self._run_with_optimizer(thing, Foo()) |
| 1302 |
opnames = list(iter_opnames(ex)) |
| 1303 |
self.assertIsNotNone(ex) |
| 1304 |
self.assertEqual(res, TIER2_THRESHOLD * 2) |
| 1305 |
call = opnames.index("_CALL_BUILTIN_FAST_WITH_KEYWORDS") |
| 1306 |
load_attr_top = opnames.index("_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES", 0, call) |
| 1307 |
load_attr_bottom = opnames.index("_LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES", call) |
| 1308 |
self.assertEqual(opnames[:load_attr_top].count("_GUARD_TYPE_VERSION"), 1) |
| 1309 |
self.assertEqual(opnames[call:load_attr_bottom].count("_CHECK_VALIDITY"), 2) |
| 1310 |
|
| 1311 |
def test_guard_type_version_executor_invalidated(self): |
| 1312 |
""" |
| 1313 |
Verify that the executor is invalided on a type change. |
| 1314 |
""" |
| 1315 |
|
| 1316 |
def thing(a): |
| 1317 |
x = 0 |
| 1318 |
for i in range(TIER2_THRESHOLD): |
| 1319 |
x += a.attr |
| 1320 |
x += a.attr |
| 1321 |
return x |
| 1322 |
|
| 1323 |
class Foo: |
| 1324 |
attr = 1 |
| 1325 |
|
| 1326 |
res, ex = self._run_with_optimizer(thing, Foo()) |
| 1327 |
self.assertEqual(res, TIER2_THRESHOLD * 2) |
| 1328 |
self.assertIsNotNone(ex) |
| 1329 |
self.assertEqual(list(iter_opnames(ex)).count("_GUARD_TYPE_VERSION"), 1) |
| 1330 |
self.assertTrue(ex.is_valid()) |
| 1331 |
Foo.attr = 0 |
| 1332 |
self.assertFalse(ex.is_valid()) |
| 1333 |
|
| 1334 |
def test_type_version_doesnt_segfault(self): |
| 1335 |
""" |
| 1336 |
Tests that setting a type version doesn't cause a segfault when later looking at the stack. |
| 1337 |
""" |
| 1338 |
|
| 1339 |
# Minimized from mdp.py benchmark |
| 1340 |
|
| 1341 |
class A: |
| 1342 |
def __init__(self): |
| 1343 |
self.attr = {} |
| 1344 |
|
| 1345 |
def method(self, arg): |
| 1346 |
self.attr[arg] = None |
| 1347 |
|
| 1348 |
def fn(a): |
| 1349 |
for _ in range(100): |
| 1350 |
(_ for _ in []) |
| 1351 |
(_ for _ in [a.method(None)]) |
| 1352 |
|
| 1353 |
fn(A()) |
| 1354 |
|
| 1355 |
def test_func_guards_removed_or_reduced(self): |
| 1356 |
def testfunc(n): |
| 1357 |
for i in range(n): |
| 1358 |
# Only works on functions promoted to constants |
| 1359 |
global_identity(i) |
| 1360 |
|
| 1361 |
testfunc(TIER2_THRESHOLD) |
| 1362 |
|
| 1363 |
ex = get_first_executor(testfunc) |
| 1364 |
self.assertIsNotNone(ex) |
| 1365 |
uops = get_opnames(ex) |
| 1366 |
self.assertIn("_PUSH_FRAME", uops) |
| 1367 |
# Strength reduced version |
| 1368 |
self.assertIn("_CHECK_FUNCTION_VERSION_INLINE", uops) |
| 1369 |
self.assertNotIn("_CHECK_FUNCTION_VERSION", uops) |
| 1370 |
# Removed guard |
| 1371 |
self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops) |
| 1372 |
|
| 1373 |
def test_jit_error_pops(self): |
| 1374 |
""" |
| 1375 |
Tests that the correct number of pops are inserted into the |
| 1376 |
exit stub |
| 1377 |
""" |
| 1378 |
items = 17 * [None] + [[]] |
| 1379 |
with self.assertRaises(TypeError): |
| 1380 |
{item for item in items} |
| 1381 |
|
| 1382 |
def test_power_type_depends_on_input_values(self): |
| 1383 |
template = textwrap.dedent(""" |
| 1384 |
import _testinternalcapi |
| 1385 |
|
| 1386 |
L, R, X, Y = {l}, {r}, {x}, {y} |
| 1387 |
|
| 1388 |
def check(actual: complex, expected: complex) -> None: |
| 1389 |
assert actual == expected, (actual, expected) |
| 1390 |
assert type(actual) is type(expected), (actual, expected) |
| 1391 |
|
| 1392 |
def f(l: complex, r: complex) -> None: |
| 1393 |
expected_local_local = pow(l, r) + pow(l, r) |
| 1394 |
expected_const_local = pow(L, r) + pow(L, r) |
| 1395 |
expected_local_const = pow(l, R) + pow(l, R) |
| 1396 |
expected_const_const = pow(L, R) + pow(L, R) |
| 1397 |
for _ in range(_testinternalcapi.TIER2_THRESHOLD): |
| 1398 |
# Narrow types: |
| 1399 |
l + l, r + r |
| 1400 |
# The powers produce results, and the addition is unguarded: |
| 1401 |
check(l ** r + l ** r, expected_local_local) |
| 1402 |
check(L ** r + L ** r, expected_const_local) |
| 1403 |
check(l ** R + l ** R, expected_local_const) |
| 1404 |
check(L ** R + L ** R, expected_const_const) |
| 1405 |
|
| 1406 |
# JIT for one pair of values... |
| 1407 |
f(L, R) |
| 1408 |
# ...then run with another: |
| 1409 |
f(X, Y) |
| 1410 |
""") |
| 1411 |
interesting = [ |
| 1412 |
(1, 1), # int ** int -> int |
| 1413 |
(1, -1), # int ** int -> float |
| 1414 |
(1.0, 1), # float ** int -> float |
| 1415 |
(1, 1.0), # int ** float -> float |
| 1416 |
(-1, 0.5), # int ** float -> complex |
| 1417 |
(1.0, 1.0), # float ** float -> float |
| 1418 |
(-1.0, 0.5), # float ** float -> complex |
| 1419 |
] |
| 1420 |
for (l, r), (x, y) in itertools.product(interesting, repeat=2): |
| 1421 |
s = template.format(l=l, r=r, x=x, y=y) |
| 1422 |
with self.subTest(l=l, r=r, x=x, y=y): |
| 1423 |
script_helper.assert_python_ok("-c", s) |
| 1424 |
|
| 1425 |
def test_symbols_flow_through_tuples(self): |
| 1426 |
def testfunc(n): |
| 1427 |
for _ in range(n): |
| 1428 |
a = 1 |
| 1429 |
b = 2 |
| 1430 |
t = a, b |
| 1431 |
x, y = t |
| 1432 |
r = x + y |
| 1433 |
return r |
| 1434 |
|
| 1435 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1436 |
self.assertEqual(res, 3) |
| 1437 |
self.assertIsNotNone(ex) |
| 1438 |
uops = get_opnames(ex) |
| 1439 |
self.assertIn("_BINARY_OP_ADD_INT", uops) |
| 1440 |
self.assertNotIn("_GUARD_NOS_INT", uops) |
| 1441 |
self.assertNotIn("_GUARD_TOS_INT", uops) |
| 1442 |
|
| 1443 |
def test_decref_escapes(self): |
| 1444 |
class Convert9999ToNone: |
| 1445 |
def __del__(self): |
| 1446 |
ns = sys._getframe(1).f_locals |
| 1447 |
if ns["i"] == _testinternalcapi.TIER2_THRESHOLD: |
| 1448 |
ns["i"] = None |
| 1449 |
|
| 1450 |
def crash_addition(): |
| 1451 |
try: |
| 1452 |
for i in range(_testinternalcapi.TIER2_THRESHOLD + 1): |
| 1453 |
n = Convert9999ToNone() |
| 1454 |
i + i # Remove guards for i. |
| 1455 |
n = None # Change i. |
| 1456 |
i + i # This crashed when we didn't treat DECREF as escaping (gh-124483) |
| 1457 |
except TypeError: |
| 1458 |
pass |
| 1459 |
|
| 1460 |
crash_addition() |
| 1461 |
|
| 1462 |
def test_narrow_type_to_constant_bool_false(self): |
| 1463 |
def f(n): |
| 1464 |
trace = [] |
| 1465 |
for i in range(n): |
| 1466 |
# false is always False, but we can only prove that it's a bool: |
| 1467 |
false = i == TIER2_THRESHOLD |
| 1468 |
trace.append("A") |
| 1469 |
if not false: # Kept. |
| 1470 |
trace.append("B") |
| 1471 |
if not false: # Removed! |
| 1472 |
trace.append("C") |
| 1473 |
trace.append("D") |
| 1474 |
if false: # Removed! |
| 1475 |
trace.append("X") |
| 1476 |
trace.append("E") |
| 1477 |
trace.append("F") |
| 1478 |
if false: # Removed! |
| 1479 |
trace.append("X") |
| 1480 |
trace.append("G") |
| 1481 |
return trace |
| 1482 |
|
| 1483 |
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1484 |
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD) |
| 1485 |
self.assertIsNotNone(ex) |
| 1486 |
uops = get_opnames(ex) |
| 1487 |
# Only one guard remains: |
| 1488 |
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1) |
| 1489 |
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0) |
| 1490 |
# But all of the appends we care about are still there: |
| 1491 |
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG")) |
| 1492 |
|
| 1493 |
def test_narrow_type_to_constant_bool_true(self): |
| 1494 |
def f(n): |
| 1495 |
trace = [] |
| 1496 |
for i in range(n): |
| 1497 |
# true always True, but we can only prove that it's a bool: |
| 1498 |
true = i != TIER2_THRESHOLD |
| 1499 |
trace.append("A") |
| 1500 |
if true: # Kept. |
| 1501 |
trace.append("B") |
| 1502 |
if not true: # Removed! |
| 1503 |
trace.append("X") |
| 1504 |
trace.append("C") |
| 1505 |
if true: # Removed! |
| 1506 |
trace.append("D") |
| 1507 |
trace.append("E") |
| 1508 |
trace.append("F") |
| 1509 |
if not true: # Removed! |
| 1510 |
trace.append("X") |
| 1511 |
trace.append("G") |
| 1512 |
return trace |
| 1513 |
|
| 1514 |
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1515 |
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD) |
| 1516 |
self.assertIsNotNone(ex) |
| 1517 |
uops = get_opnames(ex) |
| 1518 |
# Only one guard remains: |
| 1519 |
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 0) |
| 1520 |
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 1) |
| 1521 |
# But all of the appends we care about are still there: |
| 1522 |
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG")) |
| 1523 |
|
| 1524 |
def test_narrow_type_to_constant_int_zero(self): |
| 1525 |
def f(n): |
| 1526 |
trace = [] |
| 1527 |
for i in range(n): |
| 1528 |
# zero is always (int) 0, but we can only prove that it's a integer: |
| 1529 |
false = i == TIER2_THRESHOLD # this will always be false, while hopefully still fooling optimizer improvements |
| 1530 |
zero = false + 0 # this should always set the variable zero equal to 0 |
| 1531 |
trace.append("A") |
| 1532 |
if not zero: # Kept. |
| 1533 |
trace.append("B") |
| 1534 |
if not zero: # Removed! |
| 1535 |
trace.append("C") |
| 1536 |
trace.append("D") |
| 1537 |
if zero: # Removed! |
| 1538 |
trace.append("X") |
| 1539 |
trace.append("E") |
| 1540 |
trace.append("F") |
| 1541 |
if zero: # Removed! |
| 1542 |
trace.append("X") |
| 1543 |
trace.append("G") |
| 1544 |
return trace |
| 1545 |
|
| 1546 |
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1547 |
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD) |
| 1548 |
self.assertIsNotNone(ex) |
| 1549 |
uops = get_opnames(ex) |
| 1550 |
# Only one guard remains: |
| 1551 |
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1) |
| 1552 |
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0) |
| 1553 |
# But all of the appends we care about are still there: |
| 1554 |
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG")) |
| 1555 |
|
| 1556 |
def test_narrow_type_to_constant_str_empty(self): |
| 1557 |
def f(n): |
| 1558 |
trace = [] |
| 1559 |
for i in range(n): |
| 1560 |
# Hopefully the optimizer can't guess what the value is. |
| 1561 |
# empty is always "", but we can only prove that it's a string: |
| 1562 |
false = i == TIER2_THRESHOLD |
| 1563 |
empty = "X"[:false] |
| 1564 |
trace.append("A") |
| 1565 |
if not empty: # Kept. |
| 1566 |
trace.append("B") |
| 1567 |
if not empty: # Removed! |
| 1568 |
trace.append("C") |
| 1569 |
trace.append("D") |
| 1570 |
if empty: # Removed! |
| 1571 |
trace.append("X") |
| 1572 |
trace.append("E") |
| 1573 |
trace.append("F") |
| 1574 |
if empty: # Removed! |
| 1575 |
trace.append("X") |
| 1576 |
trace.append("G") |
| 1577 |
return trace |
| 1578 |
|
| 1579 |
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1580 |
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD) |
| 1581 |
self.assertIsNotNone(ex) |
| 1582 |
uops = get_opnames(ex) |
| 1583 |
# Only one guard remains: |
| 1584 |
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1) |
| 1585 |
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0) |
| 1586 |
# But all of the appends we care about are still there: |
| 1587 |
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG")) |
| 1588 |
|
| 1589 |
def test_compare_pop_two_load_const_inline_borrow(self): |
| 1590 |
def testfunc(n): |
| 1591 |
x = 0 |
| 1592 |
for _ in range(n): |
| 1593 |
a = 10 |
| 1594 |
b = 10 |
| 1595 |
if a == b: |
| 1596 |
x += 1 |
| 1597 |
return x |
| 1598 |
|
| 1599 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1600 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1601 |
self.assertIsNotNone(ex) |
| 1602 |
uops = get_opnames(ex) |
| 1603 |
self.assertNotIn("_COMPARE_OP_INT", uops) |
| 1604 |
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops) |
| 1605 |
|
| 1606 |
def test_to_bool_bool_contains_op_set(self): |
| 1607 |
""" |
| 1608 |
Test that _TO_BOOL_BOOL is removed from code like: |
| 1609 |
|
| 1610 |
res = foo in some_set |
| 1611 |
if res: |
| 1612 |
.... |
| 1613 |
|
| 1614 |
""" |
| 1615 |
def testfunc(n): |
| 1616 |
x = 0 |
| 1617 |
s = {1, 2, 3} |
| 1618 |
for _ in range(n): |
| 1619 |
a = 2 |
| 1620 |
in_set = a in s |
| 1621 |
if in_set: |
| 1622 |
x += 1 |
| 1623 |
return x |
| 1624 |
|
| 1625 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1626 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1627 |
self.assertIsNotNone(ex) |
| 1628 |
uops = get_opnames(ex) |
| 1629 |
self.assertIn("_CONTAINS_OP_SET", uops) |
| 1630 |
self.assertNotIn("_TO_BOOL_BOOL", uops) |
| 1631 |
|
| 1632 |
def test_to_bool_bool_contains_op_dict(self): |
| 1633 |
""" |
| 1634 |
Test that _TO_BOOL_BOOL is removed from code like: |
| 1635 |
|
| 1636 |
res = foo in some_dict |
| 1637 |
if res: |
| 1638 |
.... |
| 1639 |
|
| 1640 |
""" |
| 1641 |
def testfunc(n): |
| 1642 |
x = 0 |
| 1643 |
s = {1: 1, 2: 2, 3: 3} |
| 1644 |
for _ in range(n): |
| 1645 |
a = 2 |
| 1646 |
in_dict = a in s |
| 1647 |
if in_dict: |
| 1648 |
x += 1 |
| 1649 |
return x |
| 1650 |
|
| 1651 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1652 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1653 |
self.assertIsNotNone(ex) |
| 1654 |
uops = get_opnames(ex) |
| 1655 |
self.assertIn("_CONTAINS_OP_DICT", uops) |
| 1656 |
self.assertNotIn("_TO_BOOL_BOOL", uops) |
| 1657 |
|
| 1658 |
|
| 1659 |
def test_remove_guard_for_known_type_str(self): |
| 1660 |
def f(n): |
| 1661 |
for i in range(n): |
| 1662 |
false = i == TIER2_THRESHOLD |
| 1663 |
empty = "X"[:false] |
| 1664 |
empty += "" # Make JIT realize this is a string. |
| 1665 |
if empty: |
| 1666 |
return 1 |
| 1667 |
return 0 |
| 1668 |
|
| 1669 |
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1670 |
self.assertEqual(res, 0) |
| 1671 |
self.assertIsNotNone(ex) |
| 1672 |
uops = get_opnames(ex) |
| 1673 |
self.assertIn("_TO_BOOL_STR", uops) |
| 1674 |
self.assertNotIn("_GUARD_TOS_UNICODE", uops) |
| 1675 |
|
| 1676 |
def test_remove_guard_for_known_type_dict(self): |
| 1677 |
def f(n): |
| 1678 |
x = 0 |
| 1679 |
for _ in range(n): |
| 1680 |
d = {} |
| 1681 |
d["Spam"] = 1 # unguarded! |
| 1682 |
x += d["Spam"] # ...unguarded! |
| 1683 |
return x |
| 1684 |
|
| 1685 |
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1686 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1687 |
self.assertIsNotNone(ex) |
| 1688 |
uops = get_opnames(ex) |
| 1689 |
self.assertEqual(uops.count("_GUARD_NOS_DICT"), 0) |
| 1690 |
self.assertEqual(uops.count("_STORE_SUBSCR_DICT"), 1) |
| 1691 |
self.assertEqual(uops.count("_BINARY_OP_SUBSCR_DICT"), 1) |
| 1692 |
|
| 1693 |
def test_remove_guard_for_known_type_list(self): |
| 1694 |
def f(n): |
| 1695 |
x = 0 |
| 1696 |
for _ in range(n): |
| 1697 |
l = [0] |
| 1698 |
l[0] = 1 # unguarded! |
| 1699 |
[a] = l # ...unguarded! |
| 1700 |
b = l[0] # ...unguarded! |
| 1701 |
if l: # ...unguarded! |
| 1702 |
x += a + b |
| 1703 |
return x |
| 1704 |
|
| 1705 |
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1706 |
self.assertEqual(res, 2 * TIER2_THRESHOLD) |
| 1707 |
self.assertIsNotNone(ex) |
| 1708 |
uops = get_opnames(ex) |
| 1709 |
self.assertEqual(uops.count("_GUARD_NOS_LIST"), 0) |
| 1710 |
self.assertEqual(uops.count("_STORE_SUBSCR_LIST_INT"), 1) |
| 1711 |
self.assertEqual(uops.count("_GUARD_TOS_LIST"), 0) |
| 1712 |
self.assertEqual(uops.count("_UNPACK_SEQUENCE_LIST"), 1) |
| 1713 |
self.assertEqual(uops.count("_BINARY_OP_SUBSCR_LIST_INT"), 1) |
| 1714 |
self.assertEqual(uops.count("_TO_BOOL_LIST"), 1) |
| 1715 |
|
| 1716 |
def test_remove_guard_for_known_type_set(self): |
| 1717 |
def f(n): |
| 1718 |
x = 0 |
| 1719 |
for _ in range(n): |
| 1720 |
x += "Spam" in {"Spam"} # Unguarded! |
| 1721 |
return x |
| 1722 |
|
| 1723 |
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1724 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1725 |
self.assertIsNotNone(ex) |
| 1726 |
uops = get_opnames(ex) |
| 1727 |
self.assertNotIn("_GUARD_TOS_ANY_SET", uops) |
| 1728 |
self.assertIn("_CONTAINS_OP_SET", uops) |
| 1729 |
|
| 1730 |
def test_remove_guard_for_known_type_tuple(self): |
| 1731 |
def f(n): |
| 1732 |
x = 0 |
| 1733 |
for _ in range(n): |
| 1734 |
t = (1, 2, (3, (4,))) |
| 1735 |
t_0, t_1, (t_2_0, t_2_1) = t # Unguarded! |
| 1736 |
t_2_1_0 = t_2_1[0] # Unguarded! |
| 1737 |
x += t_0 + t_1 + t_2_0 + t_2_1_0 |
| 1738 |
return x |
| 1739 |
|
| 1740 |
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD) |
| 1741 |
self.assertEqual(res, 10 * TIER2_THRESHOLD) |
| 1742 |
self.assertIsNotNone(ex) |
| 1743 |
uops = get_opnames(ex) |
| 1744 |
self.assertNotIn("_GUARD_TOS_TUPLE", uops) |
| 1745 |
self.assertIn("_UNPACK_SEQUENCE_TUPLE", uops) |
| 1746 |
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops) |
| 1747 |
self.assertNotIn("_GUARD_NOS_TUPLE", uops) |
| 1748 |
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) |
| 1749 |
|
| 1750 |
def test_binary_subcsr_str_int_narrows_to_str(self): |
| 1751 |
def testfunc(n): |
| 1752 |
x = [] |
| 1753 |
s = "foo" |
| 1754 |
for _ in range(n): |
| 1755 |
y = s[0] # _BINARY_OP_SUBSCR_STR_INT |
| 1756 |
z = "bar" + y # (_GUARD_TOS_UNICODE) + _BINARY_OP_ADD_UNICODE |
| 1757 |
x.append(z) |
| 1758 |
return x |
| 1759 |
|
| 1760 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1761 |
self.assertEqual(res, ["barf"] * TIER2_THRESHOLD) |
| 1762 |
self.assertIsNotNone(ex) |
| 1763 |
uops = get_opnames(ex) |
| 1764 |
self.assertIn("_BINARY_OP_SUBSCR_STR_INT", uops) |
| 1765 |
# _BINARY_OP_SUBSCR_STR_INT narrows the result to 'str' so |
| 1766 |
# the unicode guard before _BINARY_OP_ADD_UNICODE is removed. |
| 1767 |
self.assertNotIn("_GUARD_TOS_UNICODE", uops) |
| 1768 |
self.assertIn("_BINARY_OP_ADD_UNICODE", uops) |
| 1769 |
|
| 1770 |
def test_call_type_1(self): |
| 1771 |
def testfunc(n): |
| 1772 |
x = 0 |
| 1773 |
for _ in range(n): |
| 1774 |
x += type(42) is int |
| 1775 |
return x |
| 1776 |
|
| 1777 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1778 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1779 |
self.assertIsNotNone(ex) |
| 1780 |
uops = get_opnames(ex) |
| 1781 |
self.assertIn("_CALL_TYPE_1", uops) |
| 1782 |
self.assertNotIn("_GUARD_NOS_NULL", uops) |
| 1783 |
self.assertNotIn("_GUARD_CALLABLE_TYPE_1", uops) |
| 1784 |
|
| 1785 |
def test_call_type_1_result_is_const(self): |
| 1786 |
def testfunc(n): |
| 1787 |
x = 0 |
| 1788 |
for _ in range(n): |
| 1789 |
t = type(42) |
| 1790 |
if t is not None: # guard is removed |
| 1791 |
x += 1 |
| 1792 |
return x |
| 1793 |
|
| 1794 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1795 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1796 |
self.assertIsNotNone(ex) |
| 1797 |
uops = get_opnames(ex) |
| 1798 |
self.assertIn("_CALL_TYPE_1", uops) |
| 1799 |
self.assertNotIn("_GUARD_IS_NOT_NONE_POP", uops) |
| 1800 |
|
| 1801 |
def test_call_str_1(self): |
| 1802 |
def testfunc(n): |
| 1803 |
x = 0 |
| 1804 |
for _ in range(n): |
| 1805 |
y = str(42) |
| 1806 |
if y == '42': |
| 1807 |
x += 1 |
| 1808 |
return x |
| 1809 |
|
| 1810 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1811 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1812 |
self.assertIsNotNone(ex) |
| 1813 |
uops = get_opnames(ex) |
| 1814 |
self.assertIn("_CALL_STR_1", uops) |
| 1815 |
self.assertNotIn("_GUARD_NOS_NULL", uops) |
| 1816 |
self.assertNotIn("_GUARD_CALLABLE_STR_1", uops) |
| 1817 |
|
| 1818 |
def test_call_str_1_result_is_str(self): |
| 1819 |
def testfunc(n): |
| 1820 |
x = 0 |
| 1821 |
for _ in range(n): |
| 1822 |
y = str(42) + 'foo' |
| 1823 |
if y == '42foo': |
| 1824 |
x += 1 |
| 1825 |
return x |
| 1826 |
|
| 1827 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1828 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1829 |
self.assertIsNotNone(ex) |
| 1830 |
uops = get_opnames(ex) |
| 1831 |
self.assertIn("_CALL_STR_1", uops) |
| 1832 |
self.assertIn("_BINARY_OP_ADD_UNICODE", uops) |
| 1833 |
self.assertNotIn("_GUARD_NOS_UNICODE", uops) |
| 1834 |
self.assertNotIn("_GUARD_TOS_UNICODE", uops) |
| 1835 |
|
| 1836 |
def test_call_str_1_result_is_const_for_str_input(self): |
| 1837 |
# Test a special case where the argument of str(arg) |
| 1838 |
# is known to be a string. The information about the |
| 1839 |
# argument being a string should be propagated to the |
| 1840 |
# result of str(arg). |
| 1841 |
def testfunc(n): |
| 1842 |
x = 0 |
| 1843 |
for _ in range(n): |
| 1844 |
y = str('foo') # string argument |
| 1845 |
if y: # _TO_BOOL_STR + _GUARD_IS_TRUE_POP are removed |
| 1846 |
x += 1 |
| 1847 |
return x |
| 1848 |
|
| 1849 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1850 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1851 |
self.assertIsNotNone(ex) |
| 1852 |
uops = get_opnames(ex) |
| 1853 |
self.assertIn("_CALL_STR_1", uops) |
| 1854 |
self.assertNotIn("_TO_BOOL_STR", uops) |
| 1855 |
self.assertNotIn("_GUARD_IS_TRUE_POP", uops) |
| 1856 |
|
| 1857 |
def test_call_tuple_1(self): |
| 1858 |
def testfunc(n): |
| 1859 |
x = 0 |
| 1860 |
for _ in range(n): |
| 1861 |
y = tuple([1, 2]) # _CALL_TUPLE_1 |
| 1862 |
if y == (1, 2): |
| 1863 |
x += 1 |
| 1864 |
return x |
| 1865 |
|
| 1866 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1867 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1868 |
self.assertIsNotNone(ex) |
| 1869 |
uops = get_opnames(ex) |
| 1870 |
self.assertIn("_CALL_TUPLE_1", uops) |
| 1871 |
self.assertNotIn("_GUARD_NOS_NULL", uops) |
| 1872 |
self.assertNotIn("_GUARD_CALLABLE_TUPLE_1", uops) |
| 1873 |
|
| 1874 |
def test_call_tuple_1_result_is_tuple(self): |
| 1875 |
def testfunc(n): |
| 1876 |
x = 0 |
| 1877 |
for _ in range(n): |
| 1878 |
y = tuple([1, 2]) # _CALL_TUPLE_1 |
| 1879 |
if y[0] == 1: # _BINARY_OP_SUBSCR_TUPLE_INT |
| 1880 |
x += 1 |
| 1881 |
return x |
| 1882 |
|
| 1883 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1884 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1885 |
self.assertIsNotNone(ex) |
| 1886 |
uops = get_opnames(ex) |
| 1887 |
self.assertIn("_CALL_TUPLE_1", uops) |
| 1888 |
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) |
| 1889 |
self.assertNotIn("_GUARD_NOS_TUPLE", uops) |
| 1890 |
|
| 1891 |
def test_call_tuple_1_result_propagates_for_tuple_input(self): |
| 1892 |
# Test a special case where the argument of tuple(arg) |
| 1893 |
# is known to be a tuple. The information about the |
| 1894 |
# argument being a tuple should be propagated to the |
| 1895 |
# result of tuple(arg). |
| 1896 |
def testfunc(n): |
| 1897 |
x = 0 |
| 1898 |
for _ in range(n): |
| 1899 |
y = tuple((1, 2)) # tuple argument |
| 1900 |
a, _ = y # _UNPACK_SEQUENCE_TWO_TUPLE |
| 1901 |
if a == 1: # _COMPARE_OP_INT + _GUARD_IS_TRUE_POP are removed |
| 1902 |
x += 1 |
| 1903 |
return x |
| 1904 |
|
| 1905 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1906 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1907 |
self.assertIsNotNone(ex) |
| 1908 |
uops = get_opnames(ex) |
| 1909 |
self.assertIn("_CALL_TUPLE_1", uops) |
| 1910 |
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops) |
| 1911 |
self.assertNotIn("_COMPARE_OP_INT", uops) |
| 1912 |
self.assertNotIn("_GUARD_IS_TRUE_POP", uops) |
| 1913 |
|
| 1914 |
def test_call_len(self): |
| 1915 |
def testfunc(n): |
| 1916 |
a = [1, 2, 3, 4] |
| 1917 |
for _ in range(n): |
| 1918 |
_ = len(a) - 1 |
| 1919 |
|
| 1920 |
_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1921 |
uops = get_opnames(ex) |
| 1922 |
self.assertNotIn("_GUARD_NOS_NULL", uops) |
| 1923 |
self.assertNotIn("_GUARD_CALLABLE_LEN", uops) |
| 1924 |
self.assertIn("_CALL_LEN", uops) |
| 1925 |
self.assertNotIn("_GUARD_NOS_INT", uops) |
| 1926 |
self.assertNotIn("_GUARD_TOS_INT", uops) |
| 1927 |
|
| 1928 |
def test_binary_op_subscr_tuple_int(self): |
| 1929 |
def testfunc(n): |
| 1930 |
x = 0 |
| 1931 |
for _ in range(n): |
| 1932 |
y = (1, 2) |
| 1933 |
if y[0] == 1: # _COMPARE_OP_INT + _GUARD_IS_TRUE_POP are removed |
| 1934 |
x += 1 |
| 1935 |
return x |
| 1936 |
|
| 1937 |
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) |
| 1938 |
self.assertEqual(res, TIER2_THRESHOLD) |
| 1939 |
self.assertIsNotNone(ex) |
| 1940 |
uops = get_opnames(ex) |
| 1941 |
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops) |
| 1942 |
self.assertNotIn("_COMPARE_OP_INT", uops) |
| 1943 |
self.assertNotIn("_GUARD_IS_TRUE_POP", uops) |
| 1944 |
|
| 1945 |
def test_attr_promotion_failure(self): |
| 1946 |
# We're not testing for any specific uops here, just |
| 1947 |
# testing it doesn't crash. |
| 1948 |
script_helper.assert_python_ok('-c', textwrap.dedent(""" |
| 1949 |
import _testinternalcapi |
| 1950 |
import _opcode |
| 1951 |
import email |
| 1952 |
|
| 1953 |
def get_first_executor(func): |
| 1954 |
code = func.__code__ |
| 1955 |
co_code = code.co_code |
| 1956 |
for i in range(0, len(co_code), 2): |
| 1957 |
try: |
| 1958 |
return _opcode.get_executor(code, i) |
| 1959 |
except ValueError: |
| 1960 |
pass |
| 1961 |
return None |
| 1962 |
|
| 1963 |
def testfunc(n): |
| 1964 |
for _ in range(n): |
| 1965 |
email.jit_testing = None |
| 1966 |
prompt = email.jit_testing |
| 1967 |
del email.jit_testing |
| 1968 |
|
| 1969 |
|
| 1970 |
testfunc(_testinternalcapi.TIER2_THRESHOLD) |
| 1971 |
ex = get_first_executor(testfunc) |
| 1972 |
assert ex is not None |
| 1973 |
""")) |
| 1974 |
|
| 1975 |
def test_interpreter_finalization_with_generator_alive(self): |
| 1976 |
script_helper.assert_python_ok("-c", textwrap.dedent(""" |
| 1977 |
import sys |
| 1978 |
t = tuple(range(%d)) |
| 1979 |
def simple_for(): |
| 1980 |
for x in t: |
| 1981 |
x |
| 1982 |
|
| 1983 |
def gen(): |
| 1984 |
try: |
| 1985 |
yield |
| 1986 |
except: |
| 1987 |
simple_for() |
| 1988 |
|
| 1989 |
sys.settrace(lambda *args: None) |
| 1990 |
simple_for() |
| 1991 |
g = gen() |
| 1992 |
next(g) |
| 1993 |
""" % _testinternalcapi.SPECIALIZATION_THRESHOLD)) |
| 1994 |
|
| 1995 |
def test_next_instr_for_exception_handler_set(self): |
| 1996 |
# gh-140104: We just want the exception to be caught properly. |
| 1997 |
def f(): |
| 1998 |
for i in range(TIER2_THRESHOLD + 3): |
| 1999 |
try: |
| 2000 |
undefined_variable(i) |
| 2001 |
except Exception: |
| 2002 |
pass |
| 2003 |
|
| 2004 |
f() |
| 2005 |
|
| 2006 |
def test_next_instr_for_exception_handler_set_lasts_instr(self): |
| 2007 |
# gh-140104: We just want the exception to be caught properly. |
| 2008 |
def f(): |
| 2009 |
a_list = [] |
| 2010 |
for _ in range(TIER2_THRESHOLD + 3): |
| 2011 |
try: |
| 2012 |
a_list[""] = 0 |
| 2013 |
except Exception: |
| 2014 |
pass |
| 2015 |
|
| 2016 |
|
| 2017 |
def global_identity(x): |
| 2018 |
return x |
| 2019 |
|
| 2020 |
if __name__ == "__main__": |
| 2021 |
unittest.main() |