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
18 changes: 18 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3457,6 +3457,24 @@ def testfunc(n):
# _POP_TOP_NOP is a sign the optimizer ran and didn't hit bottom.
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 1)

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
10 changes: 10 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ dummy_func(void) {

op(_LOAD_FAST_BORROW, (-- value)) {
value = PyJitRef_Borrow(GETLOCAL(oparg));
PyObject *const_val = sym_get_const(ctx, value);
if (const_val != NULL) {
// If we know we're loading a constant, convert
// to a _LOAD_CONST_INLINE_BORROW to save a memory load.
// It's safe to always borrow here, because
// JIT constants only come from _LOAD_CONST
// (which holds a strong reference), or from
// constant-folded _JIT values, which are immortal.
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)const_val);
}
}

op(_LOAD_FAST_AND_CLEAR, (-- value)) {
Expand Down
4 changes: 4 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