]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-131798: Narrow the result of _CONTAINS_OP_DICT to bool in the JIT (GH-132269)
authorNadeshiko Manju <me@manjusaka.me>
Tue, 8 Apr 2025 16:12:09 +0000 (00:12 +0800)
committerGitHub <noreply@github.com>
Tue, 8 Apr 2025 16:12:09 +0000 (09:12 -0700)
Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-04-08-21-20-12.gh-issue-131798.Ft9tIF.rst [new file with mode: 0644]
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index 3ade7cb7a2b6af244be3a4becd612610a8f868a5..00aebd68f9ef43cc944c96d16df82d67c409a1ec 100644 (file)
@@ -1629,6 +1629,33 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertIn("_CONTAINS_OP_SET", 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 f(n):
             for i in range(n):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-08-21-20-12.gh-issue-131798.Ft9tIF.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-08-21-20-12.gh-issue-131798.Ft9tIF.rst
new file mode 100644 (file)
index 0000000..90c0fb3
--- /dev/null
@@ -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.
index 72dc2bbd44e71c085264a57e3073586b85c53e85..0f7e183608e3e9551300e394faf0fe294ad92643 100644 (file)
@@ -485,6 +485,10 @@ dummy_func(void) {
         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)) {
         PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
         int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
index 160d09ca7c558076249b46086221fdd645d27158..0df52e654432b6b85f93d39f6bf4350c8ee0b913 100644 (file)
         }
 
         case _CONTAINS_OP_DICT: {
-            JitOptSymbol *b;
-            b = sym_new_not_null(ctx);
-            stack_pointer[-2] = b;
+            JitOptSymbol *res;
+            res = sym_new_type(ctx, &PyBool_Type);
+            stack_pointer[-2] = res;
             stack_pointer += -1;
             assert(WITHIN_STACK_BOUNDS());
             break;