Skip to content

Commit 4b26cde

Browse files
Handle unstable branches
1 parent 9bb03a8 commit 4b26cde

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Python/bytecodes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,13 +3063,15 @@ dummy_func(
30633063
assert(PyStackRef_BoolCheck(cond));
30643064
int flag = PyStackRef_IsFalse(cond);
30653065
DEAD(cond);
3066+
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
30663067
JUMPBY(flag ? oparg : next_instr->op.code == NOT_TAKEN);
30673068
}
30683069

30693070
replaced op(_POP_JUMP_IF_TRUE, (cond -- )) {
30703071
assert(PyStackRef_BoolCheck(cond));
30713072
int flag = PyStackRef_IsTrue(cond);
30723073
DEAD(cond);
3074+
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
30733075
JUMPBY(flag ? oparg : next_instr->op.code == NOT_TAKEN);
30743076
}
30753077

Python/generated_cases.c.h

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

Python/generated_tracer_cases.c.h

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

Python/optimizer.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,19 @@ _PyJIT_translate_single_bytecode_to_trace(
710710
case POP_JUMP_IF_FALSE:
711711
case POP_JUMP_IF_TRUE:
712712
{
713+
int counter = target_instr[1].cache;
714+
int bitcount = _Py_popcount32(counter);
715+
int jump_likely = bitcount > 8;
713716
_Py_CODEUNIT *computed_next_instr_without_modifiers = target_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
714717
_Py_CODEUNIT *computed_next_instr = computed_next_instr_without_modifiers + (computed_next_instr_without_modifiers->op.code == NOT_TAKEN);
715718
_Py_CODEUNIT *computed_jump_instr = computed_next_instr_without_modifiers + oparg;
716719
assert(next_instr == computed_next_instr || next_instr == computed_jump_instr);
717720
int jump_happened = computed_jump_instr == next_instr;
721+
// Jump is likely but it did not happen this time. Indicates we are likely tracing
722+
// an uncommmon path. Stop the trace early here to prevent trace explosion.
723+
if (jump_likely != jump_happened) {
724+
goto unsupported;
725+
}
718726
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_happened];
719727
ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(jump_happened ? computed_next_instr : computed_jump_instr, old_code));
720728
break;
@@ -1226,7 +1234,7 @@ uop_optimize(
12261234
int curr_stackentries = tstate->interp->jit_state.jit_tracer_initial_stack_depth;
12271235
int length = interp->jit_state.jit_tracer_code_curr_size;
12281236
// Trace too short, don't bother.
1229-
if (length <= 5) {
1237+
if (length <= 20) {
12301238
return 0;
12311239
}
12321240
assert(length > 0);

0 commit comments

Comments
 (0)