Skip to content

Commit

Permalink
Hacky way to make FOR_ITER a viable uop
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Nov 15, 2023
1 parent b838435 commit b28effa
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 44 deletions.
86 changes: 47 additions & 39 deletions Include/internal/pycore_opcode_metadata.h

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

10 changes: 10 additions & 0 deletions Python/abstract_interp_cases.c.h

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

29 changes: 27 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,7 @@ dummy_func(
goto enter_tier_one;
}

replaced op(_POP_JUMP_IF_FALSE, (unused/1, cond -- )) {
replaced op(_POP_JUMP_IF_FALSE, (unused/1, cond -- )) {
assert(PyBool_Check(cond));
int flag = Py_IsFalse(cond);
#if ENABLE_SPECIALIZATION
Expand Down Expand Up @@ -2512,7 +2512,7 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
}

op(_FOR_ITER, (iter -- iter, next)) {
replaced op(_FOR_ITER, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
next = (*Py_TYPE(iter)->tp_iternext)(iter);
if (next == NULL) {
Expand All @@ -2535,6 +2535,31 @@ dummy_func(
// Common case: no jump, leave it to the code generator
}

op(_FOR_ITER_TIER_TWO, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
next = (*Py_TYPE(iter)->tp_iternext)(iter);
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
GOTO_ERROR(error);
}
_PyErr_Clear(tstate);
}
/* iterator ended normally */
Py_DECREF(iter);
STACK_SHRINK(1);
/* HACK: Emulate DEOPT_IF to jump over END_FOR */
_PyFrame_SetStackPointer(frame, stack_pointer);
frame->instr_ptr += 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1;
assert(frame->instr_ptr[-1].op.code == END_FOR ||
frame->instr_ptr[-1].op.code == INSTRUMENTED_END_FOR);
Py_DECREF(current_executor);
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
goto enter_tier_one;
}
// Common case: no jump, leave it to the code generator
}

macro(FOR_ITER) = _SPECIALIZE_FOR_ITER + _FOR_ITER;

inst(INSTRUMENTED_FOR_ITER, (unused/1 -- )) {
Expand Down
49 changes: 49 additions & 0 deletions Python/executor_cases.c.h

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

1 change: 1 addition & 0 deletions Python/optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ _PyUop_Replacements[OPCODE_METADATA_SIZE] = {
[_ITER_JUMP_RANGE] = _GUARD_NOT_EXHAUSTED_RANGE,
[_ITER_JUMP_LIST] = _GUARD_NOT_EXHAUSTED_LIST,
[_ITER_JUMP_TUPLE] = _GUARD_NOT_EXHAUSTED_TUPLE,
[_FOR_ITER] = _FOR_ITER_TIER_TWO,
};

static const uint16_t
Expand Down
2 changes: 1 addition & 1 deletion Tools/cases_generator/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def variable_used_unspecialized(node: parsing.Node, name: str) -> bool:
tokens: list[lx.Token] = []
skipping = False
for i, token in enumerate(node.tokens):
if token.kind == "MACRO":
if token.kind == "CMACRO":
text = "".join(token.text.split())
# TODO: Handle nested #if
if text == "#if":
Expand Down
2 changes: 1 addition & 1 deletion Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def write_macro_expansions(
if not part.instr.is_viable_uop() and "replaced" not in part.instr.annotations:
# This note just reminds us about macros that cannot
# be expanded to Tier 2 uops. It is not an error.
# It is sometimes emitted for macros that have a
# Suppress it using 'replaced op(...)' for macros having
# manual translation in translate_bytecode_to_trace()
# in Python/optimizer.c.
if len(parts) > 1 or part.instr.name != name:
Expand Down
2 changes: 1 addition & 1 deletion Tools/cases_generator/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, inst: parsing.InstDef):
def is_viable_uop(self) -> bool:
"""Whether this instruction is viable as a uop."""
dprint: typing.Callable[..., None] = lambda *args, **kwargs: None
if "FRAME" in self.name:
if self.name == "_FOR_ITER_TIER_TWO":
dprint = print

if self.name == "_EXIT_TRACE":
Expand Down

0 comments on commit b28effa

Please sign in to comment.