Skip to content

Commit 4f2183b

Browse files
Fidget-Spinnercocolato
authored andcommitted
pythongh-142961: Fix constant folding len(tuple) in JIT (pythonGH-142963)
1 parent ca1dc21 commit 4f2183b

File tree

8 files changed

+932
-779
lines changed

8 files changed

+932
-779
lines changed

Include/internal/pycore_uop_ids.h

Lines changed: 775 additions & 771 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
@@ -3077,6 +3077,18 @@ class Obj:
30773077
for _ in range(TIER2_THRESHOLD+1):
30783078
obj.attr = EvilAttr(obj.__dict__)
30793079

3080+
def test_constant_fold_tuple(self):
3081+
def testfunc(n):
3082+
for _ in range(n):
3083+
t = (1,)
3084+
p = len(t)
3085+
3086+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3087+
self.assertIsNotNone(ex)
3088+
uops = get_opnames(ex)
3089+
3090+
self.assertNotIn("_CALL_LEN", uops)
3091+
30803092
def test_binary_subscr_list_int(self):
30813093
def testfunc(n):
30823094
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
@@ -5292,6 +5292,13 @@ dummy_func(
52925292
value = PyStackRef_FromPyObjectBorrow(ptr);
52935293
}
52945294

5295+
tier2 op(_SHUFFLE_3_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a, c)) {
5296+
res = PyStackRef_FromPyObjectBorrow(ptr);
5297+
a = arg;
5298+
c = callable;
5299+
INPUTS_DEAD();
5300+
}
5301+
52955302
tier2 op(_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, pop1, pop2 -- value)) {
52965303
PyStackRef_CLOSE(pop2);
52975304
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
}
@@ -1273,7 +1269,7 @@ dummy_func(void) {
12731269
goto error;
12741270
}
12751271
if (_Py_IsImmortal(temp)) {
1276-
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW,
1272+
REPLACE_OP(this_instr, _SHUFFLE_3_LOAD_CONST_INLINE_BORROW,
12771273
0, (uintptr_t)temp);
12781274
}
12791275
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)