Skip to content

Navigation Menu

Sign in
Appearance settings

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 c989e74

Browse filesBrowse files
authored
GH-130415: Narrow int to 0 based on boolean tests (GH-130772)
1 parent e20e47d commit c989e74
Copy full SHA for c989e74

File tree

6 files changed

+38
-2
lines changed
Filter options

6 files changed

+38
-2
lines changed

‎Lib/test/test_capi/test_opt.py

Copy file name to clipboardExpand all lines: Lib/test/test_capi/test_opt.py
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,37 @@ def f(n):
14991499
# But all of the appends we care about are still there:
15001500
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
15011501

1502+
def test_narrow_type_to_constant_int_zero(self):
1503+
def f(n):
1504+
trace = []
1505+
for i in range(n):
1506+
# zero is always (int) 0, but we can only prove that it's a integer:
1507+
false = i == TIER2_THRESHOLD # this will always be false, while hopefully still fooling optimizer improvements
1508+
zero = false + 0 # this should always set the variable zero equal to 0
1509+
trace.append("A")
1510+
if not zero: # Kept.
1511+
trace.append("B")
1512+
if not zero: # Removed!
1513+
trace.append("C")
1514+
trace.append("D")
1515+
if zero: # Removed!
1516+
trace.append("X")
1517+
trace.append("E")
1518+
trace.append("F")
1519+
if zero: # Removed!
1520+
trace.append("X")
1521+
trace.append("G")
1522+
return trace
1523+
1524+
trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
1525+
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
1526+
self.assertIsNotNone(ex)
1527+
uops = get_opnames(ex)
1528+
# Only one guard remains:
1529+
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
1530+
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
1531+
# But all of the appends we care about are still there:
1532+
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
15021533

15031534
def global_identity(x):
15041535
return x

‎Misc/ACKS

Copy file name to clipboardExpand all lines: Misc/ACKS
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ Detlef Lannert
10611061
Rémi Lapeyre
10621062
Soren Larsen
10631063
Amos Latteier
1064+
Keenan Lau
10641065
Piers Lauder
10651066
Ben Laurie
10661067
Yoni Lavi
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve JIT understanding of integers in boolean context.

‎Python/optimizer_bytecodes.c

Copy file name to clipboardExpand all lines: Python/optimizer_bytecodes.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ dummy_func(void) {
406406
op(_TO_BOOL_INT, (value -- res)) {
407407
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
408408
sym_set_type(value, &PyLong_Type);
409-
res = sym_new_type(ctx, &PyBool_Type);
409+
res = sym_new_truthiness(ctx, value, true);
410410
}
411411
}
412412

‎Python/optimizer_cases.c.h

Copy file name to clipboardExpand all lines: Python/optimizer_cases.c.h
+1-1Lines 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_symbols.c

Copy file name to clipboardExpand all lines: Python/optimizer_symbols.c
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ _Py_uop_sym_set_const(JitOptContext *ctx, JitOptSymbol *sym, PyObject *const_val
299299
else if (type == &PyBool_Type) {
300300
_Py_uop_sym_set_const(ctx, value, Py_False);
301301
}
302+
else if (type == &PyLong_Type) {
303+
_Py_uop_sym_set_const(ctx, value, Py_GetConstant(Py_CONSTANT_ZERO));
304+
}
302305
// TODO: More types (GH-130415)!
303306
make_const(sym, const_val);
304307
return;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.