8000 optimize for int=0 branching. move to correct branch · python/cpython@76793af · GitHub
[go: up one dir, main page]

Skip to content

Commit 76793af

Browse files
committed
optimize for int=0 branching. move to correct branch
1 parent 3ae9101 commit 76793af

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,67 @@ def crash_addition():
14371437

14381438
crash_addition()
14391439

1440+
def test_narrow_type_to_constant_int_zero(self):
1441+
def f(n):
1442+
trace = []
1443+
for i in range(n):
1444+
# f is always (int) 0, but we can only prove that it's a integer:
1445+
f = i - i # at this point python knows f is an int, but doesnt know that it is 0 (we know it is 0 though)
1446+
trace.append("A")
1447+
if not f: # Kept.
1448+
trace.append("B")
1449+
if not f: # Removed!
1450+
trace.append("C")
1451+
trace.append("D")
1452+
if f: # Removed!
1453+
trace.append("X")
1454+
trace.append("E")
1455+
trace.append("F")
1456+
if f: # Removed!
1457+
trace.append("X")
1458+
trace.append("G")
1459+
return trace
1460+
1461+
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1462+
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
1463+
self.assertIsNotNone(ex)
1464+
uops = get_opnames(ex)
1465+
# Only one guard remains:
1466+
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
1467+
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
1468+
# But all of the appends we care about are still there:
1469+
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
1470+
1471+
# def test_narrow_type_to_constant_bool_true(self):
1472+
# def f(n):
1473+
# trace = []
1474+
# for i in range(n):
1475+
# # f is always True, but we can only prove that it's a bool:
1476+
# f = i != TIER2_THRESHOLD
1477+
# trace.append("A")
1478+
# if f: # Kept.
1479+
# trace.append("B")
1480+
# if not f: # Removed!
1481+
# trace.append("X")
1482+
# trace.append("C")
1483+
# if f: # Removed!
1484+
# trace.append("D")
1485+
# trace.append("E")
1486+
# trace.append("F")
1487+
# if not f: # Removed!
1488+
# trace.append("X")
1489+
# trace.append("G")
1490+
# return trace
1491+
1492+
# trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1493+
# self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
1494+
# self.assertIsNotNone(ex)
1495+
# uops = get_opnames(ex)
1496+
# # Only one guard remains:
1497+
# self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 0)
1498+
# self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 1)
1499+
# # But all of the appends we care about are still there:
1500+
# self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
14401501

14411502
def global_identity(x):
14421503
return x

Python/optimizer_bytecodes.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,23 @@ dummy_func(void) {
407407
sym_set_type(value, &PyLong_Type);
408408
res = sym_new_type(ctx, &PyBool_Type);
409409
}
410+
if (!sym_is_const(value)) {
411+
assert(sym_matches_type(value, &PyLong_Type));
412+
int next_opcode = (this_instr + 1)->opcode;
413+
assert(next_opcode == _CHECK_VALIDITY_AND_SET_IP);
414+
next_opcode = (this_instr + 2)->opcode;
415+
// If the next uop is a guard, we can narrow value. However, we
416+
// *can't* narrow res, since that would cause the guard to be
417+
// removed and the narrowed value to be invalid:
418+
if (next_opcode == _GUARD_IS_FALSE_POP) {
419+
sym_set_const(value, Py_GetConstant(Py_CONSTANT_ZERO));
420+
res = sym_new_type(ctx, &PyLong_Type);
421+
}
422+
// else if (next_opcode == _GUARD_IS_TRUE_POP) {
423+
// sym_set_const(value, Py_True);
424+
// res = sym_new_type(ctx, &PyBool_Type);
425+
// }
426+
}
410427
}
411428

412429
op(_TO_BOOL_LIST, (value -- res)) {

Python/optimizer_cases.c.h

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0