Skip to content

Commit 66eafc9

Browse files
authored
gh-144681: Fix JIT trace builder assertion failure when conditional branch jump target coincides with fallthrough target (GH-144742)
1 parent 099943b commit 66eafc9

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,19 @@ def testfunc(n):
461461
uops = get_opnames(ex)
462462
self.assertIn(self.guard_is_false, uops)
463463

464+
def test_branch_coincident_targets(self):
465+
# test for gh-144681: https://github.com/python/cpython/issues/144681
466+
def testfunc(n):
467+
for _ in range(n):
468+
r = [x for x in range(10) if [].append(x) or True]
469+
return r
470+
471+
res = testfunc(TIER2_THRESHOLD)
472+
ex = get_first_executor(testfunc)
473+
474+
self.assertEqual(res, list(range(10)))
475+
self.assertIsNotNone(ex)
476+
464477
def test_for_iter_tier_two(self):
465478
class MyIter:
466479
def __init__(self, n):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a JIT assertion failure when a conditional branch jumps to the same target as the fallthrough path.

Python/optimizer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ _PyJit_translate_single_bytecode_to_trace(
786786
_Py_CODEUNIT *computed_next_instr = computed_next_instr_without_modifiers + (computed_next_instr_without_modifiers->op.code == NOT_TAKEN);
787787
_Py_CODEUNIT *computed_jump_instr = computed_next_instr_without_modifiers + oparg;
788788
assert(next_instr == computed_next_instr || next_instr == computed_jump_instr);
789-
int jump_happened = computed_jump_instr == next_instr;
790-
assert(jump_happened == (target_instr[1].cache & 1));
789+
int jump_happened = target_instr[1].cache & 1;
790+
assert(jump_happened ? (next_instr == computed_jump_instr) : (next_instr == computed_next_instr));
791791
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_happened];
792792
ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(jump_happened ? computed_next_instr : computed_jump_instr, old_code));
793793
break;

0 commit comments

Comments
 (0)