Skip to content

Commit aa14a88

Browse files
committed
simplify: port PrimitiveCompare box(b)==True path to C
Env-free handler: if PrimitiveCompare(kEqual, PrimitiveBoxBool(b), True) then return b directly (skip box+compare). Uses HIR_PCMP_Equal constant and hir_type_as_object to check for Py_True.
1 parent 6ee6641 commit aa14a88

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

Python/jit/hir/simplify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2817,9 +2817,12 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
28172817
case Opcode::kUnaryOp:
28182818
return simplifyUnaryOp(env, static_cast<const UnaryOp*>(instr));
28192819

2820-
case Opcode::kPrimitiveCompare:
2820+
case Opcode::kPrimitiveCompare: {
2821+
auto *r = static_cast<Register*>(simplify_primitive_compare_box_true_c(instr));
2822+
if (r) return r;
28212823
return simplifyPrimitiveCompare(
28222824
env, static_cast<const PrimitiveCompare*>(instr));
2825+
}
28232826
case Opcode::kPrimitiveBoxBool: {
28242827
SimplifyEnv cenv = make_c_env();
28252828
auto *r = static_cast<Register*>(simplify_primitive_box_bool_c(&cenv, instr));

Python/jit/hir/simplify_c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ void *simplify_compare_c(SimplifyEnv *env, const void *instr) {
165165
return NULL;
166166
}
167167

168+
/* ---- simplifyPrimitiveCompare (partial — box(b)==True → b) ---- */
169+
void *simplify_primitive_compare_box_true_c(const void *instr) {
170+
void *left = hir_c_get_operand(instr, 0);
171+
void *right = hir_c_get_operand(instr, 1);
172+
int32_t op = hir_c_compare_op(instr);
173+
if (op != HIR_PCMP_Equal) return NULL;
174+
175+
extern void *hir_reg_instr(void *reg);
176+
void *left_def = hir_reg_instr(left);
177+
if (left_def == NULL || hir_c_opcode(left_def) != HIR_OP_PrimitiveBoxBool)
178+
return NULL;
179+
180+
HirType right_type = hir_register_type(right);
181+
PyObject *right_obj = hir_type_as_object(&right_type);
182+
if (right_obj != Py_True) return NULL;
183+
184+
return hir_c_get_operand(left_def, 0);
185+
}
186+
168187
/* ---- simplifyIntConvert ----
169188
* If input already has the target type, IntConvert is redundant. */
170189
void *simplify_int_convert_c(SimplifyEnv *env, const void *instr) {

Python/jit/hir/simplify_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void *simplify_env_emit_use_type(SimplifyEnv *env, void *val, HirType type);
2929
void *simplify_check_c(const void *instr);
3030
void *simplify_refine_type_c(const void *instr);
3131
void *simplify_guard_type_identity_c(const void *instr);
32+
void *simplify_primitive_compare_box_true_c(const void *instr);
3233
void *simplify_int_convert_c(SimplifyEnv *env, const void *instr);
3334

3435
/* Env-using handlers (Category 2) */

0 commit comments

Comments
 (0)