Assorted minor fixes for specialization stats. (GH-100219)

This commit is contained in:
Mark Shannon 2022-12-14 15:50:02 +00:00 committed by GitHub
parent 3192c00a3c
commit 5693f45b19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 27 deletions

View file

@ -307,6 +307,7 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_NOT_PY_FUNCTION 7 #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 #define SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT 18
/* Attributes */ /* Attributes */
@ -332,6 +333,8 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE 26 #define SPEC_FAIL_ATTR_INSTANCE_ATTRIBUTE 26
#define SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE 27 #define SPEC_FAIL_ATTR_METACLASS_ATTRIBUTE 27
#define SPEC_FAIL_ATTR_PROPERTY_NOT_PY_FUNCTION 28 #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 */ /* Binary subscr and store subscr */
@ -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 uint32_t function_get_version(PyObject *o, int opcode);
static int static int
specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr, specialize_module_load_attr(
PyObject *name, int opcode, int opcode_module) PyObject *owner, _Py_CODEUNIT *instr, PyObject *name
{ ) {
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1); _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
PyModuleObject *m = (PyModuleObject *)owner; PyModuleObject *m = (PyModuleObject *)owner;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict; PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) { if (dict == NULL) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_NO_DICT); SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
return -1; return -1;
} }
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) { 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; return -1;
} }
Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__)); Py_ssize_t index = _PyDict_LookupIndex(dict, &_Py_ID(__getattr__));
assert(index != DKIX_ERROR); assert(index != DKIX_ERROR);
if (index != DKIX_EMPTY) { 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; return -1;
} }
index = _PyDict_LookupIndex(dict, name); index = _PyDict_LookupIndex(dict, name);
assert (index != DKIX_ERROR); assert (index != DKIX_ERROR);
if (index != (uint16_t)index) { 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; return -1;
} }
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(dict->ma_keys); uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(dict->ma_keys);
if (keys_version == 0) { if (keys_version == 0) {
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS); SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
return -1; return -1;
} }
write_u32(cache->version, keys_version); write_u32(cache->version, keys_version);
cache->index = (uint16_t)index; cache->index = (uint16_t)index;
_py_set_opocde(instr, opcode_module); _py_set_opocde(instr, LOAD_ATTR_MODULE);
return 0; return 0;
} }
@ -629,7 +635,10 @@ specialize_dict_access(
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name); Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
assert (index != DKIX_ERROR); assert (index != DKIX_ERROR);
if (index != (uint16_t)index) { 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; return 0;
} }
write_u32(cache->version, type->tp_version_tag); write_u32(cache->version, type->tp_version_tag);
@ -646,7 +655,10 @@ specialize_dict_access(
Py_ssize_t index = Py_ssize_t index =
_PyDict_LookupIndex(dict, name); _PyDict_LookupIndex(dict, name);
if (index != (uint16_t)index) { 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; return 0;
} }
cache->index = (uint16_t)index; cache->index = (uint16_t)index;
@ -674,8 +686,7 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
goto fail; goto fail;
} }
if (PyModule_CheckExact(owner)) { if (PyModule_CheckExact(owner)) {
if (specialize_module_load_attr(owner, instr, name, LOAD_ATTR, if (specialize_module_load_attr(owner, instr, name))
LOAD_ATTR_MODULE))
{ {
goto fail; goto fail;
} }
@ -702,7 +713,9 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
goto success; goto success;
} }
} }
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD); else {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_METHOD);
}
goto fail; goto fail;
} }
case PROPERTY: case PROPERTY:
@ -1076,7 +1089,6 @@ PyObject *descr, DescriptorClassification kind)
*/ */
write_u32(cache->type_version, owner_cls->tp_version_tag); write_u32(cache->type_version, owner_cls->tp_version_tag);
write_obj(cache->descr, descr); write_obj(cache->descr, descr);
// Fall through.
return 1; return 1;
fail: fail:
return 0; return 0;
@ -1092,6 +1104,7 @@ _Py_Specialize_LoadGlobal(
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1); _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
assert(PyUnicode_CheckExact(name)); assert(PyUnicode_CheckExact(name));
if (!PyDict_CheckExact(globals)) { if (!PyDict_CheckExact(globals)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail; goto fail;
} }
PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys; PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys;
@ -1101,15 +1114,17 @@ _Py_Specialize_LoadGlobal(
} }
Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name); Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name);
if (index == DKIX_ERROR) { 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; goto fail;
} }
if (index != DKIX_EMPTY) { if (index != DKIX_EMPTY) {
if (index != (uint16_t)index) { if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail; goto fail;
} }
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(globals_keys); uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);
if (keys_version == 0) { if (keys_version == 0) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail; goto fail;
} }
cache->index = (uint16_t)index; cache->index = (uint16_t)index;
@ -1118,6 +1133,7 @@ _Py_Specialize_LoadGlobal(
goto success; goto success;
} }
if (!PyDict_CheckExact(builtins)) { if (!PyDict_CheckExact(builtins)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail; goto fail;
} }
PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys; PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys;
@ -1127,10 +1143,11 @@ _Py_Specialize_LoadGlobal(
} }
index = _PyDictKeys_StringLookup(builtin_keys, name); index = _PyDictKeys_StringLookup(builtin_keys, name);
if (index == DKIX_ERROR) { 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; goto fail;
} }
if (index != (uint16_t)index) { if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail; goto fail;
} }
uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(globals_keys); uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);

View file

@ -32,7 +32,7 @@ for name in opcode.opname[1:]:
opmap = {name: i for i, name in enumerate(opname)} opmap = {name: i for i, name in enumerate(opname)}
opmap = dict(sorted(opmap.items())) 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): def format_ratio(num, den):
""" """
@ -90,7 +90,7 @@ def calculate_specialization_stats(family_stats, total):
if key in ("specialization.hit", "specialization.miss"): if key in ("specialization.hit", "specialization.miss"):
label = key[len("specialization."):] label = key[len("specialization."):]
elif key == "execution_count": elif key == "execution_count":
label = "unquickened" continue
elif key in ("specialization.success", "specialization.failure", "specializable"): elif key in ("specialization.success", "specialization.failure", "specializable"):
continue continue
elif key.startswith("pair"): elif key.startswith("pair"):
@ -115,7 +115,7 @@ def calculate_specialization_success_failure(family_stats):
def calculate_specialization_failure_kinds(name, family_stats, defines): def calculate_specialization_failure_kinds(name, family_stats, defines):
total_failures = family_stats.get("specialization.failure", 0) total_failures = family_stats.get("specialization.failure", 0)
failure_kinds = [ 0 ] * 30 failure_kinds = [ 0 ] * 40
for key in family_stats: for key in family_stats:
if not key.startswith("specialization.failure_kind"): if not key.startswith("specialization.failure_kind"):
continue continue
@ -224,7 +224,7 @@ def pretty(defname):
return defname.replace("_", " ").lower() return defname.replace("_", " ").lower()
def kind_to_text(kind, defines, opname): def kind_to_text(kind, defines, opname):
if kind < 7: if kind <= 7:
return pretty(defines[kind][0]) return pretty(defines[kind][0])
if opname.endswith("ATTR"): if opname.endswith("ATTR"):
opname = "ATTR" opname = "ATTR"
@ -241,10 +241,7 @@ def categorized_counts(opcode_stats):
not_specialized = 0 not_specialized = 0
specialized_instructions = { specialized_instructions = {
op for op in opcode._specialized_instructions op for op in opcode._specialized_instructions
if "__" not in op and "ADAPTIVE" not in op} if "__" not in op}
adaptive_instructions = {
op for op in opcode._specialized_instructions
if "ADAPTIVE" in op}
for i, opcode_stat in enumerate(opcode_stats): for i, opcode_stat in enumerate(opcode_stats):
if "execution_count" not in opcode_stat: if "execution_count" not in opcode_stat:
continue continue
@ -252,8 +249,6 @@ def categorized_counts(opcode_stats):
name = opname[i] name = opname[i]
if "specializable" in opcode_stat: if "specializable" in opcode_stat:
not_specialized += count not_specialized += count
elif name in adaptive_instructions:
not_specialized += count
elif name in specialized_instructions: elif name in specialized_instructions:
miss = opcode_stat.get("specialization.miss", 0) miss = opcode_stat.get("specialization.miss", 0)
not_specialized += miss not_specialized += miss