Skip to content

Commit 786f464

Browse files
gh-142961: Fix constant folding len(tuple) in JIT (GH-142963)
1 parent 049c252 commit 786f464

File tree

8 files changed

+931
-777
lines changed

8 files changed

+931
-777
lines changed

Include/internal/pycore_uop_ids.h

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

Include/internal/pycore_uop_metadata.h

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

Lib/test/test_capi/test_opt.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,18 @@ class Obj:
30083008
for _ in range(TIER2_THRESHOLD+1):
30093009
obj.attr = EvilAttr(obj.__dict__)
30103010

3011+
def test_constant_fold_tuple(self):
3012+
def testfunc(n):
3013+
for _ in range(n):
3014+
t = (1,)
3015+
p = len(t)
3016+
3017+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3018+
self.assertIsNotNone(ex)
3019+
uops = get_opnames(ex)
3020+
3021+
self.assertNotIn("_CALL_LEN", uops)
3022+
30113023
def test_binary_subscr_list_int(self):
30123024
def testfunc(n):
30133025
l = [1]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a segfault in the JIT when constant folding ``len(tuple)``.

Python/bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5285,6 +5285,13 @@ dummy_func(
52855285
value = PyStackRef_FromPyObjectBorrow(ptr);
52865286
}
52875287

5288+
tier2 op(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a, c)) {
5289+
res = PyStackRef_FromPyObjectBorrow(ptr);
5290+
a = arg;
5291+
c = callable;
5292+
INPUTS_DEAD();
5293+
}
5294+
52885295
tier2 op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
52895296
PyStackRef_CLOSE(pop2);
52905297
PyStackRef_CLOSE(pop1);

Python/executor_cases.c.h

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

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,6 @@ dummy_func(void) {
529529
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
530530
}
531531

532-
op(_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused -- value)) {
533-
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
534-
}
535-
536532
op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, unused, unused, unused, unused -- value)) {
537533
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
538534
}
@@ -1263,7 +1259,7 @@ dummy_func(void) {
12631259
goto error;
12641260
}
12651261
if (_Py_IsImmortal(temp)) {
1266-
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
1262+
REPLACE_OP(this_instr, _SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
12671263
0, (uintptr_t)temp);
12681264
}
12691265
res = sym_new_const(ctx, temp);

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)