Skip to content

gh-106701: Move the hand-written Tier 2 uops to bytecodes.c #106702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
100 changes: 50 additions & 50 deletions Include/internal/pycore_opcode_metadata.h

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

30 changes: 30 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,36 @@ dummy_func(
Py_UNREACHABLE();
}

///////// Tier-2 only opcodes /////////

op(_POP_JUMP_IF_FALSE, (flag -- )) {
if (Py_IsFalse(flag)) {
pc = oparg;
}
}

op(_POP_JUMP_IF_TRUE, (flag -- )) {
if (Py_IsTrue(flag)) {
pc = oparg;
}
}

op(JUMP_TO_TOP, (--)) {
pc = 0;
CHECK_EVAL_BREAKER();
}

op(SAVE_IP, (--)) {
frame->prev_instr = ip_offset + oparg;
}

op(EXIT_TRACE, (--)) {
frame->prev_instr--; // Back up to just before destination
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return frame;
}


// END BYTECODES //

Expand Down
40 changes: 0 additions & 40 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,46 +2764,6 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
#define ENABLE_SPECIALIZATION 0
#include "executor_cases.c.h"

// NOTE: These pop-jumps move the uop pc, not the bytecode ip
case _POP_JUMP_IF_FALSE:
{
if (Py_IsFalse(stack_pointer[-1])) {
pc = oparg;
}
stack_pointer--;
break;
}

case _POP_JUMP_IF_TRUE:
{
if (Py_IsTrue(stack_pointer[-1])) {
pc = oparg;
}
stack_pointer--;
break;
}

case JUMP_TO_TOP:
{
pc = 0;
CHECK_EVAL_BREAKER();
break;
}

case SAVE_IP:
{
frame->prev_instr = ip_offset + oparg;
break;
}

case EXIT_TRACE:
{
frame->prev_instr--; // Back up to just before destination
_PyFrame_SetStackPointer(frame, stack_pointer);
Py_DECREF(self);
return frame;
}

default:
{
fprintf(stderr, "Unknown uop %d, operand %" PRIu64 "\n", opcode, operand);
Expand Down
36 changes: 36 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.

12 changes: 8 additions & 4 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ def __init__(self, inst: parser.InstDef):

def is_viable_uop(self) -> bool:
"""Whether this instruction is viable as a uop."""
if self.name == "EXIT_TRACE":
return True # This has 'return frame' but it's okay
if self.always_exits:
# print(f"Skipping {self.name} because it always exits")
return False
Expand Down Expand Up @@ -1278,7 +1280,7 @@ def write_metadata(self) -> None:
typing.assert_never(thing)

with self.out.block("const char * const _PyOpcode_uop_name[512] =", ";"):
self.write_uop_items(lambda name, counter: f"[{counter}] = \"{name}\",")
self.write_uop_items(lambda name, counter: f"[{name}] = \"{name}\",")

self.out.emit("#endif // NEED_OPCODE_METADATA")

Expand Down Expand Up @@ -1324,17 +1326,19 @@ def write_pseudo_instrs(self) -> None:
def write_uop_items(self, make_text: typing.Callable[[str, int], str]) -> None:
"""Write '#define XXX NNN' for each uop"""
counter = 300 # TODO: Avoid collision with pseudo instructions
seen = set()

def add(name: str) -> None:
if name in seen:
return
nonlocal counter
self.out.emit(make_text(name, counter))
counter += 1
seen.add(name)

# These two are first by convention
add("EXIT_TRACE")
add("SAVE_IP")
add("_POP_JUMP_IF_FALSE")
add("_POP_JUMP_IF_TRUE")
add("JUMP_TO_TOP")

for instr in self.instrs.values():
if instr.kind == "op" and instr.is_viable_uop():
Expand Down