Skip to content

Commit d01886c

Browse files
pythongh-115685: Type/values propagate for TO_BOOL in tier 2 (pythonGH-115686)
1 parent c04a981 commit d01886c

File tree

6 files changed

+163
-30
lines changed

6 files changed

+163
-30
lines changed

Include/internal/pycore_uop_ids.h

+21-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

+5
Original file line numberDiff line numberDiff line change
@@ -4043,6 +4043,11 @@ dummy_func(
40434043
value = ptr;
40444044
}
40454045

4046+
tier2 pure op (_POP_TOP_LOAD_CONST_INLINE_BORROW, (ptr/4, pop -- value)) {
4047+
Py_DECREF(pop);
4048+
value = ptr;
4049+
}
4050+
40464051
tier2 pure op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
40474052
value = Py_NewRef(ptr);
40484053
null = NULL;

Python/executor_cases.c.h

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

+61
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,67 @@ dummy_func(void) {
254254
}
255255
}
256256

257+
op(_TO_BOOL, (value -- res)) {
258+
(void)value;
259+
res = sym_new_type(ctx, &PyBool_Type);
260+
OUT_OF_SPACE_IF_NULL(res);
261+
}
262+
263+
op(_TO_BOOL_BOOL, (value -- value)) {
264+
if (sym_matches_type(value, &PyBool_Type)) {
265+
REPLACE_OP(this_instr, _NOP, 0, 0);
266+
}
267+
else {
268+
if(!sym_set_type(value, &PyBool_Type)) {
269+
goto hit_bottom;
270+
}
271+
}
272+
}
273+
274+
op(_TO_BOOL_INT, (value -- res)) {
275+
if (sym_is_const(value) && sym_matches_type(value, &PyLong_Type)) {
276+
PyObject *load = _PyLong_IsZero((PyLongObject *)sym_get_const(value))
277+
? Py_False : Py_True;
278+
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
279+
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
280+
}
281+
else {
282+
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
283+
}
284+
if(!sym_set_type(value, &PyLong_Type)) {
285+
goto hit_bottom;
286+
}
287+
}
288+
289+
op(_TO_BOOL_LIST, (value -- res)) {
290+
if(!sym_set_type(value, &PyList_Type)) {
291+
goto hit_bottom;
292+
}
293+
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
294+
}
295+
296+
op(_TO_BOOL_NONE, (value -- res)) {
297+
if (sym_get_const(value) == Py_None) {
298+
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)Py_False);
299+
}
300+
sym_set_const(value, Py_None);
301+
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, Py_False));
302+
}
303+
304+
op(_TO_BOOL_STR, (value -- res)) {
305+
if (sym_is_const(value) && sym_matches_type(value, &PyUnicode_Type)) {
306+
PyObject *load = sym_get_const(value) == &_Py_STR(empty) ? Py_False : Py_True;
307+
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
308+
OUT_OF_SPACE_IF_NULL(res = sym_new_const(ctx, load));
309+
}
310+
else {
311+
OUT_OF_SPACE_IF_NULL(res = sym_new_type(ctx, &PyBool_Type));
312+
}
313+
if(!sym_set_type(value, &PyUnicode_Type)) {
314+
goto hit_bottom;
315+
}
316+
}
317+
257318
op(_LOAD_CONST, (-- value)) {
258319
// There should be no LOAD_CONST. It should be all
259320
// replaced by peephole_opt.

Python/optimizer_cases.c.h

+63-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)