From c82f9400839c58d9f12e91b67256c92a6f467293 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 3 Apr 2025 20:06:20 +0200 Subject: [PATCH 1/3] Allow the JIT to remove an extra _TO_BOOL_BOOL instruction after _CONTAINS_OP_SET by setting the return type to bool. --- Lib/test/test_capi/test_opt.py | 26 ++++++++++++++++++++++++++ Python/optimizer_bytecodes.c | 4 ++++ Python/optimizer_cases.c.h | 6 +++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 9ac5d1ecf7eb1e..bef0a52a0734b8 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -1603,6 +1603,32 @@ def testfunc(n): self.assertNotIn("_COMPARE_OP_INT", uops) self.assertIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops) + def test_to_bool_bool_contains_op_set(self): + """ + Test that _TO_BOOL_BOOL is removed from code like: + + res = foo in some_set + if res: + .... + + """ + def testfunc(n): + x = 0 + s = {1, 2, 3} + for _ in range(n): + a = 2 + in_set = a in s + if in_set: + 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_SET", 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/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index da36704d91e4b3..d7b3564db1b90a 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -477,6 +477,10 @@ dummy_func(void) { res = sym_new_type(ctx, &PyBool_Type); } + op(_CONTAINS_OP_SET, (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; diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index ea25b8224a459b..19fe21994cd9a1 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1291,9 +1291,9 @@ } case _CONTAINS_OP_SET: { - 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; From 692855220869c220fab3ec4fd66a813f0cc7477c Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 3 Apr 2025 20:11:11 +0200 Subject: [PATCH 2/3] Add news entry --- .../2025-04-03-20-11-02.gh-issue-131798.yhdUKW.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-04-03-20-11-02.gh-issue-131798.yhdUKW.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-03-20-11-02.gh-issue-131798.yhdUKW.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-03-20-11-02.gh-issue-131798.yhdUKW.rst new file mode 100644 index 00000000000000..8a448ee5e23b9b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-03-20-11-02.gh-issue-131798.yhdUKW.rst @@ -0,0 +1,2 @@ +Allow the JIT to remove an extra ``_TO_BOOL_BOOL`` instruction after +``_CONTAINS_OP_SET`` by setting the return type to bool. From 52c1a54bc77e7d74b0176935f32d9098c0ef4c50 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 3 Apr 2025 23:09:35 +0200 Subject: [PATCH 3/3] Fix linter --- Lib/test/test_capi/test_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index bef0a52a0734b8..0e13799ad47381 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -1621,7 +1621,7 @@ def testfunc(n): if in_set: x += 1 return x - + res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD) self.assertEqual(res, TIER2_THRESHOLD) self.assertIsNotNone(ex)