Skip to content
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

Assorted minor fixes for specialization stats. #100219

Merged
merged 4 commits into from
Dec 14, 2022
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
51 changes: 34 additions & 17 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_NOT_PY_FUNCTION 7


#define SPEC_FAIL_LOAD_GLOBAL_NON_DICT 17
#define SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT 18

/* Attributes */
Expand All @@ -332,6 +333,8 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE 26
#define SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE 27
#define SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION 28
#define SPEC_FAIL_ATTR_NOT_IN_KEYS 29
#define SPEC_FAIL_ATTR_NOT_IN_DICT 30

/* Binary subscr and store subscr */

Expand Down Expand Up @@ -448,41 +451,44 @@ static bool function_check_args(PyObject *o, int expected_argcount, int opcode);
static uint32_t function_get_version(PyObject *o, int opcode);

static int
specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
PyObject *name, int opcode, int opcode_module)
{
specialize_module_load_attr(
PyObject *owner, _Py_CODEUNIT *instr, PyObject *name
) {
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
PyModuleObject *m = (PyModuleObject *)owner;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_NO_DICT);
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
return -1;
}
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
return -1;
}
Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
assert(index != DKIX_ERROR);
if (index != DKIX_EMPTY) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND);
return -1;
}
index = _PyDict_LookupIndex(dict, name);
assert (index != DKIX_ERROR);
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_RANGE);
SPECIALIZATION_FAIL(LOAD_ATTR,
index == DKIX_EMPTY ?
SPEC_FAIL_ATTR_MODULE_ATTR_NOT_FOUND :
SPEC_FAIL_OUT_OF_RANGE);
return -1;
}
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(dict->ma_keys);
if (keys_version == 0) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
return -1;
}
write_u32(cache->version, keys_version);
cache->index = (uint16_t)index;
_py_set_opocde(instr, opcode_module);
_py_set_opocde(instr, LOAD_ATTR_MODULE);
return 0;
}

Expand Down Expand Up @@ -629,7 +635,10 @@ specialize_dict_access(
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
assert (index != DKIX_ERROR);
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
SPECIALIZATION_FAIL(base_op,
index == DKIX_EMPTY ?
SPEC_FAIL_ATTR_NOT_IN_KEYS :
SPEC_FAIL_OUT_OF_RANGE);
return 0;
}
write_u32(cache->version, type->tp_version_tag);
Expand All @@ -646,7 +655,10 @@ specialize_dict_access(
Py_ssize_t index =
_PyDict_LookupIndex(dict, name);
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_OUT_OF_RANGE);
SPECIALIZATION_FAIL(base_op,
index == DKIX_EMPTY ?
SPEC_FAIL_ATTR_NOT_IN_DICT :
SPEC_FAIL_OUT_OF_RANGE);
return 0;
}
cache->index = (uint16_t)index;
Expand Down Expand Up @@ -674,8 +686,7 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
goto fail;
}
if (PyModule_CheckExact(owner)) {
if (specialize_module_load_attr(owner, instr, name, LOAD_ATTR,
LOAD_ATTR_MODULE))
if (specialize_module_load_attr(owner, instr, name))
{
goto fail;
}
Expand All @@ -702,7 +713,9 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
goto success;
}
}
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
else {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
}
goto fail;
}
case PROPERTY:
Expand Down Expand Up @@ -1076,7 +1089,6 @@ PyObject *descr, DescriptorClassification kind)
*/
write_u32(cache->type_version, owner_cls->tp_version_tag);
write_obj(cache->descr, descr);
// Fall through.
return 1;
fail:
return 0;
Expand All @@ -1092,6 +1104,7 @@ _Py_Specialize_LoadGlobal(
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
assert(PyUnicode_CheckExact(name));
if (!PyDict_CheckExact(globals)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail;
}
PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys;
Expand All @@ -1101,15 +1114,17 @@ _Py_Specialize_LoadGlobal(
}
Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name);
if (index == DKIX_ERROR) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
goto fail;
}
if (index != DKIX_EMPTY) {
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
}
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);
if (keys_version == 0) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
}
cache->index = (uint16_t)index;
Expand All @@ -1118,6 +1133,7 @@ _Py_Specialize_LoadGlobal(
goto success;
}
if (!PyDict_CheckExact(builtins)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail;
}
PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys;
Expand All @@ -1127,10 +1143,11 @@ _Py_Specialize_LoadGlobal(
}
index = _PyDictKeys_StringLookup(builtin_keys, name);
if (index == DKIX_ERROR) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
goto fail;
}
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
}
uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);
Expand Down
15 changes: 5 additions & 10 deletions Tools/scripts/summarize_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
opmap = {name: i for i, name in enumerate(opname)}
opmap = dict(sorted(opmap.items()))

TOTAL = "specialization.deferred", "specialization.hit", "specialization.miss", "execution_count"
TOTAL = "specialization.hit", "specialization.miss", "execution_count"

def format_ratio(num, den):
"""
Expand Down Expand Up @@ -90,7 +90,7 @@ def calculate_specialization_stats(family_stats, total):
if key in ("specialization.hit", "specialization.miss"):
label = key[len("specialization."):]
elif key == "execution_count":
label = "unquickened"
continue
elif key in ("specialization.success", "specialization.failure", "specializable"):
continue
elif key.startswith("pair"):
Expand All @@ -115,7 +115,7 @@ def calculate_specialization_success_failure(family_stats):

def calculate_specialization_failure_kinds(name, family_stats, defines):
total_failures = family_stats.get("specialization.failure", 0)
failure_kinds = [ 0 ] * 30
failure_kinds = [ 0 ] * 40
for key in family_stats:
if not key.startswith("specialization.failure_kind"):
continue
Expand Down Expand Up @@ -224,7 +224,7 @@ def pretty(defname):
return defname.replace("_", " ").lower()

def kind_to_text(kind, defines, opname):
if kind < 7:
if kind <= 7:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably deal with these numeric constants (this 7 here, and the 40 above) in some more flexible way, but that can be left as a follow-on PR.

return pretty(defines[kind][0])
if opname.endswith("ATTR"):
opname = "ATTR"
Expand All @@ -241,19 +241,14 @@ def categorized_counts(opcode_stats):
not_specialized = 0
specialized_instructions = {
op for op in opcode._specialized_instructions
if "__" not in op and "ADAPTIVE" not in op}
adaptive_instructions = {
op for op in opcode._specialized_instructions
if "ADAPTIVE" in op}
if "__" not in op}
for i, opcode_stat in enumerate(opcode_stats):
if "execution_count" not in opcode_stat:
continue
count = opcode_stat['execution_count']
name = opname[i]
if "specializable" in opcode_stat:
not_specialized += count
elif name in adaptive_instructions:
not_specialized += count
elif name in specialized_instructions:
miss = opcode_stat.get("specialization.miss", 0)
not_specialized += miss
Expand Down