Skip to content

gh-134584: Specialize STORE_FAST by reference and type in JIT #135761

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Include/internal/pycore_opcode_metadata.h

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

90 changes: 47 additions & 43 deletions Include/internal/pycore_uop_ids.h

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

72 changes: 44 additions & 28 deletions Include/internal/pycore_uop_metadata.h

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

73 changes: 73 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,79 @@ def testfunc(n):
assert ex is not None
"""))

def test_store_fast_pop_top_specialize_immortal(self):
def testfunc(n):
for _ in range(n):
x = None # _POP_TOP, as x's type is not yet known by optimizer.
x = None # _POP_TOP_NOP, as x = None

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_NOP", uops)

def test_store_fast_pop_top_specialize_int(self):
def testfunc(n):
y = int(1e6) # Big number so no int caching
for _ in range(n):
x = y + y # _POP_TOP, as x's type is not yet known by optimizer.
x = None # _POP_TOP_INT, as x = int

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_INT", uops)

def test_store_fast_pop_top_specialize_float(self):
def testfunc(n):
y = 1.0
for _ in range(n):
x = y + y # _POP_TOP, as x's type is not yet known by optimizer.
x = None # _POP_TOP_FLOAT, as x = int

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_FLOAT", uops)

def test_store_fast_pop_top_specialize_unicode(self):
def testfunc(n):
y = "hi"
for _ in range(n):
x = y + y # _POP_TOP, as x's type is not yet known by optimizer.
x = None # _POP_TOP_STR, as x = int

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_UNICODE", uops)

def test_store_pop_top_specialize_none(self):
def testfunc(n):
for _ in range(n):
global_identity(None)

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_NOP", uops)



def global_identity(x):
return x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Specialize ``POP_TOP`` and thus ``STORE_FAST`` in the JIT compiler by specializing for reference lifetime and type. This will also enable easier top of stack caching in the JIT compiler.
28 changes: 25 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,14 @@ dummy_func(
value = PyStackRef_FromPyObjectBorrow(obj);
}

replicate(8) inst(STORE_FAST, (value --)) {
_PyStackRef tmp = GETLOCAL(oparg);
replicate(8) op(_SWAP_FAST, (value -- trash)) {
trash = GETLOCAL(oparg);
GETLOCAL(oparg) = value;
DEAD(value);
PyStackRef_XCLOSE(tmp);
}

macro(STORE_FAST) = _SWAP_FAST + POP_TOP;

pseudo(STORE_FAST_MAYBE_NULL, (unused --)) = {
STORE_FAST,
};
Expand Down Expand Up @@ -344,6 +345,27 @@ dummy_func(
PyStackRef_XCLOSE(value);
}

op(_POP_TOP_NOP, (value --)) {
assert(PyStackRef_IsNull(value) || (!PyStackRef_RefcountOnObject(value)) ||
_Py_IsImmortal((PyStackRef_AsPyObjectBorrow(value))));
DEAD(value);
}

op(_POP_TOP_INT, (value --)) {
assert(PyLong_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
PyStackRef_CLOSE_SPECIALIZED(value, _PyLong_ExactDealloc);
}

op(_POP_TOP_FLOAT, (value --)) {
assert(PyFloat_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
PyStackRef_CLOSE_SPECIALIZED(value, _PyFloat_ExactDealloc);
}

op(_POP_TOP_UNICODE, (value --)) {
assert(PyUnicode_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
PyStackRef_CLOSE_SPECIALIZED(value, _PyUnicode_ExactDealloc);
}

tier2 op(_POP_TWO, (nos, tos --)) {
PyStackRef_CLOSE(tos);
PyStackRef_CLOSE(nos);
Expand Down
Loading
Loading