8000 GH-122390: Replace `_Py_GetbaseOpcode` with `_Py_GetBaseCodeUnit` by markshannon · Pull Request #122942 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-122390: Replace _Py_GetbaseOpcode with _Py_GetBaseCodeUnit #122942

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 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Replace _Py_GetbaseOpcode with _Py_GetBaseCodeUnit, and simplify some…
… uses.
  • Loading branch information
markshannon committed Aug 12, 2024
commit df033e4e9386b8f83525c62f0a212080597e6228
2 changes: 1 addition & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ adaptive_counter_backoff(_Py_BackoffCounter counter) {

extern int _Py_Instrument(PyCodeObject *co, PyInterpreterState *interp);

extern int _Py_GetBaseOpcode(PyCodeObject *code, int offset);
extern _Py_CODEUNIT _Py_GetBaseCodeUnit(PyCodeObject *code, int offset);

extern int _PyInstruction_GetLength(PyCodeObject *code, int offset);

Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Known values:
Python 3.14a1 3601 (Fix miscompilation of private names in generic classes)
Python 3.14a1 3602 (Add LOAD_SPECIAL. Remove BEFORE_WITH and BEFORE_ASYNC_WITH)
Python 3.14a1 3603 (Remove BUILD_CONST_KEY_MAP)
Python 3.14a1 3604 (Move ENTER_EXECUTOR to opcode 255)

Python 3.15 will start with 3650

Expand All @@ -267,7 +268,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3603
#define PYC_MAGIC_NUMBER 3604
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

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

102 changes: 51 additions & 51 deletions Include/opcode_ids.h

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

102 changes: 51 additions & 51 deletions Lib/_opcode_metadata.py

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

2 changes: 1 addition & 1 deletion Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def check_bool_function_result(self, func, ops, expected):
self.assertEqual(func(op), expected)

def test_invalid_opcodes(self):
invalid = [-100, -1, 255, 512, 513, 1000]
invalid = [-100, -1, 512, 513, 1000]
self.check_bool_function_result(_opcode.is_valid, invalid, False)
self.check_bool_function_result(_opcode.has_arg, invalid, False)
self.check_bool_function_result(_opcode.has_const, invalid, False)
Expand Down
62 changes: 12 additions & 50 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1630,15 +1630,10 @@ deopt_code(PyCodeObject *code, _Py_CODEUNIT *instructions)
{
Py_ssize_t len = Py_SIZE(code);
for (int i = 0; i < len; i++) {
int opcode = _Py_GetBaseOpcode(code, i);
if (opcode == ENTER_EXECUTOR) {
_PyExecutorObject *exec = code->co_executors->executors[instructions[i].op.arg];
opcode = _PyOpcode_Deopt[exec->vm_data.opcode];
instructions[i].op.arg = exec->vm_data.oparg;
}
assert(opcode != ENTER_EXECUTOR);
int caches = _PyOpcode_Caches[opcode];
instructions[i].op.code = opcode;
_Py_CODEUNIT inst = _Py_GetBaseCodeUnit(code, i);
assert(inst.op.code <= RESUME);
int caches = _PyOpcode_Caches[inst.op.code];
instructions[i] = inst;
for (int j = 1; j <= caches; j++) {
instructions[i+j].cache = 0;
}
Expand Down Expand Up @@ -1940,33 +1935,12 @@ code_richcompare(PyObject *self, PyObject *other, int op)
goto unequal;
}
for (int i = 0; i < Py_SIZE(co); i++) {
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
_Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
uint8_t co_code = _Py_GetBaseOpcode(co, i);
uint8_t co_arg = co_instr.op.arg;
uint8_t cp_code = _Py_GetBaseOpcode(cp, i);
uint8_t cp_arg = cp_instr.op.arg;

if (co_code == ENTER_EXECUTOR) {
const int exec_index = co_arg;
_PyExecutorObject *exec = co->co_executors->executors[exec_index];
co_code = _PyOpcode_Deopt[exec->vm_data.opcode];
co_arg = exec->vm_data.oparg;
}
assert(co_code != ENTER_EXECUTOR);

if (cp_code == ENTER_EXECUTOR) {
const int exec_index = cp_arg;
_PyExecutorObject *exec = cp->co_executors->executors[exec_index];
cp_code = _PyOpcode_Deopt[exec->vm_data.opcode];
cp_arg = exec->vm_data.oparg;
}
assert(cp_code != ENTER_EXECUTOR);

if (co_code != cp_code || co_arg != cp_arg) {
_Py_CODEUNIT co_instr = _Py_GetBaseCodeUnit(co, i);
_Py_CODEUNIT cp_instr = _Py_GetBaseCodeUnit(cp, i);
if (co_instr.cache != cp_instr.cache) {
goto unequal;
}
i += _PyOpcode_Caches[co_code];
i += _PyOpcode_Caches[co_instr.op.code];
}

/* compare constants */
Expand Down Expand Up @@ -2045,22 +2019,10 @@ code_hash(PyCodeObject *co)
SCRAMBLE_IN(co->co_firstlineno);
SCRAMBLE_IN(Py_SIZE(co));
for (int i = 0; i < Py_SIZE(co); i++) {
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
uint8_t co_code = co_instr.op.code;
uint8_t co_arg = co_instr.op.arg;
if (co_code == ENTER_EXECUTOR) {
_PyExecutorObject *exec = co->co_executors->executors[co_arg];
assert(exec != NULL);
assert(exec->vm_data.opcode != ENTER_EXECUTOR);
co_code = _PyOpcode_Deopt[exec->vm_data.opcode];
co_arg = exec->vm_data.oparg;
}
else {
co_code = _Py_GetBaseOpcode(co, i);
}
SCRAMBLE_IN(co_code);
SCRAMBLE_IN(co_arg);
i += _PyOpcode_Caches[co_code];
_Py_CODEUNIT co_instr = _Py_GetBaseCodeUnit(co, i);
SCRAMBLE_IN(co_instr.op.code);
SCRAMBLE_IN(co_instr.op.arg);
i += _PyOpcode_Caches[co_instr.op.code];
}
if ((Py_hash_t)uhash == -1) {
return -2;
Expand Down
Loading
0