Skip to content

Commit 844cc42

Browse files
initialize frame values to NULL when args are known
1 parent aad2f80 commit 844cc42

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,8 +2557,30 @@ def testfunc(n):
25572557
self.assertIsNotNone(ex)
25582558
uops = get_opnames(ex)
25592559
self.assertIn("_POP_TOP_NOP", uops)
2560+
self.assertIn("_PUSH_FRAME", uops)
2561+
self.assertLessEqual(len(matching_opnames(ex, "_POP_TOP")), 1)
2562+
2563+
def test_store_fast_refcount_elimination_when_uninitialized(self):
2564+
def foo():
2565+
# Since y is known to be
2566+
# uninitialized (NULL) here,
2567+
# The refcount is eliminated in the STORE_FAST.
2568+
y = 2
2569+
return y
2570+
def testfunc(n):
2571+
# The STORE_FAST for the range here needs a POP_TOP
2572+
# (for now, until we do loop peeling).
2573+
for _ in range(n):
2574+
foo()
2575+
2576+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2577+
self.assertIsNotNone(ex)
2578+
uops = get_opnames(ex)
2579+
self.assertIn("_POP_TOP_NOP", uops)
2580+
self.assertIn("_PUSH_FRAME", uops)
25602581
self.assertLessEqual(len(matching_opnames(ex, "_POP_TOP")), 1)
25612582

2583+
25622584
def test_float_op_refcount_elimination(self):
25632585
def testfunc(args):
25642586
a, b, n = args

Python/optimizer_symbols.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,12 @@ _Py_uop_frame_new(
846846
frame->locals[i] = args[i];
847847
}
848848

849+
// If the args are known, then it's safe to just initialize
850+
// every other non-set local to null symbol.
851+
bool default_null = args != NULL;
852+
849853
for (int i = arg_len; i < co->co_nlocalsplus; i++) {
850-
JitOptRef local = _Py_uop_sym_new_unknown(ctx);
854+
JitOptRef local = default_null ? _Py_uop_sym_new_null(ctx) : _Py_uop_sym_new_unknown(ctx);
851855
frame->locals[i] = local;
852856
}
853857

0 commit comments

Comments
 (0)