8000 gh-90997: bpo-46841: Disassembly of quickened code by penguin-wwy · Pull Request #32099 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-90997: bpo-46841: Disassembly of quickened code #32099

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 16 commits into from
Apr 19, 2022
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
Prev Previous commit
Next Next commit
Get quickened in dis
  • Loading branch information
penguin-wwy committed Apr 6, 2022
commit e89653894a376bb68d2bf3c7c625ffaaaff7d096
2 changes: 1 addition & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ typedef struct {
/* We want to compare to zero for efficiency, so we offset values accordingly */
#define QUICKENING_INITIAL_WARMUP_VALUE (-QUICKENING_WARMUP_DELAY)

PyAPI_FUNC(void) _PyCode_Quicken(PyCodeObject *code);
void _PyCode_Quicken(PyCodeObject *code);

static inline void
_PyCode_Warmup(PyCodeObject *code)
Expand Down
103 changes: 72 additions & 31 deletions Lib/test/test_dis.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import types
import unittest
from test.support import captured_stdout, requires_debug_ranges, import_helper, cpython_only
from test.support import captured_stdout, requires_debug_ranges, cpython_only
from test.support.bytecode_helper import BytecodeTestCase

import opcode
Expand Down Expand Up @@ -583,7 +583,7 @@ def foo(x):
_h.__code__.co_firstlineno + 3,
)

def load_test(x, y):
def load_test(x, y=0):
a, b = x, y
return a, b

Expand All @@ -603,10 +603,37 @@ def load_test(x, y):
load_test.__code__.co_firstlineno + 1,
load_test.__code__.co_firstlineno + 2)

def binary_test(x, y):
a = x + y
b = x - y
return a + b
def loop_test():
for i in [1, 2, 3] * 3:
load_test(i)

dis_loop_test_quickened_code = """\
%3d 0 RESUME_QUICK 0

%3d 2 BUILD_LIST 0
4 LOAD_CONST 1 ((1, 2, 3))
6 LIST_EXTEND 1
8 LOAD_CONST 2 (3)
10 BINARY_OP_ADAPTIVE 5 (*)
14 GET_ITER
16 FOR_ITER 17 (to 52)
18 STORE_FAST 0 (i)

%3d 20 LOAD_GLOBAL_MODULE 1 (NULL + load_test)
32 LOAD_FAST 0 (i)
34 PRECALL_PYFUNC 1
38 CALL_PY_WITH_DEFAULTS 1
48 POP_TOP
50 JUMP_BACKWARD_QUICK 18 (to 16)

%3d >> 52 LOAD_CONST 0 (None)
54 RETURN_VALUE
""" % (loop_test.__code__.co_firstlineno,
loop_test.__code__.co_firstlineno + 1,
loop_test.__code__.co_firstlineno + 2,
loop_test.__code__.co_firstlineno + 1,)

QUICKENING_WARMUP_DELAY = 8

class DisTestBase(unittest.TestCase):
"Common utilities for DisTests and TestDisTraceback"
Expand Down Expand Up @@ -887,11 +914,14 @@ def check(expected, **kwargs):
check(dis_nested_2, depth=None)
check(dis_nested_2)

_testinternalcapi = import_helper.import_module('_testinternalcapi')
@staticmethod
def code_quicken(f, times=QUICKENING_WARMUP_DELAY):
for _ in range(times):
f()

@cpython_only
def test_super_instructions(self):
self._testinternalcapi.code_quicken(load_test.__code__)
self.code_quicken(lambda: load_test(0, 0))
got = self.get_disassembly(load_test, adaptive=True)
self.do_disassembly_compare(got, dis_load_test_quickened_code, True)

Expand All @@ -906,35 +936,46 @@ def test_binary_specialize(self):
10 RETURN_VALUE
"""
co_int = compile('a + b', "<int>", "eval")
self._testinternalcapi.code_quicken(co_int)
got = self.get_disassembly(co_int, adaptive=True)
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADAPTIVE 0 (+)", True)
exec(co_int, {}, {'a': 1, 'b': 2})
self.code_quicken(lambda: exec(co_int, {}, {'a': 1, 'b': 2}))
got = self.get_disassembly(co_int, adaptive=True)
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_INT 0 (+)", True)

co_unicode = compile('a + b', "<unicode>", "eval")
self._testinternalcapi.code_quicken(co_unicode)
exec(co_unicode, {}, {'a': 'a', 'b': 'b'})
self.code_quicken(lambda: exec(co_unicode, {}, {'a': 'a', 'b': 'b'}))
got = self.get_disassembly(co_unicode, adaptive=True)
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE 0 (+)", True)

binary_subsrc_quicken = """\
0 RESUME_QUICK 0

1 2 LOAD_NAME 0 (a)
4 LOAD_CONST 0 (0)
6 %s
16 RETURN_VALUE
"""
co_list = compile('a[0]', "<list>", "eval")
self.code_quicken(lambda: exec(co_list, {}, {'a': [0]}))
got = self.get_disassembly(co_list, adaptive=True)
self.do_disassembly_compare(got, binary_subsrc_quicken % "BINARY_SUBSCR_LIST_INT", True)

co_dict = compile('a[0]', "<dict>", "eval")
self.code_quicken(lambda: exec(co_dict, {}, {'a': {0: '1'}}))
got = self.get_disassembly(co_dict, adaptive=True)
self.do_disassembly_compare(got, binary_subsrc_quicken % "BINARY_SUBSCR_DICT", True)

@cpython_only
def test_load_attr_specialize(self):
load_attr_quicken = """\
0 RESUME_QUICK 0

1 2 LOAD_CONST 0 ('a')
4 %s
4 LOAD_ATTR_SLOT 0 (__class__)
14 RETURN_VALUE
"""
co = compile("'a'.__class__", "", "eval")
self._testinternalcapi.code_quicken(co)
self.code_quicken(lambda: exec(co, {}, {}))
got = self.get_disassembly(co, adaptive=True)
self.do_disassembly_compare(got, load_attr_quicken % "LOAD_ATTR_ADAPTIVE 0 (__class__)", True)
exec(co, {}, {})
got = self.get_disassembly(co, adaptive=True)
self.do_disassembly_compare(got, load_attr_quicken % "LOAD_ATTR_SLOT 0 (__class__)", True)
self.do_disassembly_compare(got, load_attr_quicken, True)

@cpython_only
def test_call_specialize(self):
Expand All @@ -944,21 +985,21 @@ def test_call_specialize(self):
1 2 PUSH_NULL
4 LOAD_NAME 0 (str)
6 LOAD_CONST 0 (1)
8 %s
12 %s
8 PRECALL_NO_KW_STR_1 1
12 CALL_ADAPTIVE 1
22 RETURN_VALUE
"""
co = compile("str(1)", "", "eval")
self._testinternalcapi.code_quicken(co)
got = self.get_disassembly(co, adaptive=True)
self.do_disassembly_compare(got, call_quicken % (
"PRECALL_ADAPTIVE 1",
"CALL_ADAPTIVE 1"), True)
exec(co, {}, {})
self.code_quicken(lambda: exec(co, {}, {}))
got = self.get_disassembly(co, adaptive=True)
self.do_disassembly_compare(got, call_quicken % (
"PRECALL_NO_KW_STR_1 1",
"CALL_ADAPTIVE 1"), True)
self.do_disassembly_compare(got, call_quicken, True)

@cpython_only
def test_loop_quicken(self):
# Loop can trigger a quicken where the loop is located
self.code_quicken(loop_test, 1)
got = self.get_disassembly(loop_test, adaptive=True)
self.do_disassembly_compare(got, dis_loop_test_quickened_code, True)


class DisWithFileTests(DisTests):
Expand Down
15 changes: 0 additions & 15 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Python.h"
#include "pycore_atomic_funcs.h" // _Py_atomic_int_get()
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_code.h" // _PyCode_Quicken
#include "pycore_fileutils.h" // _Py_normpath
#include "pycore_gc.h" // PyGC_Head
#include "pycore_hashtable.h" // _Py_hashtable_new()
Expand Down Expand Up @@ -492,19 +491,6 @@ decode_locale_ex(PyObject *self, PyObject *args)
return res;
}

static PyObject *
code_quicken(PyObject *self, PyObject *code)
{
if (!PyCode_Check(code)) {
PyErr_SetString(PyExc_TypeError, "argument must be a code object");
return NULL;
}
PyCodeObject *co = (PyCodeObject *) code;
co->co_warmup = 0;
_PyCode_Quicken(co);
Py_RETURN_NONE;
}


static PyMethodDef TestMethods[] = {
{"get_configs", get_configs, METH_NOARGS},
Expand All @@ -522,7 +508,6 @@ static PyMethodDef TestMethods[] = {
{"get_getpath_codeobject", get_getpath_codeobject, METH_NOARGS, NULL},
{"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
{"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
{"code_quicken", code_quicken, METH_O, NULL},
{NULL, NULL} /* sentinel */
};

Expand Down
0