Skip to content

Commit

Permalink
pythongh-128563: Move GO_TO_INSTRUCTION and PREDICT to cases generator (
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner authored Jan 22, 2025
1 parent 767cf70 commit 86c1a60
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 59 deletions.
4 changes: 2 additions & 2 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def test_predictions(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP1);
PREDICTED(OP1);
PREDICTED_OP1:;
_PyStackRef res;
res = Py_None;
stack_pointer[-1] = res;
Expand Down Expand Up @@ -646,7 +646,7 @@ def test_macro_instruction(self):
frame->instr_ptr = next_instr;
next_instr += 6;
INSTRUCTION_STATS(OP);
PREDICTED(OP);
PREDICTED_OP:;
_Py_CODEUNIT* const this_instr = next_instr - 6;
(void)this_instr;
_PyStackRef left;
Expand Down
33 changes: 1 addition & 32 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,6 @@ GETITEM(PyObject *v, Py_ssize_t i) {
#define JUMPBY(x) (next_instr += (x))
#define SKIP_OVER(x) (next_instr += (x))

/* OpCode prediction macros
Some opcodes tend to come in pairs thus making it possible to
predict the second code when the first is run. For example,
COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Verifying the prediction costs a single high-speed test of a register
variable against a constant. If the pairing was good, then the
processor's own internal branch predication has a high likelihood of
success, resulting in a nearly zero-overhead transition to the
next opcode. A successful prediction saves a trip through the eval-loop
including its unpredictable switch-case branch. Combined with the
processor's internal branch prediction, a successful PREDICT has the
effect of making the two opcodes run as if they were a single new opcode
with the bodies combined.
If collecting opcode statistics, your choices are to either keep the
predictions turned-on and interpret the results as if some opcodes
had been combined or turn-off predictions so that the opcode frequency
counter updates for both opcodes.
Opcode prediction is disabled with threaded code, since the latter allows
the CPU to record separate branch prediction information for each
opcode.
*/

#define PREDICT_ID(op) PRED_##op
#define PREDICTED(op) PREDICT_ID(op):


/* Stack manipulation macros */

Expand Down Expand Up @@ -260,8 +231,6 @@ GETITEM(PyObject *v, Py_ssize_t i) {
GETLOCAL(i) = value; \
PyStackRef_XCLOSE(tmp); } while (0)

#define GO_TO_INSTRUCTION(op) goto PREDICT_ID(op)

#ifdef Py_STATS
#define UPDATE_MISS_STATS(INSTNAME) \
do { \
Expand All @@ -281,7 +250,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
/* This is only a single jump on release builds! */ \
UPDATE_MISS_STATS((INSTNAME)); \
assert(_PyOpcode_Deopt[opcode] == (INSTNAME)); \
GO_TO_INSTRUCTION(INSTNAME); \
goto PREDICTED_##INSTNAME; \
}


Expand Down
49 changes: 25 additions & 24 deletions Python/generated_cases.c.h

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

18 changes: 18 additions & 0 deletions Tools/cases_generator/generators_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def __init__(self, out: CWriter):
"DISPATCH": self.dispatch,
"INSTRUCTION_SIZE": self.instruction_size,
"POP_INPUT": self.pop_input,
"GO_TO_INSTRUCTION": self.go_to_instruction,
}
self.out = out

Expand Down Expand Up @@ -402,6 +403,23 @@ def sync_sp(
self._print_storage(storage)
return True

def go_to_instruction(
self,
tkn: Token,
tkn_iter: TokenIterator,
uop: Uop,
storage: Storage,
inst: Instruction | None,
) -> bool:
next(tkn_iter)
name = next(tkn_iter)
next(tkn_iter)
next(tkn_iter)
assert name.kind == "IDENTIFIER"
self.emit("\n")
self.emit(f"goto PREDICTED_{name.text};\n")
return True

def emit_save(self, storage: Storage) -> None:
storage.save(self.out)
self._print_storage(storage)
Expand Down
2 changes: 1 addition & 1 deletion Tools/cases_generator/tier1_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def generate_tier1(
out.emit(f"next_instr += {inst.size};\n")
out.emit(f"INSTRUCTION_STATS({name});\n")
if inst.is_target:
out.emit(f"PREDICTED({name});\n")
out.emit(f"PREDICTED_{name}:;\n")
if needs_this:
out.emit(f"_Py_CODEUNIT* const this_instr = next_instr - {inst.size};\n")
out.emit(unused_guard)
Expand Down

0 comments on commit 86c1a60

Please sign in to comment.