8000 GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONS… · python/cpython@ddd9599 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
8000 %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.

Python/opcode_targets.h

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

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,13 @@ dummy_func(void) {
479479
value = sym_new_const(ctx, val);
480480
}
481481

482+
op(_LOAD_CONST_MORTAL, (-- value)) {
483+
PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
484+
int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
485+
REPLACE_OP(this_instr, opcode, 0, (uintptr_t)val);
486+
value = sym_new_const(ctx, val);
487+
}
488+
482489
op(_LOAD_CONST_IMMORTAL, (-- value)) {
483490
PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
484491
REPLACE_OP(this_instr, _LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)val);

Python/optimizer_cases.c.h

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

Python/specialize.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,15 +478,6 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, PyObject *consts,
478478
}
479479
i += caches;
480480
}
481-
else if (opcode == LOAD_CONST) {
482-
/* We can't do this in the bytecode compiler as
483-
* marshalling can intern strings and make them immortal. */
484-
485-
PyObject *obj = PyTuple_GET_ITEM(consts, oparg);
486-
if (_Py_IsImmortal(obj)) {
487-
instructions[i].op.code = LOAD_CONST_IMMORTAL;
488-
}
489-
}
490481
if (opcode != EXTENDED_ARG) {
491482
oparg = 0;
492483
}

0 commit comments

Comments
 (0)
0