Skip to content

Commit b343a84

Browse files
block anything with free variables
1 parent 0c48c45 commit b343a84

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,7 @@ def testfunc(n):
31153115
self.assertIn("_POP_TOP_NOP", uops)
31163116

31173117
def test_strength_reduce_constant_load_fast(self):
3118-
# If we detect a _LOAD_FAST is actually loading an immortal constant,
3118+
# If we detect a _LOAD_FAST is actually loading a constant,
31193119
# reduce that to a _LOAD_CONST_INLINE_BORROW which saves
31203120
# the read from locals.
31213121
def testfunc(n):

Python/optimizer_bytecodes.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,23 @@ dummy_func(void) {
8888
op(_LOAD_FAST, (-- value)) {
8989
value = GETLOCAL(oparg);
9090
PyObject *const_val = sym_get_const(ctx, value);
91-
if (const_val != NULL && _Py_IsImmortal(const_val)) {
92-
// Note: non-immortal is not safe to replace
93-
// to _LOAD_CONST_INLINE, as it might not be held in co_const.
91+
PyCodeObject *co = get_current_code_object(ctx);
92+
// We don't reason about free variables yet, so we need to forbid
93+
// anything with those.
94+
if (const_val != NULL && co->co_nfreevars == 0) {
95+
// It's safe to always borrow here, for
96+
// the same reason as _LOAD_CONST.
9497
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)const_val);
9598
}
9699
}
97100

98101
op(_LOAD_FAST_BORROW, (-- value)) {
99102
value = PyJitRef_Borrow(GETLOCAL(oparg));
100103
PyObject *const_val = sym_get_const(ctx, value);
101-
if (const_val != NULL) {
104+
PyCodeObject *co = get_current_code_object(ctx);
105+
// We don't reason about free variables yet, so we need to forbid
106+
// anything with those.
107+
if (const_val != NULL && co->co_nfreevars == 0) {
102108
// It's safe to always borrow here, because
103109
// _LOAD_FAST_BORROW guarantees it.
104110
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)const_val);

Python/optimizer_cases.c.h

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)