Skip to content

Commit ddd9599

Browse files
authored
GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL (GH-128708)
1 parent 29fe807 commit ddd9599

14 files changed

+121
-65
lines changed

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Known values:
265265
Python 3.14a4 3610 (Add VALUE_WITH_FAKE_GLOBALS format to annotationlib)
266266
Python 3.14a4 3611 (Add NOT_TAKEN instruction)
267267
Python 3.14a4 3612 (Add POP_ITER and INSTRUMENTED_POP_ITER)
268+
Python 3.14a4 3613 (Add LOAD_CONST_MORTAL instruction)
268269
269270
Python 3.15 will start with 3650
270271
@@ -277,7 +278,7 @@ PC/launcher.c must also be updated.
277278
278279
*/
279280

280-
#define PYC_MAGIC_NUMBER 3612
281+
#define PYC_MAGIC_NUMBER 3613
281282
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
282283
(little-endian) and then appending b'\r\n'. */
283284
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

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

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 0 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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode_ids.h

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

Lib/_opcode_metadata.py

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

Lib/test/test_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def loop_test():
892892
%3d RESUME_CHECK 0
893893
894894
%3d BUILD_LIST 0
895-
LOAD_CONST 0 ((1, 2, 3))
895+
LOAD_CONST_MORTAL 0 ((1, 2, 3))
896896
LIST_EXTEND 1
897897
LOAD_SMALL_INT 3
898898
BINARY_OP 5 (*)
@@ -2548,7 +2548,7 @@ def test_specialized_code(self):
25482548
expect = '''
25492549
0 RESUME 0
25502550
2551-
1 LOAD_CONST_IMMORTAL 0 (None)
2551+
1 LOAD_CONST 0 (None)
25522552
RETURN_VALUE
25532553
'''
25542554
for flag in ['-S', '--specialized']:

Python/bytecodes.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,25 @@ dummy_func(
285285
}
286286

287287
family(LOAD_CONST, 0) = {
288+
LOAD_CONST_MORTAL,
288289
LOAD_CONST_IMMORTAL,
289290
};
290291

291-
pure inst(LOAD_CONST, (-- value)) {
292-
value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));
292+
inst(LOAD_CONST, (-- value)) {
293+
/* We can't do this in the bytecode compiler as
294+
* marshalling can intern strings and make them immortal. */
295+
PyObject *obj = GETITEM(FRAME_CO_CONSTS, oparg);
296+
value = PyStackRef_FromPyObjectNew(obj);
297+
#if ENABLE_SPECIALIZATION
298+
if (this_instr->op.code == LOAD_CONST) {
299+
this_instr->op.code = _Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL;
300+
}
301+
#endif
302+
}
303+
304+
inst(LOAD_CONST_MORTAL, (-- value)) {
305+
PyObject *obj = GETITEM(FRAME_CO_CONSTS, oparg);
306+
value = PyStackRef_FromPyObjectNew(obj);
293307
}
294308

295309
inst(LOAD_CONST_IMMORTAL, (-- value)) {

Python/executor_cases.c.h

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

Python/generated_cases.c.h

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)