Skip to content

gh-98831: Modernize the LOAD_GLOBAL family #101502

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 3 commits into from
Feb 1, 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
57 changes: 19 additions & 38 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,13 @@ dummy_func(
}
}

// error: LOAD_GLOBAL has irregular stack effect
inst(LOAD_GLOBAL) {
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
LOAD_GLOBAL,
LOAD_GLOBAL_MODULE,
LOAD_GLOBAL_BUILTIN,
};

inst(LOAD_GLOBAL, (unused/1, unused/1, unused/2, unused/1 -- null if (oparg & 1), v)) {
#if ENABLE_SPECIALIZATION
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
Expand All @@ -1082,10 +1087,7 @@ dummy_func(
STAT_INC(LOAD_GLOBAL, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */
int push_null = oparg & 1;
PEEK(0) = NULL;
PyObject *name = GETITEM(names, oparg>>1);
PyObject *v;
if (PyDict_CheckExact(GLOBALS())
&& PyDict_CheckExact(BUILTINS()))
{
Expand All @@ -1099,7 +1101,7 @@ dummy_func(
format_exc_check_arg(tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
}
goto error;
ERROR_IF(true, error);
}
Py_INCREF(v);
}
Expand All @@ -1109,9 +1111,7 @@ dummy_func(
/* namespace 1: globals */
v = PyObject_GetItem(GLOBALS(), name);
if (v == NULL) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
goto error;
}
ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error);
_PyErr_Clear(tstate);

/* namespace 2: builtins */
Expand All @@ -1122,58 +1122,42 @@ dummy_func(
tstate, PyExc_NameError,
NAME_ERROR_MSG, name);
}
goto error;
ERROR_IF(true, error);
}
}
}
/* Skip over inline cache */
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
STACK_GROW(push_null);
PUSH(v);
null = NULL;
}

// error: LOAD_GLOBAL has irregular stack effect
inst(LOAD_GLOBAL_MODULE) {
inst(LOAD_GLOBAL_MODULE, (unused/1, index/1, version/2, unused/1 -- null if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
PyDictObject *dict = (PyDictObject *)GLOBALS();
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
uint32_t version = read_u32(cache->module_keys_version);
DEOPT_IF(dict->ma_keys->dk_version != version, LOAD_GLOBAL);
assert(DK_IS_UNICODE(dict->ma_keys));
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(dict->ma_keys);
PyObject *res = entries[cache->index].me_value;
res = entries[index].me_value;
DEOPT_IF(res == NULL, LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
Py_INCREF(res);
STAT_INC(LOAD_GLOBAL, hit);
STACK_GROW(push_null+1);
SET_TOP(Py_NewRef(res));
null = NULL;
}

// error: LOAD_GLOBAL has irregular stack effect
inst(LOAD_GLOBAL_BUILTIN) {
inst(LOAD_GLOBAL_BUILTIN, (unused/1, index/1, mod_version/2, bltn_version/1 -- null if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
DEOPT_IF(!PyDict_CheckExact(BUILTINS()), LOAD_GLOBAL);
PyDictObject *mdict = (PyDictObject *)GLOBALS();
PyDictObject *bdict = (PyDictObject *)BUILTINS();
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
uint32_t mod_version = read_u32(cache->module_keys_version);
uint16_t bltn_version = cache->builtin_keys_version;
DEOPT_IF(mdict->ma_keys->dk_version != mod_version, LOAD_GLOBAL);
DEOPT_IF(bdict->ma_keys->dk_version != bltn_version, LOAD_GLOBAL);
assert(DK_IS_UNICODE(bdict->ma_keys));
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(bdict->ma_keys);
PyObject *res = entries[cache->index].me_value;
res = entries[index].me_value;
DEOPT_IF(res == NULL, LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
Py_INCREF(res);
STAT_INC(LOAD_GLOBAL, hit);
STACK_GROW(push_null+1);
SET_TOP(Py_NewRef(res));
null = NULL;
}

inst(DELETE_FAST, (--)) {
Expand Down Expand Up @@ -3227,9 +3211,6 @@ family(call, INLINE_CACHE_ENTRIES_CALL) = {
family(for_iter, INLINE_CACHE_ENTRIES_FOR_ITER) = {
FOR_ITER, FOR_ITER_LIST,
FOR_ITER_RANGE };
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
LOAD_GLOBAL, LOAD_GLOBAL_BUILTIN,
LOAD_GLOBAL_MODULE };
family(store_fast) = { STORE_FAST, STORE_FAST__LOAD_FAST, STORE_FAST__STORE_FAST };
family(unpack_sequence, INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE) = {
UNPACK_SEQUENCE, UNPACK_SEQUENCE_LIST,
Expand Down
66 changes: 37 additions & 29 deletions Python/generated_cases.c.h

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

20 changes: 10 additions & 10 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case LOAD_NAME:
return 0;
case LOAD_GLOBAL:
return -1;
return 0;
case LOAD_GLOBAL_MODULE:
return -1;
return 0;
case LOAD_GLOBAL_BUILTIN:
return -1;
return 0;
case DELETE_FAST:
return 0;
case MAKE_CELL:
Expand Down Expand Up @@ -487,11 +487,11 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case LOAD_NAME:
return 1;
case LOAD_GLOBAL:
return -1;
return ((oparg & 1) ? 1 : 0) + 1;
case LOAD_GLOBAL_MODULE:
return -1;
return ((oparg & 1) ? 1 : 0) + 1;
case LOAD_GLOBAL_BUILTIN:
return -1;
return ((oparg & 1) ? 1 : 0) + 1;
case DELETE_FAST:
return 0;
case MAKE_CELL:
Expand Down Expand Up @@ -694,7 +694,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
}
#endif
enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC0, INSTR_FMT_IBC000, INSTR_FMT_IBC00000000, INSTR_FMT_IBIB, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC000 };
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC0, INSTR_FMT_IBC000, INSTR_FMT_IBC0000, INSTR_FMT_IBC00000000, INSTR_FMT_IBIB, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC000 };
struct opcode_metadata {
enum Direction dir_op1;
enum Direction dir_op2;
Expand Down Expand Up @@ -769,9 +769,9 @@ struct opcode_metadata {
[STORE_GLOBAL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[DELETE_GLOBAL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL_MODULE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL_BUILTIN] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[LOAD_GLOBAL_MODULE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[LOAD_GLOBAL_BUILTIN] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[DELETE_FAST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[MAKE_CELL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[DELETE_DEREF] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down