mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
GH-131798: Narrow the result of _CONTAINS_OP_DICT to bool in the JIT (GH-132269)
Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
This commit is contained in:
parent
71009cb835
commit
d753d8aed7
4 changed files with 36 additions and 3 deletions
|
@ -1629,6 +1629,33 @@ class TestUopsOptimization(unittest.TestCase):
|
||||||
self.assertIn("_CONTAINS_OP_SET", uops)
|
self.assertIn("_CONTAINS_OP_SET", uops)
|
||||||
self.assertNotIn("_TO_BOOL_BOOL", uops)
|
self.assertNotIn("_TO_BOOL_BOOL", uops)
|
||||||
|
|
||||||
|
def test_to_bool_bool_contains_op_dict(self):
|
||||||
|
"""
|
||||||
|
Test that _TO_BOOL_BOOL is removed from code like:
|
||||||
|
|
||||||
|
res = foo in some_dict
|
||||||
|
if res:
|
||||||
|
....
|
||||||
|
|
||||||
|
"""
|
||||||
|
def testfunc(n):
|
||||||
|
x = 0
|
||||||
|
s = {1: 1, 2: 2, 3: 3}
|
||||||
|
for _ in range(n):
|
||||||
|
a = 2
|
||||||
|
in_dict = a in s
|
||||||
|
if in_dict:
|
||||||
|
x += 1
|
||||||
|
return x
|
||||||
|
|
||||||
|
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
|
||||||
|
self.assertEqual(res, TIER2_THRESHOLD)
|
||||||
|
self.assertIsNotNone(ex)
|
||||||
|
uops = get_opnames(ex)
|
||||||
|
self.assertIn("_CONTAINS_OP_DICT", uops)
|
||||||
|
self.assertNotIn("_TO_BOOL_BOOL", uops)
|
||||||
|
|
||||||
|
|
||||||
def test_remove_guard_for_known_type_str(self):
|
def test_remove_guard_for_known_type_str(self):
|
||||||
def f(n):
|
def f(n):
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Allow the JIT to remove an extra ``_TO_BOOL_BOOL`` instruction after
|
||||||
|
``_CONTAINS_OP_DICT`` by setting the return type to bool.
|
|
@ -485,6 +485,10 @@ dummy_func(void) {
|
||||||
res = sym_new_type(ctx, &PyBool_Type);
|
res = sym_new_type(ctx, &PyBool_Type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
op(_CONTAINS_OP_DICT, (left, right -- res)) {
|
||||||
|
res = sym_new_type(ctx, &PyBool_Type);
|
||||||
|
}
|
||||||
|
|
||||||
op(_LOAD_CONST, (-- value)) {
|
op(_LOAD_CONST, (-- value)) {
|
||||||
PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
|
PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
|
||||||
int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
|
int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
|
||||||
|
|
6
Python/optimizer_cases.c.h
generated
6
Python/optimizer_cases.c.h
generated
|
@ -1292,9 +1292,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
case _CONTAINS_OP_DICT: {
|
case _CONTAINS_OP_DICT: {
|
||||||
JitOptSymbol *b;
|
JitOptSymbol *res;
|
||||||
b = sym_new_not_null(ctx);
|
res = sym_new_type(ctx, &PyBool_Type);
|
||||||
stack_pointer[-2] = b;
|
stack_pointer[-2] = res;
|
||||||
stack_pointer += -1;
|
stack_pointer += -1;
|
||||||
assert(WITHIN_STACK_BOUNDS());
|
assert(WITHIN_STACK_BOUNDS());
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue