Skip to content

Commit dcb0432

Browse files
committed
simplify: port PrimitiveBoxBool to C via SimplifyEnv (chunk 4)
First Category 2 handler using the C SimplifyEnv bridge. simplify_primitive_box_bool_c uses simplify_env_emit_use_type + simplify_env_emit_load_const to emit replacement instructions. Validates the make_c_env/sync_c_env pattern for Env-using handlers.
1 parent c181912 commit dcb0432

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

Python/jit/hir/simplify.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,8 +2801,12 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
28012801
case Opcode::kPrimitiveCompare:
28022802
return simplifyPrimitiveCompare(
28032803
env, static_cast<const PrimitiveCompare*>(instr));
2804-
case Opcode::kPrimitiveBoxBool:
2805-
return simplifyPrimitiveBoxBool(env, instr);
2804+
case Opcode::kPrimitiveBoxBool: {
2805+
SimplifyEnv cenv = make_c_env();
2806+
auto *r = static_cast<Register*>(simplify_primitive_box_bool_c(&cenv, instr));
2807+
sync_c_env(cenv);
2808+
return r;
2809+
}
28062810
case Opcode::kIndexUnbox:
28072811
case Opcode::kPrimitiveUnbox:
28082812
return simplifyUnbox(env, instr);

Python/jit/hir/simplify_c.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ void *simplify_guard_type_identity_c(const void *instr) {
7070
return NULL;
7171
}
7272

73-
/* ---- simplifyIsTruthy (partial — CBool constant folding) ----
74-
* If input is a known CBool constant, replace with the constant directly. */
75-
void *simplify_is_truthy_cbool_c(SimplifyEnv *env, const void *instr) {
73+
/* ---- simplifyPrimitiveBoxBool ----
74+
* If input is a known int constant, replace with Py_True/Py_False. */
75+
void *simplify_primitive_box_bool_c(SimplifyEnv *env, const void *instr) {
7676
void *input = hir_c_get_operand(instr, 0);
7777
HirType input_type = hir_register_type(input);
78-
HirType t_cbool = HIR_TYPE_CBOOL;
79-
if (hir_type_is_subtype(input_type, t_cbool) &&
80-
hir_type_has_int_spec(&input_type)) {
81-
intptr_t val = hir_type_int_spec(&input_type);
82-
PyObject *result = val ? Py_True : Py_False;
83-
HirType result_type = hir_type_from_object(result);
78+
if (hir_type_has_int_spec(&input_type)) {
79+
simplify_env_emit_use_type(env, input, input_type);
80+
PyObject *bool_obj = hir_type_int_spec(&input_type) ? Py_True : Py_False;
81+
HirType result_type = hir_type_from_object(bool_obj);
8482
return simplify_env_emit_load_const(env, result_type);
8583
}
8684
return NULL;

Python/jit/hir/simplify_c.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ 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);
3232

33-
/* Env-using handlers */
34-
void *simplify_is_truthy_cbool_c(SimplifyEnv *env, const void *instr);
33+
/* Env-using handlers (Category 2) */
34+
void *simplify_primitive_box_bool_c(SimplifyEnv *env, const void *instr);
3535

3636
#ifdef __cplusplus
3737
}

0 commit comments

Comments
 (0)