Skip to content

Commit

Permalink
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 committed Jan 21, 2025
1 parent e65a1eb commit b191528
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 57 deletions.
33 changes: 1 addition & 32 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,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 @@ -259,8 +230,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 @@ -280,7 +249,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 PRED_##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 PRED_{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"PRED_{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 b191528

Please sign in to comment.