Skip to content
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
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

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

19 changes: 19 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3316,6 +3316,25 @@ def f(n):
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)

def test_cached_load_special(self):
class CM:
def __enter__(self):
return self
def __exit__(self, *args):
pass
def f(n):
cm = CM()
x = 0
for _ in range(n):
with cm:
x += 1
return x
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)
self.assertNotIn("_LOAD_SPECIAL", uops)

def test_store_fast_refcount_elimination(self):
def foo(x):
# Since x is known to be
Expand Down
1 change: 1 addition & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3887,6 +3887,7 @@ dummy_func(
}

macro(LOAD_SPECIAL) =
_RECORD_TOS_TYPE +
_INSERT_NULL +
_LOAD_SPECIAL;

Expand Down
27 changes: 25 additions & 2 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,31 @@ dummy_func(void) {
}

op(_LOAD_SPECIAL, (method_and_self[2] -- method_and_self[2])) {
method_and_self[0] = sym_new_not_null(ctx);
method_and_self[1] = sym_new_unknown(ctx);
Comment on lines -1685 to -1686
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You might want to leave these behind.
Some branches don't set method_and_self[1] and method_and_self[0], if you leave this. You can avoid all the complicated setting and else branches.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if I leave this then wouldn't the value of these change and then I can't use the recorded values?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh yeah woops

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is it necessary to set these on all paths? I think we only care about the optimized path

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You need to check LOAD_SPECIAL in bytecodes.c, if it creates a new value, it must be a new symbol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I refactored it a bit to set in all cases when it is not optimized.

bool optimized = false;
PyTypeObject *type = sym_get_probable_type(method_and_self[1]);
if (type != NULL) {
PyObject *name = _Py_SpecialMethods[oparg].name;
PyObject *descr = _PyType_Lookup(type, name);
if (descr != NULL && _PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR))
{
ADD_OP(_GUARD_TYPE_VERSION, 0, type->tp_version_tag);
bool immortal = _Py_IsImmortal(descr) || (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE);
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
0, (uintptr_t)descr);
ADD_OP(_SWAP, 3, 0);
ADD_OP(_POP_TOP, 0, 0);
if ((type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) == 0) {
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
_Py_BloomFilter_Add(dependencies, type);
}
method_and_self[0] = sym_new_const(ctx, descr);
optimized = true;
}
}
if (!optimized) {
method_and_self[0] = sym_new_not_null(ctx);
method_and_self[1] = sym_new_unknown(ctx);
}
}

op(_JUMP_TO_TOP, (--)) {
Expand Down
27 changes: 25 additions & 2 deletions Python/optimizer_cases.c.h

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

1 change: 1 addition & 0 deletions Python/record_functions.c.h

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

Loading