Skip to content
Closed
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
34 changes: 34 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,40 @@ def testfunc(n):
self.assertNotIn("_POP_TOP_INT", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_strength_reduce_constant_load_fast(self):
# If we detect a _LOAD_FAST is actually loading an immortal constant,
# reduce that to a _LOAD_CONST_INLINE_BORROW which saves
# the read from locals.
def testfunc(n):
for _ in range(n):
x = 0
y = x

_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_LOAD_FAST_2", uops)

def test_strength_reduce_constant_load_fast_borrow(self):
# If we detect a _LOAD_FAST_BORROW is actually loading a constant,
# reduce that to a _LOAD_CONST_INLINE_BORROW which saves
# the read from locals.
def testfunc(n):
class A: pass
a = A()
for _ in range(n):
x = 0
a.x = x

_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_LOAD_FAST_BORROW_4", uops)

def test_143026(self):
# https://github.com/python/cpython/issues/143026

Expand Down
12 changes: 12 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,22 @@ dummy_func(void) {

op(_LOAD_FAST, (-- value)) {
value = GETLOCAL(oparg);
PyObject *const_val = sym_get_const(ctx, value);
if (const_val != NULL && _Py_IsImmortal(const_val)) {
// Note: non-immortal is not safe to replace
// to _LOAD_CONST_INLINE, as it might not be held in co_const.
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)const_val);
}
}

op(_LOAD_FAST_BORROW, (-- value)) {
value = PyJitRef_Borrow(GETLOCAL(oparg));
PyObject *const_val = sym_get_const(ctx, value);
if (const_val != NULL) {
// It's safe to always borrow here, because
// _LOAD_FAST_BORROW guarantees it.
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)const_val);
}
}

op(_LOAD_FAST_AND_CLEAR, (-- value)) {
Expand Down
8 changes: 8 additions & 0 deletions Python/optimizer_cases.c.h

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

Loading