Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
Fidget-Spinner committed Feb 19, 2024
commit 712fcf55cf5d1efe25b3df6eeada025a86b026c7
59 changes: 56 additions & 3 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _run_with_optimizer(self, testfunc, arg):
def test_int_type_propagation(self):
def testfunc(loops):
num = 0
while num < loops:
for i in range(loops):
x = num + num
a = x + 1
num += 1
Expand All @@ -593,7 +593,7 @@ def double(x):
return x + x
def testfunc(loops):
num = 0
while num < loops:
for i in range(loops):
x = num + num
a = double(x)
num += 1
Expand All @@ -617,7 +617,7 @@ def double(x):
return x + x
def testfunc(loops):
num = 0
while num < loops:
for i in range(loops):
a = double(num)
x = a + a
num += 1
Expand Down Expand Up @@ -821,6 +821,59 @@ def testfunc(n):
# We'll also need to verify that propagation actually occurs.
self.assertIn("_BINARY_OP_MULTIPLY_FLOAT", uops)

def test_compare_op_type_propagation_float(self):
def testfunc(n):
a = 1.0
for _ in range(n):
x = a == a
x = a == a
x = a == a
x = a == a
return x

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertTrue(res)
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_FLOAT"]
self.assertLessEqual(len(guard_both_float_count), 1)
self.assertIn("_COMPARE_OP_FLOAT", uops)

def test_compare_op_type_propagation_int(self):
def testfunc(n):
a = 1
for _ in range(n):
x = a == a
x = a == a
x = a == a
x = a == a
return x

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertTrue(res)
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_INT"]
self.assertLessEqual(len(guard_both_float_count), 1)
self.assertIn("_COMPARE_OP_INT", uops)

def test_compare_op_type_propagation_unicode(self):
def testfunc(n):
a = ""
for _ in range(n):
x = a == a
x = a == a
x = a == a
x = a == a
return x

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertTrue(res)
self.assertIsNotNone(ex)
uops = {opname for opname, _, _ in ex}
guard_both_float_count = [opname for opname, _, _ in ex if opname == "_GUARD_BOTH_UNICODE"]
self.assertLessEqual(len(guard_both_float_count), 1)
self.assertIn("_COMPARE_OP_STR", uops)

if __name__ == "__main__":
unittest.main()
8 changes: 8 additions & 0 deletions Python/tier2_redundancy_eliminator_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ dummy_func(void) {
sym_set_type(right, &PyFloat_Type);
}

op(_GUARD_BOTH_UNICODE, (left, right -- left, right)) {
if (sym_matches_type(left, &PyUnicode_Type) &&
sym_matches_type(right, &PyUnicode_Type)) {
REPLACE_OP(this_instr, _NOP, 0 ,0);
}
sym_set_type(left, &PyUnicode_Type);
sym_set_type(right, &PyUnicode_Type);
}

op(_BINARY_OP_ADD_INT, (left, right -- res)) {
if (is_const(left) && is_const(right)) {
Expand Down
10 changes: 10 additions & 0 deletions Python/tier2_redundancy_eliminator_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.