Skip to content

Commit 2c603cc

Browse files
fix branch prediction for real
1 parent 57f417e commit 2c603cc

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

Python/bytecodes.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,15 +3059,13 @@ dummy_func(
30593059
assert(PyStackRef_BoolCheck(cond));
30603060
int flag = PyStackRef_IsFalse(cond);
30613061
DEAD(cond);
3062-
RECORD_JUMP_TAKEN();
30633062
JUMPBY(flag ? oparg : next_instr->op.code == NOT_TAKEN);
30643063
}
30653064

30663065
replaced op(_POP_JUMP_IF_TRUE, (cond -- )) {
30673066
assert(PyStackRef_BoolCheck(cond));
30683067
int flag = PyStackRef_IsTrue(cond);
30693068
DEAD(cond);
3070-
RECORD_JUMP_TAKEN();
30713069
JUMPBY(flag ? oparg : next_instr->op.code == NOT_TAKEN);
30723070
}
30733071

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ _PyJIT_translate_single_bytecode_to_trace(
617617
const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode];
618618

619619
// Strange control-flow, unsupported opcode, etc.
620-
if (jump_taken || opcode == WITH_EXCEPT_START || opcode == RERAISE || opcode == CLEANUP_THROW || opcode == PUSH_EXC_INFO) {
620+
if (jump_taken ||
621+
opcode == WITH_EXCEPT_START || opcode == RERAISE || opcode == CLEANUP_THROW || opcode == PUSH_EXC_INFO) {
621622
unsupported:
622623
// Rewind to previous instruction and replace with _EXIT_TRACE.
623624
_PyUOpInstruction *curr = &trace[trace_length-1];
@@ -671,11 +672,11 @@ _PyJIT_translate_single_bytecode_to_trace(
671672
case POP_JUMP_IF_TRUE:
672673
{
673674
RESERVE(1);
674-
int jump_likely = jump_taken;
675+
_Py_CODEUNIT *computed_next_instr = target_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
676+
_Py_CODEUNIT *computed_jump_instr = computed_next_instr + oparg;
677+
int jump_likely = computed_jump_instr == next_instr;
675678
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely];
676-
_Py_CODEUNIT *next_instr = target_instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
677-
_Py_CODEUNIT *false_target = next_instr + oparg;
678-
ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(false_target, old_code));
679+
ADD_TO_TRACE(uopcode, 0, 0, INSTR_IP(jump_likely ? computed_next_instr : computed_jump_instr, old_code));
679680
break;
680681
}
681682
case JUMP_BACKWARD_JIT:

0 commit comments

Comments
 (0)