Skip to content

Commit 19b7ead

Browse files
authored
GH-109214: Convert _SAVE_CURRENT_IP to _SET_IP in tier 2 trace creation. (GH-110755)
1 parent fb7843e commit 19b7ead

File tree

8 files changed

+22
-59
lines changed

8 files changed

+22
-59
lines changed

Include/internal/pycore_opcode_metadata.h

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

Python/abstract_interp_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/bytecodes.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,6 @@ dummy_func(
803803
}
804804

805805
macro(RETURN_VALUE) =
806-
_SET_IP + // Tier 2 only; special-cased oparg
807806
_SAVE_CURRENT_IP + // Sets frame->prev_instr
808807
_POP_FRAME;
809808

@@ -828,7 +827,6 @@ dummy_func(
828827

829828
macro(RETURN_CONST) =
830829
LOAD_CONST +
831-
_SET_IP + // Tier 2 only; special-cased oparg
832830
_SAVE_CURRENT_IP + // Sets frame->prev_instr
833831
_POP_FRAME;
834832

@@ -3099,7 +3097,6 @@ dummy_func(
30993097
_CHECK_FUNCTION_EXACT_ARGS +
31003098
_CHECK_STACK_SPACE +
31013099
_INIT_CALL_PY_EXACT_ARGS +
3102-
_SET_IP + // Tier 2 only; special-cased oparg
31033100
_SAVE_CURRENT_IP + // Sets frame->prev_instr
31043101
_PUSH_FRAME;
31053102

@@ -3109,7 +3106,6 @@ dummy_func(
31093106
_CHECK_FUNCTION_EXACT_ARGS +
31103107
_CHECK_STACK_SPACE +
31113108
_INIT_CALL_PY_EXACT_ARGS +
3112-
_SET_IP + // Tier 2 only; special-cased oparg
31133109
_SAVE_CURRENT_IP + // Sets frame->prev_instr
31143110
_PUSH_FRAME;
31153111

@@ -3948,17 +3944,13 @@ dummy_func(
39483944
}
39493945

39503946
op(_SET_IP, (--)) {
3947+
TIER_TWO_ONLY
39513948
frame->prev_instr = ip_offset + oparg;
39523949
}
39533950

39543951
op(_SAVE_CURRENT_IP, (--)) {
3955-
#if TIER_ONE
3952+
TIER_ONE_ONLY
39563953
frame->prev_instr = next_instr - 1;
3957-
#endif
3958-
#if TIER_TWO
3959-
// Relies on a preceding _SET_IP
3960-
frame->prev_instr--;
3961-
#endif
39623954
}
39633955

39643956
op(_EXIT_TRACE, (--)) {

Python/ceval_macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
372372
/* Marker to specify tier 1 only instructions */
373373
#define TIER_ONE_ONLY
374374

375+
/* Marker to specify tier 2 only instructions */
376+
#define TIER_TWO_ONLY
377+
375378
/* Implementation of "macros" that modify the instruction pointer,
376379
* stack pointer, or frame pointer.
377380
* These need to treated differently by tier 1 and 2. */

Python/executor_cases.c.h

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

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

Python/optimizer.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ translate_bytecode_to_trace(
648648
uint32_t orig_oparg = oparg; // For OPARG_TOP/BOTTOM
649649
for (int i = 0; i < nuops; i++) {
650650
oparg = orig_oparg;
651+
uint32_t uop = expansion->uops[i].uop;
651652
uint64_t operand = 0;
652653
// Add one to account for the actual opcode/oparg pair:
653654
int offset = expansion->uops[i].offset + 1;
@@ -680,6 +681,7 @@ translate_bytecode_to_trace(
680681
break;
681682
case OPARG_SET_IP: // op==_SET_IP; oparg=next instr
682683
oparg = INSTR_IP(instr + offset, code);
684+
uop = _SET_IP;
683685
break;
684686

685687
default:
@@ -690,8 +692,8 @@ translate_bytecode_to_trace(
690692
expansion->uops[i].offset);
691693
Py_FatalError("garbled expansion");
692694
}
693-
ADD_TO_TRACE(expansion->uops[i].uop, oparg, operand);
694-
if (expansion->uops[i].uop == _POP_FRAME) {
695+
ADD_TO_TRACE(uop, oparg, operand);
696+
if (uop == _POP_FRAME) {
695697
TRACE_STACK_POP();
696698
DPRINTF(2,
697699
"Returning to %s (%s:%d) at byte offset %d\n",
@@ -701,7 +703,7 @@ translate_bytecode_to_trace(
701703
2 * INSTR_IP(instr, code));
702704
goto top;
703705
}
704-
if (expansion->uops[i].uop == _PUSH_FRAME) {
706+
if (uop == _PUSH_FRAME) {
705707
assert(i + 1 == nuops);
706708
int func_version_offset =
707709
offsetof(_PyCallCache, func_version)/sizeof(_Py_CODEUNIT)

Tools/cases_generator/generate_cases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def write_macro_expansions(
658658
for part in parts:
659659
if isinstance(part, Component):
660660
# All component instructions must be viable uops
661-
if not part.instr.is_viable_uop():
661+
if not part.instr.is_viable_uop() and part.instr.name != "_SAVE_CURRENT_IP":
662662
# This note just reminds us about macros that cannot
663663
# be expanded to Tier 2 uops. It is not an error.
664664
# It is sometimes emitted for macros that have a
@@ -671,8 +671,8 @@ def write_macro_expansions(
671671
)
672672
return
673673
if not part.active_caches:
674-
if part.instr.name == "_SET_IP":
675-
size, offset = OPARG_SIZES["OPARG_SET_IP"], cache_offset
674+
if part.instr.name == "_SAVE_CURRENT_IP":
675+
size, offset = OPARG_SIZES["OPARG_SET_IP"], cache_offset - 1
676676
else:
677677
size, offset = OPARG_SIZES["OPARG_FULL"], 0
678678
else:

0 commit comments

Comments
 (0)