FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-130415: Optimize JIT path for _TO_BOOL_INT branching by Klaus117 · Pull Request #130477 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 31 additions & 0 deletions Lib/test/test_capi/test_opt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,37 @@ def crash_addition():

crash_addition()

def test_narrow_type_to_constant_int_zero(self):
def f(n):
trace = []
for i in range(n):
# zero is always (int) 0, but we can only prove that it's a integer:
zero = i - i
trace.append("A")
if not zero: # Kept.
trace.append("B")
if not zero: # Removed!
trace.append("C")
trace.append("D")
if zero: # Removed!
trace.append("X")
trace.append("E")
trace.append("F")
if zero: # Removed!
trace.append("X")
trace.append("G")
return trace

trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
# Only one guard remains:
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))


def global_identity(x):
return x
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve the JIT's understanding of integers in boolean contexts.
13 changes: 13 additions & 0 deletions Python/optimizer_bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ dummy_func(void) {
sym_set_type(value, &PyLong_Type);
res = sym_new_type(ctx, &PyBool_Type);
}
if (!sym_is_const(value)) {
assert(sym_matches_type(value, &PyLong_Type));
int next_opcode = (this_instr + 1)->opcode;
assert(next_opcode == _CHECK_VALIDITY_AND_SET_IP);
next_opcode = (this_instr + 2)->opcode;
// If the next uop is a guard, we can narrow value. However, we
// *can't* narrow res, since that would cause the guard to be
// removed and the narrowed value to be invalid:
if (next_opcode == _GUARD_IS_FALSE_POP) {
sym_set_const(value, Py_GetConstant(Py_CONSTANT_ZERO));
res = sym_new_type(ctx, &PyBool_Type);
}
}
}

op(_TO_BOOL_LIST, (value -- res)) {
Expand Down
18 changes: 18 additions & 0 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.


Back | FazBrowse Home | New Git URL