8000 GH-130415: Use boolean guards to narrow types to values in the JIT by brandtbucher · Pull Request #130659 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-130415: Use boolean guards to narrow types to values in the JIT #130659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 2, 2025
Next Next commit
Add failing regression tests
  • Loading branch information
brandtbucher committed Feb 27, 2025
commit d0f236a793d1cbdce47c6286177243c51e73ccee
62 changes: 62 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,68 @@ def crash_addition():

crash_addition()

def test_narrow_type_to_constant_bool_false(self):
def f(n):
trace = []
for i in range(n):
# f is always False, but we can only prove that it's a bool:
f = i == TIER2_THRESHOLD
trace.append("A")
if not f: # Kept.
trace.append("B")
if not f: # Removed!
trace.append("C")
trace.append("D")
if f: # Removed!
trace.append("X")
trace.append("E")
trace.append("F")
if f: # Removed!
trace.append("X")
trace.append("G")
return trace

trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
# Only one guard remains:
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))

def test_narrow_type_to_constant_bool_true(self):
def f(n):
trace = []
for i in range(n):
# f is always True, but we can only prove that it's a bool:
f = i != TIER2_THRESHOLD
trace.append("A")
if f: # Kept.
trace.append("B")
if not f: # Removed!
trace.append("X")
trace.append("C")
if f: # Removed!
trace.append("D")
trace.append("E")
trace.append("F")
if not f: # Removed!
trace.append("X")
trace.append("G")
return trace

trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
# Only one guard remains:
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 0)
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 1)
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))


def global_identity(x):
return x
Expand Down
0