FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131798: constant fold special method lookup in JIT by kumaraditya303 · Pull Request #148432 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (2) .h  (3) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,7 @@ dummy_func(
}

macro(LOAD_SPECIAL) =
_RECORD_TOS_TYPE +
_INSERT_NULL +
_LOAD_SPECIAL;

Expand Down
26 changes: 24 additions & 2 deletions Python/optimizer_bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1824,8 +1824,30 @@ 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 thread
kumaraditya303 marked this conversation as resolved.
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 && (Py_TYPE(descr)->tp_flags & 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
26 changes: 24 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

Back | FazBrowse Home | New Git URL