Skip to content

Commit c8521ad

Browse files
committed
gh-148211: decompose _INSERT_1_LOAD_CONST_INLINE(_BORROW) in JIT
1 parent 6e08161 commit c8521ad

File tree

9 files changed

+1149
-1329
lines changed

9 files changed

+1149
-1329
lines changed

Include/internal/pycore_uop_ids.h

Lines changed: 1096 additions & 1104 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: 0 additions & 38 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3211,7 +3211,8 @@ def f(n):
32113211
uops = get_opnames(ex)
32123212
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
32133213
self.assertNotIn("_INSERT_1_LOAD_CONST_INLINE", uops)
3214-
self.assertIn("_INSERT_1_LOAD_CONST_INLINE_BORROW", uops)
3214+
self.assertNotIn("_INSERT_1_LOAD_CONST_INLINE_BORROW", uops)
3215+
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
32153216

32163217
def test_store_fast_refcount_elimination(self):
32173218
def foo(x):

Python/bytecodes.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5929,18 +5929,6 @@ dummy_func(
59295929
value = PyStackRef_FromPyObjectBorrow(ptr);
59305930
}
59315931

5932-
tier2 op(_INSERT_1_LOAD_CONST_INLINE, (ptr/4, left -- res, l)) {
5933-
res = PyStackRef_FromPyObjectNew(ptr);
5934-
l = left;
5935-
INPUTS_DEAD();
5936-
}
5937-
5938-
tier2 op(_INSERT_1_LOAD_CONST_INLINE_BORROW, (ptr/4, left -- res, l)) {
5939-
res = PyStackRef_FromPyObjectBorrow(ptr);
5940-
l = left;
5941-
INPUTS_DEAD();
5942-
}
5943-
59445932
tier2 op(_INSERT_2_LOAD_CONST_INLINE_BORROW, (ptr/4, left, right -- res, l, r)) {
59455933
res = PyStackRef_FromPyObjectBorrow(ptr);
59465934
l = left;

Python/executor_cases.c.h

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

Python/optimizer_analysis.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ type_watcher_callback(PyTypeObject* type)
155155
}
156156

157157
static PyObject *
158-
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool insert)
158+
convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj)
159159
{
160160
assert(inst->opcode == _LOAD_GLOBAL_MODULE || inst->opcode == _LOAD_GLOBAL_BUILTINS || inst->opcode == _LOAD_ATTR_MODULE);
161161
assert(PyDict_CheckExact(obj));
@@ -175,22 +175,10 @@ convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool insert)
175175
if (res == NULL) {
176176
return NULL;
177177
}
178-
if (insert) {
179-
if (_Py_IsImmortal(res)) {
180-
inst->opcode = _INSERT_1_LOAD_CONST_INLINE_BORROW;
181-
} else {
182-
inst->opcode = _INSERT_1_LOAD_CONST_INLINE;
183-
}
178+
if (_Py_IsImmortal(res)) {
179+
inst->opcode = _LOAD_CONST_INLINE_BORROW;
184180
} else {
185-
if (_Py_IsImmortal(res)) {
186-
inst->opcode = _LOAD_CONST_INLINE_BORROW;
187-
} else {
188-
inst->opcode = _LOAD_CONST_INLINE;
189-
}
190-
if (inst->oparg & 1) {
191-
assert(inst[1].opcode == _PUSH_NULL_CONDITIONAL);
192-
assert(inst[1].oparg & 1);
193-
}
181+
inst->opcode = _LOAD_CONST_INLINE;
194182
}
195183
inst->operand0 = (uint64_t)res;
196184
return res;
@@ -341,7 +329,8 @@ optimize_to_bool(
341329
if (truthiness >= 0) {
342330
PyObject *load = truthiness ? Py_True : Py_False;
343331
if (insert_mode) {
344-
ADD_OP(_INSERT_1_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
332+
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
333+
ADD_OP(_SWAP, 2, 0);
345334
} else {
346335
ADD_OP(_POP_TOP, 0, 0);
347336
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
@@ -404,8 +393,9 @@ lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction
404393
0, (uintptr_t)lookup);
405394
}
406395
else {
407-
ADD_OP(immortal ? _INSERT_1_LOAD_CONST_INLINE_BORROW : _INSERT_1_LOAD_CONST_INLINE,
396+
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
408397
0, (uintptr_t)lookup);
398+
ADD_OP(_SWAP, 2, 0);
409399
}
410400
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
411401
_Py_BloomFilter_Add(dependencies, type);
@@ -616,8 +606,6 @@ const uint16_t op_without_push[MAX_UOP_ID + 1] = {
616606
[_COPY] = _NOP,
617607
[_LOAD_CONST_INLINE] = _NOP,
618608
[_LOAD_CONST_INLINE_BORROW] = _NOP,
619-
[_INSERT_1_LOAD_CONST_INLINE] = _POP_TOP_LOAD_CONST_INLINE,
620-
[_INSERT_1_LOAD_CONST_INLINE_BORROW] = _POP_TOP_LOAD_CONST_INLINE_BORROW,
621609
[_LOAD_FAST] = _NOP,
622610
[_LOAD_FAST_BORROW] = _NOP,
623611
[_LOAD_SMALL_INT] = _NOP,

Python/optimizer_bytecodes.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,15 @@ dummy_func(void) {
837837
if (watched_mutations < _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS) {
838838
PyDict_Watch(GLOBALS_WATCHER_ID, dict);
839839
_Py_BloomFilter_Add(dependencies, dict);
840-
PyObject *res = convert_global_to_const(this_instr, dict, true);
840+
PyObject *res = convert_global_to_const(this_instr, dict);
841841
if (res == NULL) {
842842
attr = sym_new_not_null(ctx);
843843
}
844844
else {
845+
bool immortal = _Py_IsImmortal(res);
846+
ADD_OP(immortal ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE,
847+
0, (uintptr_t)res);
848+
ADD_OP(_SWAP, 2, 0);
845849
attr = sym_new_const(ctx, res);
846850
}
847851

@@ -1607,7 +1611,8 @@ dummy_func(void) {
16071611
}
16081612

16091613
op(_REPLACE_WITH_TRUE, (value -- res, v)) {
1610-
ADD_OP(_INSERT_1_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_True);
1614+
ADD_OP(_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_True);
1615+
ADD_OP(_SWAP, 2, 0);
16111616
res = sym_new_const(ctx, Py_True);
16121617
v = value;
16131618
}
@@ -2007,7 +2012,11 @@ dummy_func(void) {
20072012
ctx->builtins_watched = true;
20082013
}
20092014
if (ctx->frame->globals_checked_version != 0 && ctx->frame->globals_watched) {
2010-
cnst = convert_global_to_const(this_instr, builtins, false);
2015+
cnst = convert_global_to_const(this_instr, builtins);
2016+
if (cnst != NULL && this_instr->oparg & 1) {
2017+
assert(this_instr[1].opcode == _PUSH_NULL_CONDITIONAL);
2018+
assert(this_instr[1].oparg & 1);
2019+
}
20112020
}
20122021
}
20132022
if (cnst == NULL) {
@@ -2046,7 +2055,11 @@ dummy_func(void) {
20462055
ctx->frame->globals_checked_version = version;
20472056
}
20482057
if (ctx->frame->globals_checked_version == version) {
2049-
cnst = convert_global_to_const(this_instr, globals, false);
2058+
cnst = convert_global_to_const(this_instr, globals);
2059+
if (cnst != NULL && this_instr->oparg & 1) {
2060+
assert(this_instr[1].opcode == _PUSH_NULL_CONDITIONAL);
2061+
assert(this_instr[1].oparg & 1);
2062+
}
20502063
}
20512064
}
20522065
}

0 commit comments

Comments
 (0)