Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,12 @@ def test_predictions(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP1);
PREDICTED(OP1);
_PyStackRef res;
res = Py_None;
stack_pointer[-1] = res;
PRED_OP1:
{
_PyStackRef res;
res = Py_None;
stack_pointer[-1] = res;
}
DISPATCH();
}

Expand Down Expand Up @@ -646,36 +648,38 @@ def test_macro_instruction(self):
frame->instr_ptr = next_instr;
next_instr += 6;
INSTRUCTION_STATS(OP);
PREDICTED(OP);
_Py_CODEUNIT* const this_instr = next_instr - 6;
(void)this_instr;
_PyStackRef left;
_PyStackRef right;
_PyStackRef arg2;
_PyStackRef res;
// _OP1
PRED_OP:
{
right = stack_pointer[-1];
left = stack_pointer[-2];
uint16_t counter = read_u16(&this_instr[1].cache);
(void)counter;
_PyFrame_SetStackPointer(frame, stack_pointer);
op1(left, right);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
/* Skip 2 cache entries */
// OP2
{
arg2 = stack_pointer[-3];
uint32_t extra = read_u32(&this_instr[4].cache);
(void)extra;
_PyFrame_SetStackPointer(frame, stack_pointer);
res = op2(arg2, left, right);
stack_pointer = _PyFrame_GetStackPointer(frame);
_Py_CODEUNIT* const this_instr = next_instr - 6;
(void)this_instr;
_PyStackRef left;
_PyStackRef right;
_PyStackRef arg2;
_PyStackRef res;
// _OP1
{
right = stack_pointer[-1];
left = stack_pointer[-2];
uint16_t counter = read_u16(&this_instr[1].cache);
(void)counter;
_PyFrame_SetStackPointer(frame, stack_pointer);
op1(left, right);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
/* Skip 2 cache entries */
// OP2
{
arg2 = stack_pointer[-3];
uint32_t extra = read_u32(&this_instr[4].cache);
(void)extra;
_PyFrame_SetStackPointer(frame, stack_pointer);
res = op2(arg2, left, right);
stack_pointer = _PyFrame_GetStackPointer(frame);
}
stack_pointer[-3] = res;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
}
stack_pointer[-3] = res;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}

Expand Down
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
Loading
Loading