8000 gh-134584: Specialize POP_TOP by reference and type in JIT by Fidget-Spinner · Pull Request #135761 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134584: Specialize POP_TOP 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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 10000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

16 changes: 16 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

40 changes: 40 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,46 @@ def testfunc(n):
assert ex is not None
"""))

def test_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 test_pop_top_specialize_int(self):
def testfunc(n):
for _ in range(n):
global_identity(100000)

testfunc(TIER2_THRESHOLD)

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

self.assertIn("_POP_TOP_INT", uops)

def test_pop_top_specialize_float(self):
def testfunc(n):
for _ in range(n):
global_identity(1e6)

testfunc(TIER2_THRESHOLD)

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

self.assertIn("_POP_TOP_FLOAT", uops)



def global_identity(x):
return x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Specialize :opcode:`POP_TOP` in the JIT compiler by specializing for reference lifetime and type. This will also enable easier top of stack caching in the JIT compiler.
21 changes: 21 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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
40 changes: 40 additions & 0 deletions Python/executor_cases.c.h

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

2 changes: 1 addition & 1 deletion Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
#define sym_new_tuple _Py_uop_sym_new_tuple
#define sym_tuple_getitem _Py_uop_sym_tuple_getitem
#define sym_tuple_length _Py_uop_sym_tuple_length
#define sym_is_immortal _Py_uop_sym_is_immortal
#define sym_is_immortal _Py_uop_symbol_is_immortal
#define sym_is_compact_int _Py_uop_sym_is_compact_int
#define sym_new_compact_int _Py_uop_sym_new_compact_int
#define sym_new_truthiness _Py_uop_sym_new_truthiness
Expand Down
28 changes: 24 additions & 4 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
#define sym_new_tuple _Py_uop_sym_new_tuple
#define sym_tuple_getitem _Py_uop_sym_tuple_getitem
#define sym_tuple_length _Py_uop_sym_tuple_length
#define sym_is_immortal _Py_uop_sym_is_immortal
#define sym_is_immortal _Py_uop_symbol_is_immortal
#define sym_new_compact_int _Py_uop_sym_new_compact_int
#define sym_is_compact_int _Py_uop_sym_is_compact_int
#define sym_new_truthiness _Py_uop_sym_new_truthiness
Expand Down Expand Up @@ -534,15 +534,15 @@ dummy_func(void) {
}

op(_LOAD_CONST_INLINE, (ptr/4 -- value)) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
value = sym_new_const(ctx, ptr);
}

op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}

op(_POP_TOP_LOAD_CONST_INLINE, (ptr/4, pop -- value)) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
value = sym_new_const(ctx, ptr);
}

op(_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
Expand All @@ -561,6 +561,24 @@ dummy_func(void) {
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
}

op(_POP_TOP, (value -- )) {
PyTypeObject *typ = sym_get_type(value);
if (PyJitRef_IsBorrowed(value) ||
sym_is_immortal(PyJitRef_Unwrap(value)) ||
sym_is_null(value)) {
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
}
else if (typ == &PyLong_Type) {
REPLACE_OP(this_instr, _POP_TOP_INT, 0, 0);
}
else if (typ == &PyFloat_Type) {
REPLACE_OP(this_instr, _POP_TOP_FLOAT, 0, 0);
}
else if (typ == &PyUnicode_Type) {
REPLACE_OP(this_instr, _POP_TOP_UNICODE, 0, 0);
}
}

op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
assert(oparg > 0);
top = bottom;
Expand Down Expand Up @@ -803,7 +821,9 @@ dummy_func(void) {
}

op(_RETURN_VALUE, (retval -- res)) {
JitOptRef temp = retval;
// We wrap and unwrap the value to mimic PyStackRef_MakeHeapSafe
// in bytecodes.c
JitOptRef temp = PyJitRef_Wrap(PyJitRef_Unwrap(retval));
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a bug in the previous implementation. This was only exposed in this PR.

DEAD(retval);
SAVE_STACK();
ctx->frame->stack_pointer = stack_pointer;
Expand Down
Loading
Loading
0