From 39c8d51048e5e58a50c95847e2dc942734cb4352 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Fri, 13 Mar 2026 23:29:39 +0800 Subject: [PATCH 1/3] Disable over-aggressive optimization for _GUARD_CODE_VERSION --- Python/optimizer_bytecodes.c | 7 ++++--- Python/optimizer_cases.c.h | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 2b68ba4d2cde92..88afde7b5acc92 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1747,9 +1747,10 @@ dummy_func(void) { op(_GUARD_CODE_VERSION, (version/2 -- )) { PyCodeObject *co = get_current_code_object(ctx); - if (co->co_version == version) { - _Py_BloomFilter_Add(dependencies, co); - REPLACE_OP(this_instr, _NOP, 0, 0); + if (co->co_version != version) { + // TODO: + // If we've previously guarded on this code version in a trace, we + // can avoid guarding it again. } else { ctx->done = true; diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index fe083411b7b739..a1702f4e208d5a 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -4265,9 +4265,7 @@ case _GUARD_CODE_VERSION: { uint32_t version = (uint32_t)this_instr->operand0; PyCodeObject *co = get_current_code_object(ctx); - if (co->co_version == version) { - _Py_BloomFilter_Add(dependencies, co); - REPLACE_OP(this_instr, _NOP, 0, 0); + if (co->co_version != version) { } else { ctx->done = true; From 0f74390c313c5317d63cb275b5cb6a922074a536 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Fri, 13 Mar 2026 23:37:11 +0800 Subject: [PATCH 2/3] small fix --- Python/optimizer_bytecodes.c | 3 ++- Python/optimizer_cases.c.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 88afde7b5acc92..be7a2fd4c6ade9 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1748,7 +1748,8 @@ dummy_func(void) { op(_GUARD_CODE_VERSION, (version/2 -- )) { PyCodeObject *co = get_current_code_object(ctx); if (co->co_version != version) { - // TODO: + _Py_BloomFilter_Add(dependencies, co); + // TODO gh-144651: // If we've previously guarded on this code version in a trace, we // can avoid guarding it again. } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index a1702f4e208d5a..ed711a5c3e2fad 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -4266,6 +4266,7 @@ uint32_t version = (uint32_t)this_instr->operand0; PyCodeObject *co = get_current_code_object(ctx); if (co->co_version != version) { + _Py_BloomFilter_Add(dependencies, co); } else { ctx->done = true; From 290bdd0ea8ea9d7e874f89d342d75be817f739b1 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Fri, 13 Mar 2026 23:58:09 +0800 Subject: [PATCH 3/3] woops --- Python/optimizer_bytecodes.c | 2 +- Python/optimizer_cases.c.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index be7a2fd4c6ade9..140a5fa06fb7a4 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1747,7 +1747,7 @@ dummy_func(void) { op(_GUARD_CODE_VERSION, (version/2 -- )) { PyCodeObject *co = get_current_code_object(ctx); - if (co->co_version != version) { + if (co->co_version == version) { _Py_BloomFilter_Add(dependencies, co); // TODO gh-144651: // If we've previously guarded on this code version in a trace, we diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index ed711a5c3e2fad..6b1dd4f64dabaf 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -4265,7 +4265,7 @@ case _GUARD_CODE_VERSION: { uint32_t version = (uint32_t)this_instr->operand0; PyCodeObject *co = get_current_code_object(ctx); - if (co->co_version != version) { + if (co->co_version == version) { _Py_BloomFilter_Add(dependencies, co); } else {