Skip to content

Commit 8d2c3b5

Browse files
committed
Tiny fixes to code generator
1 parent 4712a5f commit 8d2c3b5

File tree

9 files changed

+156
-45
lines changed

9 files changed

+156
-45
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 49 additions & 41 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: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ dummy_func(
25122512
#endif /* ENABLE_SPECIALIZATION */
25132513
}
25142514

2515-
op(_FOR_ITER, (iter -- iter, next)) {
2515+
replaced op(_FOR_ITER, (iter -- iter, next)) {
25162516
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
25172517
next = (*Py_TYPE(iter)->tp_iternext)(iter);
25182518
if (next == NULL) {
@@ -2531,6 +2531,40 @@ dummy_func(
25312531
/* Jump forward oparg, then skip following END_FOR instruction */
25322532
JUMPBY(oparg + 1);
25332533
DISPATCH();
2534+
<<<<<<< HEAD
2535+
||||||| parent of 7dc8c93549a (Tiny fixes to code generator)
2536+
#endif
2537+
#if TIER_TWO
2538+
frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame)) + oparg;
2539+
DEOPT_IF(true);
2540+
#endif
2541+
=======
2542+
}
2543+
// Common case: no jump, leave it to the code generator
2544+
}
2545+
2546+
op(_FOR_ITER_TIER_TWO, (iter -- iter, next)) {
2547+
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
2548+
next = (*Py_TYPE(iter)->tp_iternext)(iter);
2549+
if (next == NULL) {
2550+
if (_PyErr_Occurred(tstate)) {
2551+
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2552+
GOTO_ERROR(error);
2553+
}
2554+
_PyErr_Clear(tstate);
2555+
}
2556+
/* iterator ended normally */
2557+
Py_DECREF(iter);
2558+
STACK_SHRINK(1);
2559+
/* HACK: Emulate DEOPT_IF to jump over END_FOR */
2560+
_PyFrame_SetStackPointer(frame, stack_pointer);
2561+
frame->instr_ptr += 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1;
2562+
assert(frame->instr_ptr[-1].op.code == END_FOR ||
2563+
frame->instr_ptr[-1].op.code == INSTRUMENTED_END_FOR);
2564+
Py_DECREF(current_executor);
2565+
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
2566+
goto enter_tier_one;
2567+
>>>>>>> 7dc8c93549a (Tiny fixes to code generator)
25342568
}
25352569
// Common case: no jump, leave it to the code generator
25362570
}

Python/executor_cases.c.h

Lines changed: 50 additions & 0 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: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ _PyUop_Replacements[OPCODE_METADATA_SIZE] = {
391391
[_ITER_JUMP_RANGE] = _GUARD_NOT_EXHAUSTED_RANGE,
392392
[_ITER_JUMP_LIST] = _GUARD_NOT_EXHAUSTED_LIST,
393393
[_ITER_JUMP_TUPLE] = _GUARD_NOT_EXHAUSTED_TUPLE,
394+
[_FOR_ITER] = _FOR_ITER_TIER_TWO,
394395
};
395396

396397
static const uint16_t

Tools/cases_generator/flags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def variable_used_unspecialized(node: parsing.Node, name: str) -> bool:
175175
tokens: list[lx.Token] = []
176176
skipping = False
177177
for i, token in enumerate(node.tokens):
178-
if token.kind == "MACRO":
178+
if token.kind == "CMACRO":
179179
text = "".join(token.text.split())
180180
# TODO: Handle nested #if
181181
if text == "#if":

Tools/cases_generator/generate_cases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def write_macro_expansions(
658658
if not part.instr.is_viable_uop() and "replaced" not in part.instr.annotations:
659659
# This note just reminds us about macros that cannot
660660
# be expanded to Tier 2 uops. It is not an error.
661-
# It is sometimes emitted for macros that have a
661+
# Suppress it using 'replaced op(...)' for macros having
662662
# manual translation in translate_bytecode_to_trace()
663663
# in Python/optimizer.c.
664664
if len(parts) > 1 or part.instr.name != name:

Tools/cases_generator/instructions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, inst: parsing.InstDef):
115115
def is_viable_uop(self) -> bool:
116116
"""Whether this instruction is viable as a uop."""
117117
dprint: typing.Callable[..., None] = lambda *args, **kwargs: None
118-
if "FRAME" in self.name:
118+
if self.name == "_FOR_ITER_TIER_TWO":
119119
dprint = print
120120

121121
if self.name == "_EXIT_TRACE":

0 commit comments

Comments
 (0)